View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  
20  package org.apache.directmemory.tests.osgi.cache;
21  
22  
23  import org.apache.directmemory.cache.CacheService;
24  import org.apache.directmemory.measures.Every;
25  import org.apache.directmemory.measures.Monitor;
26  import org.apache.directmemory.memory.Pointer;
27  import org.apache.directmemory.tests.osgi.DirectMemoryOsgiTestSupport;
28  import org.junit.Test;
29  import org.junit.runner.RunWith;
30  import org.ops4j.pax.exam.Customizer;
31  import org.ops4j.pax.exam.Option;
32  import org.ops4j.pax.exam.junit.Configuration;
33  import org.ops4j.pax.exam.junit.JUnit4TestRunner;
34  import org.osgi.framework.Constants;
35  
36  import java.io.IOException;
37  import java.io.InputStream;
38  
39  import static org.junit.Assert.assertEquals;
40  import static org.junit.Assert.assertNotNull;
41  import static org.ops4j.pax.exam.CoreOptions.equinox;
42  import static org.ops4j.pax.exam.CoreOptions.felix;
43  import static org.ops4j.pax.exam.OptionUtils.combine;
44  import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.modifyBundle;
45  import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.newBundle;
46  
47  @RunWith( JUnit4TestRunner.class )
48  public class CacheServiceTest
49      extends DirectMemoryOsgiTestSupport
50  {
51  
52      /**
53       * This tests basic cache operations(put,retrieve) inside OSGi
54       */
55      @Test
56      public void testCacheService()
57      {
58          String key = "2";
59          String obj = "Simple String Object";
60          CacheService cacheService = getOsgiService( CacheService.class );
61  
62          //Test retrieving an object added by an other bundle.
63          Object result = cacheService.retrieve( "1" );
64          assertNotNull( result );
65  
66          cacheService.scheduleDisposalEvery( Every.seconds( 1 ) );
67          cacheService.dump();
68          Monitor.dump( "cache" );
69  
70          Pointer p = cacheService.put( "2", obj );
71          result = cacheService.retrieve( "2" );
72          cacheService.dump();
73          assertEquals( obj, result );
74      }
75  
76      /**
77       * This test basic cache operations(put,retrieve) inside OSGi using an object of an imported class (provided by an other bundle).
78       */
79      @Test
80      public void testCacheServiceWithImportedObject()
81      {
82          SimpleObject obj1 = new SimpleObject( "2", "Object Two" );
83          SimpleObject obj2 = new SimpleObject( "3", "Object Three" );
84  
85          CacheService cacheService = getOsgiService( CacheService.class );
86          cacheService.scheduleDisposalEvery( Every.seconds( 1 ) );
87          cacheService.dump();
88  
89          Pointer p1 = cacheService.put( "2", obj1 );
90          Pointer p2 = cacheService.put( "3", obj2 );
91          Object result1 = cacheService.retrieve( "2" );
92          Object result2 = cacheService.retrieve( "3" );
93  
94          cacheService.dump();
95          Monitor.dump( "cache" );
96  
97          assertEquals( obj1, result1 );
98          assertEquals( obj2, result2 );
99      }
100 
101 
102     @Configuration
103     public Option[] configure()
104         throws IOException
105     {
106         return combine( getDynamicMemoryOptions(),
107 
108                         bundle( newBundle().add( SimpleObject.class ).add( CacheServiceExportingActivator.class ).set(
109                             Constants.BUNDLE_ACTIVATOR, CacheServiceExportingActivator.class.getCanonicalName() ).set(
110                             Constants.BUNDLE_SYMBOLICNAME,
111                             "org.apache.directmemory.tests.osgi.cacheservice.exporter" ).set( Constants.BUNDLE_VERSION,
112                                                                                               "1.0.0" ).set(
113                             Constants.DYNAMICIMPORT_PACKAGE, "*" ).build() ).start(),
114 
115                         new Customizer()
116                         {
117                             @Override
118                             public InputStream customizeTestProbe( InputStream testProbe )
119                             {
120                                 return modifyBundle( testProbe ).set( Constants.DYNAMICIMPORT_PACKAGE, "*" ).build();
121                             }
122                         },
123                         //Uncomment the line below to debug test
124                         //enabledDebuggingOnPort(5005,true),
125                         felix(), equinox() );
126     }
127 }