001: package junit.api;
002:
003: import junit.support.EventRecordingCacheListener;
004: import junit.support.MethodInvocation;
005: import static org.junit.Assert.*;
006: import org.junit.Before;
007: import org.junit.Test;
008:
009: import javax.cache.Cache;
010: import javax.cache.CacheException;
011: import javax.cache.CacheListener;
012: import java.util.HashMap;
013: import java.util.LinkedList;
014: import java.util.List;
015: import java.util.Map;
016: import java.util.concurrent.CountDownLatch;
017:
018: /**
019: * Tests the {@link javax.cache.Cache} interface
020: *
021: * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
022: */
023: public class CacheTest extends CacheAPITestBase {
024: private EventRecordingCacheListener listener;
025:
026: @Before
027: public void addCacheListener() {
028: listener = new EventRecordingCacheListener();
029: cache.addListener(listener);
030: }
031:
032: @Test
033: public void lifecycle() {
034: // How to we test for Cache.State.STARTING when we don't have a mechanism to start the cache?
035: assertEquals(Cache.State.RUNNING, cache.getState());
036:
037: final CountDownLatch latch = new CountDownLatch(1);
038:
039: cache.addListener(new CacheListener() {
040:
041: public void onLoad(Object key) {
042: }
043:
044: public void onPut(Object key) {
045: }
046:
047: public void onEvict(Object key) {
048: }
049:
050: public void onRemove(Object key) {
051: }
052:
053: public void onClear() {
054: }
055:
056: public void onUpdate(Object key) {
057: }
058:
059: public void onExpiry(Object key) {
060: }
061:
062: public void onShutdown() {
063: try {
064: latch.await();
065: } catch (InterruptedException e) {
066:
067: }
068: }
069:
070: public void onLoadException(Object key, Exception e) {
071: }
072: });
073:
074: destroyCache();
075: assertEquals(Cache.State.STOPPING, cache.getState());
076: latch.countDown();
077: assertEquals(Cache.State.TERMINATED, cache.getState());
078:
079: List<MethodInvocation> expected = new LinkedList<MethodInvocation>();
080: expected.add(new MethodInvocation(listenerShutdownMethod));
081:
082: // shut down the cache
083: destroyCache();
084:
085: assertEquals(expected, listener.getEvents());
086: }
087:
088: @Test
089: public void invocationAfterShutdown() throws Exception {
090: destroyCache();
091: assertEquals(Cache.State.TERMINATED, cache.getState());
092:
093: try {
094: cache.get(key);
095: fail("Should have thrown exception");
096: } catch (IllegalStateException expected) {
097:
098: }
099:
100: try {
101: cache.remove(key);
102: fail("Should have thrown exception");
103: } catch (IllegalStateException expected) {
104:
105: }
106:
107: try {
108: cache.put(key, value);
109: fail("Should have thrown exception");
110: } catch (IllegalStateException expected) {
111:
112: }
113:
114: try {
115: cache.containsKey(key);
116: fail("Should have thrown exception");
117: } catch (IllegalStateException expected) {
118:
119: }
120:
121: try {
122: cache.containsValue(key);
123: fail("Should have thrown exception");
124: } catch (IllegalStateException expected) {
125:
126: }
127:
128: try {
129: cache.putAll(null);
130: fail("Should have thrown exception");
131: } catch (IllegalStateException expected) {
132:
133: }
134:
135: try {
136: cache.getAll(null);
137: fail("Should have thrown exception");
138: } catch (IllegalStateException expected) {
139:
140: }
141:
142: try {
143: cache.keySet();
144: fail("Should have thrown exception");
145: } catch (IllegalStateException expected) {
146:
147: }
148:
149: try {
150: cache.values();
151: fail("Should have thrown exception");
152: } catch (IllegalStateException expected) {
153:
154: }
155:
156: try {
157: cache.entrySet();
158: fail("Should have thrown exception");
159: } catch (IllegalStateException expected) {
160:
161: }
162:
163: try {
164: cache.clear();
165: fail("Should have thrown exception");
166: } catch (IllegalStateException expected) {
167:
168: }
169:
170: try {
171: cache.load(key);
172: fail("Should have thrown exception");
173: } catch (IllegalStateException expected) {
174:
175: }
176:
177: try {
178: cache.loadAll(null);
179: fail("Should have thrown exception");
180: } catch (IllegalStateException expected) {
181:
182: }
183:
184: try {
185: cache.evict();
186: fail("Should have thrown exception");
187: } catch (IllegalStateException expected) {
188:
189: }
190:
191: try {
192: cache.getCacheEntry(key);
193: fail("Should have thrown exception");
194: } catch (IllegalStateException expected) {
195:
196: }
197:
198: try {
199: cache.peek(key);
200: fail("Should have thrown exception");
201: } catch (IllegalStateException expected) {
202:
203: }
204:
205: try {
206: cache.isEmpty();
207: fail("Should have thrown exception");
208: } catch (IllegalStateException expected) {
209:
210: }
211:
212: try {
213: cache.size();
214: fail("Should have thrown exception");
215: } catch (IllegalStateException expected) {
216:
217: }
218:
219: }
220:
221: @Test
222: public void basicOperation() {
223: assertNull(cache.get(key));
224: assertNull(cache.put(key, value));
225: assertEquals(value, cache.get(key));
226: assertEquals(1, cache.size());
227: assertEquals(value, cache.remove(key));
228: assertNull(cache.get(key));
229: assertNull(cache.put(key, value));
230: assertEquals(value, cache.get(key));
231: assertEquals(1, cache.size());
232: assertEquals(value, cache.put(key, value + 2));
233: assertEquals(value + 2, cache.get(key));
234: assertEquals(1, cache.size());
235: cache.clear();
236: assertEquals(0, cache.size());
237: assertTrue(cache.isEmpty());
238: assertNull(cache.get(key));
239:
240: List<MethodInvocation> expected = new LinkedList<MethodInvocation>();
241: expected.add(new MethodInvocation(listenerPutMethod, key));
242: expected.add(new MethodInvocation(listenerRemoveMethod, key));
243: expected.add(new MethodInvocation(listenerPutMethod, key));
244: expected.add(new MethodInvocation(listenerUpdateMethod, key));
245: expected.add(new MethodInvocation(listenerRemoveMethod, key)); // for the cache.clear() call.
246:
247: assertEquals(expected, listener.getEvents());
248: }
249:
250: @Test
251: public void collectionOperations() throws CacheException {
252: Map data = new HashMap();
253: data.put("key1", "value1");
254: data.put("key2", "value2");
255:
256: assertTrue(cache.isEmpty());
257: cache.putAll(data);
258: assertEquals(data, cache.getAll(data.keySet()));
259: cache.clear();
260: assertTrue(cache.isEmpty());
261: }
262: }
|