2 * Copyright 2006 the original author or authors.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.wamblee.test;
18 import java.util.Iterator;
22 import junit.framework.TestCase;
25 * Useful assertions for use in test cases.
27 public final class AssertionUtils {
30 * Disabled constructor.
33 private AssertionUtils() {
38 * Asserts that two object arrays are equal.
41 * Expected object array.
43 * Actual object array.
45 public static void assertEquals(Object[] aExpected, Object[] aActual) {
46 assertEquals("", aExpected, aActual);
50 * Asserts that two object arrays are equal.
59 public static void assertEquals(String aMsg, Object[] aExpected,
61 TestCase.assertEquals(aMsg + ": Array lengths ", aExpected.length,
64 for (int i = 0; i < aExpected.length; i++) {
65 TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
71 * Asserts that two objects are equal, and in case the object is an Object[]
72 * delegates to {@link #assertEquals(String, Object[], Object[]).
81 public static void assertEquals(String aMsg, Object aExpected,
83 if (aExpected instanceof Object[]) {
84 AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
90 TestCase.assertEquals(aMsg, aExpected, aActual);
94 * Asserts that two maps are equal by comparing all keys and by checking
95 * that the values for the same keys are the same.
104 public static void assertEquals(String aMsg, Map aExpectedMap, Map aActual) {
105 TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
108 Set keys = aExpectedMap.keySet();
110 for (Iterator i = keys.iterator(); i.hasNext();) {
111 String key = (String) i.next();
112 TestCase.assertTrue("Map does not containg entry for key:" + key,
113 aActual.containsKey(key));
114 AssertionUtils.assertEquals("Value of key " + key + " of map",
115 aExpectedMap.get(key), aActual.get(key));