source code formatting.
[utils] / support / general / src / test / java / org / wamblee / test / AssertionUtils.java
1 /*
2  * Copyright 2006 the original author or authors.
3  *
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
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.wamblee.test;
17
18 import junit.framework.TestCase;
19
20 import org.apache.commons.logging.Log;
21 import org.apache.commons.logging.LogFactory;
22
23 import java.util.Arrays;
24 import java.util.Iterator;
25 import java.util.Map;
26 import java.util.Set;
27
28
29 /**
30  * Useful assertions for use in test cases.
31  *
32  * @author Erik Brakkee
33  */
34 public final class AssertionUtils {
35     private static final Log LOG = LogFactory.getLog(AssertionUtils.class);
36
37     /**
38      * Disabled constructor.
39      *
40      */
41     private AssertionUtils() {
42         // Empty
43     }
44
45     /**
46      * Asserts that two object arrays are equal.
47      *
48      * @param aExpected
49      *            Expected object array.
50      * @param aActual
51      *            Actual object array.
52      */
53     public static <T> void assertEquals(T[] aExpected, T[] aActual) {
54         assertEquals("", aExpected, aActual);
55     }
56
57     /**
58      * Asserts that two object arrays are equal.
59      *
60      * @param aMsg
61      *            Message.
62      * @param aExpected
63      *            Expected array.
64      * @param aActual
65      *            Actual array.
66      */
67     public static <T> void assertEquals(String aMsg, T[] aExpected, T[] aActual) {
68         TestCase.assertEquals(aMsg + " expected " + Arrays.asList(aExpected) +
69             ", actual " + Arrays.asList(aActual) + ": Array lengths ",
70             aExpected.length, aActual.length);
71
72         for (int i = 0; i < aExpected.length; i++) {
73             TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
74                 aActual[i]);
75         }
76     }
77
78     /**
79      * Asserts that two objects are equal, and in case the object is an Object[]
80      * delegates to {@link #assertEquals(String, Object[], Object[]).
81      *
82      * @param aMsg
83      *            Message.
84      * @param aExpected
85      *            Expected result.
86      * @param aActual
87      *            Actual result.
88      */
89     public static <T> void assertEquals(String aMsg, T aExpected, T aActual) {
90         if (aExpected instanceof Object[]) {
91             AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
92                 (Object[]) aActual);
93
94             return;
95         }
96
97         TestCase.assertEquals(aMsg, aExpected, aActual);
98     }
99
100     /**
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.
103      *
104      * @param aMsg
105      *            Message.
106      * @param aExpectedMap
107      *            Expected result.
108      * @param aActual
109      *            Actual result.
110      */
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(),
114             aActual.size());
115
116         Set keys = aExpectedMap.keySet();
117
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));
124         }
125     }
126
127     /**
128      * Asserts that an exception occurs.
129      * @param aRunnable Test cases should create a subclass of this which contains the
130      * code that should throw an exception.
131      * @param aType Type of exception that is expected.
132      */
133     public static void assertException(ErroneousCode aObject, Class aType) {
134         try {
135             aObject.run();
136             throw new RuntimeException("No exception occurred");
137         } catch (Throwable t) {
138             if (aType.isInstance(t)) {
139                 LOG.info("Expected exception occured " + t.getMessage());
140
141                 return; // ok 
142             } else {
143                 throw new RuntimeException(t);
144             }
145         }
146     }
147
148     public static interface ErroneousCode {
149         void run() throws Exception;
150     }
151 }