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.Arrays;
19 import java.util.Comparator;
20 import java.util.Iterator;
21 import java.util.List;
25 import junit.framework.TestCase;
28 * Useful assertions for use in test cases.
30 * @author Erik Brakkee
32 public final class AssertionUtils {
35 * Disabled constructor.
38 private AssertionUtils() {
43 * Asserts that two object arrays are equal.
46 * Expected object array.
48 * Actual object array.
50 public static <T> void assertEquals(T[] aExpected, T[] aActual) {
51 assertEquals("", aExpected, aActual);
56 * Asserts that two object arrays are equal.
65 public static <T> void assertEquals(String aMsg, T[] aExpected,
67 TestCase.assertEquals(aMsg + ": Array lengths ", aExpected.length,
70 for (int i = 0; i < aExpected.length; i++) {
71 TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
78 * Asserts that two objects are equal, and in case the object is an Object[]
79 * delegates to {@link #assertEquals(String, Object[], Object[]).
88 public static <T> void assertEquals(String aMsg, T aExpected,
90 if (aExpected instanceof Object[]) {
91 AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
97 TestCase.assertEquals(aMsg, aExpected, aActual);
101 * Asserts that two maps are equal by comparing all keys and by checking
102 * that the values for the same keys are the same.
106 * @param aExpectedMap
111 public static <Key,Value> void assertEquals(String aMsg,
112 Map<Key,Value> aExpectedMap, Map<Key,Value> aActual) {
113 TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
116 Set keys = aExpectedMap.keySet();
118 for (Iterator i = keys.iterator(); i.hasNext();) {
119 String key = (String) i.next();
120 TestCase.assertTrue("Map does not containg entry for key:" + key,
121 aActual.containsKey(key));
122 AssertionUtils.assertEquals("Value of key " + key + " of map",
123 aExpectedMap.get(key), aActual.get(key));
127 public static interface ErroneousCode {
128 void run() throws Exception;
132 * Asserts that an exception occurs.
133 * @param aRunnable Test cases should create a subclass of this which contains the
134 * code that should throw an exception.
135 * @param aType Type of exception that is expected.
137 public static void assertException(ErroneousCode aObject, Class aType) {
140 throw new RuntimeException("No exception occurred");
141 } catch (Throwable t) {
142 if ( aType.isInstance(t)) {
146 throw new RuntimeException(t);