(no commit message)
[utils] / support / general / src / test / java / org / wamblee / test / AssertionUtils.java
1 /*
2  * Copyright 2005-2010 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 java.util.Arrays;
19 import java.util.Map;
20 import java.util.Set;
21 import java.util.Map.Entry;
22 import java.util.logging.Logger;
23
24 import junit.framework.TestCase;
25
26 /**
27  * Useful assertions for use in test cases.
28  * 
29  * @author Erik Brakkee
30  */
31 public final class AssertionUtils {
32     private static final Logger LOG = Logger.getLogger(AssertionUtils.class
33         .getName());
34
35     /**
36      * Disabled constructor.
37      * 
38      */
39     private AssertionUtils() {
40         // Empty
41     }
42
43     /**
44      * Asserts that two object arrays are equal.
45      * 
46      * @param aExpected
47      *            Expected object array.
48      * @param aActual
49      *            Actual object array.
50      */
51     public static <T> void assertEquals(T[] aExpected, T[] aActual) {
52         assertEquals("", aExpected, aActual);
53     }
54
55     /**
56      * Asserts that two object arrays are equal.
57      * 
58      * @param aMsg
59      *            Message.
60      * @param aExpected
61      *            Expected array.
62      * @param aActual
63      *            Actual array.
64      */
65     public static <T> void assertEquals(String aMsg, T[] aExpected, T[] aActual) {
66         TestCase.assertEquals(aMsg + " expected " + Arrays.asList(aExpected) +
67             ", actual " + Arrays.asList(aActual) + ": Array lengths ",
68             aExpected.length, aActual.length);
69
70         for (int i = 0; i < aExpected.length; i++) {
71             TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
72                 aActual[i]);
73         }
74     }
75
76 /**
77      * Asserts that two objects are equal, and in case the object is an Object[]
78      * delegates to {@link #assertEquals(String, Object[], Object[]).
79      *
80      * @param aMsg
81      *            Message.
82      * @param aExpected
83      *            Expected result.
84      * @param aActual
85      *            Actual result.
86      */
87     public static <T> void assertEquals(String aMsg, T aExpected, T aActual) {
88         if (aExpected instanceof Object[]) {
89             AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
90                 (Object[]) aActual);
91
92             return;
93         }
94
95         TestCase.assertEquals(aMsg, aExpected, aActual);
96     }
97
98     /**
99      * Asserts that two maps are equal by comparing all keys and by checking
100      * that the values for the same keys are the same.
101      * 
102      * @param aMsg
103      *            Message.
104      * @param aExpectedMap
105      *            Expected result.
106      * @param aActual
107      *            Actual result.
108      */
109     public static <Key, Value> void assertEquals(String aMsg,
110         Map<Key, Value> aExpectedMap, Map<Key, Value> aActual) {
111         TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
112             .size());
113
114         Set<Entry<Key, Value>> expectedEntries = aExpectedMap.entrySet();
115
116         for (Entry<Key, Value> entry : expectedEntries) {
117             Key key = entry.getKey();
118             TestCase.assertTrue("Map does not containg entry for key:" + key,
119                 aActual.containsKey(key));
120             AssertionUtils.assertEquals("Value of key " + key + " of map",
121                 entry.getValue(), aActual.get(key));
122         }
123     }
124
125     /**
126      * Asserts that an exception occurs.
127      * 
128      * @param aRunnable
129      *            Test cases should create a subclass of this which contains the
130      *            code that should throw an exception.
131      * @param aType
132      *            Type of exception that is expected.
133      */
134     public static void assertException(ErroneousCode aObject, Class aType) {
135         try {
136             aObject.run();
137             throw new RuntimeException("No exception occurred");
138         } catch (Throwable t) {
139             if (aType.isInstance(t)) {
140                 LOG.info("Expected exception occured " + t.getMessage());
141
142                 return; // ok
143             }
144             throw new RuntimeException(t);
145         }
146     }
147
148     public static interface ErroneousCode {
149         void run() throws Exception;
150     }
151 }