2 * Copyright 2005-2010 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;
21 import java.util.Map.Entry;
22 import java.util.logging.Logger;
24 import junit.framework.TestCase;
27 * Useful assertions for use in test cases.
29 * @author Erik Brakkee
31 public final class AssertionUtils {
32 private static final Logger LOG = Logger.getLogger(AssertionUtils.class.getName());
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);
55 * Asserts that two object arrays are equal.
64 public static <T> void assertEquals(String aMsg, T[] aExpected, T[] aActual) {
65 TestCase.assertEquals(aMsg + " expected " + Arrays.asList(aExpected) +
66 ", actual " + Arrays.asList(aActual) + ": Array lengths ",
67 aExpected.length, aActual.length);
69 for (int i = 0; i < aExpected.length; i++) {
70 TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
76 * Asserts that two objects are equal, and in case the object is an Object[]
77 * delegates to {@link #assertEquals(String, Object[], Object[]).
86 public static <T> void assertEquals(String aMsg, T aExpected, T aActual) {
87 if (aExpected instanceof Object[]) {
88 AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
94 TestCase.assertEquals(aMsg, aExpected, aActual);
98 * Asserts that two maps are equal by comparing all keys and by checking
99 * that the values for the same keys are the same.
103 * @param aExpectedMap
108 public static <Key, Value> void assertEquals(String aMsg,
109 Map<Key, Value> aExpectedMap, Map<Key, Value> aActual) {
110 TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
113 Set<Entry<Key,Value>> expectedEntries = aExpectedMap.entrySet();
115 for (Entry<Key,Value> entry: expectedEntries) {
116 Key key = entry.getKey();
117 TestCase.assertTrue("Map does not containg entry for key:" + key,
118 aActual.containsKey(key));
119 AssertionUtils.assertEquals("Value of key " + key + " of map",
120 entry.getValue(), aActual.get(key));
125 * Asserts that an exception occurs.
128 * Test cases should create a subclass of this which contains the
129 * code that should throw an exception.
131 * Type of exception that is expected.
133 public static void assertException(ErroneousCode aObject, Class aType) {
136 throw new RuntimeException("No exception occurred");
137 } catch (Throwable t) {
138 if (aType.isInstance(t)) {
139 LOG.info("Expected exception occured " + t.getMessage());
143 throw new RuntimeException(t);
147 public static interface ErroneousCode {
148 void run() throws Exception;