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 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       * This tests basic cache operations(put,retrieve) inside OSGi
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       * This test basic cache operations(put,retrieve) inside OSGi using an object of an imported class (provided by an other bundle).
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                         //Uncomment the line below to debug test
112                         //enabledDebuggingOnPort(5005,false),
113                         felix(), equinox() );
114     }
115 }