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;
19 import java.util.List;
23 import junit.framework.TestCase;
26 * Useful assertions for use in test cases.
28 * @author Erik Brakkee
30 public final class AssertionUtils {
33 * Disabled constructor.
36 private AssertionUtils() {
41 * Asserts that two object arrays are equal.
44 * Expected object array.
46 * Actual object array.
48 public static void assertEquals(Object[] aExpected, Object[] aActual) {
49 assertEquals("", aExpected, aActual);
53 * Asserts that two object arrays are equal.
62 public static void assertEquals(String aMsg, Object[] aExpected,
64 TestCase.assertEquals(aMsg + ": Array lengths ", aExpected.length,
67 for (int i = 0; i < aExpected.length; i++) {
68 TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
74 * Asserts that two objects are equal, and in case the object is an Object[]
75 * delegates to {@link #assertEquals(String, Object[], Object[]).
84 public static void assertEquals(String aMsg, Object aExpected,
86 if (aExpected instanceof Object[]) {
87 AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
93 TestCase.assertEquals(aMsg, aExpected, aActual);
97 * Asserts that two maps are equal by comparing all keys and by checking
98 * that the values for the same keys are the same.
102 * @param aExpectedMap
107 public static void assertEquals(String aMsg, Map aExpectedMap, Map aActual) {
108 TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
111 Set keys = aExpectedMap.keySet();
113 for (Iterator i = keys.iterator(); i.hasNext();) {
114 String key = (String) i.next();
115 TestCase.assertTrue("Map does not containg entry for key:" + key,
116 aActual.containsKey(key));
117 AssertionUtils.assertEquals("Value of key " + key + " of map",
118 aExpectedMap.get(key), aActual.get(key));
122 public static interface ErroneousCode {
123 void run() throws Exception;
127 * Asserts that an exception occurs.
128 * @param aRunnable Test cases should create a subclass of this which contains the
129 * code that should throw an exception.
130 * @param aType Type of exception that is expected.
132 public static void assertException(ErroneousCode aObject, Class aType) {
135 } catch (Throwable t) {
136 if ( aType.isInstance(t)) {
140 throw new RuntimeException(t);