(no commit message)
[utils] / support / general / src / test / java / org / wamblee / test / AssertionUtils.java
index 07caaa93d8d806a999d4a18749620ef1371cbe41..bd55115b66a9778b6e5b0d6262f53bf6a733a9c0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright 2006 the original author or authors.
+ * Copyright 2005-2010 the original author or authors.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
 package org.wamblee.test;
 
 import java.util.Arrays;
-import java.util.Comparator;
-import java.util.Iterator;
-import java.util.List;
 import java.util.Map;
 import java.util.Set;
-
-import org.apache.commons.logging.Log;
-import org.apache.commons.logging.LogFactory;
+import java.util.Map.Entry;
+import java.util.logging.Logger;
 
 import junit.framework.TestCase;
 
 /**
  * Useful assertions for use in test cases.
- *
+ * 
  * @author Erik Brakkee
  */
 public final class AssertionUtils {
-       
-       private static final Log LOG = LogFactory.getLog(AssertionUtils.class);
+    private static final Logger LOG = Logger.getLogger(AssertionUtils.class
+        .getName());
 
     /**
      * Disabled constructor.
@@ -43,7 +39,7 @@ public final class AssertionUtils {
     private AssertionUtils() {
         // Empty
     }
-    
+
     /**
      * Asserts that two object arrays are equal.
      * 
@@ -55,7 +51,6 @@ public final class AssertionUtils {
     public static <T> void assertEquals(T[] aExpected, T[] aActual) {
         assertEquals("", aExpected, aActual);
     }
-    
 
     /**
      * Asserts that two object arrays are equal.
@@ -67,24 +62,21 @@ public final class AssertionUtils {
      * @param aActual
      *            Actual array.
      */
-    public static <T> void assertEquals(String aMsg, T[] aExpected,
-            T[] aActual) {
-        TestCase.assertEquals(aMsg + " expected " + 
-                Arrays.asList(aExpected) + ", actual " + 
-                Arrays.asList(aActual) + ": Array lengths ", aExpected.length,
-                aActual.length);
+    public static <T> void assertEquals(String aMsg, T[] aExpected, T[] aActual) {
+        TestCase.assertEquals(aMsg + " expected " + Arrays.asList(aExpected) +
+            ", actual " + Arrays.asList(aActual) + ": Array lengths ",
+            aExpected.length, aActual.length);
 
         for (int i = 0; i < aExpected.length; i++) {
             TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
-                    aActual[i]);
+                aActual[i]);
         }
     }
-    
 
-    /**
+/**
      * Asserts that two objects are equal, and in case the object is an Object[]
      * delegates to {@link #assertEquals(String, Object[], Object[]).
-     * 
+     *
      * @param aMsg
      *            Message.
      * @param aExpected
@@ -92,11 +84,10 @@ public final class AssertionUtils {
      * @param aActual
      *            Actual result.
      */
-    public static <T> void assertEquals(String aMsg, T aExpected,
-            T aActual) {
+    public static <T> void assertEquals(String aMsg, T aExpected, T aActual) {
         if (aExpected instanceof Object[]) {
             AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
-                    (Object[]) aActual);
+                (Object[]) aActual);
 
             return;
         }
@@ -115,44 +106,46 @@ public final class AssertionUtils {
      * @param aActual
      *            Actual result.
      */
-    public static <Key,Value> void assertEquals(String aMsg, 
-            Map<Key,Value> aExpectedMap, Map<Key,Value> aActual) {
+    public static <Key, Value> void assertEquals(String aMsg,
+        Map<Key, Value> aExpectedMap, Map<Key, Value> aActual) {
         TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
-                .size());
+            .size());
 
-        Set keys = aExpectedMap.keySet();
+        Set<Entry<Key, Value>> expectedEntries = aExpectedMap.entrySet();
 
-        for (Iterator i = keys.iterator(); i.hasNext();) {
-            String key = (String) i.next();
+        for (Entry<Key, Value> entry : expectedEntries) {
+            Key key = entry.getKey();
             TestCase.assertTrue("Map does not containg entry for key:" + key,
-                    aActual.containsKey(key));
+                aActual.containsKey(key));
             AssertionUtils.assertEquals("Value of key " + key + " of map",
-                    aExpectedMap.get(key), aActual.get(key));
+                entry.getValue(), aActual.get(key));
         }
     }
-   
-    public static interface ErroneousCode { 
-       void run() throws Exception; 
-    }
-    
-    /** 
+
+    /**
      * Asserts that an exception occurs.
-     * @param aRunnable Test cases should create a subclass of this which contains the 
-     * code that should throw an exception.   
-     * @param aType Type of exception that is expected.   
+     * 
+     * @param aRunnable
+     *            Test cases should create a subclass of this which contains the
+     *            code that should throw an exception.
+     * @param aType
+     *            Type of exception that is expected.
      */
-    public static void assertException(ErroneousCode aObject, Class aType) { 
-       try { 
-               aObject.run();
-               throw new RuntimeException("No exception occurred");
-       } catch (Throwable t) { 
-               if ( aType.isInstance(t)) { 
-                       LOG.info("Expected exception occured " + t.getMessage());
-                       return; // ok 
-               }
-               else { 
-                       throw new RuntimeException(t);
-               }
-       }
+    public static void assertException(ErroneousCode aObject, Class aType) {
+        try {
+            aObject.run();
+            throw new RuntimeException("No exception occurred");
+        } catch (Throwable t) {
+            if (aType.isInstance(t)) {
+                LOG.info("Expected exception occured " + t.getMessage());
+
+                return; // ok
+            }
+            throw new RuntimeException(t);
+        }
+    }
+
+    public static interface ErroneousCode {
+        void run() throws Exception;
     }
 }