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