View Javadoc

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