001: package junit.api;
002:
003: import junit.CacheImplementationHelper;
004: import junit.support.EventRecordingCacheManagerListener;
005: import junit.support.MethodInvocation;
006: import static org.junit.Assert.*;
007: import org.junit.Before;
008: import org.junit.Test;
009:
010: import javax.cache.Cache;
011: import javax.cache.CacheException;
012: import javax.cache.CacheManager;
013: import javax.cache.CacheManagerListener;
014: import java.util.LinkedList;
015: import java.util.List;
016: import java.util.concurrent.CountDownLatch;
017:
018: /**
019: * Tests public {@link javax.cache.CacheManager} operations
020: *
021: * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
022: */
023: public class CacheManagerTest extends CacheAPITestBase {
024: private EventRecordingCacheManagerListener listener;
025:
026: @Before
027: public void addListener() {
028: listener = new EventRecordingCacheManagerListener();
029: cacheManager.addListener(listener);
030: }
031:
032: @Test
033: public void lifecycle() {
034: assertEquals(CacheManager.State.RUNNING, cacheManager
035: .getState());
036:
037: final CountDownLatch latch = new CountDownLatch(1);
038:
039: cacheManager.addListener(new CacheManagerListener() {
040: public void onRegister(String cacheName) {
041: }
042:
043: public void onUnregister(String cacheName) {
044: }
045:
046: public void stopping() {
047: try {
048: latch.await();
049: } catch (InterruptedException e) {
050:
051: }
052: }
053:
054: public void terminated() {
055: }
056: });
057:
058: cacheManager.shutdown();
059: assertEquals(CacheManager.State.STOPPING, cache.getState());
060: latch.countDown();
061: assertEquals(CacheManager.State.TERMINATED, cache.getState());
062:
063: // test that the listener was properly called.
064: List<MethodInvocation> expected = new LinkedList<MethodInvocation>();
065: expected.add(new MethodInvocation(
066: CacheAPITestBase.managerListenerStoppingMethod));
067: expected.add(new MethodInvocation(
068: CacheAPITestBase.managerListenerTerminatedMethod));
069:
070: assertEquals(expected, listener.getEvents());
071: }
072:
073: @Test
074: public void getCache() throws CacheException {
075: // should thi s return null or throw an exception?
076: assertNull(cacheManager.getCache("NonsensicalCache"));
077: assertFalse(cacheManager.cacheExists("NonsensicalCache"));
078:
079: assertTrue(cacheManager.cacheExists(cache.getName()));
080:
081: // test that the same cache instance is returned for multi ple calls
082: assertSame(cacheManager.getCache(cache.getName()), cacheManager
083: .getCache(cache.getName()));
084:
085: // how should we behave wi th nulls?
086: assertNull(cacheManager.getCache(null));
087: }
088:
089: @Test
090: public void createCache() throws CacheException {
091: String myCacheName = "myCache";
092: assertFalse(cacheManager.cacheExists(myCacheName));
093: cacheManager.newCache(myCacheName);
094: assertTrue(cacheManager.cacheExists(myCacheName));
095: assertNotNull(cacheManager.getCache(myCacheName));
096: }
097:
098: @Test
099: public void registerUnregister() throws CacheException {
100: String newCacheName = "newCache";
101: assertFalse(cacheManager.cacheExists(newCacheName));
102: Cache newCache = CacheImplementationHelper
103: .newCache(newCacheName);
104: cacheManager.registerCache(newCacheName, newCache);
105: assertTrue(cacheManager.cacheExists(newCacheName));
106: assertTrue(cacheManager.getCacheNames().contains(newCacheName));
107: cacheManager.unregisterCache(newCacheName);
108: assertFalse(cacheManager.cacheExists(newCacheName));
109: assertFalse(cacheManager.getCacheNames().contains(newCacheName));
110:
111: // test that the listener was properly called.
112: List<MethodInvocation> expected = new LinkedList<MethodInvocation>();
113: expected.add(new MethodInvocation(
114: CacheAPITestBase.managerListenerRegisterMethod,
115: newCacheName));
116: expected.add(new MethodInvocation(
117: CacheAPITestBase.managerListenerUnregisterMethod,
118: newCacheName));
119:
120: assertEquals(expected, listener.getEvents());
121: }
122: }
|