1
2
3
4
5
6
7
8
9
10
11
12
13
14
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
34
35
36
37
38 public class JCacheStatisticsTest extends AbstractCacheTest {
39
40
41 private static final Log LOG = LogFactory.getLog(JCacheStatisticsTest.class.getName());
42
43
44
45
46 public void testStatisticsFromStatisticsObject() throws InterruptedException {
47
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
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
72 cache.get("key1");
73
74 CacheStatistics statistics = cache.getCacheStatistics();
75 assertEquals(1, statistics.getCacheHits());
76 assertEquals(0, statistics.getCacheMisses());
77
78
79 cache.get("key1");
80
81 statistics = cache.getCacheStatistics();
82 assertEquals(2, statistics.getCacheHits());
83 assertEquals(0, statistics.getCacheMisses());
84
85
86 Thread.sleep(5020);
87
88
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
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
115
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
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
156
157 public void testClearStatistics() throws InterruptedException {
158
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
166 cache.get("key1");
167
168 CacheStatistics statistics = cache.getCacheStatistics();
169 assertEquals(1, statistics.getCacheHits());
170 assertEquals(0, statistics.getCacheMisses());
171
172
173 statistics.clearStatistics();
174 statistics = cache.getCacheStatistics();
175 assertEquals(0, statistics.getCacheHits());
176 assertEquals(0, statistics.getCacheMisses());
177 }
178
179
180 }