001: package junit.api;
002:
003: import junit.CacheImplementationHelper;
004: import junit.support.FailingCacheLoader;
005: import org.junit.After;
006: import org.junit.Before;
007: import org.junit.BeforeClass;
008:
009: import javax.cache.Cache;
010: import javax.cache.CacheException;
011: import javax.cache.CacheListener;
012: import javax.cache.CacheManager;
013: import javax.cache.CacheManagerListener;
014: import javax.cache.spi.CacheLoader;
015: import java.lang.reflect.Method;
016: import java.util.Collection;
017:
018: /**
019: * Base class that contains helpers and test setup and teardown methods.
020: *
021: * @author <a href="mailto:manik@jboss.org">Manik Surtani</a>
022: */
023: public abstract class CacheAPITestBase {
024: protected String cacheName = "TestCache";
025: protected Cache cache;
026: protected CacheManager cacheManager;
027: protected String key = "key", value = "value";
028:
029: public static Method listenerLoadMethod, listenerPutMethod,
030: listenerEvictMethod, listenerRemoveMethod,
031: listenerClearMethod, listenerUpdateMethod,
032: listenerExpiryMethod, listenerShutdownMethod,
033: listenerLoadExceptionMethod;
034:
035: public static Method loaderLoadMethod, loaderLoadAllMethod,
036: loaderPutMethod;
037: public static Method managerListenerRegisterMethod,
038: managerListenerUnregisterMethod,
039: managerListenerStoppingMethod,
040: managerListenerTerminatedMethod;
041:
042: @BeforeClass
043: public static void setCacheListenerMethods()
044: throws NoSuchMethodException {
045: listenerLoadMethod = CacheListener.class.getMethod("onLoad",
046: Object.class);
047: listenerPutMethod = CacheListener.class.getMethod("onPut",
048: Object.class);
049: listenerEvictMethod = CacheListener.class.getMethod("onEvict",
050: Object.class);
051: listenerRemoveMethod = CacheListener.class.getMethod(
052: "onRemove", Object.class);
053: listenerClearMethod = CacheListener.class.getMethod("onClear");
054: listenerUpdateMethod = CacheListener.class.getMethod(
055: "onUpdate", Object.class);
056: listenerExpiryMethod = CacheListener.class.getMethod(
057: "onExpiry", Object.class);
058: listenerShutdownMethod = CacheListener.class
059: .getMethod("onShutdown");
060: listenerLoadExceptionMethod = CacheListener.class.getMethod(
061: "onLoadException", Object.class, Exception.class);
062:
063: loaderLoadMethod = CacheLoader.class.getMethod("load",
064: Object.class);
065: loaderLoadAllMethod = CacheLoader.class.getMethod("loadAll",
066: Collection.class);
067: loaderPutMethod = CacheLoader.class.getMethod("censorPut",
068: Object.class, Object.class);
069:
070: managerListenerRegisterMethod = CacheManagerListener.class
071: .getMethod("onRegister", String.class);
072: managerListenerUnregisterMethod = CacheManagerListener.class
073: .getMethod("onUnregister", String.class);
074: managerListenerStoppingMethod = CacheManagerListener.class
075: .getMethod("stopping");
076: managerListenerTerminatedMethod = CacheManagerListener.class
077: .getMethod("terminated");
078: }
079:
080: @Before
081: public void createCache() throws CacheException {
082: // change this to work with your Cache implementation!!
083: cacheManager = CacheImplementationHelper.getCacheManager();
084: cache = CacheImplementationHelper.getDefaultCache();
085: cacheName = cache.getName();
086: }
087:
088: protected void setFailingCacheLoader() throws CacheException {
089: setCacheLoader(new FailingCacheLoader());
090: }
091:
092: protected void setCacheLoader(CacheLoader loader)
093: throws CacheException {
094: destroyCache();
095: cache = CacheImplementationHelper.getCacheWithLoader(loader);
096: cacheName = cache.getName();
097: }
098:
099: @After
100: public void destroyCache() {
101: if (cache != null) {
102: cache.shutdown();
103: cache = null;
104: }
105:
106: if (cacheManager != null) {
107: cacheManager.shutdown();
108: cacheManager = null;
109: }
110:
111: }
112: }
|