1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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
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
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
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
124
125 felix(), equinox() );
126 }
127 }