[jboss-cvs] JBossAS SVN: r106965 - in projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha: web and 5 other directories.

jboss-cvs-commits at lists.jboss.org jboss-cvs-commits at lists.jboss.org
Tue Jul 20 22:54:42 EDT 2010


Author: pferraro
Date: 2010-07-20 22:54:41 -0400 (Tue, 20 Jul 2010)
New Revision: 106965

Added:
   projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/
   projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/
   projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/
   projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/
   projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/
   projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/
   projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/AtomicMapEntryTest.java
   projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/BatchingManagerTest.java
   projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/RetryCacheInvokerTest.java
Log:
Unit tests

Added: projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/AtomicMapEntryTest.java
===================================================================
--- projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/AtomicMapEntryTest.java	                        (rev 0)
+++ projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/AtomicMapEntryTest.java	2010-07-21 02:54:41 UTC (rev 106965)
@@ -0,0 +1,166 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ha.web.tomcat.service.session.distributedcache.impl;
+
+import org.easymock.EasyMock;
+import org.infinispan.atomic.AtomicMap;
+import org.jboss.web.tomcat.service.session.distributedcache.spi.DistributableSessionMetadata;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class AtomicMapEntryTest
+{
+   @Test
+   public void test()
+   {
+      for (AtomicMapEntry entry: AtomicMapEntry.values())
+      {
+         switch (entry)
+         {
+            case VERSION:
+            {
+               this.test(entry, new Integer(1), new Object());
+               break;
+            }
+            case TIMESTAMP:
+            {
+               this.test(entry, new Long(1), new Object());
+               break;
+            }
+            case METADATA:
+            {
+               this.test(entry, new DistributableSessionMetadata(), new Object());
+               break;
+            }
+            case ATTRIBUTES:
+            {
+               this.test(entry, new Object(), null);
+               break;
+            }
+         }
+      }
+   }
+   
+   private void test(AtomicMapEntry entry, Object valid, Object invalid)
+   {
+      @SuppressWarnings("unchecked")
+      AtomicMap<Object, Object> map = EasyMock.createStrictMock(AtomicMap.class);
+      
+      // Test null put
+      EasyMock.replay(map);
+      
+      entry.put(map, null);
+
+      EasyMock.verify(map);
+      EasyMock.reset(map);
+      
+      
+      // Test null get
+      EasyMock.expect(map.get(Byte.valueOf((byte) entry.ordinal()))).andReturn(null);
+      
+      EasyMock.replay(map);
+      
+      Object result = entry.get(map);
+
+      EasyMock.verify(map);
+      
+      Assert.assertNull(result);
+      
+      EasyMock.reset(map);
+      
+      
+      if (invalid != null)
+      {
+         // Test illegal argument put
+         EasyMock.replay(map);
+         
+         IllegalArgumentException iae = null;
+         
+         try
+         {
+            entry.put(map, invalid);
+         }
+         catch (IllegalArgumentException e)
+         {
+            iae = e;
+         }
+         
+         EasyMock.verify(map);
+         
+         Assert.assertNotNull(iae);
+         
+         EasyMock.reset(map);
+
+
+         // Test illegal argument get
+         EasyMock.expect(map.get(Byte.valueOf((byte) entry.ordinal()))).andReturn(invalid);
+
+         EasyMock.replay(map);
+         
+         ClassCastException cce = null;
+         
+         try
+         {
+            entry.get(map);
+         }
+         catch (ClassCastException e)
+         {
+            cce = e;
+         }
+         
+         EasyMock.verify(map);
+         
+         Assert.assertNotNull(cce);
+         
+         EasyMock.reset(map);
+      }
+      
+      // Test legal argument put
+      EasyMock.expect(map.put(EasyMock.eq(Byte.valueOf((byte) entry.ordinal())), EasyMock.same(valid))).andReturn(null);
+      
+      EasyMock.replay(map);
+      
+      entry.put(map, valid);
+      
+      EasyMock.verify(map);
+      
+      EasyMock.reset(map);
+      
+      
+      // Test legal argument get
+      EasyMock.expect(map.get(Byte.valueOf((byte) entry.ordinal()))).andReturn(valid);
+      
+      EasyMock.replay(map);
+      
+      result = entry.get(map);
+      
+      EasyMock.verify(map);
+      
+      Assert.assertSame(valid, result);
+      
+      EasyMock.reset(map);
+   }
+}

Added: projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/BatchingManagerTest.java
===================================================================
--- projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/BatchingManagerTest.java	                        (rev 0)
+++ projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/BatchingManagerTest.java	2010-07-21 02:54:41 UTC (rev 106965)
@@ -0,0 +1,149 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ha.web.tomcat.service.session.distributedcache.impl;
+
+import javax.transaction.Status;
+import javax.transaction.Transaction;
+import javax.transaction.TransactionManager;
+
+import org.easymock.EasyMock;
+import org.jboss.web.tomcat.service.session.distributedcache.spi.BatchingManager;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class BatchingManagerTest
+{
+   private final TransactionManager tm = EasyMock.createStrictMock(TransactionManager.class);
+   private final BatchingManager bm = new BatchingManagerImpl(this.tm);
+   
+   @Test
+   public void isBatchInProgress() throws Exception
+   {
+      EasyMock.expect(this.tm.getTransaction()).andReturn(null);
+      
+      EasyMock.replay(this.tm);
+      
+      boolean result = this.bm.isBatchInProgress();
+      
+      EasyMock.verify(this.tm);
+      
+      Assert.assertFalse(result);
+      
+      EasyMock.reset(this.tm);
+      
+      
+      Transaction transaction = EasyMock.createStrictMock(Transaction.class);
+      
+      EasyMock.expect(this.tm.getTransaction()).andReturn(transaction);
+      
+      EasyMock.replay(this.tm);
+      
+      result = this.bm.isBatchInProgress();
+      
+      EasyMock.verify(this.tm);
+      
+      Assert.assertTrue(result);
+      
+      EasyMock.reset(this.tm);
+   }
+   
+   @Test
+   public void setRollbackOnly() throws Exception
+   {
+      this.tm.setRollbackOnly();
+      
+      EasyMock.replay(this.tm);
+      
+      this.bm.setBatchRollbackOnly();
+      
+      EasyMock.verify(this.tm);
+      
+      EasyMock.reset(this.tm);
+   }
+   
+   @Test
+   public void startBatch() throws Exception
+   {
+      this.tm.begin();
+      
+      EasyMock.replay(this.tm);
+      
+      this.bm.startBatch();
+      
+      EasyMock.verify(this.tm);
+      
+      EasyMock.reset(this.tm);
+   }
+   
+   @Test
+   public void endBatch() throws Exception
+   {
+      this.commitBatch(Status.STATUS_ACTIVE);
+      this.commitBatch(Status.STATUS_COMMITTED);
+      this.commitBatch(Status.STATUS_COMMITTING);
+      this.rollbackBatch(Status.STATUS_MARKED_ROLLBACK);
+      this.commitBatch(Status.STATUS_NO_TRANSACTION);
+      this.commitBatch(Status.STATUS_PREPARED);
+      this.commitBatch(Status.STATUS_PREPARING);
+      this.commitBatch(Status.STATUS_ROLLEDBACK);
+      this.commitBatch(Status.STATUS_ROLLING_BACK);
+      this.commitBatch(Status.STATUS_UNKNOWN);
+   }
+   
+   private void commitBatch(int status) throws Exception
+   {
+      Transaction transaction = EasyMock.createStrictMock(Transaction.class);
+      
+      EasyMock.expect(this.tm.getTransaction()).andReturn(transaction);
+      EasyMock.expect(transaction.getStatus()).andReturn(status);
+      
+      this.tm.commit();
+      
+      EasyMock.replay(this.tm, transaction);
+      
+      this.bm.endBatch();
+      
+      EasyMock.verify(this.tm, transaction);
+      EasyMock.reset(this.tm, transaction);
+   }
+   
+   private void rollbackBatch(int status) throws Exception
+   {
+      Transaction transaction = EasyMock.createStrictMock(Transaction.class);
+      
+      EasyMock.expect(this.tm.getTransaction()).andReturn(transaction);
+      EasyMock.expect(transaction.getStatus()).andReturn(status);
+      
+      this.tm.rollback();
+      
+      EasyMock.replay(this.tm, transaction);
+      
+      this.bm.endBatch();
+      
+      EasyMock.verify(this.tm, transaction);
+      EasyMock.reset(this.tm, transaction);
+   }
+}

Added: projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/RetryCacheInvokerTest.java
===================================================================
--- projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/RetryCacheInvokerTest.java	                        (rev 0)
+++ projects/cluster/ha-server-cache-ispn/trunk/src/test/java/org/jboss/ha/web/tomcat/service/session/distributedcache/impl/RetryCacheInvokerTest.java	2010-07-21 02:54:41 UTC (rev 106965)
@@ -0,0 +1,228 @@
+/*
+ * JBoss, Home of Professional Open Source.
+ * Copyright 2010, Red Hat Middleware LLC, and individual contributors
+ * as indicated by the @author tags. See the copyright.txt file in the
+ * distribution for a full listing of individual contributors.
+ *
+ * This is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU Lesser General Public License as
+ * published by the Free Software Foundation; either version 2.1 of
+ * the License, or (at your option) any later version.
+ *
+ * This software is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this software; if not, write to the Free
+ * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
+ * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
+ */
+package org.jboss.ha.web.tomcat.service.session.distributedcache.impl;
+
+import org.easymock.EasyMock;
+import org.infinispan.AdvancedCache;
+import org.infinispan.Cache;
+import org.infinispan.atomic.AtomicMap;
+import org.infinispan.context.Flag;
+import org.infinispan.remoting.transport.jgroups.SuspectException;
+import org.infinispan.util.concurrent.TimeoutException;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * @author Paul Ferraro
+ *
+ */
+public class RetryCacheInvokerTest
+{
+   @Test
+   public void simple()
+   {
+      @SuppressWarnings("unchecked")
+      Cache<String, AtomicMap<Object, Object>> cache = EasyMock.createStrictMock(Cache.class);
+      @SuppressWarnings("unchecked")
+      CacheInvoker.Operation<Object> operation = EasyMock.createStrictMock(CacheInvoker.Operation.class);
+      Object expected = new Object();
+      
+      CacheInvoker invoker = new RetryingCacheInvoker();
+      
+      EasyMock.expect(operation.invoke(cache)).andReturn(expected);
+      
+      EasyMock.replay(cache, operation);
+      
+      Object result = invoker.invoke(cache, operation);
+      
+      EasyMock.verify(cache, operation);
+      
+      Assert.assertSame(expected, result);
+      
+      EasyMock.reset(cache, operation);
+   }
+   
+   @Test
+   public void forceSynchronous()
+   {
+      CacheInvoker invoker = new RetryingCacheInvoker();
+      
+      invoker.setForceSynchronous(true);
+      
+      @SuppressWarnings("unchecked")
+      Cache<String, AtomicMap<Object, Object>> cache = EasyMock.createStrictMock(Cache.class);
+      @SuppressWarnings("unchecked")
+      AdvancedCache<String, AtomicMap<Object, Object>> advancedCache = EasyMock.createStrictMock(AdvancedCache.class);
+      @SuppressWarnings("unchecked")
+      CacheInvoker.Operation<Object> operation = EasyMock.createStrictMock(CacheInvoker.Operation.class);
+      Object expected = new Object();
+      
+      EasyMock.expect(cache.getAdvancedCache()).andReturn(advancedCache);
+      EasyMock.expect(advancedCache.withFlags(Flag.FORCE_SYNCHRONOUS)).andReturn(advancedCache);
+      EasyMock.expect(operation.invoke(cache)).andReturn(expected);
+      
+      EasyMock.replay(cache, operation);
+      
+      Object result = invoker.invoke(cache, operation);
+      
+      EasyMock.verify(cache, operation);
+      
+      Assert.assertSame(expected, result);
+      
+      EasyMock.reset(cache, operation);
+      
+      
+      invoker.setForceSynchronous(false);
+      
+      EasyMock.expect(operation.invoke(cache)).andReturn(expected);
+      
+      EasyMock.replay(cache, operation);
+      
+      result = invoker.invoke(cache, operation);
+      
+      EasyMock.verify(cache, operation);
+      
+      Assert.assertSame(expected, result);
+      
+      EasyMock.reset(cache, operation);
+   }
+   
+   @Test
+   public void retryAfterTimeout()
+   {
+      @SuppressWarnings("unchecked")
+      Cache<String, AtomicMap<Object, Object>> cache = EasyMock.createStrictMock(Cache.class);
+      @SuppressWarnings("unchecked")
+      CacheInvoker.Operation<Object> operation = EasyMock.createStrictMock(CacheInvoker.Operation.class);
+      Object expected = new Object();
+      
+      CacheInvoker invoker = new RetryingCacheInvoker(1);
+      
+      EasyMock.expect(operation.invoke(cache)).andThrow(new TimeoutException());
+      EasyMock.expect(operation.invoke(cache)).andReturn(expected);
+      
+      EasyMock.replay(cache, operation);
+      
+      Object result = invoker.invoke(cache, operation);
+      
+      EasyMock.verify(cache, operation);
+      
+      Assert.assertSame(expected, result);
+      
+      EasyMock.reset(cache, operation);
+   }
+   
+   @Test
+   public void retryAfterSuspect()
+   {
+      @SuppressWarnings("unchecked")
+      Cache<String, AtomicMap<Object, Object>> cache = EasyMock.createStrictMock(Cache.class);
+      @SuppressWarnings("unchecked")
+      CacheInvoker.Operation<Object> operation = EasyMock.createStrictMock(CacheInvoker.Operation.class);
+      Object expected = new Object();
+      
+      CacheInvoker invoker = new RetryingCacheInvoker(1);
+      
+      EasyMock.expect(operation.invoke(cache)).andThrow(new SuspectException());
+      EasyMock.expect(operation.invoke(cache)).andReturn(expected);
+      
+      EasyMock.replay(cache, operation);
+      
+      Object result = invoker.invoke(cache, operation);
+      
+      EasyMock.verify(cache, operation);
+      
+      Assert.assertSame(expected, result);
+      
+      EasyMock.reset(cache, operation);
+   }
+   
+   @Test
+   public void timeout()
+   {
+      @SuppressWarnings("unchecked")
+      Cache<String, AtomicMap<Object, Object>> cache = EasyMock.createStrictMock(Cache.class);
+      @SuppressWarnings("unchecked")
+      CacheInvoker.Operation<Object> operation = EasyMock.createStrictMock(CacheInvoker.Operation.class);
+      TimeoutException lastException = new TimeoutException();
+      
+      CacheInvoker invoker = new RetryingCacheInvoker(1);
+      
+      EasyMock.expect(operation.invoke(cache)).andThrow(new TimeoutException());
+      EasyMock.expect(operation.invoke(cache)).andThrow(lastException);
+      
+      EasyMock.replay(cache, operation);
+      
+      RuntimeException exception = null;
+      
+      try
+      {
+         invoker.invoke(cache, operation);
+      }
+      catch (RuntimeException e)
+      {
+         exception = e;
+      }
+      
+      EasyMock.verify(cache, operation);
+      
+      Assert.assertNotNull(exception);
+      Assert.assertSame(lastException, exception.getCause());
+      
+      EasyMock.reset(cache, operation);
+   }
+   
+   @Test
+   public void suspect()
+   {
+      @SuppressWarnings("unchecked")
+      Cache<String, AtomicMap<Object, Object>> cache = EasyMock.createStrictMock(Cache.class);
+      @SuppressWarnings("unchecked")
+      CacheInvoker.Operation<Object> operation = EasyMock.createStrictMock(CacheInvoker.Operation.class);
+      SuspectException lastException = new SuspectException();
+      
+      CacheInvoker invoker = new RetryingCacheInvoker(1);
+      
+      EasyMock.expect(operation.invoke(cache)).andThrow(new SuspectException());
+      EasyMock.expect(operation.invoke(cache)).andThrow(lastException);
+      
+      EasyMock.replay(cache, operation);
+      
+      RuntimeException exception = null;
+      
+      try
+      {
+         invoker.invoke(cache, operation);
+      }
+      catch (RuntimeException e)
+      {
+         exception = e;
+      }
+      
+      EasyMock.verify(cache, operation);
+      
+      Assert.assertNotNull(exception);
+      Assert.assertSame(lastException, exception.getCause());
+      
+      EasyMock.reset(cache, operation);
+   }
+}



More information about the jboss-cvs-commits mailing list