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 collections are equal including equality of the elements.
45 public static void assertEquals(List aExpected, List aActual) {
46 assertEquals(aExpected.toArray(), aActual.toArray());
50 * Asserts that two collections are equal including equality of the elements.
55 public static void assertEquals(String aMsg, List aExpected, List aActual) {
56 assertEquals(aMsg, aExpected.toArray(), aActual.toArray());
60 * Asserts that two object arrays are equal.
63 * Expected object array.
65 * Actual object array.
67 public static void assertEquals(Object[] aExpected, Object[] aActual) {
68 assertEquals("", aExpected, aActual);
72 * Asserts that two object arrays are equal.
81 public static void assertEquals(String aMsg, Object[] aExpected,
83 TestCase.assertEquals(aMsg + ": Array lengths ", aExpected.length,
86 for (int i = 0; i < aExpected.length; i++) {
87 TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
93 * Asserts that two objects are equal, and in case the object is an Object[]
94 * delegates to {@link #assertEquals(String, Object[], Object[]).
103 public static void assertEquals(String aMsg, Object aExpected,
105 if (aExpected instanceof Object[]) {
106 AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
112 TestCase.assertEquals(aMsg, aExpected, aActual);
116 * Asserts that two maps are equal by comparing all keys and by checking
117 * that the values for the same keys are the same.
121 * @param aExpectedMap
126 public static void assertEquals(String aMsg, Map aExpectedMap, Map aActual) {
127 TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
130 Set keys = aExpectedMap.keySet();
132 for (Iterator i = keys.iterator(); i.hasNext();) {
133 String key = (String) i.next();
134 TestCase.assertTrue("Map does not containg entry for key:" + key,
135 aActual.containsKey(key));
136 AssertionUtils.assertEquals("Value of key " + key + " of map",
137 aExpectedMap.get(key), aActual.get(key));
141 public static interface ErroneousCode {
142 void run() throws Exception;
146 * Asserts that an exception occurs.
147 * @param aRunnable Test cases should create a subclass of this which contains the
148 * code that should throw an exception.
149 * @param aType Type of exception that is expected.
151 public static void assertException(ErroneousCode aObject, Class aType) {
154 } catch (Throwable t) {
155 if ( aType.isInstance(t)) {
159 throw new RuntimeException(t);