View Javadoc

1   package org.apache.directmemory.tests.osgi.ehcache;
2   
3   /*
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  import java.io.IOException;
23  import java.io.InputStream;
24  import java.util.ArrayList;
25  import java.util.Arrays;
26  import java.util.List;
27  
28  import net.sf.ehcache.CacheException;
29  import net.sf.ehcache.CacheManager;
30  import net.sf.ehcache.Ehcache;
31  import net.sf.ehcache.Element;
32  import org.apache.directmemory.tests.osgi.DirectMemoryOsgiTestSupport;
33  import org.apache.directmemory.tests.osgi.cache.SimpleObject;
34  import org.junit.Assert;
35  import org.junit.Test;
36  import org.junit.runner.RunWith;
37  import org.ops4j.pax.exam.Customizer;
38  import org.ops4j.pax.exam.Option;
39  import org.ops4j.pax.exam.junit.Configuration;
40  import org.ops4j.pax.exam.junit.JUnit4TestRunner;
41  import org.ops4j.pax.exam.options.MavenArtifactProvisionOption;
42  import org.osgi.framework.Constants;
43  
44  import static org.ops4j.pax.exam.CoreOptions.equinox;
45  import static org.ops4j.pax.exam.CoreOptions.felix;
46  import static org.ops4j.pax.exam.CoreOptions.mavenBundle;
47  import static org.ops4j.pax.exam.OptionUtils.combine;
48  import static org.ops4j.pax.swissbox.tinybundles.core.TinyBundles.modifyBundle;
49  
50  @RunWith(JUnit4TestRunner.class)
51  public class EhCacheTest extends DirectMemoryOsgiTestSupport {
52  
53    @Test
54    public void testPutRetreive() {
55      Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
56      CacheManager cacheManager = CacheManager.getInstance();
57      Ehcache ehcache = cacheManager.getEhcache("testCache");
58  
59      ehcache.put(new Element("testKey", "testValue"));
60      stats(ehcache);
61      Assert.assertEquals("testValue", ehcache.get("testKey").getObjectValue());
62    }
63  
64    @Test
65    public void testSizing() {
66      Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
67      CacheManager cacheManager = CacheManager.getInstance();
68      Ehcache ehcache = cacheManager.getEhcache("testCache");
69      for (int i = 0; i < 30000; i++) {
70        if ((i % 1000) == 0) {
71          System.out.println("heatbeat " + i);
72          stats(ehcache);
73        }
74        ehcache.put(new Element(i, new byte[1024]));
75      }
76      stats(ehcache);
77      Assert.assertTrue(true);
78    }
79  
80    @Test
81    public void testOffHeapExceedMemory()
82            throws IOException {
83      Thread.currentThread().setContextClassLoader(getClass().getClassLoader());
84      CacheManager cacheManager = CacheManager.getInstance();
85      Ehcache ehcache = cacheManager.getEhcache("testCache");
86      Element element = null;
87      try {
88        for (int i = 0; i < 3000000; i++) {
89          if ((i % 1000) == 0) {
90            System.out.println("heatbeat 2 " + i);
91            stats(ehcache);
92          }
93          element = new Element(i, new byte[1024]);
94          ehcache.put(element);
95        }
96        Assert.fail("CacheException expected for DirectMemory OffHeap Memory Exceeded");
97      } catch (CacheException e) {
98        stats(ehcache);
99        Assert.assertTrue("CacheException expected for DirectMemory OffHeap Memory Exceeded", true);
100     }
101 
102   }
103 
104   private void stats(Ehcache ehcache) {
105     System.out.println("OnHeapSize=" + ehcache.calculateInMemorySize() + ", OnHeapElements="
106             + ehcache.getMemoryStoreSize());
107     System.out.println("OffHeapSize=" + ehcache.calculateOffHeapSize() + ", OffHeapElements="
108             + ehcache.getOffHeapStoreSize());
109     System.out.println("DiskStoreSize=" + ehcache.calculateOnDiskSize() + ", DiskStoreElements="
110             + ehcache.getDiskStoreSize());
111   }
112 
113   public static Option[] getDynamicMemoryEhCacheOptions() {
114     List<MavenArtifactProvisionOption> mavenOptions = Arrays.asList(
115             mavenBundle().groupId("org.apache.servicemix.bundles").artifactId("org.apache.servicemix.bundles.ehcache").version(
116                     System.getProperty( "ehcache.bundle.version", "2.6.6_1" )),
117             mavenBundle().groupId("org.apache.directmemory").artifactId("directmemory-ehcache").version(
118                     System.getProperty("direct.memory.version")));
119 
120     List<Option> options = new ArrayList<Option>();
121     options.addAll(Arrays.asList(DirectMemoryOsgiTestSupport.getDynamicMemoryOptions()));
122     options.addAll(mavenOptions);
123 
124     if (Boolean.getBoolean("osgi.debug")) {
125       options.add(enabledDebuggingOnPort(Integer.getInteger("osgi.debug.port"),
126               Boolean.getBoolean("osgi.debug.suspend")));
127     }
128 
129     return options.toArray(new Option[options.size()]);
130   }
131 
132   @Configuration
133   public Option[] configure() {
134     return combine(getDynamicMemoryEhCacheOptions(), new Customizer() {
135       @Override
136       public InputStream customizeTestProbe(InputStream testProbe) {
137         return modifyBundle(testProbe)
138                 .add(SimpleObject.class)
139                 .add("/ehcache.xml", EhCacheTest.class.getResource("/ehcache.xml"))
140                 .set(Constants.DYNAMICIMPORT_PACKAGE, "*").build();
141       }
142     },
143             //Uncomment the line below to debug test
144             //enabledDebuggingOnPort(5005,true),
145             felix(), equinox());
146   }
147 }