b23eee933ecb6fbf3cc2878381a759e52c6797c8
[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.getName());
33
34     /**
35      * Disabled constructor.
36      * 
37      */
38     private AssertionUtils() {
39         // Empty
40     }
41
42     /**
43      * Asserts that two object arrays are equal.
44      * 
45      * @param aExpected
46      *            Expected object array.
47      * @param aActual
48      *            Actual object array.
49      */
50     public static <T> void assertEquals(T[] aExpected, T[] aActual) {
51         assertEquals("", aExpected, aActual);
52     }
53
54     /**
55      * Asserts that two object arrays are equal.
56      * 
57      * @param aMsg
58      *            Message.
59      * @param aExpected
60      *            Expected array.
61      * @param aActual
62      *            Actual array.
63      */
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);
68
69         for (int i = 0; i < aExpected.length; i++) {
70             TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
71                 aActual[i]);
72         }
73     }
74
75 /**
76      * Asserts that two objects are equal, and in case the object is an Object[]
77      * delegates to {@link #assertEquals(String, Object[], Object[]).
78      *
79      * @param aMsg
80      *            Message.
81      * @param aExpected
82      *            Expected result.
83      * @param aActual
84      *            Actual result.
85      */
86     public static <T> void assertEquals(String aMsg, T aExpected, T aActual) {
87         if (aExpected instanceof Object[]) {
88             AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
89                 (Object[]) aActual);
90
91             return;
92         }
93
94         TestCase.assertEquals(aMsg, aExpected, aActual);
95     }
96
97     /**
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.
100      * 
101      * @param aMsg
102      *            Message.
103      * @param aExpectedMap
104      *            Expected result.
105      * @param aActual
106      *            Actual result.
107      */
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
111             .size());
112
113         Set<Entry<Key,Value>> expectedEntries = aExpectedMap.entrySet();
114
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));
121         }
122     }
123
124     /**
125      * Asserts that an exception occurs.
126      * 
127      * @param aRunnable
128      *            Test cases should create a subclass of this which contains the
129      *            code that should throw an exception.
130      * @param aType
131      *            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             } 
143             throw new RuntimeException(t);
144         }
145     }
146
147     public static interface ErroneousCode {
148         void run() throws Exception;
149     }
150 }