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