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