Author: bstansberry(a)jboss.com
Date: 2007-10-25 01:59:28 -0400 (Thu, 25 Oct 2007)
New Revision: 4685
Added:
core/trunk/src/test/java/org/jboss/cache/registry/
core/trunk/src/test/java/org/jboss/cache/registry/CacheRegistryTest.java
Log:
[JBCACHE-1156] Test of registry for caches and configurations
Added: core/trunk/src/test/java/org/jboss/cache/registry/CacheRegistryTest.java
===================================================================
--- core/trunk/src/test/java/org/jboss/cache/registry/CacheRegistryTest.java
(rev 0)
+++ core/trunk/src/test/java/org/jboss/cache/registry/CacheRegistryTest.java 2007-10-25
05:59:28 UTC (rev 4685)
@@ -0,0 +1,141 @@
+/*
+ * JBoss, Home of Professional Open Source
+ *
+ * Distributable under LGPL license.
+ * See terms of license at
gnu.org.
+ */
+package org.jboss.cache.registry;
+
+
+import static org.testng.AssertJUnit.assertEquals;
+import static org.testng.AssertJUnit.assertFalse;
+import static org.testng.AssertJUnit.assertNull;
+import static org.testng.AssertJUnit.assertTrue;
+
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.Set;
+
+import org.jboss.cache.Cache;
+import org.jboss.cache.CacheStatus;
+import org.jboss.cache.config.Configuration;
+import org.jgroups.JChannelFactory;
+import org.testng.annotations.AfterMethod;
+import org.testng.annotations.Test;
+
+/**
+ * Tests CacheRegistryImpl.
+ *
+ * @author Brian Stansberry
+ */
+@Test(groups = {"functional"})
+public class CacheRegistryTest
+{
+ /** A file that includes every configuration element I could think of */
+ public static final String DEFAULT_CONFIGURATION_FILE =
"META-INF/jbc2-configs.xml";
+
+ private Set<Cache<Object, Object>> caches = new
HashSet<Cache<Object, Object>>();
+
+ @AfterMethod(alwaysRun = true)
+ public void tearDown() throws Exception
+ {
+ for (Cache<Object, Object> cache : caches)
+ {
+ try
+ {
+ cache.stop();
+ cache.destroy();
+ }
+ catch (Exception e)
+ {
+ e.printStackTrace(System.out);
+ }
+ }
+ }
+
+ /**
+ * A test that instantiates a CacheRegistryImpl and cycles through all its
+ * configs, creating and releasing each.
+ *
+ * TODO It's late; I just put a bunch of stuff in one test method.
+ * Refactor or something.
+ *
+ * @throws Exception
+ */
+ public void testBasic() throws Exception
+ {
+ JChannelFactory cf = new JChannelFactory();
+ cf.setMultiplexerConfig("stacks.xml"); // the default stacks in
jgroups.jar
+ CacheRegistryImpl registry = new CacheRegistryImpl(DEFAULT_CONFIGURATION_FILE,
cf);
+ registry.start();
+
+ ConfigurationRegistry configRegistry = registry.getConfigurationRegistry();
+
+ Set<String> configNames = registry.getConfigurationNames();
+ assertEquals(7, configNames.size());
+ Set<String> cacheNames = registry.getCacheNames();
+ assertEquals(0, cacheNames.size());
+
+ for (String configName : configNames)
+ {
+ assertNull(configName + " not created", registry.getCache(configName,
false));
+ Cache<Object, Object> cache = registry.getCache(configName, true);
+ caches.add(cache);
+ assertEquals(CacheStatus.INSTANTIATED, cache.getCacheStatus());
+ cache.create();
+ cache.start();
+ Configuration rawConfig = configRegistry.getConfiguration(configName);
+ Configuration realConfig = cache.getConfiguration();
+ assertFalse(rawConfig == realConfig);
+ assertEquals(rawConfig.getClusterName(), realConfig.getClusterName());
+ }
+
+ cacheNames = registry.getCacheNames();
+ assertEquals(configNames, cacheNames);
+
+ for (String configName : configNames)
+ {
+ registry.releaseCache(configName);
+ }
+
+ cacheNames = registry.getCacheNames();
+ assertEquals(0, cacheNames.size());
+
+ Set<String> configNames2 = registry.getConfigurationNames();
+ assertEquals(configNames, configNames2);
+
+ for (Iterator<Cache<Object, Object>> it = caches.iterator();
it.hasNext();)
+ {
+ assertEquals(CacheStatus.DESTROYED, it.next().getCacheStatus());
+ it.remove();
+ }
+
+ String configName = configNames.iterator().next();
+ assertNull(configName + " not created", registry.getCache(configName,
false));
+ Cache<Object, Object> cache = registry.getCache(configName, true);
+ assertFalse(null == cache);
+ caches.add(cache);
+
+ cache.create();
+ cache.start();
+
+ Cache<Object, Object> cache2 = registry.getCache(configName, true);
+
+ assertTrue(cache == cache2);
+
+ registry.releaseCache(configName);
+
+ // One release does not cause registry to stop cache
+ assertEquals(CacheStatus.STARTED, cache.getCacheStatus());
+
+ registry.stop();
+
+ // Now it's stopped
+ assertEquals(CacheStatus.DESTROYED, cache.getCacheStatus());
+ caches.remove(cache);
+
+ cacheNames = registry.getCacheNames();
+ assertEquals(0, cacheNames.size());
+ assertEquals(cacheNames, registry.getConfigurationNames());
+ }
+}