Source Code Cross Referenced for JCacheStatisticsTest.java in  » Cache » ehcache » net » sf » ehcache » jcache » Java Source Code / Java DocumentationJava Source Code and Java Documentation

Java Source Code / Java Documentation
1. 6.0 JDK Core
2. 6.0 JDK Modules
3. 6.0 JDK Modules com.sun
4. 6.0 JDK Modules com.sun.java
5. 6.0 JDK Modules sun
6. 6.0 JDK Platform
7. Ajax
8. Apache Harmony Java SE
9. Aspect oriented
10. Authentication Authorization
11. Blogger System
12. Build
13. Byte Code
14. Cache
15. Chart
16. Chat
17. Code Analyzer
18. Collaboration
19. Content Management System
20. Database Client
21. Database DBMS
22. Database JDBC Connection Pool
23. Database ORM
24. Development
25. EJB Server geronimo
26. EJB Server GlassFish
27. EJB Server JBoss 4.2.1
28. EJB Server resin 3.1.5
29. ERP CRM Financial
30. ESB
31. Forum
32. GIS
33. Graphic Library
34. Groupware
35. HTML Parser
36. IDE
37. IDE Eclipse
38. IDE Netbeans
39. Installer
40. Internationalization Localization
41. Inversion of Control
42. Issue Tracking
43. J2EE
44. JBoss
45. JMS
46. JMX
47. Library
48. Mail Clients
49. Net
50. Parser
51. PDF
52. Portal
53. Profiler
54. Project Management
55. Report
56. RSS RDF
57. Rule Engine
58. Science
59. Scripting
60. Search Engine
61. Security
62. Sevlet Container
63. Source Control
64. Swing Library
65. Template Engine
66. Test Coverage
67. Testing
68. UML
69. Web Crawler
70. Web Framework
71. Web Mail
72. Web Server
73. Web Services
74. Web Services apache cxf 2.0.1
75. Web Services AXIS2
76. Wiki Engine
77. Workflow Engines
78. XML
79. XML UI
Java
Java Tutorial
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
C# / C Sharp
C# / CSharp Tutorial
ASP.Net
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
PHP
Python
SQL Server / T-SQL
Oracle PL / SQL
Oracle PL/SQL Tutorial
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Java Source Code / Java Documentation » Cache » ehcache » net.sf.ehcache.jcache 
Source Cross Referenced  Class Diagram Java Document (Java Doc) 


001:        /**
002:         *  Copyright 2003-2007 Luck Consulting Pty Ltd
003:         *
004:         *  Licensed under the Apache License, Version 2.0 (the "License");
005:         *  you may not use this file except in compliance with the License.
006:         *  You may obtain a copy of the License at
007:         *
008:         *      http://www.apache.org/licenses/LICENSE-2.0
009:         *
010:         *  Unless required by applicable law or agreed to in writing, software
011:         *  distributed under the License is distributed on an "AS IS" BASIS,
012:         *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013:         *  See the License for the specific language governing permissions and
014:         *  limitations under the License.
015:         */package net.sf.ehcache.jcache;
016:
017:        import net.sf.ehcache.AbstractCacheTest;
018:        import net.sf.ehcache.Ehcache;
019:        import net.sf.ehcache.Element;
020:        import net.sf.jsr107cache.Cache;
021:        import net.sf.jsr107cache.CacheStatistics;
022:        import org.apache.commons.logging.Log;
023:        import org.apache.commons.logging.LogFactory;
024:
025:        import java.io.ByteArrayInputStream;
026:        import java.io.ByteArrayOutputStream;
027:        import java.io.IOException;
028:        import java.io.ObjectInputStream;
029:        import java.io.ObjectOutputStream;
030:
031:        /**
032:         * Tests for the statistics class
033:         *
034:         * @author Greg Luck
035:         * @version $Id:JCacheStatisticsTest.java 318 2007-01-25 01:48:35Z gregluck $
036:         */
037:        public class JCacheStatisticsTest extends AbstractCacheTest {
038:
039:            private static final Log LOG = LogFactory
040:                    .getLog(JCacheStatisticsTest.class.getName());
041:
042:            /**
043:             * Test statistics directly from Statistics Object
044:             */
045:            public void testStatisticsFromStatisticsObject()
046:                    throws InterruptedException {
047:                //Set size so the second element overflows to disk.
048:                Ehcache ehcache = new net.sf.ehcache.Cache("testStatistics", 1,
049:                        true, false, 5, 2);
050:                manager.addCache(ehcache);
051:                JCache cache = new JCache(ehcache, null);
052:                exerciseStatistics(cache);
053:
054:                //Exercise aftter setting accuracy
055:                ehcache = new net.sf.ehcache.Cache("testStatistics2", 1, true,
056:                        false, 5, 2);
057:                manager.addCache(ehcache);
058:                cache = new JCache(ehcache, null);
059:                cache
060:                        .setStatisticsAccuracy(CacheStatistics.STATISTICS_ACCURACY_NONE);
061:                exerciseStatistics(cache);
062:
063:                ehcache = new net.sf.ehcache.Cache("testStatistics4", 1, true,
064:                        false, 5, 2);
065:                manager.addCache(ehcache);
066:                cache = new JCache(ehcache, null);
067:                cache
068:                        .setStatisticsAccuracy(CacheStatistics.STATISTICS_ACCURACY_BEST_EFFORT);
069:                exerciseStatistics(cache);
070:
071:            }
072:
073:            private void exerciseStatistics(Cache cache)
074:                    throws InterruptedException {
075:                cache.put("key1", "value1");
076:                cache.put("key2", "value1");
077:                //key1 should be in the Disk Store
078:                cache.get("key1");
079:
080:                CacheStatistics statistics = cache.getCacheStatistics();
081:                assertEquals(1, statistics.getCacheHits());
082:                assertEquals(0, statistics.getCacheMisses());
083:
084:                //key 1 should now be in the LruMemoryStore
085:                cache.get("key1");
086:
087:                statistics = cache.getCacheStatistics();
088:                assertEquals(2, statistics.getCacheHits());
089:                assertEquals(0, statistics.getCacheMisses());
090:
091:                //Let the idle expire
092:                Thread.sleep(5020);
093:
094:                //key 1 should now be expired
095:                cache.get("key1");
096:                statistics = cache.getCacheStatistics();
097:                assertEquals(2, statistics.getCacheHits());
098:                assertEquals(2, statistics.getCacheMisses());
099:                assertNotNull(statistics.toString());
100:            }
101:
102:            /**
103:             * CacheStatistics should always be sensible when the cache has not started.
104:             */
105:            public void testCacheStatisticsDegradesElegantlyWhenCacheDisposed() {
106:                Ehcache ehcache = new net.sf.ehcache.Cache("test", 1, true,
107:                        false, 5, 2);
108:                Cache cache = new JCache(ehcache, null);
109:                try {
110:                    CacheStatistics statistics = cache.getCacheStatistics();
111:                    fail();
112:                } catch (IllegalStateException e) {
113:                    assertEquals("The test Cache is not alive.", e.getMessage());
114:                }
115:
116:            }
117:
118:            /**
119:             * We want to be able to use Statistics as a value object.
120:             * We need to do some magic with the reference held to Cache
121:             */
122:            public void testSerialization() throws IOException,
123:                    ClassNotFoundException {
124:
125:                Ehcache ehcache = new net.sf.ehcache.Cache("test", 1, true,
126:                        false, 5, 2);
127:                manager.addCache(ehcache);
128:                Cache cache = new JCache(ehcache, null);
129:                cache.put("key1", "value1");
130:                cache.put("key2", "value1");
131:                cache.get("key1");
132:                cache.get("key1");
133:
134:                CacheStatistics statistics = cache.getCacheStatistics();
135:                assertEquals(2, statistics.getCacheHits());
136:                assertEquals(0, statistics.getCacheMisses());
137:                assertEquals(CacheStatistics.STATISTICS_ACCURACY_BEST_EFFORT,
138:                        statistics.getStatisticsAccuracy());
139:                statistics.clearStatistics();
140:
141:                ByteArrayOutputStream bout = new ByteArrayOutputStream();
142:                ObjectOutputStream oos = new ObjectOutputStream(bout);
143:                oos.writeObject(statistics);
144:                byte[] serializedValue = bout.toByteArray();
145:                oos.close();
146:                CacheStatistics afterDeserializationStatistics = null;
147:                ByteArrayInputStream bin = new ByteArrayInputStream(
148:                        serializedValue);
149:                ObjectInputStream ois = new ObjectInputStream(bin);
150:                afterDeserializationStatistics = (CacheStatistics) ois
151:                        .readObject();
152:                ois.close();
153:
154:                //Check after Serialization
155:                assertEquals(2, afterDeserializationStatistics.getCacheHits());
156:                assertEquals(0, afterDeserializationStatistics.getCacheMisses());
157:                assertEquals(CacheStatistics.STATISTICS_ACCURACY_BEST_EFFORT,
158:                        statistics.getStatisticsAccuracy());
159:                statistics.clearStatistics();
160:
161:            }
162:
163:            /**
164:             * Test statistics directly from Statistics Object
165:             */
166:            public void testClearStatistics() throws InterruptedException {
167:                //Set size so the second element overflows to disk.
168:                Ehcache ehcache = new net.sf.ehcache.Cache("test", 1, true,
169:                        false, 5, 2);
170:                manager.addCache(ehcache);
171:                Cache cache = new JCache(ehcache, null);
172:
173:                cache.put("key1", "value1");
174:                cache.put("key2", "value1");
175:                //key1 should be in the Disk Store
176:                cache.get("key1");
177:
178:                CacheStatistics statistics = cache.getCacheStatistics();
179:                assertEquals(1, statistics.getCacheHits());
180:                assertEquals(0, statistics.getCacheMisses());
181:
182:                //clear stats
183:                statistics.clearStatistics();
184:                statistics = cache.getCacheStatistics();
185:                assertEquals(0, statistics.getCacheHits());
186:                assertEquals(0, statistics.getCacheMisses());
187:            }
188:
189:            /**
190:             * Tests average get time
191:             */
192:            public void testAverageGetTime() {
193:                //set to 0 to make it run slow
194:                Ehcache ehcache = new net.sf.ehcache.Cache("test", 0, true,
195:                        false, 5, 2);
196:                manager.addCache(ehcache);
197:                Cache cache = new JCache(ehcache, null);
198:                JCacheStatistics statistics = (JCacheStatistics) cache
199:                        .getCacheStatistics();
200:                float averageGetTime = statistics.getAverageGetTime();
201:                assertTrue(0 == statistics.getAverageGetTime());
202:
203:                for (int i = 0; i < 10000; i++) {
204:                    ehcache.put(new Element("" + i, "value1"));
205:                }
206:                ehcache.put(new Element("key1", "value1"));
207:                ehcache.put(new Element("key2", "value1"));
208:                for (int i = 0; i < 110000; i++) {
209:                    ehcache.get("" + i);
210:                }
211:
212:                statistics = (JCacheStatistics) cache.getCacheStatistics();
213:                averageGetTime = statistics.getAverageGetTime();
214:                assertTrue(averageGetTime >= .05);
215:                statistics.clearStatistics();
216:                statistics = (JCacheStatistics) cache.getCacheStatistics();
217:                assertTrue(0 == statistics.getAverageGetTime());
218:            }
219:
220:            /**
221:             * Tests eviction statistics
222:             */
223:            public void testEvictionStatistics() throws InterruptedException {
224:                //set to 0 to make it run slow
225:                Ehcache ehcache = new net.sf.ehcache.Cache("test", 10, false,
226:                        false, 2, 2);
227:                manager.addCache(ehcache);
228:                Cache cache = new JCache(ehcache, null);
229:                JCacheStatistics statistics = (JCacheStatistics) cache
230:                        .getCacheStatistics();
231:                assertEquals(0, statistics.getEvictionCount());
232:
233:                for (int i = 0; i < 10000; i++) {
234:                    ehcache.put(new Element("" + i, "value1"));
235:                }
236:                statistics = (JCacheStatistics) cache.getCacheStatistics();
237:                assertEquals(9990, statistics.getEvictionCount());
238:
239:                Thread.sleep(2010);
240:
241:                //expiries do not count
242:                statistics = (JCacheStatistics) cache.getCacheStatistics();
243:                assertEquals(9990, statistics.getEvictionCount());
244:
245:                statistics.clearStatistics();
246:
247:                statistics = (JCacheStatistics) cache.getCacheStatistics();
248:                assertEquals(0, statistics.getEvictionCount());
249:
250:            }
251:
252:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.