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 java.io.InputStream;
24
25 import org.apache.directmemory.cache.Cache;
26 import org.apache.directmemory.measures.Every;
27 import org.apache.directmemory.measures.Monitor;
28 import org.apache.directmemory.measures.Ram;
29 import org.apache.directmemory.memory.Pointer;
30 import org.apache.directmemory.tests.osgi.DirectMemoryOsgiTestSupport;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33
34 import org.ops4j.pax.exam.Customizer;
35 import org.ops4j.pax.exam.Option;
36 import org.ops4j.pax.exam.junit.Configuration;
37 import org.ops4j.pax.exam.junit.JUnit4TestRunner;
38 import org.osgi.framework.Constants;
39
40
41 import static org.junit.Assert.*;
42 import static org.ops4j.pax.exam.CoreOptions.equinox;
43 import static org.ops4j.pax.exam.CoreOptions.felix;
44 import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
45 import static org.ops4j.pax.exam.OptionUtils.combine;
46 import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.modifyBundle;
47
48 @RunWith( JUnit4TestRunner.class )
49 public class CacheTest
50 extends DirectMemoryOsgiTestSupport
51 {
52
53
54
55
56 @Test
57 public void testCacheSingleton()
58 {
59 String key = "1";
60 String obj = "Simple String Object";
61 Cache.init( 1, Ram.Mb( 16 ) );
62 Cache.scheduleDisposalEvery( Every.seconds( 1 ) );
63 Cache.dump();
64
65 Pointer p = Cache.put( "1", obj );
66 Object result = Cache.retrieve( "1" );
67
68 Cache.dump();
69 Monitor.dump( "cache" );
70
71 assertEquals( obj, result );
72 }
73
74
75
76
77 @Test
78 public void testCacheSingletonWithImportedObject()
79 {
80 SimpleObject obj1 = new SimpleObject( "1", "Object One" );
81 SimpleObject obj2 = new SimpleObject( "2", "Object Two" );
82 Cache.init( 1, Ram.Mb( 16 ) );
83 Cache.scheduleDisposalEvery( Every.seconds( 1 ) );
84 Cache.dump();
85
86 Pointer p1 = Cache.put( "1", obj1 );
87 Pointer p2 = Cache.put( "2", obj2 );
88 Object result1 = Cache.retrieve( "1" );
89 Object result2 = Cache.retrieve( "2" );
90
91 Cache.dump();
92 Monitor.dump( "cache" );
93
94 assertEquals( obj1, result1 );
95 assertEquals( obj2, result2 );
96 }
97
98
99 @Configuration
100 public Option[] configure()
101 {
102 return combine( getDynamicMemoryOptions(), new Customizer()
103 {
104 @Override
105 public InputStream customizeTestProbe( InputStream testProbe )
106 {
107 return modifyBundle( testProbe ).add( SimpleObject.class ).set( Constants.DYNAMICIMPORT_PACKAGE,
108 "*" ).build();
109 }
110 },
111
112
113 felix(), equinox() );
114 }
115 }