Added ObjectConfiguration as a holder for setter configuration (there will be more...
[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 java.util.Arrays;
19 import java.util.Comparator;
20 import java.util.Iterator;
21 import java.util.List;
22 import java.util.Map;
23 import java.util.Set;
24
25 import junit.framework.TestCase;
26
27 /**
28  * Useful assertions for use in test cases.
29  *
30  * @author Erik Brakkee
31  */
32 public final class AssertionUtils {
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     /**
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,
66             T[] aActual) {
67         TestCase.assertEquals(aMsg + " expected " + 
68                 Arrays.asList(aExpected) + ", actual " + 
69                 Arrays.asList(aActual) + ": Array lengths ", aExpected.length,
70                 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     /**
80      * Asserts that two objects are equal, and in case the object is an Object[]
81      * delegates to {@link #assertEquals(String, Object[], Object[]).
82      * 
83      * @param aMsg
84      *            Message.
85      * @param aExpected
86      *            Expected result.
87      * @param aActual
88      *            Actual result.
89      */
90     public static <T> void assertEquals(String aMsg, T aExpected,
91             T aActual) {
92         if (aExpected instanceof Object[]) {
93             AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
94                     (Object[]) aActual);
95
96             return;
97         }
98
99         TestCase.assertEquals(aMsg, aExpected, aActual);
100     }
101
102     /**
103      * Asserts that two maps are equal by comparing all keys and by checking
104      * that the values for the same keys are the same.
105      * 
106      * @param aMsg
107      *            Message.
108      * @param aExpectedMap
109      *            Expected result.
110      * @param aActual
111      *            Actual result.
112      */
113     public static <Key,Value> void assertEquals(String aMsg, 
114             Map<Key,Value> aExpectedMap, Map<Key,Value> aActual) {
115         TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
116                 .size());
117
118         Set keys = aExpectedMap.keySet();
119
120         for (Iterator i = keys.iterator(); i.hasNext();) {
121             String key = (String) i.next();
122             TestCase.assertTrue("Map does not containg entry for key:" + key,
123                     aActual.containsKey(key));
124             AssertionUtils.assertEquals("Value of key " + key + " of map",
125                     aExpectedMap.get(key), aActual.get(key));
126         }
127     }
128    
129     public static interface ErroneousCode { 
130         void run() throws Exception; 
131     }
132     
133     /** 
134      * Asserts that an exception occurs.
135      * @param aRunnable Test cases should create a subclass of this which contains the 
136      * code that should throw an exception.   
137      * @param aType Type of exception that is expected.   
138      */
139     public static void assertException(ErroneousCode aObject, Class aType) { 
140         try { 
141                 aObject.run();
142                 throw new RuntimeException("No exception occurred");
143         } catch (Throwable t) { 
144                 if ( aType.isInstance(t)) { 
145                         return; // ok 
146                 }
147                 else { 
148                         throw new RuntimeException(t);
149                 }
150         }
151     }
152 }