001 /* 002 $Id: GroovyTestCase.java,v 1.25 2005/08/20 19:02:15 blackdrag Exp $ 003 004 Copyright 2003 (C) James Strachan and Bob Mcwhirter. All Rights Reserved. 005 006 Redistribution and use of this software and associated documentation 007 ("Software"), with or without modification, are permitted provided 008 that the following conditions are met: 009 010 1. Redistributions of source code must retain copyright 011 statements and notices. Redistributions must also contain a 012 copy of this document. 013 014 2. Redistributions in binary form must reproduce the 015 above copyright notice, this list of conditions and the 016 following disclaimer in the documentation and/or other 017 materials provided with the distribution. 018 019 3. The name "groovy" must not be used to endorse or promote 020 products derived from this Software without prior written 021 permission of The Codehaus. For written permission, 022 please contact info@codehaus.org. 023 024 4. Products derived from this Software may not be called "groovy" 025 nor may "groovy" appear in their names without prior written 026 permission of The Codehaus. "groovy" is a registered 027 trademark of The Codehaus. 028 029 5. Due credit should be given to The Codehaus - 030 http://groovy.codehaus.org/ 031 032 THIS SOFTWARE IS PROVIDED BY THE CODEHAUS AND CONTRIBUTORS 033 ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 034 NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 035 FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 036 THE CODEHAUS OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 037 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 038 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 039 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 040 HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 041 STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 042 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 043 OF THE POSSIBILITY OF SUCH DAMAGE. 044 045 */ 046 package groovy.util; 047 048 import groovy.lang.Closure; 049 import groovy.lang.GroovyRuntimeException; 050 import groovy.lang.GroovyShell; 051 052 import java.util.logging.Logger; 053 054 import junit.framework.TestCase; 055 056 import org.codehaus.groovy.runtime.InvokerHelper; 057 058 /** 059 * A default JUnit TestCase in Groovy. This provides a number of helper methods 060 * plus avoids the JUnit restriction of requiring all test* methods to be void 061 * return type. 062 * 063 * @author <a href="mailto:bob@werken.com">bob mcwhirter</a> 064 * @author <a href="mailto:james@coredevelopers.net">James Strachan</a> 065 * @version $Revision: 1.25 $ 066 */ 067 public class GroovyTestCase extends TestCase { 068 069 protected Logger log = Logger.getLogger(getClass().getName()); 070 private static int counter; 071 private boolean useAgileDoxNaming = false; 072 073 public GroovyTestCase() { 074 } 075 076 /** 077 * Overload the getName() method to make the test cases look more like AgileDox 078 * (thanks to Joe Walnes for this tip!) 079 */ 080 public String getName() { 081 if (useAgileDoxNaming) { 082 return super.getName().substring(4).replaceAll("([A-Z])", " $1").toLowerCase(); 083 } 084 else { 085 return super.getName(); 086 } 087 } 088 089 public String getMethodName() { 090 return super.getName(); 091 } 092 093 /** 094 * Asserts that the arrays are equivalent and contain the same values 095 * 096 * @param expected 097 * @param value 098 */ 099 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 }