{
JChannelFactory cf = new JChannelFactory();
cf.setMultiplexerConfig(DEFAULT_STACKS_FILE);
cf.setExposeChannels(false);
cf.start();
CacheManager registry = new CacheManager(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);
// Cache shouldn't be started
assertEquals(CacheStatus.INSTANTIATED, cache.getCacheStatus());
cache.create();
cache.start();
// Config should be a clone
Configuration rawConfig = configRegistry.getConfiguration(configName);
Configuration realConfig = cache.getConfiguration();
assertFalse(rawConfig == realConfig);
assertEquals(rawConfig.getClusterName(), realConfig.getClusterName());
}
cacheNames = registry.getCacheNames();
assertEquals(configNames, cacheNames);
// Test basic releasing of caches
for (String configName : configNames)
{
registry.releaseCache(configName);
}
cacheNames = registry.getCacheNames();
assertEquals(0, cacheNames.size());
// We shouldn't have affected configuration set
Set<String> configNames2 = registry.getConfigurationNames();
assertEquals(configNames, configNames2);
// Releasing only checkout of cache should have destroyed it
for (Iterator<Cache<Object, Object>> it = caches.iterator(); it.hasNext();)
{
assertEquals(CacheStatus.DESTROYED, it.next().getCacheStatus());
it.remove();
}
// Get cache w/o asking to create returns null
String configName = configNames.iterator().next();
assertNull(configName + " not created", registry.getCache(configName, false));
// Get cache w/ asking to create returns cache
Cache<Object, Object> cache = registry.getCache(configName, true);
assertFalse(null == cache);
caches.add(cache);
cache.create();
cache.start();
// Test 2 checkouts of the same cache
Cache<Object, Object> cache2 = registry.getCache(configName, true);
assertEquals(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());
}