filtering of collections.
[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 collections are equal including equality of the elements. 
42      * @param aExpected
43      * @param aActual
44      */
45     public static void assertEquals(List aExpected, List aActual) { 
46         assertEquals(aExpected.toArray(), aActual.toArray());
47     }
48     
49     /**
50      * Asserts that two collections are equal including equality of the elements. 
51      * @param aMsg
52      * @param aExpected
53      * @param aActual
54      */
55     public static void assertEquals(String aMsg, List aExpected, List aActual) { 
56         assertEquals(aMsg, aExpected.toArray(), aActual.toArray());
57     }
58
59     /**
60      * Asserts that two object arrays are equal.
61      * 
62      * @param aExpected
63      *            Expected object array.
64      * @param aActual
65      *            Actual object array.
66      */
67     public static void assertEquals(Object[] aExpected, Object[] aActual) {
68         assertEquals("", aExpected, aActual);
69     }
70
71     /**
72      * Asserts that two object arrays are equal.
73      * 
74      * @param aMsg
75      *            Message.
76      * @param aExpected
77      *            Expected array.
78      * @param aActual
79      *            Actual array.
80      */
81     public static void assertEquals(String aMsg, Object[] aExpected,
82             Object[] aActual) {
83         TestCase.assertEquals(aMsg + ": Array lengths ", aExpected.length,
84                 aActual.length);
85
86         for (int i = 0; i < aExpected.length; i++) {
87             TestCase.assertEquals(aMsg + ": Element " + i, aExpected[i],
88                     aActual[i]);
89         }
90     }
91
92     /**
93      * Asserts that two objects are equal, and in case the object is an Object[]
94      * delegates to {@link #assertEquals(String, Object[], Object[]).
95      * 
96      * @param aMsg
97      *            Message.
98      * @param aExpected
99      *            Expected result.
100      * @param aActual
101      *            Actual result.
102      */
103     public static void assertEquals(String aMsg, Object aExpected,
104             Object aActual) {
105         if (aExpected instanceof Object[]) {
106             AssertionUtils.assertEquals(aMsg, (Object[]) aExpected,
107                     (Object[]) aActual);
108
109             return;
110         }
111
112         TestCase.assertEquals(aMsg, aExpected, aActual);
113     }
114
115     /**
116      * Asserts that two maps are equal by comparing all keys and by checking
117      * that the values for the same keys are the same.
118      * 
119      * @param aMsg
120      *            Message.
121      * @param aExpectedMap
122      *            Expected result.
123      * @param aActual
124      *            Actual result.
125      */
126     public static void assertEquals(String aMsg, Map aExpectedMap, Map aActual) {
127         TestCase.assertEquals("Map sizes differ", aExpectedMap.size(), aActual
128                 .size());
129
130         Set keys = aExpectedMap.keySet();
131
132         for (Iterator i = keys.iterator(); i.hasNext();) {
133             String key = (String) i.next();
134             TestCase.assertTrue("Map does not containg entry for key:" + key,
135                     aActual.containsKey(key));
136             AssertionUtils.assertEquals("Value of key " + key + " of map",
137                     aExpectedMap.get(key), aActual.get(key));
138         }
139     }
140 }