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