1 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 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 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 102 protected void assertArrayEquals(Object [] expected, Object [] value) { 103 String message = 104 "expected array: " + InvokerHelper.toString(expected) + " value array: " + InvokerHelper.toString(value); 105 assertNotNull(message + ": expected should not be null", value); 106 assertNotNull(message + ": value should not be null", value); 107 assertEquals(message, expected.length, value.length); 108 for (int i = 0, size = expected.length; i < size; i++) { 109 assertEquals("value[" + i + "] when " + message, expected[i], value[i]); 110 } 111 } 112 113 119 protected void assertLength(int length, char[] array) { 120 assertEquals(length, array.length); 121 } 122 123 129 protected void assertLength(int length, int[] array) { 130 assertEquals(length, array.length); 131 } 132 133 139 protected void assertLength(int length, Object [] array) { 140 assertEquals(length, array.length); 141 } 142 143 149 protected void assertContains(char expected, char[] array) { 150 for (int i = 0; i < array.length; ++i) { 151 if (array[i] == expected) { 152 return; 153 } 154 } 155 156 StringBuffer message = new StringBuffer (); 157 158 message.append(expected + " not in {"); 159 160 for (int i = 0; i < array.length; ++i) { 161 message.append("'" + array[i] + "'"); 162 163 if (i < (array.length - 1)) { 164 message.append(", "); 165 } 166 } 167 168 message.append(" }"); 169 170 fail(message.toString()); 171 } 172 173 179 protected void assertContains(int expected, int[] array) { 180 for (int i = 0; i < array.length; ++i) { 181 if (array[i] == expected) { 182 return; 183 } 184 } 185 186 StringBuffer message = new StringBuffer (); 187 188 message.append(expected + " not in {"); 189 190 for (int i = 0; i < array.length; ++i) { 191 message.append("'" + array[i] + "'"); 192 193 if (i < (array.length - 1)) { 194 message.append(", "); 195 } 196 } 197 198 message.append(" }"); 199 200 fail(message.toString()); 201 } 202 203 210 protected void assertToString(Object value, String expected) { 211 Object console = InvokerHelper.invokeMethod(value, "toString", null); 212 assertEquals("toString() on value: " + value, expected, console); 213 } 214 215 222 protected void assertInspect(Object value, String expected) { 223 Object console = InvokerHelper.invokeMethod(value, "inspect", null); 224 assertEquals("inspect() on value: " + value, expected, console); 225 } 226 227 232 protected void assertScript(final String script) throws Exception { 233 log.info("About to execute script"); 234 236 String testClassName = getTestClassName(); 240 241 File file = new File ("target/test-classes/" + testClassName); 242 243 log.info("Creating file " + file); 244 245 DefaultGroovyMethods.withPrintWriter(file, new Closure(null) { 246 protected void doCall(PrintWriter writer) { 247 writer.println(script); 248 } 249 }); 250 251 GroovyShell shell = new GroovyShell(); 252 shell.evaluate(new GroovyCodeSource(file)); 253 } 254 255 protected String getTestClassName() { 256 return "TestScript" + getMethodName() + (counter++) + ".groovy"; 257 } 258 259 264 protected void shouldFail(Closure code) { 265 boolean failed = false; 266 try { 267 code.call(); 268 } 269 catch (Exception e) { 270 failed = true; 271 System.out.println("Worked: caught expected exception: " + e); 272 } 273 assertTrue("Closure " + code + " should have failed", failed); 274 } 275 276 283 protected void shouldFail(Class clazz, Closure code) { 284 boolean failed = false; 285 try { 286 code.call(); 287 } 288 catch (Exception e) { 289 if (clazz.isInstance(e)) { 290 failed = true; 291 System.out.println("Worked: caught expected exception: " + e); 292 } 293 } 294 assertTrue("Closure " + code + " should have failed with an exception of type " + clazz.getName(), failed); 295 } 296 297 298 301 protected String fixEOLs( String value ) 302 { 303 return value.replaceAll( "(\\r\\n?)|\n", "\n" ); 304 } 305 } 306 | Popular Tags |