Package org.jboss.ha.cachemanager

Source Code of org.jboss.ha.cachemanager.CacheManagerUnitTestCase

/*
* JBoss, Home of Professional Open Source
*
* Distributable under LGPL license.
* See terms of license at gnu.org.
*/
package org.jboss.ha.cachemanager;


import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;

import junit.framework.TestCase;

import org.jboss.cache.Cache;
import org.jboss.cache.CacheStatus;
import org.jboss.cache.config.Configuration;
import org.jboss.cache.config.ConfigurationRegistry;
import org.jboss.cache.pojo.PojoCache;
import org.jgroups.JChannelFactory;

/**
* Tests CacheManager.
*
* @author Brian Stansberry
*/
public class CacheManagerUnitTestCase extends TestCase
{
   /**
    * A file that includes every configuration element I could think of
    */
   public static final String DEFAULT_CONFIGURATION_FILE = "jboss-cache-configs.xml";
   public static final String DEFAULT_CONFIG = "local-query";

   private Set<Cache<Object, Object>> caches = new HashSet<Cache<Object, Object>>();

   public void tearDown() throws Exception
   {
      for (Cache<Object, Object> cache : caches)
      {
         try
         {
            cache.destroy();
         }
         catch (Exception ignored)
         {
         }
      }
      caches.clear();
   }

   /**
    * A test that instantiates a CacheManager and cycles through all its
    * configs, creating and releasing each.
    * <p/>
    * TODO: Break this up into more fine-grained tests
    *
    * @throws Exception
    */
   public void testBasic() throws Exception
   {
      JChannelFactory cf = new JChannelFactory();
      cf.setMultiplexerConfig("stacks.xml"); // the default stacks in jgroups.jar
      CacheManager registry = new CacheManager(DEFAULT_CONFIGURATION_FILE, cf);
      registry.start();

      ConfigurationRegistry configRegistry = registry.getConfigurationRegistry();

      Set<String> configNames = registry.getConfigurationNames();
      assertEquals(17, 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);
      assertTrue(areCachesEquivalent(cache, cache2));

      registry.releaseCache(configName);

      // One release does not cause registry to stop cache
      assertEquals(CacheStatus.STARTED, cache.getCacheStatus());

      registry.stop();

      // Should still not be stopped
      assertEquals(CacheStatus.STARTED, cache.getCacheStatus());

      registry.releaseCache(configName);

      // Now it should be stopped
      assertEquals(CacheStatus.DESTROYED, cache.getCacheStatus());
      caches.remove(cache);

      cacheNames = registry.getCacheNames();
      assertEquals(0, cacheNames.size());
      assertEquals(cacheNames, registry.getConfigurationNames());
   }

   public void testNullConfigResource() throws Exception
   {
      JChannelFactory cf = new JChannelFactory();
      cf.setMultiplexerConfig("stacks.xml"); // the default stacks in jgroups.jar
      String configResource = null;
      CacheManager registry = new CacheManager(configResource, cf);
      registry.start();

      assertEquals("No configs", 0, registry.getConfigurationNames().size());
   }
  
   public void testEagerStartCaches() throws Exception
   {
      JChannelFactory cf = new JChannelFactory();
      cf.setMultiplexerConfig("stacks.xml");
      cf.setExposeChannels(false);
      cf.start();
      CacheManager registry = new CacheManager(DEFAULT_CONFIGURATION_FILE, cf);
     
      Set<String> cores = new HashSet<String>();
      cores.add("ha-partition");
      cores.add("local-query");
      registry.setEagerStartCaches(cores);
//      Set<String> pojos = new HashSet<String>();
//      pojos.add("test5");
//      pojos.add("test6");
//      registry.setEagerStartPojoCaches(pojos);
     
      registry.start();
     
      assertEquals(cores, registry.getCacheNames());
//      assertEquals(pojos, registry.getPojoCacheNames());
     
      Set<Cache> caches = new HashSet<Cache>();
     
      for (String name : cores)
      {
         Cache cache = registry.getCache(name, false);
         assertNotNull(cache);
         assertEquals(CacheStatus.STARTED, cache.getCacheStatus());
         caches.add(cache);
      }
     
//      for (String name : pojos)
//      {
//         PojoCache pojocache = registry.getPojoCache(name, false);
//         assertNotNull(pojocache);
//         Cache cache = pojocache.getCache();
//         assertEquals(CacheStatus.STARTED, cache.getCacheStatus());
//         caches.add(cache);
//      }
//     
      for (String name : cores)
      {
         registry.releaseCache(name);
      }
     
//      for (String name : pojos)
//      {
//         registry.releaseCache(name);
//      }
     
      for (Cache cache : caches)
      {
         assertEquals(CacheStatus.STARTED, cache.getCacheStatus());        
      }
     
      registry.stop();
     
      for (Cache cache : caches)
      {
         assertEquals(CacheStatus.DESTROYED, cache.getCacheStatus());        
      }
     
   }
  
   public void testAliasing() throws Exception
   {
      JChannelFactory cf = new JChannelFactory();
      cf.setMultiplexerConfig("stacks.xml");
      cf.setExposeChannels(false);
      cf.start();
      CacheManager registry = new CacheManager(DEFAULT_CONFIGURATION_FILE, cf);
      registry.start();

      Set<String> configNames = registry.getConfigurationNames();
      assertEquals(17, configNames.size());
     
      assertEquals(0, registry.getCacheNames().size());
     
      assertEquals(0, registry.getPojoCacheNames().size());
     
      Map<String, String> aliases = new HashMap<String, String>();
      aliases.put("alias", DEFAULT_CONFIG);
      registry.setConfigAliases(aliases);
     
      Map<String, String> registered = registry.getConfigAliases();
      assertEquals(1, registered.size());
      assertEquals(DEFAULT_CONFIG, registered.get("alias"));
     
      configNames = registry.getConfigurationNames();
      assertEquals(18, configNames.size());
      assertTrue(configNames.contains("alias"));
     
      Cache cache = registry.getCache("alias", true);
      assertNotNull(cache);
      Cache other = registry.getCache(DEFAULT_CONFIG, false);
      assertEquals(cache, other);
     
      assertEquals(1, registry.getCacheNames().size());
     
      registry.releaseCache(DEFAULT_CONFIG);
     
      assertEquals(1, registry.getCacheNames().size());
     
      registry.releaseCache("alias");
     
      assertEquals(0, registry.getCacheNames().size());
     
      PojoCache pcache = registry.getPojoCache("alias", true);
      assertNotNull(pcache);
      PojoCache otherPC = registry.getPojoCache(DEFAULT_CONFIG, false);
      assertEquals(pcache, otherPC);
     
      assertEquals(1, registry.getPojoCacheNames().size());
     
      registry.releaseCache(DEFAULT_CONFIG);
     
      assertEquals(1, registry.getPojoCacheNames().size());
     
      registry.releaseCache("alias");
     
      assertEquals(0, registry.getPojoCacheNames().size());     
   }
  
   @SuppressWarnings("unchecked")
   private boolean areCachesEquivalent(Cache<Object, Object> a, Cache<Object, Object> b)
   {
      boolean equiv = a == b;
      if (!equiv && a instanceof CacheManagerManagedCache && b instanceof CacheManagerManagedCache)
      {
         CacheManagerManagedCache cast_a = (CacheManagerManagedCache) a;
         CacheManagerManagedCache cast_b = (CacheManagerManagedCache) b;
         equiv = (cast_a.getDelegate() == cast_b.getDelegate());
      }
      return equiv;
   }
}
TOP

Related Classes of org.jboss.ha.cachemanager.CacheManagerUnitTestCase

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.