View Javadoc

1   /*
2    $Id: GroovyTestCase.java,v 1.25 2005/08/20 19:02:15 blackdrag 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.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 }