/** * Asserts that the array of characters has a given length * * @param length expected length * @param array the array */ protected void assertLength(int length, char[] array) { assertEquals(length, array.length); }
/** * Asserts that the arrays are equivalent and contain the same values * * @param expected * @param value */ protected void assertArrayEquals(Object[] expected, Object[] value) { String message = "expected array: " + InvokerHelper.toString(expected) + " value array: " + InvokerHelper.toString(value); assertNotNull(message + ": expected should not be null", expected); assertNotNull(message + ": value should not be null", value); assertEquals(message, expected.length, value.length); for (int i = 0, size = expected.length; i < size; i++) { assertEquals("value[" + i + "] when " + message, expected[i], value[i]); } }
/** * Asserts that the array of characters contains a given char * * @param expected expected character to be found * @param array the array */ protected void assertContains(char expected, char[] array) { for (int i = 0; i < array.length; ++i) { if (array[i] == expected) { return; } } StringBuilder message = new StringBuilder(); message.append(expected).append(" not in {"); for (int i = 0; i < array.length; ++i) { message.append("'").append(array[i]).append("'"); if (i < (array.length - 1)) { message.append(", "); } } message.append(" }"); fail(message.toString()); }
/** * Convenience method for subclasses of GroovyTestCase, identical to * <pre> GroovyTestCase.notYetImplemented(this); </pre>. * * @return <false> when not itself already in the call stack * @see #notYetImplemented(java.lang.Object) */ public boolean notYetImplemented() { return notYetImplemented(this); }
protected String getTestClassName() { return TEST_SCRIPT_NAME_PREFIX + getMethodName() + (scriptFileNameCounter.getAndIncrement()) + ".groovy"; }
final Method testMethod = findRunningJUnitTestMethod(caller.getClass()); try { log.info("Running " + testMethod.getName() + " as not yet implemented"); testMethod.invoke(caller, (Object[]) new Class[] {}); fail(testMethod.getName() + " is marked as not yet implemented but passes unexpectedly");
/** * Asserts that the script runs without any exceptions * * @param script the script that should pass without any exception thrown */ protected void assertScript(final String script) throws Exception { GroovyShell shell = new GroovyShell(); shell.evaluate(script, getTestClassName()); }
/** * Asserts that the given code closure fails when it is evaluated * and that a particular exception is thrown. * * @param clazz the class of the expected exception * @param code the closure that should fail * @return the message of the expected Throwable */ protected String shouldFail(Class clazz, Closure code) { Throwable th = null; try { code.call(); } catch (GroovyRuntimeException gre) { th = unwrap(gre); } catch (Throwable e) { th = e; } if (th==null) { fail("Closure " + code + " should have failed with an exception of type " + clazz.getName()); } else if (! clazz.isInstance(th)) { fail("Closure " + code + " should have failed with an exception of type " + clazz.getName() + ", instead got Exception " + th); } return th.getMessage(); }
public static void assertEquals(String message, Object expected, Object actual) { if (expected == null && actual == null) return; if (expected != null && DefaultTypeTransformation.compareEqual(expected, actual)) return; failNotEquals(message, expected, actual); }
/** * From JUnit. Finds from the call stack the active running JUnit test case * @return the test case method * @throws RuntimeException if no method could be found. */ private static Method findRunningJUnitTestMethod(Class caller) { final Class[] args = new Class[] {}; // search the inial junit test final Throwable t = new Exception(); for (int i=t.getStackTrace().length-1; i>=0; --i) { final StackTraceElement element = t.getStackTrace()[i]; if (element.getClassName().equals(caller.getName())) { try { final Method m = caller.getMethod(element.getMethodName(), args); if (isPublicTestMethod(m)) { return m; } } catch (final Exception e) { // can't access, ignore it } } } throw new RuntimeException("No JUnit test case method found in call stack"); }
/** * Asserts that the given code closure fails when it is evaluated * * @param code * @return the message of the thrown Throwable */ protected String shouldFail(Closure code) { boolean failed = false; String result = null; try { code.call(); } catch (Throwable e) { failed = true; result = e.getMessage(); } assertTrue("Closure " + code + " should have failed", failed); return result; }
/** * Convenience method for subclasses of GroovyTestCase, identical to * <pre> GroovyTestCase.notYetImplemented(this); </pre>. * * @return <code>false</code> when not itself already in the call stack * @see #notYetImplemented(java.lang.Object) */ public boolean notYetImplemented() { return notYetImplemented(this); }
protected String getTestClassName() { return TEST_SCRIPT_NAME_PREFIX + getMethodName() + (counter++) + ".groovy"; }
final Method testMethod = findRunningJUnitTestMethod(caller.getClass()); try { log.info("Running " + testMethod.getName() + " as not yet implemented"); testMethod.invoke(caller, (Object[]) new Class[] {}); fail(testMethod.getName() + " is marked as not yet implemented but passes unexpectedly");
/** * Asserts that the script runs without any exceptions * * @param script the script that should pass without any exception thrown */ protected void assertScript(final String script) throws Exception { GroovyShell shell = new GroovyShell(); shell.evaluate(script, getTestClassName()); }
public static void assertEquals(String message, Object expected, Object actual) { if (expected == null && actual == null) return; if (expected != null && DefaultTypeTransformation.compareEqual(expected, actual)) return; failNotEquals(message, expected, actual); }
/** * From JUnit. Finds from the call stack the active running JUnit test case * @return the test case method * @throws RuntimeException if no method could be found. */ private static Method findRunningJUnitTestMethod(Class caller) { final Class[] args = new Class[] {}; // search the inial junit test final Throwable t = new Exception(); for (int i=t.getStackTrace().length-1; i>=0; --i) { final StackTraceElement element = t.getStackTrace()[i]; if (element.getClassName().equals(caller.getName())) { try { final Method m = caller.getMethod(element.getMethodName(), args); if (isPublicTestMethod(m)) { return m; } } catch (final Exception e) { // can't access, ignore it } } } throw new RuntimeException("No JUnit test case method found in call stack"); }
/** * Asserts that the given code closure fails when it is evaluated * * @param code * @return the message of the thrown Throwable */ protected String shouldFail(Closure code) { boolean failed = false; String result = null; try { code.call(); } catch (GroovyRuntimeException gre) { failed = true; result = ScriptBytecodeAdapter.unwrap(gre).getMessage(); } catch (Throwable e) { failed = true; result = e.getMessage(); } assertTrue("Closure " + code + " should have failed", failed); return result; }
/** * Asserts that the array of objects has a given length * * @param length expected length * @param array the array */ protected void assertLength(int length, Object[] array) { assertEquals(length, array.length); }
/** * Asserts that the arrays are equivalent and contain the same values * * @param expected * @param value */ protected void assertArrayEquals(Object[] expected, Object[] value) { String message = "expected array: " + InvokerHelper.toString(expected) + " value array: " + InvokerHelper.toString(value); assertNotNull(message + ": expected should not be null", expected); assertNotNull(message + ": value should not be null", value); assertEquals(message, expected.length, value.length); for (int i = 0, size = expected.length; i < size; i++) { assertEquals("value[" + i + "] when " + message, expected[i], value[i]); } }