[jboss-cvs] JBossAS SVN: r108046 - projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/framework/server/ispn.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Wed Sep 8 11:59:26 EDT 2010


Author: pferraro
Date: 2010-09-08 11:59:26 -0400 (Wed, 08 Sep 2010)
New Revision: 108046

Modified:
   projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/framework/server/ispn/HAPartitionCacheHandlerTest.java
Log:
Refactor HAPartitionCacheHandler to be cache container oriented, rather than cache oriented.
This renders the acquire/release methods as no-ops.

Modified: projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/framework/server/ispn/HAPartitionCacheHandlerTest.java
===================================================================
--- projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/framework/server/ispn/HAPartitionCacheHandlerTest.java	2010-09-08 15:59:08 UTC (rev 108045)
+++ projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/framework/server/ispn/HAPartitionCacheHandlerTest.java	2010-09-08 15:59:26 UTC (rev 108046)
@@ -1,18 +1,7 @@
 package org.jboss.ha.framework.server.ispn;
 
-import java.util.ArrayList;
-import java.util.LinkedList;
-import java.util.List;
-import java.util.concurrent.Callable;
-import java.util.concurrent.ExecutionException;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
-
 import org.easymock.EasyMock;
-import org.infinispan.Cache;
 import org.infinispan.config.GlobalConfiguration;
-import org.infinispan.lifecycle.ComponentStatus;
 import org.infinispan.manager.EmbeddedCacheManager;
 import org.jboss.ha.ispn.CacheContainerRegistry;
 import org.jgroups.ChannelFactory;
@@ -23,471 +12,64 @@
 public class HAPartitionCacheHandlerTest
 {
    @Test
-   public void defaults() throws Exception
+   public void getCacheChannelFactory() throws Exception
    {
-      CacheContainerRegistry registry = EasyMock.createStrictMock(CacheContainerRegistry.class);
-      EmbeddedCacheManager container = EasyMock.createStrictMock(EmbeddedCacheManager.class);
-      @SuppressWarnings("unchecked")
-      Cache<Object, Object> cache = EasyMock.createStrictMock(Cache.class);
-      
-      InfinispanHAPartitionCacheHandler<Object, Object> handler = new HAPartitionCacheHandler<Object, Object>(registry);
-      
-      Assert.assertNull(handler.getCache());
-      
-      EasyMock.expect(registry.getCacheContainer(null)).andReturn(container);
-      EasyMock.expect(container.getCache()).andReturn(cache);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.acquireCache();
-
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertSame(cache, handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-
-      // Release w/out start
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.INSTANTIATED);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.releaseCache();
-
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertNull(handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-      
-      // Re-acquire
-      EasyMock.expect(registry.getCacheContainer(null)).andReturn(container);
-      EasyMock.expect(container.getCache()).andReturn(cache);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.acquireCache();
-
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertSame(cache, handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-      
-      // start cache
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.INSTANTIATED);
-      cache.start();
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.startCache();
-      
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertSame(cache, handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-
-      // Already started
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.RUNNING);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.startCache();
-      
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertSame(cache, handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-
-      // Release
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.RUNNING);
-      cache.stop();
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.releaseCache();
-      
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertNull(handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
+      this.getCacheChannelFactory(null);
+      this.getCacheChannelFactory("ha-partition");
    }
-
-   @Test
-   public void custom() throws Exception
-   {
-      CacheContainerRegistry registry = EasyMock.createStrictMock(CacheContainerRegistry.class);
-      EmbeddedCacheManager container = EasyMock.createStrictMock(EmbeddedCacheManager.class);
-      @SuppressWarnings("unchecked")
-      Cache<Object, Object> cache = EasyMock.createStrictMock(Cache.class);
-      
-      HAPartitionCacheHandler<Object, Object> handler = new HAPartitionCacheHandler<Object, Object>(registry);
-      handler.setCacheContainerName("container");
-      handler.setCacheName("cache");
-      
-      Assert.assertNull(handler.getCache());
-      
-      EasyMock.expect(registry.getCacheContainer("container")).andReturn(container);
-      EasyMock.expect(container.getCache("cache")).andReturn(cache);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.acquireCache();
-
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertSame(cache, handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-
-      // Release w/out start
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.INSTANTIATED);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.releaseCache();
-      
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertNull(handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-      
-      // Re-acquire
-      EasyMock.expect(registry.getCacheContainer("container")).andReturn(container);
-      EasyMock.expect(container.getCache("cache")).andReturn(cache);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.acquireCache();
-      
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertSame(cache, handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-      
-      // start cache
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.INSTANTIATED);
-      cache.start();
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.startCache();
-      
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertSame(cache, handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-      
-      // Already started
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.RUNNING);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.startCache();
-      
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertSame(cache, handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-
-      // Release
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.RUNNING);
-      cache.stop();
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.releaseCache();
-      
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertNull(handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
-   }
    
-   @Test
-   public void concurrent() throws Exception
+   private void getCacheChannelFactory(String containerName)
    {
       CacheContainerRegistry registry = EasyMock.createStrictMock(CacheContainerRegistry.class);
       EmbeddedCacheManager container = EasyMock.createStrictMock(EmbeddedCacheManager.class);
-      @SuppressWarnings("unchecked")
-      final Cache<Object, Object> cache = EasyMock.createStrictMock(Cache.class);
+      ChannelFactory expected = EasyMock.createStrictMock(ChannelFactory.class);
       
-      final InfinispanHAPartitionCacheHandler<Object, Object> handler = new HAPartitionCacheHandler<Object, Object>(registry);
+      GlobalConfiguration configuration = new GlobalConfiguration();
+      configuration.getTransportProperties().put("channelFactory", expected);
       
-      // Acquire and start cache
-      EasyMock.expect(registry.getCacheContainer(null)).andReturn(container);
-      EasyMock.expect(container.getCache()).andReturn(cache);
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.INSTANTIATED);
-      cache.start();
+      HAPartitionCacheHandler handler = new HAPartitionCacheHandler(registry);
+      handler.setCacheContainerName(containerName);
       
-      EasyMock.replay(registry, container, cache);
+      EasyMock.expect(registry.getCacheContainer(EasyMock.same(containerName))).andReturn(container);
+      EasyMock.expect(container.getGlobalConfiguration()).andReturn(configuration);
       
-      handler.acquireCache();
-      handler.startCache();
-
-      EasyMock.verify(registry, container, cache);
+      EasyMock.replay(registry, container);
       
-      Assert.assertSame(cache, handler.getCache());
+      ChannelFactory result = handler.getCacheChannelFactory();
       
-      EasyMock.reset(registry, container, cache);
-
-      EasyMock.makeThreadSafe(cache, true);
+      EasyMock.verify(registry, container);
       
-      Callable<Void> task = new Callable<Void>()
-      {
-         @Override
-         public Void call() throws Exception
-         {
-            handler.acquireCache();
-            
-            try
-            {
-               handler.startCache();
-            }
-            finally
-            {
-               handler.releaseCache();
-            }
-            
-            return null;
-         }
-      };
-      
-      int tasks = 100;
-      
-      ExecutorService executor = Executors.newFixedThreadPool(tasks);
-      List<Future<Void>> futures = new ArrayList<Future<Void>>(tasks);
-      
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.RUNNING).times(tasks);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      List<Throwable> exceptions = new LinkedList<Throwable>();
-      
-      try
-      {
-         for (int i = 0; i < tasks; ++i)
-         {
-            futures.add(executor.submit(task));
-         }
-         
-         for (Future<Void> future: futures)
-         {
-            try
-            {
-               future.get();
-            }
-            catch (InterruptedException e)
-            {
-               Thread.currentThread().interrupt();
-            }
-            catch (ExecutionException e)
-            {
-               exceptions.add(e.getCause());
-            }
-         }
-      }
-      finally
-      {
-         executor.shutdown();
-      }
-      
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertTrue(exceptions.toString(), exceptions.isEmpty());
-      Assert.assertSame(cache, handler.getCache());
-
-      EasyMock.reset(registry, container, cache);
-      
-      // Release
-      EasyMock.expect(cache.getStatus()).andReturn(ComponentStatus.RUNNING);
-      cache.stop();
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.releaseCache();
-      
-      EasyMock.verify(registry, container, cache);
-      
-      Assert.assertNull(handler.getCache());
-      
-      EasyMock.reset(registry, container, cache);
+      Assert.assertSame(expected, result);
    }
    
    @Test
-   public void startBeforeAcquire()
+   public void getChannelStackName() throws Exception
    {
-      CacheContainerRegistry registry = EasyMock.createStrictMock(CacheContainerRegistry.class);
-      
-      InfinispanHAPartitionCacheHandler<Object, Object> handler = new HAPartitionCacheHandler<Object, Object>(registry);
-      
-      EasyMock.replay(registry);
-      
-      Exception exception = null;
-      
-      try
-      {
-         handler.startCache();
-      }
-      catch (Exception e)
-      {
-         exception = e;
-      }
-      
-      EasyMock.verify(registry);
-      
-      Assert.assertNotNull(exception);
-      Assert.assertTrue(exception.toString(), exception instanceof IllegalStateException);
-      Assert.assertNull(handler.getCache());
+      this.getChannelStackName(null);
+      this.getChannelStackName("ha-partition");
    }
-
-   @Test
-   public void releaseBeforeAcquire()
-   {
-      CacheContainerRegistry registry = EasyMock.createStrictMock(CacheContainerRegistry.class);
-      
-      InfinispanHAPartitionCacheHandler<Object, Object> handler = new HAPartitionCacheHandler<Object, Object>(registry);
-      
-      EasyMock.replay(registry);
-      
-      Exception exception = null;
-      
-      try
-      {
-         handler.releaseCache();
-      }
-      catch (Exception e)
-      {
-         exception = e;
-      }
-      
-      EasyMock.verify(registry);
-      
-      Assert.assertNotNull(exception);
-      Assert.assertTrue(exception.toString(), exception instanceof IllegalStateException);
-      Assert.assertNull(handler.getCache());
-   }
    
-   @Test
-   public void getCacheChannelFactoryBeforeAcquire()
+   private void getChannelStackName(String containerName)
    {
       CacheContainerRegistry registry = EasyMock.createStrictMock(CacheContainerRegistry.class);
-      
-      org.jboss.ha.framework.server.spi.HAPartitionCacheHandler handler = new HAPartitionCacheHandler<Object, Object>(registry);
-      
-      try
-      {
-         handler.getCacheChannelFactory();
-         
-         Assert.fail("Cache was not yet acquired");
-      }
-      catch (Exception e)
-      {
-         Assert.assertEquals(IllegalStateException.class.getName(), e.getClass().getName());
-      }
-   }
-
-   @Test
-   public void getChannelStackNameBeforeAcquire()
-   {
-      CacheContainerRegistry registry = EasyMock.createStrictMock(CacheContainerRegistry.class);
-      
-      org.jboss.ha.framework.server.spi.HAPartitionCacheHandler handler = new HAPartitionCacheHandler<Object, Object>(registry);
-      
-      try
-      {
-         handler.getChannelStackName();
-         
-         Assert.fail("Cache was not yet acquired");
-      }
-      catch (Exception e)
-      {
-         Assert.assertEquals(IllegalStateException.class.getName(), e.getClass().getName());
-      }
-   }
-   
-   @Test
-   public void getCacheChannelFactory() throws Exception
-   {
-      CacheContainerRegistry registry = EasyMock.createStrictMock(CacheContainerRegistry.class);
       EmbeddedCacheManager container = EasyMock.createStrictMock(EmbeddedCacheManager.class);
-      @SuppressWarnings("unchecked")
-      Cache<Object, Object> cache = EasyMock.createStrictMock(Cache.class);
       
-      org.jboss.ha.framework.server.spi.HAPartitionCacheHandler handler = new HAPartitionCacheHandler<Object, Object>(registry);
-      
-      EasyMock.expect(registry.getCacheContainer(null)).andReturn(container);
-      EasyMock.expect(container.getCache()).andReturn(cache);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.acquireCache();
-
-      EasyMock.verify(registry, container, cache);
-      EasyMock.reset(registry, container, cache);
-
-      EmbeddedCacheManager manager = EasyMock.createStrictMock(EmbeddedCacheManager.class);
-      ChannelFactory factory = EasyMock.createStrictMock(ChannelFactory.class);
+      String expected = "udp";
       GlobalConfiguration configuration = new GlobalConfiguration();
-      configuration.getTransportProperties().put("channelFactory", factory);
+      configuration.getTransportProperties().setProperty("stack", expected);
       
-      EasyMock.expect(cache.getCacheManager()).andReturn(manager);
-      EasyMock.expect(manager.getGlobalConfiguration()).andReturn(configuration);
+      HAPartitionCacheHandler handler = new HAPartitionCacheHandler(registry);
+      handler.setCacheContainerName(containerName);
       
-      EasyMock.replay(registry, container, cache, manager, factory);
+      EasyMock.expect(registry.getCacheContainer(EasyMock.same(containerName))).andReturn(container);
+      EasyMock.expect(container.getGlobalConfiguration()).andReturn(configuration);
       
-      ChannelFactory result = handler.getCacheChannelFactory();
-
-      EasyMock.verify(registry, container, cache, manager, factory);
+      EasyMock.replay(registry, container);
       
-      Assert.assertSame(factory, result);
-      
-      EasyMock.reset(registry, container, cache, manager, factory);
-   }
-   
-   @Test
-   public void getChannelStackName() throws Exception
-   {
-      CacheContainerRegistry registry = EasyMock.createStrictMock(CacheContainerRegistry.class);
-      EmbeddedCacheManager container = EasyMock.createStrictMock(EmbeddedCacheManager.class);
-      @SuppressWarnings("unchecked")
-      Cache<Object, Object> cache = EasyMock.createStrictMock(Cache.class);
-      
-      org.jboss.ha.framework.server.spi.HAPartitionCacheHandler handler = new HAPartitionCacheHandler<Object, Object>(registry);
-      
-      EasyMock.expect(registry.getCacheContainer(null)).andReturn(container);
-      EasyMock.expect(container.getCache()).andReturn(cache);
-      
-      EasyMock.replay(registry, container, cache);
-      
-      handler.acquireCache();
-
-      EasyMock.verify(registry, container, cache);
-      EasyMock.reset(registry, container, cache);
-
-      EmbeddedCacheManager manager = EasyMock.createStrictMock(EmbeddedCacheManager.class);
-      GlobalConfiguration configuration = new GlobalConfiguration();
-      configuration.getTransportProperties().setProperty("stack", "udp");
-      
-      EasyMock.expect(cache.getCacheManager()).andReturn(manager);
-      EasyMock.expect(manager.getGlobalConfiguration()).andReturn(configuration);
-      
-      EasyMock.replay(registry, container, cache, manager);
-      
       String result = handler.getChannelStackName();
-
-      EasyMock.verify(registry, container, cache, manager);
       
-      Assert.assertEquals("udp", result);
+      EasyMock.verify(registry, container);
       
-      EasyMock.reset(registry, container, cache, manager);
+      Assert.assertSame(expected, result);
    }
 }



More information about the jboss-cvs-commits mailing list