(no commit message)
[utils] / support / general / src / test / java / org / wamblee / test / AssertionUtils.java
diff --git a/support/general/src/test/java/org/wamblee/test/AssertionUtils.java b/support/general/src/test/java/org/wamblee/test/AssertionUtils.java
new file mode 100644 (file)
index 0000000..8d179db
--- /dev/null
@@ -0,0 +1,120 @@
+/*
+ * Copyright 2006 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.
+ * You may obtain a copy of the License at
+ * 
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ * 
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.wamblee.test;
+
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+/**
+ * Useful assertions for use in test cases.
+ *
+ * @author Erik Brakkee
+ */
+public final class AssertionUtils {
+
+    /**
+     * Disabled constructor.
+     * 
+     */
+    private AssertionUtils() {
+        // Empty
+    }
+
+    /**
+     * Asserts that two object arrays are equal.
+     * 
+     * @param aExpected
+     *            Expected object array.
+     * @param aActual
+     *            Actual object array.
+     */
+    public static void assertEquals(Object[] aExpected, Object[] aActual) {
+        assertEquals("", aExpected, aActual);
+    }
+
+    /**
+     * Asserts that two object arrays are equal.
+     * 
+     * @param aMsg
+     *            Message.
+     * @param aExpected
+     *            Expected array.
+     * @param aActual
+     *            Actual array.
+     */
+    public static void assertEquals(String aMsg, Object[] aExpected,
+            Object[] aActual) {
+        TestCase.assertEquals(aMsg + ": Array lengths ", aExpected.length,
+                aActual.length);
+
+        for (int i = 0; i < aExpected.length; i++) {
+            TestCase.assertEquals(aMsg + ": Element " + i, aExpected[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
+     *            Expected result.
+     * @param aActual
+     *            Actual result.
+     */
+    public static void assertEquals(String aMsg, Object aExpected,
+            Object aActual) {
+        if (aExpected instanceof Object[]) {
+            AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
+                    (Object[]) aActual);
+
+            return;
+        }
+
+        TestCase.assertEquals(aMsg, aExpected, aActual);
+    }
+
+    /**
+     * Asserts that two maps are equal by comparing all keys and by checking
+     * that the values for the same keys are the same.
+     * 
+     * @param aMsg
+     *            Message.
+     * @param aExpectedMap
+     *            Expected result.
+     * @param aActual
+     *            Actual result.
+     */
+    public static void assertEquals(String aMsg, Map aExpectedMap, Map aActual) {
+        TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
+                .size());
+
+        Set keys = aExpectedMap.keySet();
+
+        for (Iterator i = keys.iterator(); i.hasNext();) {
+            String key = (String) i.next();
+            TestCase.assertTrue("Map does not containg entry for key:" + key,
+                    aActual.containsKey(key));
+            AssertionUtils.assertEquals("Value of key " + key + " of map",
+                    aExpectedMap.get(key), aActual.get(key));
+        }
+    }
+}