1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package com.gargoylesoftware.base.testing;
39
40 import java.util.Collection;
41 import java.util.Iterator;
42 import java.util.LinkedList;
43 import junit.framework.TestCase;
44
45 /***
46 * An extension of junit.framework.TestCase that adds those methods that we really
47 * wish were part of JUnit.
48 *
49 * @version $Revision: 1.5 $
50 * @author <a href="mailto:mbowler@GargoyleSoftware.com">Mike Bowler</a>
51 */
52 public class BaseTestCase extends TestCase {
53 /***
54 * Create an instance
55 * @param name The name of the test
56 */
57 public BaseTestCase( final String name ) {
58 super(name);
59 }
60
61
62 /***
63 * Convenience method to signal that this test hasn't been finished yet. This
64 * will print the name of the test to System.out.
65 */
66 public void notImplemented() {
67 System.out.println( "Test not implemented yet [" + getClass().getName() + " : " + getName() + "]" );
68 }
69
70
71 /***
72 * Assert that the two collections are the same irrespective of order.
73 *
74 * @param a The first collection
75 * @param b The second collection
76 */
77 public void assertCollectionsEqual( final Collection a, final Collection b ) {
78 final Collection copyOfB = new LinkedList( b );
79 final Iterator iterator = a.iterator();
80 while( iterator.hasNext() ) {
81 final Object object = iterator.next();
82 if( copyOfB.contains( object ) == false ) {
83 fail( "Expected: " + a + " but got: " + b );
84 }
85
86 copyOfB.remove( object );
87 }
88
89 if( copyOfB.isEmpty() == false ) {
90 fail("Second collection has elements that aren't in the first collection: "+copyOfB);
91 }
92 }
93
94
95 /***
96 * Assert that the two objects are the same. Junit has a method like this
97 * however it does not display what the two objects are. This method will
98 * display the toString() representations of the two objects in the case that
99 * the assertion fails.
100 *
101 * @param description The failure message to use if the two objects are not the same.
102 * @param a The first object to compare.
103 * @param b The second object to compare.
104 */
105 public static void assertSame( final String description, final Object a, final Object b ) {
106 if( a != b ) {
107 fail( description + ": Objects not the same <" + a + "> and <" + b + ">" );
108 }
109 }
110
111
112 /***
113 * Assert that the two objects are the same. Junit has a method like this
114 * however it does not display what the two objects are. This method will
115 * display the toString() representations of the two objects in the case that
116 * the assertion fails.
117 *
118 * @param a The first object to compare.
119 * @param b The second object to compare.
120 */
121 public static void assertSame( final Object a, final Object b ) {
122 if( a != b ) {
123 fail( "Objects not the same <" + a + "> and <" + b + ">" );
124 }
125 }
126
127
128 /***
129 * Assert that the specified condition is false. Older versions of junit have assertTrue()
130 * but not assertFalse so we add it here to be sure that it is present.
131 *
132 * @param description The failure message to be used if the condition is not false.
133 * @param condition The value to check.
134 */
135 public static void assertFalse( final String description, final boolean condition ) {
136 if( condition == true ) {
137 fail( description + ": Expected false" );
138 }
139 }
140
141
142 /***
143 * Assert that the specified condition is false. Older versions of junit have assertTrue()
144 * but not assertFalse so we add it here to be sure that it is present.
145 *
146 * @param condition The value to check.
147 */
148 public static void assertFalse( final boolean condition ) {
149 if( condition == true ) {
150 fail( "Expected false" );
151 }
152 }
153
154
155 /***
156 * Assert that the specified object is an instance of this class
157 *
158 * @param label A description of the test
159 * @param object The object to test
160 * @param clazz The class
161 */
162 public void assertInstanceOf( final String label, final Object object, final Class clazz ) {
163 if( clazz.isAssignableFrom( object.getClass() ) == false ) {
164 fail( label + ": object [" + object + "] is not an instance of class ["
165 + clazz.getName() + "]" );
166 }
167 }
168
169
170 /***
171 * Assert that the specified object is an instance of this class
172 *
173 * @param object The object to test
174 * @param clazz The class
175 */
176 public void assertInstanceOf( final Object object, final Class clazz ) {
177 if( clazz.isAssignableFrom( object.getClass() ) == false ) {
178 fail("object ["+object+"] is not an instance of class ["+clazz.getName()+"]" );
179 }
180 }
181 }