001: /*
002: * Copyright 2005-2007 the original author or authors.
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: */
016: package net.sf.dozer.util.mapping.cache;
017:
018: import net.sf.dozer.util.mapping.AbstractDozerTest;
019:
020: /**
021: * @author tierney.matt
022: */
023: public class CacheTest extends AbstractDozerTest {
024:
025: public void testPutGetFromCache() throws Exception {
026: Cache cache = new Cache(getRandomString(), 50);
027: int numCacheEntriesToAdd = 45;
028: for (int i = 0; i < numCacheEntriesToAdd; i++) {
029: Object key = CacheKeyFactory.createKey(new Object[] {
030: "testkey", String.valueOf(i) });
031:
032: assertNull("cache entry should not already exist", cache
033: .get(key));
034:
035: CacheEntry cacheEntry = new CacheEntry(key, "testvalue" + i);
036: cache.put(cacheEntry);
037:
038: CacheEntry cacheEntry2 = cache.get(key);
039: assertNotNull("cache entry should not be null", cacheEntry2);
040: assertEquals("cache entries should be equal", cacheEntry,
041: cacheEntry2);
042: assertSame("cache entries should be same instance",
043: cacheEntry, cacheEntry2);
044: }
045: assertEquals("invlid cache size", numCacheEntriesToAdd, cache
046: .getSize());
047: assertEquals("invlid cache hit count", numCacheEntriesToAdd,
048: cache.getHitCount());
049: assertEquals("invlid cache miss count", numCacheEntriesToAdd,
050: cache.getMissCount());
051: }
052:
053: public void testMaximumCacheSize() throws Exception {
054: int maxSize = 25;
055: Cache cache = new Cache(getRandomString(), maxSize);
056: // Add a bunch of entries to cache to verify the cache doesnt grow larger than specified max size
057: for (int i = 0; i < maxSize + 125; i++) {
058: CacheEntry cacheEntry = new CacheEntry("testkey" + i,
059: "testvalue" + i);
060: cache.put(cacheEntry);
061: }
062: assertEquals("cache size should not exceed max size", maxSize,
063: cache.getSize());
064: }
065:
066: public void testMaximumCacheSize_Zero() throws Exception {
067: int maxSize = 0;
068: Cache cache = new Cache(getRandomString(), maxSize);
069: // Add a bunch of entries to cache to verify the cache doesnt grow larger than specified max size
070: for (int i = 0; i < maxSize + 5; i++) {
071: CacheEntry cacheEntry = new CacheEntry("testkey" + i,
072: "testvalue" + i);
073: cache.put(cacheEntry);
074: }
075: assertEquals("cache size should not exceed max size", maxSize,
076: cache.getSize());
077: }
078:
079: public void testClear() throws Exception {
080: Cache cache = new Cache(getRandomString(), 50);
081: Object key = CacheKeyFactory
082: .createKey(new Object[] { "testkey" });
083: CacheEntry cacheEntry = new CacheEntry(key, "testvalue");
084: cache.put(cacheEntry);
085:
086: assertEquals("cache should contain entry", 1, cache.getSize());
087: cache.clear();
088: assertEquals("cache should have been cleared", 0, cache
089: .getSize());
090: }
091:
092: public void testGetMaxSize() {
093: int maxSize = 550;
094: Cache cache = new Cache(getRandomString(), maxSize);
095:
096: assertEquals("invalid max size", maxSize, cache.getMaxSize());
097: }
098:
099: public void testGetNull() {
100: Cache cache = new Cache(getRandomString(), 5);
101: try {
102: cache.get(null);
103: fail("should have thrown exception");
104: } catch (IllegalArgumentException e) {
105: }
106: }
107:
108: public void testPutNull() {
109: Cache cache = new Cache(getRandomString(), 5);
110: try {
111: cache.put(null);
112: fail("should have thrown exception");
113: } catch (IllegalArgumentException e) {
114: }
115: }
116: }
|