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
39
40
41
42
43
44
45
46 package groovy.util;
47
48 import groovy.lang.Closure;
49 import groovy.lang.GroovyShell;
50
51 import java.util.logging.Logger;
52
53 import junit.framework.TestCase;
54
55 import org.codehaus.groovy.runtime.InvokerHelper;
56
57 /***
58 * A default JUnit TestCase in Groovy. This provides a number of helper methods
59 * plus avoids the JUnit restriction of requiring all test* methods to be void
60 * return type.
61 *
62 * @author <a href="mailto:bob@werken.com">bob mcwhirter</a>
63 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a>
64 * @version $Revision: 1.24 $
65 */
66 public class GroovyTestCase extends TestCase {
67
68 protected Logger log = Logger.getLogger(getClass().getName());
69 private static int counter;
70 private boolean useAgileDoxNaming = false;
71
72 public GroovyTestCase() {
73 }
74
75 /***
76 * Overload the getName() method to make the test cases look more like AgileDox
77 * (thanks to Joe Walnes for this tip!)
78 */
79 public String getName() {
80 if (useAgileDoxNaming) {
81 return super.getName().substring(4).replaceAll("([A-Z])", " $1").toLowerCase();
82 }
83 else {
84 return super.getName();
85 }
86 }
87
88 public String getMethodName() {
89 return super.getName();
90 }
91
92 /***
93 * Asserts that the arrays are equivalent and contain the same values
94 *
95 * @param expected
96 * @param value
97 */
98 protected void assertArrayEquals(Object[] expected, Object[] value) {
99 String message =
100 "expected array: " + InvokerHelper.toString(expected) + " value array: " + InvokerHelper.toString(value);
101 assertNotNull(message + ": expected should not be null", expected);
102 assertNotNull(message + ": value should not be null", value);
103 assertEquals(message, expected.length, value.length);
104 for (int i = 0, size = expected.length; i < size; i++) {
105 assertEquals("value[" + i + "] when " + message, expected[i], value[i]);
106 }
107 }
108
109 /***
110 * Asserts that the array of characters has a given length
111 *
112 * @param length expected length
113 * @param array the array
114 */
115 protected void assertLength(int length, char[] array) {
116 assertEquals(length, array.length);
117 }
118
119 /***
120 * Asserts that the array of ints has a given length
121 *
122 * @param length expected length
123 * @param array the array
124 */
125 protected void assertLength(int length, int[] array) {
126 assertEquals(length, array.length);
127 }
128
129 /***
130 * Asserts that the array of objects has a given length
131 *
132 * @param length expected length
133 * @param array the array
134 */
135 protected void assertLength(int length, Object[] array) {
136 assertEquals(length, array.length);
137 }
138
139 /***
140 * Asserts that the array of characters contains a given char
141 *
142 * @param expected expected character to be found
143 * @param array the array
144 */
145 protected void assertContains(char expected, char[] array) {
146 for (int i = 0; i < array.length; ++i) {
147 if (array[i] == expected) {
148 return;
149 }
150 }
151
152 StringBuffer message = new StringBuffer();
153
154 message.append(expected + " not in {");
155
156 for (int i = 0; i < array.length; ++i) {
157 message.append("'" + array[i] + "'");
158
159 if (i < (array.length - 1)) {
160 message.append(", ");
161 }
162 }
163
164 message.append(" }");
165
166 fail(message.toString());
167 }
168
169 /***
170 * Asserts that the array of ints contains a given int
171 *
172 * @param expected expected int
173 * @param array the array
174 */
175 protected void assertContains(int expected, int[] array) {
176 for (int i = 0; i < array.length; ++i) {
177 if (array[i] == expected) {
178 return;
179 }
180 }
181
182 StringBuffer message = new StringBuffer();
183
184 message.append(expected + " not in {");
185
186 for (int i = 0; i < array.length; ++i) {
187 message.append("'" + array[i] + "'");
188
189 if (i < (array.length - 1)) {
190 message.append(", ");
191 }
192 }
193
194 message.append(" }");
195
196 fail(message.toString());
197 }
198
199 /***
200 * Asserts that the value of toString() on the given object matches the
201 * given text string
202 *
203 * @param value the object to be output to the console
204 * @param expected the expected String representation
205 */
206 protected void assertToString(Object value, String expected) {
207 Object console = InvokerHelper.invokeMethod(value, "toString", null);
208 assertEquals("toString() on value: " + value, expected, console);
209 }
210
211 /***
212 * Asserts that the value of inspect() on the given object matches the
213 * given text string
214 *
215 * @param value the object to be output to the console
216 * @param expected the expected String representation
217 */
218 protected void assertInspect(Object value, String expected) {
219 Object console = InvokerHelper.invokeMethod(value, "inspect", null);
220 assertEquals("inspect() on value: " + value, expected, console);
221 }
222
223 /***
224 * Asserts that the script runs without any exceptions
225 *
226 * @param script the script that should pass without any exception thrown
227 */
228 protected void assertScript(final String script) throws Exception {
229 GroovyShell shell = new GroovyShell();
230 shell.evaluate(script, getTestClassName());
231 }
232
233 protected String getTestClassName() {
234 return "TestScript" + getMethodName() + (counter++) + ".groovy";
235 }
236
237 /***
238 * Asserts that the given code closure fails when it is evaluated
239 *
240 * @param code
241 */
242 protected void shouldFail(Closure code) {
243 boolean failed = false;
244 try {
245 code.call();
246 }
247 catch (Exception e) {
248 failed = true;
249 }
250 assertTrue("Closure " + code + " should have failed", failed);
251 }
252
253 /***
254 * Asserts that the given code closure fails when it is evaluated
255 * and that a particular exception is thrown.
256 *
257 * @param clazz the class of the expected exception
258 * @param code the closure that should fail
259 */
260 protected void shouldFail(Class clazz, Closure code) {
261 boolean failed = false;
262 try {
263 code.call();
264 }
265 catch (Exception e) {
266 if (clazz.isInstance(e)) {
267 failed = true;
268 }
269 assertTrue("Closure " + code + " should have failed with an exception of type " + clazz.getName() + ", instead got Exception " + e, failed);
270 return;
271 }
272 assertTrue("Closure " + code + " should have failed with an exception of type " + clazz.getName(), failed);
273 }
274
275
276 /***
277 * Returns a copy of a string in which all EOLs are \n.
278 */
279 protected String fixEOLs( String value )
280 {
281 return value.replaceAll( "(//r//n?)|\n", "\n" );
282 }
283 }