|
|||||||||||||||||||
30 day Evaluation Version distributed via the Maven Jar Repository. Clover is not free. You have 30 days to evaluate it. Please visit http://www.thecortex.net/clover to obtain a licensed version of Clover | |||||||||||||||||||
Source file | Conditionals | Statements | Methods | TOTAL | |||||||||||||||
GroovyTestCase.java | 0% | 0% | 0% | 0% |
|
1 |
/*
|
|
2 |
$Id: GroovyTestCase.java,v 1.15 2004/07/13 14:37:48 jstrachan Exp $
|
|
3 |
|
|
4 |
Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved.
|
|
5 |
|
|
6 |
Redistribution and use of this software and associated documentation
|
|
7 |
("Software"), with or without modification, are permitted provided
|
|
8 |
that the following conditions are met:
|
|
9 |
|
|
10 |
1. Redistributions of source code must retain copyright
|
|
11 |
statements and notices. Redistributions must also contain a
|
|
12 |
copy of this document.
|
|
13 |
|
|
14 |
2. Redistributions in binary form must reproduce the
|
|
15 |
above copyright notice, this list of conditions and the
|
|
16 |
following disclaimer in the documentation and/or other
|
|
17 |
materials provided with the distribution.
|
|
18 |
|
|
19 |
3. The name "groovy" must not be used to endorse or promote
|
|
20 |
products derived from this Software without prior written
|
|
21 |
permission of The Codehaus. For written permission,
|
|
22 |
please contact info@codehaus.org.
|
|
23 |
|
|
24 |
4. Products derived from this Software may not be called "groovy"
|
|
25 |
nor may "groovy" appear in their names without prior written
|
|
26 |
permission of The Codehaus. "groovy" is a registered
|
|
27 |
trademark of The Codehaus.
|
|
28 |
|
|
29 |
5. Due credit should be given to The Codehaus -
|
|
30 |
http://groovy.codehaus.org/
|
|
31 |
|
|
32 |
THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS
|
|
33 |
``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
|
|
34 |
NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
35 |
FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
|
|
36 |
THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
|
|
37 |
INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
|
|
38 |
(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
39 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
40 |
HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
|
|
41 |
STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
|
|
42 |
ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
|
|
43 |
OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
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 | 0 |
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 | 0 |
public String getName() {
|
84 | 0 |
if (useAgileDoxNaming) {
|
85 | 0 |
return super.getName().substring(4).replaceAll("([A-Z])", " $1").toLowerCase(); |
86 |
} |
|
87 |
else {
|
|
88 | 0 |
return super.getName(); |
89 |
} |
|
90 |
} |
|
91 |
|
|
92 | 0 |
public String getMethodName() {
|
93 | 0 |
return super.getName(); |
94 |
} |
|
95 |
|
|
96 | 0 |
protected void assertArrayEquals(Object[] expected, Object[] value) { |
97 | 0 |
String message = |
98 |
"expected array: " + InvokerHelper.toString(expected) + " value array: " + InvokerHelper.toString(value); |
|
99 | 0 |
assertNotNull(message + ": expected should not be null", value);
|
100 | 0 |
assertNotNull(message + ": value should not be null", value);
|
101 | 0 |
assertEquals(message, expected.length, value.length); |
102 | 0 |
for (int i = 0, size = expected.length; i < size; i++) { |
103 | 0 |
assertEquals("value[" + i + "] when " + message, expected[i], value[i]); |
104 |
} |
|
105 |
} |
|
106 | 0 |
protected void assertLength(int length, char[] array) { |
107 | 0 |
assertEquals(length, array.length); |
108 |
} |
|
109 |
|
|
110 | 0 |
protected void assertLength(int length, int[] array) { |
111 | 0 |
assertEquals(length, array.length); |
112 |
} |
|
113 |
|
|
114 | 0 |
protected void assertLength(int length, Object[] array) { |
115 | 0 |
assertEquals(length, array.length); |
116 |
} |
|
117 |
|
|
118 | 0 |
protected void assertContains(char expected, char[] array) { |
119 | 0 |
for (int i = 0; i < array.length; ++i) { |
120 | 0 |
if (array[i] == expected) {
|
121 | 0 |
return;
|
122 |
} |
|
123 |
} |
|
124 |
|
|
125 | 0 |
StringBuffer message = new StringBuffer();
|
126 |
|
|
127 | 0 |
message.append(expected + " not in {");
|
128 |
|
|
129 | 0 |
for (int i = 0; i < array.length; ++i) { |
130 | 0 |
message.append("'" + array[i] + "'"); |
131 |
|
|
132 | 0 |
if (i < (array.length - 1)) {
|
133 | 0 |
message.append(", ");
|
134 |
} |
|
135 |
} |
|
136 |
|
|
137 | 0 |
message.append(" }");
|
138 |
|
|
139 | 0 |
fail(message.toString()); |
140 |
} |
|
141 |
|
|
142 | 0 |
protected void assertContains(int expected, int[] array) { |
143 | 0 |
for (int i = 0; i < array.length; ++i) { |
144 | 0 |
if (array[i] == expected) {
|
145 | 0 |
return;
|
146 |
} |
|
147 |
} |
|
148 |
|
|
149 | 0 |
StringBuffer message = new StringBuffer();
|
150 |
|
|
151 | 0 |
message.append(expected + " not in {");
|
152 |
|
|
153 | 0 |
for (int i = 0; i < array.length; ++i) { |
154 | 0 |
message.append("'" + array[i] + "'"); |
155 |
|
|
156 | 0 |
if (i < (array.length - 1)) {
|
157 | 0 |
message.append(", ");
|
158 |
} |
|
159 |
} |
|
160 |
|
|
161 | 0 |
message.append(" }");
|
162 |
|
|
163 | 0 |
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 | 0 |
protected void assertToString(Object value, String expected) { |
174 | 0 |
Object console = InvokerHelper.invokeMethod(value, "toString", null); |
175 | 0 |
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 | 0 |
protected void assertInspect(Object value, String expected) { |
186 | 0 |
Object console = InvokerHelper.invokeMethod(value, "inspect", null); |
187 | 0 |
assertEquals("inspect() on value: " + value, expected, console);
|
188 |
} |
|
189 |
|
|
190 |
/**
|
|
191 |
* Asserts that the script runs without any exceptions
|
|
192 |
* @param script
|
|
193 |
*/
|
|
194 | 0 |
protected void assertScript(final String script) throws Exception { |
195 | 0 |
log.info("About to execute script");
|
196 |
//log.info(script);
|
|
197 |
|
|
198 |
// lets write the file to the target directory so its available
|
|
199 |
// to the MetaClass.getClassNode()
|
|
200 |
// the file is also used to determine the CodeSource if running with a security manager
|
|
201 | 0 |
String testClassName = getTestClassName(); |
202 |
|
|
203 | 0 |
File file = new File("target/test-classes/" + testClassName); |
204 |
|
|
205 | 0 |
log.info("Creating file " + file);
|
206 |
|
|
207 | 0 |
DefaultGroovyMethods.withPrintWriter(file, new Closure(null) { |
208 | 0 |
protected void doCall(PrintWriter writer) { |
209 | 0 |
writer.println(script); |
210 |
} |
|
211 |
}); |
|
212 |
|
|
213 | 0 |
GroovyShell shell = new GroovyShell();
|
214 | 0 |
shell.evaluate(new GroovyCodeSource(file));
|
215 |
} |
|
216 |
|
|
217 | 0 |
protected String getTestClassName() {
|
218 | 0 |
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 | 0 |
protected void shouldFail(Closure code) { |
227 | 0 |
boolean failed = false; |
228 | 0 |
try {
|
229 | 0 |
code.call(); |
230 |
} |
|
231 |
catch (Exception e) {
|
|
232 | 0 |
failed = true;
|
233 | 0 |
System.out.println("Worked: caught expected exception: " + e);
|
234 |
} |
|
235 | 0 |
assertTrue("Closure " + code + " should have failed", failed); |
236 |
} |
|
237 |
|
|
238 | 0 |
protected void shouldFail(Class clazz, Closure code) { |
239 | 0 |
boolean failed = false; |
240 | 0 |
try {
|
241 | 0 |
code.call(); |
242 |
} |
|
243 |
catch (Exception e) {
|
|
244 | 0 |
if (clazz.isInstance(e)) {
|
245 | 0 |
failed = true;
|
246 | 0 |
System.out.println("Worked: caught expected exception: " + e);
|
247 |
} |
|
248 |
} |
|
249 | 0 |
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 | 0 |
protected String fixEOLs( String value )
|
258 |
{ |
|
259 | 0 |
return value.replaceAll( "(\\r\\n?)|\n", "\n" ); |
260 |
} |
|
261 |
} |
|
262 |
|
|