KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.util.HashSet JavaDoc;
19 import java.util.Set JavaDoc;
20
21 import net.sf.dozer.util.mapping.DozerTestBase;
22 import net.sf.dozer.util.mapping.MappingException;
23 import net.sf.dozer.util.mapping.exception.NotFoundException;
24
25 /**
26  * @author tierney.matt
27  */

28 public class DozerCacheManagerTest extends DozerTestBase {
29   private DozerCacheManager cacheMgr;
30   
31   protected void setUp() throws Exception JavaDoc {
32     super.setUp();
33     cacheMgr = DozerCacheManager.createNew();
34   }
35   
36   public void testCreateNew() throws Exception JavaDoc {
37     DozerCacheManager cacheMgr2 = DozerCacheManager.createNew();
38     
39     assertFalse("cache mgrs should not be equal", cacheMgr.equals(cacheMgr2));
40     assertNotSame("cache mgrs should not be same instance", cacheMgr, cacheMgr2);
41   }
42   
43   public void testGetInstance() throws Exception JavaDoc {
44     DozerCacheManager cacheMgr = DozerCacheManager.getInstance();
45     DozerCacheManager cacheMgr2 = DozerCacheManager.getInstance();
46     
47     assertEquals("cache mgrs should be equal", cacheMgr, cacheMgr2);
48     assertSame("cache mgrs should be the same instance", cacheMgr, cacheMgr2);
49   }
50   
51   public void testAddGetExistsCache() throws Exception JavaDoc {
52     String JavaDoc cacheName = getRandomString();
53     cacheMgr.addCache(cacheName, 1);
54     
55     boolean cacheExists = cacheMgr.cacheExists(cacheName);
56     assertTrue("cache should exist", cacheExists);
57     
58     Cache cache = cacheMgr.getCache(cacheName);
59     assertNotNull("cache should not be null", cache);
60     assertEquals("cache should be empty", cache.getSize(), 0);
61     assertEquals("invalid cache name", cacheName, cache.getName());
62   }
63   
64   public void testGetUnknownCache() throws Exception JavaDoc {
65     String JavaDoc cacheName = getRandomString();
66     boolean cacheExists = cacheMgr.cacheExists(cacheName);
67     assertFalse("cache should not exist", cacheExists);
68     
69     try {
70       cacheMgr.getCache(cacheName);
71       fail("trying to get an unknown cache should have thrown a MappingException");
72     } catch (NotFoundException e) {
73     }
74   }
75   
76   public void testAddDuplicateCachesSingleton() throws Exception JavaDoc {
77     String JavaDoc cacheName = getRandomString();
78     cacheMgr.addCache(cacheName, 1);
79     
80     try {
81       //try adding it again
82
cacheMgr.addCache(cacheName, 1);
83       fail("trying to add duplicate caches should have thrown an ObjectExistsException");
84     } catch (MappingException e) {
85     }
86   }
87   
88   public void testAddDuplicateCachesNonSingleton() throws Exception JavaDoc {
89     //You should be able to add caches with the same name to non singleton instances
90
//of the cache manager because they each have their own copies of caches to manage.
91
//The caches are uniquely identified by the cache managers by using the instance id.
92
DozerCacheManager cacheMgr2 = DozerCacheManager.createNew();
93
94     //add cache to each cache mgr instance
95
String JavaDoc cacheName = getRandomString();
96     cacheMgr.addCache(cacheName, 1);
97     cacheMgr2.addCache(cacheName, 1);
98     
99     assertTrue("cache should exist in cache mgr1", cacheMgr.cacheExists(cacheName));
100     assertTrue("cache should also exist in cache mgr2", cacheMgr2.cacheExists(cacheName));
101     
102     Cache cache1 = cacheMgr.getCache(cacheName);
103     Cache cache2 = cacheMgr2.getCache(cacheName);
104     
105     assertFalse("caches should not be the same instance", cache1 == cache2);
106     assertEquals("invalid cache name", cacheName, cache1.getName());
107     assertEquals("invalid cache name for cache2", cacheName, cache2.getName());
108   }
109   
110   public void testGetStatisticTypes() {
111     String JavaDoc name = getRandomString();
112     String JavaDoc name2 = name + "-2";
113     cacheMgr.addCache(name, 100);
114     cacheMgr.addCache(name2, 100);
115     
116     Set JavaDoc expected = new HashSet JavaDoc();
117     expected.add(name);
118     expected.add(name2);
119
120     assertEquals("invalid cache names types found", expected, cacheMgr.getCacheNames());
121   }
122   
123   public void testClearAllCacheEntries() {
124     String JavaDoc name = getRandomString();
125     Cache cache = new Cache(name, 5);
126     CacheEntry entry = new CacheEntry(getRandomString(), "value");
127     cache.put(entry);
128     cacheMgr.addCache(cache);
129     
130     assertEquals("invalid initial cache entry size", 1, cacheMgr.getCache(name).getEntries().size());
131     cacheMgr.clearAllEntries();
132     assertEquals("invalid cache entry size after clearAll", 0, cacheMgr.getCache(name).getEntries().size());
133   }
134   
135   public void testGetCaches() {
136     String JavaDoc 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 JavaDoc expected = new HashSet JavaDoc();
143     expected.add(cache);
144     expected.add(cache2);
145     
146     assertEquals("invalid caches found", expected, cacheMgr.getCaches());
147   }
148 }
149
Popular Tags