Source Code Cross Referenced for JCacheEntryTest.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 org.apache.commons.logging.Log;
018:        import org.apache.commons.logging.LogFactory;
019:
020:        import net.sf.jsr107cache.CacheManager;
021:        import net.sf.jsr107cache.Cache;
022:        import net.sf.jsr107cache.CacheException;
023:        import net.sf.jsr107cache.CacheEntry;
024:
025:        import net.sf.ehcache.AbstractCacheTest;
026:        import net.sf.ehcache.Element;
027:
028:        import java.util.Map;
029:        import java.util.HashMap;
030:        import java.util.Set;
031:
032:        /**
033:         * Tests for CacheEntry
034:         *
035:         * @author Greg Luck
036:         * @version $Id:JCacheEntryTest.java 318 2007-01-25 01:48:35Z gregluck $
037:         */
038:        public class JCacheEntryTest extends AbstractCacheTest {
039:            private static final Log LOG = LogFactory.getLog(JCacheTest.class
040:                    .getName());
041:
042:            /**
043:             * teardown
044:             * limits to what we can do here under jsr107
045:             */
046:            protected void tearDown() throws Exception {
047:                getTestCache().clear();
048:            }
049:
050:            private Cache getTestCache() throws CacheException {
051:                Cache cache = CacheManager.getInstance().getCache(
052:                        "testCacheEntry");
053:                if (cache == null) {
054:                    Map env = new HashMap();
055:                    env.put("name", "testCacheEntry");
056:                    env.put("maxElementsInMemory", "1");
057:                    env.put("overflowToDisk", "true");
058:                    env.put("eternal", "false");
059:                    env.put("timeToLiveSeconds", "1");
060:                    env.put("timeToIdleSeconds", "0");
061:                    cache = CacheManager.getInstance().getCacheFactory()
062:                            .createCache(env);
063:                    CacheManager.getInstance().registerCache("testCacheEntry",
064:                            cache);
065:                }
066:                return CacheManager.getInstance().getCache("testCacheEntry");
067:            }
068:
069:            /**
070:             * Test create and access times
071:             * jsr107 does not allow a CacheEntry to be put into a cache. So testing
072:             * recycling of elements is pointless.
073:             */
074:            public void testAccessTimes() throws Exception {
075:                Cache cache = getTestCache();
076:
077:                CacheEntry entry = new JCacheEntry(
078:                        new Element("key1", "value1"));
079:                long creationTime = entry.getCreationTime();
080:                assertTrue(entry.getCreationTime() > (System
081:                        .currentTimeMillis() - 500));
082:                assertTrue(entry.getHits() == 0);
083:                assertTrue(entry.getLastAccessTime() == 0);
084:
085:                cache.put(entry.getKey(), entry.getValue());
086:
087:                entry = cache.getCacheEntry("key1");
088:                long entryCreationTime = entry.getCreationTime();
089:                assertNotNull(entry);
090:                cache.get("key1");
091:                cache.get("key1");
092:                cache.get("key1");
093:                //check creation time does not change
094:                assertEquals(entryCreationTime, entry.getCreationTime());
095:                assertTrue(entry.getLastAccessTime() != 0);
096:                assertTrue(entry.getHits() == 4);
097:
098:            }
099:
100:            /**
101:             * This implementation does not have a notion of cost.
102:             */
103:            public void testCost() throws Exception {
104:                Cache cache = getTestCache();
105:
106:                CacheEntry entry = new JCacheEntry(
107:                        new Element("key1", "value1"));
108:                assertEquals(0, entry.getCost());
109:            }
110:
111:            /**
112:             * Check the expiry time is correct.
113:             */
114:            public void testExpirationTime() throws Exception {
115:
116:                Cache cache = getTestCache();
117:                CacheEntry entry = new JCacheEntry(
118:                        new Element("key1", "value1"));
119:                cache.put(entry.getKey(), entry.getValue());
120:                CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
121:
122:                //test expiry time s 1000ms after create time.
123:                assertTrue(retrievedEntry.getExpirationTime() > (System
124:                        .currentTimeMillis() + 995));
125:                assertTrue(retrievedEntry.getExpirationTime() < (System
126:                        .currentTimeMillis() + 1005));
127:            }
128:
129:            /**
130:             * Check the expiry time is correct.
131:             */
132:            public void testCannotSet() throws Exception {
133:
134:                Cache cache = getTestCache();
135:                CacheEntry entry = new JCacheEntry(
136:                        new Element("key1", "value1"));
137:                cache.put(entry.getKey(), entry.getValue());
138:                CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
139:
140:                try {
141:                    retrievedEntry.setValue("test value");
142:                } catch (UnsupportedOperationException e) {
143:                    assertEquals(
144:                            "Ehcache does not support modification of Elements. They are immutable.",
145:                            e.getMessage());
146:                }
147:
148:            }
149:
150:            /**
151:             * Each get should cause a hit.
152:             */
153:            public void testHits() throws Exception {
154:
155:                Cache cache = getTestCache();
156:                CacheEntry entry = new JCacheEntry(
157:                        new Element("key1", "value1"));
158:                assertEquals(0, entry.getHits());
159:
160:                cache.put(entry.getKey(), entry.getValue());
161:                CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
162:
163:                assertEquals(1, retrievedEntry.getHits());
164:
165:                cache.get(entry.getKey());
166:                retrievedEntry = cache.getCacheEntry(entry.getKey());
167:
168:                assertEquals(3, retrievedEntry.getHits());
169:
170:            }
171:
172:            /**
173:             * Last access time should be 0 if not accessed and then the last get time.
174:             */
175:            public void testLastAccessTime() throws Exception {
176:
177:                Cache cache = getTestCache();
178:                CacheEntry entry = new JCacheEntry(
179:                        new Element("key1", "value1"));
180:                assertEquals(0, entry.getLastAccessTime());
181:
182:                cache.put(entry.getKey(), entry.getValue());
183:                CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
184:
185:                //test access is in the last 5ms
186:                assertTrue(retrievedEntry.getLastAccessTime() <= System
187:                        .currentTimeMillis());
188:
189:            }
190:
191:            /**
192:             * 0 when first created. 0 when first put. 1 when replaced.
193:             */
194:            public void testLastUpdateTime() throws Exception {
195:
196:                Cache cache = getTestCache();
197:                CacheEntry entry = new JCacheEntry(
198:                        new Element("key1", "value1"));
199:                assertEquals(0, entry.getLastUpdateTime());
200:
201:                cache.put(entry.getKey(), entry.getValue());
202:                //update it
203:                cache.put(entry.getKey(), entry.getValue());
204:                CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
205:
206:                //test update is in the last 5ms
207:                assertTrue(retrievedEntry.getLastUpdateTime() > System
208:                        .currentTimeMillis() - 10);
209:                assertTrue(retrievedEntry.getLastUpdateTime() <= System
210:                        .currentTimeMillis());
211:
212:            }
213:
214:            /**
215:             * 0 when first created. 1 when first put. > 2 when replaced.
216:             */
217:            public void testVersion() throws Exception {
218:
219:                Cache cache = getTestCache();
220:                CacheEntry entry = new JCacheEntry(
221:                        new Element("key1", "value1"));
222:                assertEquals(1, entry.getVersion());
223:
224:                cache.put(entry.getKey(), entry.getValue());
225:                //update it
226:                cache.put(entry.getKey(), entry.getValue());
227:                CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
228:
229:                assertTrue(retrievedEntry.getVersion() >= 2);
230:            }
231:
232:            /**
233:             * valid when first created. valid if not expired, invalid if expired.
234:             */
235:            public void testIsValid() throws Exception {
236:
237:                Cache cache = getTestCache();
238:                CacheEntry entry = new JCacheEntry(
239:                        new Element("key1", "value1"));
240:                assertEquals(true, entry.isValid());
241:
242:                cache.put(entry.getKey(), entry.getValue());
243:                CacheEntry retrievedEntry = cache.getCacheEntry(entry.getKey());
244:                assertEquals(true, retrievedEntry.isValid());
245:
246:                Thread.sleep(1020);
247:                assertEquals(false, retrievedEntry.isValid());
248:
249:            }
250:
251:            /**
252:             * Test getting the entry set
253:             */
254:            public void testEntrySet() throws Exception {
255:
256:                JCache jcache = (JCache) getTestCache();
257:                CacheEntry entry = new JCacheEntry(
258:                        new Element("key1", "value1"));
259:                assertEquals(true, entry.isValid());
260:
261:                jcache.put(entry.getKey(), entry.getValue());
262:
263:                //Entry Set works
264:                Set entries = jcache.entrySet();
265:                assertEquals(1, entries.size());
266:
267:                //Entry Set is not live
268:                jcache.remove("key1");
269:                assertEquals(1, entries.size());
270:
271:            }
272:
273:        }
www.java2java.com | Contact Us
Copyright 2010 - 2030 Java Source and Support. All rights reserved.
All other trademarks are property of their respective owners.