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