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 junit.framework.TestCase;
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
23 import java.util.Arrays;
24 import java.util.Iterator;
29 * Useful assertions for use in test cases.
31 * @author Erik Brakkee
33 public final class AssertionUtils {
34 private static final Log LOG = LogFactory.getLog(AssertionUtils.class);
37 * Disabled constructor.
40 private AssertionUtils() {
45 * Asserts that two object arrays are equal.
48 * Expected object array.
50 * Actual object array.
52 public static <T> void assertEquals(T[] aExpected, T[] aActual) {
53 assertEquals("", aExpected, aActual);
57 * Asserts that two object arrays are equal.
66 public static <T> void assertEquals(String aMsg, T[] aExpected, T[] aActual) {
67 TestCase.assertEquals(aMsg + " expected " + Arrays.asList(aExpected) +
68 ", actual " + Arrays.asList(aActual) + ": Array lengths ",
69 aExpected.length, aActual.length);
71 for (int i = 0; i < aExpected.length; i++) {
72 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, T aActual) {
89 if (aExpected instanceof Object[]) {
90 AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
96 TestCase.assertEquals(aMsg, aExpected, aActual);
100 * Asserts that two maps are equal by comparing all keys and by checking
101 * that the values for the same keys are the same.
105 * @param aExpectedMap
110 public static <Key, Value> void assertEquals(String aMsg,
111 Map<Key, Value> aExpectedMap, Map<Key, Value> aActual) {
112 TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
115 Set keys = aExpectedMap.keySet();
117 for (Iterator i = keys.iterator(); i.hasNext();) {
118 String key = (String) i.next();
119 TestCase.assertTrue("Map does not containg entry for key:" + key,
120 aActual.containsKey(key));
121 AssertionUtils.assertEquals("Value of key " + key + " of map",
122 aExpectedMap.get(key), aActual.get(key));
127 * Asserts that an exception occurs.
130 * Test cases should create a subclass of this which contains the
131 * code that should throw an exception.
133 * Type of exception that is expected.
135 public static void assertException(ErroneousCode aObject, Class aType) {
138 throw new RuntimeException("No exception occurred");
139 } catch (Throwable t) {
140 if (aType.isInstance(t)) {
141 LOG.info("Expected exception occured " + t.getMessage());
145 throw new RuntimeException(t);
150 public static interface ErroneousCode {
151 void run() throws Exception;