1   /**
2    *  Copyright 2003-2007 Greg Luck
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  
17  package net.sf.ehcache.jcache;
18  
19  import net.sf.ehcache.AbstractCacheTest;
20  import net.sf.ehcache.Ehcache;
21  import org.apache.commons.logging.Log;
22  import org.apache.commons.logging.LogFactory;
23  
24  import net.sf.jsr107cache.Cache;
25  import net.sf.jsr107cache.CacheStatistics;
26  import java.io.ByteArrayInputStream;
27  import java.io.ByteArrayOutputStream;
28  import java.io.IOException;
29  import java.io.ObjectInputStream;
30  import java.io.ObjectOutputStream;
31  
32  /**
33   * Tests for the statistics class
34   *
35   * @author Greg Luck
36   * @version $Id:JCacheStatisticsTest.java 318 2007-01-25 01:48:35Z gregluck $
37   */
38  public class JCacheStatisticsTest extends AbstractCacheTest {
39  
40  
41      private static final Log LOG = LogFactory.getLog(JCacheStatisticsTest.class.getName());
42  
43      /**
44       * Test statistics directly from Statistics Object
45       */
46      public void testStatisticsFromStatisticsObject() throws InterruptedException {
47          //Set size so the second element overflows to disk.
48          Ehcache ehcache = new net.sf.ehcache.Cache("testStatistics", 1, true, false, 5, 2);
49          manager.addCache(ehcache);
50          JCache cache = new JCache(ehcache, null);
51          exerciseStatistics(cache);
52  
53          //Exercise aftter setting accuracy
54          ehcache = new net.sf.ehcache.Cache("testStatistics2", 1, true, false, 5, 2);
55          manager.addCache(ehcache);
56          cache = new JCache(ehcache, null);
57          cache.setStatisticsAccuracy(CacheStatistics.STATISTICS_ACCURACY_NONE);
58          exerciseStatistics(cache);
59  
60          ehcache = new net.sf.ehcache.Cache("testStatistics4", 1, true, false, 5, 2);
61          manager.addCache(ehcache);
62          cache = new JCache(ehcache, null);
63          cache.setStatisticsAccuracy(CacheStatistics.STATISTICS_ACCURACY_BEST_EFFORT);
64          exerciseStatistics(cache);
65  
66      }
67  
68      private void exerciseStatistics(Cache cache) throws InterruptedException {
69          cache.put("key1", "value1");
70          cache.put("key2", "value1");
71          //key1 should be in the Disk Store
72          cache.get("key1");
73  
74          CacheStatistics statistics = cache.getCacheStatistics();
75          assertEquals(1, statistics.getCacheHits());
76          assertEquals(0, statistics.getCacheMisses());
77  
78          //key 1 should now be in the LruMemoryStore
79          cache.get("key1");
80  
81          statistics = cache.getCacheStatistics();
82          assertEquals(2, statistics.getCacheHits());
83          assertEquals(0, statistics.getCacheMisses());
84  
85          //Let the idle expire
86          Thread.sleep(5020);
87  
88          //key 1 should now be expired
89          cache.get("key1");
90          statistics = cache.getCacheStatistics();
91          assertEquals(2, statistics.getCacheHits());
92          assertEquals(2, statistics.getCacheMisses());
93          assertNotNull(statistics.toString());
94      }
95  
96  
97      /**
98       * CacheStatistics should always be sensible when the cache has not started.
99       */
100     public void testCacheStatisticsDegradesElegantlyWhenCacheDisposed() {
101         Ehcache ehcache = new net.sf.ehcache.Cache("test", 1, true, false, 5, 2);
102         Cache cache = new JCache(ehcache, null);
103         try {
104             CacheStatistics statistics = cache.getCacheStatistics();
105             fail();
106         } catch (IllegalStateException e) {
107             assertEquals("The test Cache is not alive.", e.getMessage());
108         }
109 
110     }
111 
112 
113     /**
114      * We want to be able to use Statistics as a value object.
115      * We need to do some magic with the reference held to Cache
116      */
117     public void testSerialization() throws IOException, ClassNotFoundException {
118 
119         Ehcache ehcache = new net.sf.ehcache.Cache("test", 1, true, false, 5, 2);
120         manager.addCache(ehcache);
121         Cache cache = new JCache(ehcache, null);
122         cache.put("key1", "value1");
123         cache.put("key2", "value1");
124         cache.get("key1");
125         cache.get("key1");
126 
127         CacheStatistics statistics = cache.getCacheStatistics();
128         assertEquals(2, statistics.getCacheHits());
129         assertEquals(0, statistics.getCacheMisses());
130         assertEquals(CacheStatistics.STATISTICS_ACCURACY_BEST_EFFORT, statistics.getStatisticsAccuracy());
131         statistics.clearStatistics();
132 
133 
134         ByteArrayOutputStream bout = new ByteArrayOutputStream();
135         ObjectOutputStream oos = new ObjectOutputStream(bout);
136         oos.writeObject(statistics);
137         byte[] serializedValue = bout.toByteArray();
138         oos.close();
139         CacheStatistics afterDeserializationStatistics = null;
140         ByteArrayInputStream bin = new ByteArrayInputStream(serializedValue);
141         ObjectInputStream ois = new ObjectInputStream(bin);
142         afterDeserializationStatistics = (CacheStatistics) ois.readObject();
143         ois.close();
144 
145         //Check after Serialization
146         assertEquals(2, afterDeserializationStatistics.getCacheHits());
147         assertEquals(0, afterDeserializationStatistics.getCacheMisses());
148         assertEquals(CacheStatistics.STATISTICS_ACCURACY_BEST_EFFORT, statistics.getStatisticsAccuracy());
149         statistics.clearStatistics();
150 
151     }
152 
153 
154     /**
155      * Test statistics directly from Statistics Object
156      */
157     public void testClearStatistics() throws InterruptedException {
158         //Set size so the second element overflows to disk.
159         Ehcache ehcache = new net.sf.ehcache.Cache("test", 1, true, false, 5, 2);
160         manager.addCache(ehcache);
161         Cache cache = new JCache(ehcache, null);
162 
163         cache.put("key1", "value1");
164         cache.put("key2", "value1");
165         //key1 should be in the Disk Store
166         cache.get("key1");
167 
168         CacheStatistics statistics = cache.getCacheStatistics();
169         assertEquals(1, statistics.getCacheHits());
170         assertEquals(0, statistics.getCacheMisses());
171 
172         //clear stats
173         statistics.clearStatistics();
174         statistics = cache.getCacheStatistics();
175         assertEquals(0, statistics.getCacheHits());
176         assertEquals(0, statistics.getCacheMisses());
177     }
178 
179 
180 }