Refactor ClusterManagerTest to use correctly junit 12/2212/3
authorAlissa Bonas <abonas@redhat.com>
Mon, 28 Oct 2013 10:26:36 +0000 (12:26 +0200)
committerGerrit Code Review <gerrit@opendaylight.org>
Thu, 31 Oct 2013 15:32:42 +0000 (15:32 +0000)
Change-Id: Id3b71d21de5a5330e236aed9c05cc47dd257c566
Signed-off-by: Alissa Bonas <abonas@redhat.com>
opendaylight/clustering/services_implementation/src/test/java/org/opendaylight/controller/clustering/services_implementation/internal/ClusterManagerTest.java

index d2a02c9f10cd803883839cadb09b1f4bd602e003..df9036467f66966273f9a83bb57c5db774fefd40 100644 (file)
@@ -1,6 +1,7 @@
 package org.opendaylight.controller.clustering.services_implementation.internal;
 
 import static org.junit.Assert.assertFalse;
 package org.opendaylight.controller.clustering.services_implementation.internal;
 
 import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.fail;
 
 import java.net.InetAddress;
 import java.util.HashSet;
 
 import java.net.InetAddress;
 import java.util.HashSet;
@@ -18,7 +19,7 @@ import org.opendaylight.controller.clustering.services.IClusterServices.cacheMod
 public class ClusterManagerTest {
 
     @Test
 public class ClusterManagerTest {
 
     @Test
-    public void NoManagerSetTest() throws CacheExistException,
+    public void noManagerSetTest() throws CacheExistException,
             CacheConfigException {
         ClusterManager cm = new ClusterManager();
         CacheImpl<String, Integer> c1 = null;
             CacheConfigException {
         ClusterManager cm = new ClusterManager();
         CacheImpl<String, Integer> c1 = null;
@@ -35,7 +36,7 @@ public class ClusterManagerTest {
     }
 
     @Test
     }
 
     @Test
-    public void WithManagerTest() throws CacheExistException,
+    public void withManagerTest() throws CacheExistException,
             CacheConfigException {
 
         ClusterManager cm = new ClusterManager();
             CacheConfigException {
 
         ClusterManager cm = new ClusterManager();
@@ -48,41 +49,42 @@ public class ClusterManagerTest {
         assertFalse(cm.existCache("NonExistantContainerName",
                 "NonExistantCacheName"));
 
         assertFalse(cm.existCache("NonExistantContainerName",
                 "NonExistantCacheName"));
 
+        String cacheName = "Cache1";
+        String containerName = "Container1";
         // Create cache with no cacheMode set, expecting it to fail
         HashSet<cacheMode> cacheModeSet = new HashSet<cacheMode>();
         // Create cache with no cacheMode set, expecting it to fail
         HashSet<cacheMode> cacheModeSet = new HashSet<cacheMode>();
-        Assert.assertNull(cm.createCache("Container1", "Cache1", cacheModeSet));
+        Assert.assertNull(cm.createCache(containerName,cacheName, cacheModeSet));
 
         // Create first cache as transactional
         cacheModeSet.add(cacheMode.TRANSACTIONAL);
         try {
 
         // Create first cache as transactional
         cacheModeSet.add(cacheMode.TRANSACTIONAL);
         try {
-            c1 = (CacheImpl<String, Integer>) cm.createCache("Container1",
-                    "Cache1", cacheModeSet);
-        } catch (CacheExistException cee) {
-            Assert.assertTrue(false);
-        } catch (CacheConfigException cce) {
-            Assert.assertTrue(false);
+            c1 = (CacheImpl<String, Integer>) cm.createCache(containerName,
+                    cacheName, cacheModeSet);
+        } catch (CacheExistException | CacheConfigException cce) {
+           fail("Failed to create cache " + cacheName);
         }
 
         // Try creating exact same cache again
         try {
         }
 
         // Try creating exact same cache again
         try {
-            c1 = (CacheImpl<String, Integer>) cm.createCache("Container1",
-                    "Cache1", cacheModeSet);
+            c1 = (CacheImpl<String, Integer>) cm.createCache(containerName,
+                    cacheName, cacheModeSet);
         } catch (CacheExistException cee) {
         } catch (CacheExistException cee) {
-            Assert.assertTrue(true);
+
         } catch (CacheConfigException cce) {
         } catch (CacheConfigException cce) {
-            Assert.assertTrue(false);
+            fail("Creating cache failed with " + cce);
         }
 
         // Create second cache with both types of cacheMode, expecting it to
         // complain
         }
 
         // Create second cache with both types of cacheMode, expecting it to
         // complain
+        String cacheName2 = "Cache2";
         cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);
         try {
         cacheModeSet.add(cacheMode.NON_TRANSACTIONAL);
         try {
-            c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
-                    "Cache2", cacheModeSet);
+            c2 = (CacheImpl<String, Integer>) cm.createCache(containerName,
+                    cacheName2, cacheModeSet);
         } catch (CacheExistException cee) {
         } catch (CacheExistException cee) {
-            Assert.assertTrue(false);
+            fail("Failed to create cache " + cacheName2 + cee);
         } catch (CacheConfigException cce) {
         } catch (CacheConfigException cce) {
-            Assert.assertTrue(true);
+
         }
 
         // Create second cache NON_TRANSACTIONAL but with both ASYNC and SYNC,
         }
 
         // Create second cache NON_TRANSACTIONAL but with both ASYNC and SYNC,
@@ -91,49 +93,47 @@ public class ClusterManagerTest {
         cacheModeSet.add(cacheMode.SYNC);
         cacheModeSet.add(cacheMode.ASYNC);
         try {
         cacheModeSet.add(cacheMode.SYNC);
         cacheModeSet.add(cacheMode.ASYNC);
         try {
-            c2 = (CacheImpl<String, Integer>) cm.createCache("Container1", "Cache2", cacheModeSet);
+            c2 = (CacheImpl<String, Integer>) cm.createCache(containerName, cacheName2, cacheModeSet);
         } catch (CacheExistException cee) {
         } catch (CacheExistException cee) {
-            Assert.assertTrue(false);
+            fail("Attempted to create cache " + cacheName2 + " with illegal cache modes set " + cacheModeSet);
         } catch (CacheConfigException cce) {
         } catch (CacheConfigException cce) {
-            Assert.assertTrue(true);
+
         }
 
         // Create second cache properly this time, as non_transactional and
         // ASYNC
         cacheModeSet.remove(cacheMode.SYNC);
         try {
         }
 
         // Create second cache properly this time, as non_transactional and
         // ASYNC
         cacheModeSet.remove(cacheMode.SYNC);
         try {
-            c2 = (CacheImpl<String, Integer>) cm.createCache("Container1",
-                    "Cache2", cacheModeSet);
-        } catch (CacheExistException cee) {
-            Assert.assertTrue(false);
-        } catch (CacheConfigException cce) {
-            Assert.assertTrue(false);
+            c2 = (CacheImpl<String, Integer>) cm.createCache(containerName,
+                    cacheName2, cacheModeSet);
+        } catch (CacheExistException | CacheConfigException e) {
+            fail("Failed to create cache " + cacheName + " though it was supposed to succeed." + e);
         }
 
         // Make sure correct caches exists
         }
 
         // Make sure correct caches exists
-        Assert.assertTrue(cm.existCache("Container1", "Cache1"));
-        c1 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache1");
-        Assert.assertTrue(c1 != null);
+        Assert.assertTrue(cm.existCache(containerName, cacheName));
+        c1 = (CacheImpl<String, Integer>) cm.getCache(containerName, cacheName);
+        Assert.assertNotNull(c1);
 
 
-        Assert.assertTrue(cm.existCache("Container1", "Cache2"));
-        c2 = (CacheImpl<String, Integer>) cm.getCache("Container1", "Cache2");
-        Assert.assertTrue(c2 != null);
+        Assert.assertTrue(cm.existCache(containerName, cacheName2));
+        c2 = (CacheImpl<String, Integer>) cm.getCache(containerName, cacheName2);
+        Assert.assertNotNull(c2);
 
 
-        Assert.assertNull(cm.getCache("Container1", "Cache3"));
+        Assert.assertNull(cm.getCache(containerName, "Cache3"));
 
         // Get CacheList
         HashSet<String> cacheList = (HashSet<String>) cm
                 .getCacheList("Container2");
         Assert.assertEquals(0, cacheList.size());
 
 
         // Get CacheList
         HashSet<String> cacheList = (HashSet<String>) cm
                 .getCacheList("Container2");
         Assert.assertEquals(0, cacheList.size());
 
-        cacheList = (HashSet<String>) cm.getCacheList("Container1");
+        cacheList = (HashSet<String>) cm.getCacheList(containerName);
         Assert.assertEquals(2, cacheList.size());
         Assert.assertEquals(2, cacheList.size());
-        Assert.assertTrue(cacheList.contains("Cache1"));
-        Assert.assertTrue(cacheList.contains("Cache2"));
+        Assert.assertTrue(cacheList.contains(cacheName));
+        Assert.assertTrue(cacheList.contains(cacheName2));
 
         // Get CacheProperties
 
         // Get CacheProperties
-        Assert.assertNull(cm.getCacheProperties("Container1", ""));
-        Properties p = cm.getCacheProperties("Container1", "Cache1");
+        Assert.assertNull(cm.getCacheProperties(containerName, ""));
+        Properties p = cm.getCacheProperties(containerName, cacheName);
         Assert.assertEquals(3, p.size());
         Assert.assertNotNull(p
                 .getProperty(IClusterServices.cacheProps.TRANSACTION_PROP
         Assert.assertEquals(3, p.size());
         Assert.assertNotNull(p
                 .getProperty(IClusterServices.cacheProps.TRANSACTION_PROP
@@ -146,10 +146,10 @@ public class ClusterManagerTest {
                         .toString()));
 
         // Destroy cache1 and make sure it's gone
                         .toString()));
 
         // Destroy cache1 and make sure it's gone
-        cm.destroyCache("Container1", "Cache1");
-        cm.destroyCache("Container1", "Cache3");
-        Assert.assertFalse(cm.existCache("Container1", "Cache1"));
-        Assert.assertTrue(cm.existCache("Container1", "Cache2"));
+        cm.destroyCache(containerName, cacheName);
+        cm.destroyCache(containerName, "Cache3");
+        Assert.assertFalse(cm.existCache(containerName, cacheName));
+        Assert.assertTrue(cm.existCache(containerName, cacheName2));
 
         // Check amIStandBy()
         boolean standby = cm.amIStandby();
 
         // Check amIStandBy()
         boolean standby = cm.amIStandby();