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 java.util.HashSet;
019: import java.util.Set;
020:
021: import net.sf.dozer.util.mapping.AbstractDozerTest;
022: import net.sf.dozer.util.mapping.MappingException;
023:
024: /**
025: * @author tierney.matt
026: */
027: public class DozerCacheManagerTest extends AbstractDozerTest {
028: private DozerCacheManager cacheMgr;
029:
030: protected void setUp() throws Exception {
031: super .setUp();
032: cacheMgr = new DozerCacheManager();
033: }
034:
035: public void testCreateNew() throws Exception {
036: DozerCacheManager cacheMgr2 = new DozerCacheManager();
037:
038: assertFalse("cache mgrs should not be equal", cacheMgr
039: .equals(cacheMgr2));
040: assertNotSame("cache mgrs should not be same instance",
041: cacheMgr, cacheMgr2);
042: }
043:
044: public void testAddGetExistsCache() throws Exception {
045: String cacheName = getRandomString();
046: cacheMgr.addCache(cacheName, 1);
047:
048: boolean cacheExists = cacheMgr.cacheExists(cacheName);
049: assertTrue("cache should exist", cacheExists);
050:
051: Cache cache = cacheMgr.getCache(cacheName);
052: assertNotNull("cache should not be null", cache);
053: assertEquals("cache should be empty", cache.getSize(), 0);
054: assertEquals("invalid cache name", cacheName, cache.getName());
055: }
056:
057: public void testGetUnknownCache() throws Exception {
058: String cacheName = getRandomString();
059: boolean cacheExists = cacheMgr.cacheExists(cacheName);
060: assertFalse("cache should not exist", cacheExists);
061:
062: try {
063: cacheMgr.getCache(cacheName);
064: fail("trying to get an unknown cache should have thrown a MappingException");
065: } catch (MappingException e) {
066: }
067: }
068:
069: public void testAddDuplicateCachesSingleton() throws Exception {
070: String cacheName = getRandomString();
071: cacheMgr.addCache(cacheName, 1);
072:
073: try {
074: // try adding it again
075: cacheMgr.addCache(cacheName, 1);
076: fail("trying to add duplicate caches should have thrown an ObjectExistsException");
077: } catch (MappingException e) {
078: }
079: }
080:
081: public void testAddDuplicateCachesNonSingleton() throws Exception {
082: // You should be able to add caches with the same name to non singleton instances
083: // of the cache manager because they each have their own copies of caches to manage.
084: // The caches are uniquely identified by the cache managers by using the instance id.
085: DozerCacheManager cacheMgr2 = new DozerCacheManager();
086:
087: // add cache to each cache mgr instance
088: String cacheName = getRandomString();
089: cacheMgr.addCache(cacheName, 1);
090: cacheMgr2.addCache(cacheName, 1);
091:
092: assertTrue("cache should exist in cache mgr1", cacheMgr
093: .cacheExists(cacheName));
094: assertTrue("cache should also exist in cache mgr2", cacheMgr2
095: .cacheExists(cacheName));
096:
097: Cache cache1 = cacheMgr.getCache(cacheName);
098: Cache cache2 = cacheMgr2.getCache(cacheName);
099:
100: assertFalse("caches should not be the same instance",
101: cache1 == cache2);
102: assertEquals("invalid cache name", cacheName, cache1.getName());
103: assertEquals("invalid cache name for cache2", cacheName, cache2
104: .getName());
105: }
106:
107: public void testGetStatisticTypes() {
108: String name = getRandomString();
109: String name2 = name + "-2";
110: cacheMgr.addCache(name, 100);
111: cacheMgr.addCache(name2, 100);
112:
113: Set expected = new HashSet();
114: expected.add(name);
115: expected.add(name2);
116:
117: assertEquals("invalid cache names types found", expected,
118: cacheMgr.getCacheNames());
119: }
120:
121: public void testClearAllCacheEntries() {
122: String name = getRandomString();
123: Cache cache = new Cache(name, 5);
124: CacheEntry entry = new CacheEntry(getRandomString(), "value");
125: cache.put(entry);
126: cacheMgr.addCache(cache);
127:
128: assertEquals("invalid initial cache entry size", 1, cacheMgr
129: .getCache(name).getEntries().size());
130: cacheMgr.clearAllEntries();
131: assertEquals("invalid cache entry size after clearAll", 0,
132: cacheMgr.getCache(name).getEntries().size());
133: }
134:
135: public void testGetCaches() {
136: String name = getRandomString();
137: Cache cache = new Cache(name, 5);
138: Cache cache2 = new Cache(name + "2", 5);
139: cacheMgr.addCache(cache);
140: cacheMgr.addCache(cache2);
141:
142: Set expected = new HashSet();
143: expected.add(cache);
144: expected.add(cache2);
145:
146: assertEquals("invalid caches found", expected, cacheMgr
147: .getCaches());
148: }
149: }
|