KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > sf > dozer > util > mapping > cache > CacheTest


1 /*
2  * Copyright 2005-2007 the original author or authors.
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 net.sf.dozer.util.mapping.cache;
17
18 import net.sf.dozer.util.mapping.DozerTestBase;
19
20 /**
21  * @author tierney.matt
22  */

23 public class CacheTest extends DozerTestBase {
24
25   public void testPutGetFromCache() throws Exception JavaDoc {
26     Cache cache = new Cache(getRandomString(), 50);
27     int numCacheEntriesToAdd = 45;
28     for (int i = 0; i < numCacheEntriesToAdd; i++) {
29       Object JavaDoc key = CacheKeyFactory.createKey(new Object JavaDoc[] {"testkey",String.valueOf(i)});
30       
31       assertNull("cache entry should not already exist", cache.get(key));
32       
33       CacheEntry cacheEntry = new CacheEntry(key, "testvalue" + i);
34       cache.put(cacheEntry);
35       
36       CacheEntry cacheEntry2 = cache.get(key);
37       assertNotNull("cache entry should not be null", cacheEntry2);
38       assertEquals("cache entries should be equal", cacheEntry, cacheEntry2);
39       assertSame("cache entries should be same instance", cacheEntry, cacheEntry2);
40     }
41     assertEquals("invlid cache size", numCacheEntriesToAdd, cache.getSize());
42     assertEquals("invlid cache hit count", numCacheEntriesToAdd, cache.getHitCount());
43     assertEquals("invlid cache miss count", numCacheEntriesToAdd, cache.getMissCount());
44   }
45   
46   public void testMaximumCacheSize() throws Exception JavaDoc {
47     int maxSize = 25;
48     Cache cache = new Cache(getRandomString(), maxSize);
49     //Add a bunch of entries to cache to verify the cache doesnt grow larger than specified max size
50
for (int i = 0; i < maxSize + 125; i++) {
51       CacheEntry cacheEntry = new CacheEntry("testkey" + i, "testvalue" + i);
52       cache.put(cacheEntry);
53     }
54     assertEquals("cache size should not exceed max size", maxSize, cache.getSize());
55   }
56   
57   public void testMaximumCacheSize_Zero() throws Exception JavaDoc {
58     int maxSize = 0;
59     Cache cache = new Cache(getRandomString(), maxSize);
60     //Add a bunch of entries to cache to verify the cache doesnt grow larger than specified max size
61
for (int i = 0; i < maxSize + 5; i++) {
62       CacheEntry cacheEntry = new CacheEntry("testkey" + i, "testvalue" + i);
63       cache.put(cacheEntry);
64     }
65     assertEquals("cache size should not exceed max size", maxSize, cache.getSize());
66   }
67   
68   public void testClear() throws Exception JavaDoc {
69     Cache cache = new Cache(getRandomString(), 50);
70     Object JavaDoc key = CacheKeyFactory.createKey(new Object JavaDoc[] {"testkey"});
71     CacheEntry cacheEntry = new CacheEntry(key, "testvalue");
72     cache.put(cacheEntry);
73     
74     assertEquals("cache should contain entry", 1, cache.getSize());
75     cache.clear();
76     assertEquals("cache should have been cleared", 0, cache.getSize());
77   }
78   
79   public void testGetMaxSize() {
80     int maxSize = 550;
81     Cache cache = new Cache(getRandomString(), maxSize);
82     
83     assertEquals("invalid max size", maxSize, cache.getMaxSize());
84   }
85   
86   public void testGetNull() {
87     Cache cache = new Cache(getRandomString(), 5);
88     try {
89       cache.get(null);
90       fail("should have thrown exception");
91     } catch (IllegalArgumentException JavaDoc e) {
92     }
93   }
94   
95   public void testPutNull() {
96     Cache cache = new Cache(getRandomString(), 5);
97     try {
98       cache.put(null);
99       fail("should have thrown exception");
100     } catch (IllegalArgumentException JavaDoc e) {
101     }
102   }
103 }
104
Popular Tags