protected void runTest() throws Throwable {
try {
long start = System.currentTimeMillis();
method.invoke(testObject, (Object[])null);
if (expectedException != None.class) {
throw new AssertionFailedError(
"No error was generated for a test case which specifies an error.");
}
if (timeout > 0){
long elapsed = System.currentTimeMillis() - start;
if (elapsed > timeout) {
throw new AssertionFailedError("Test took longer than the specified timeout.");
}
}
} catch (InvocationTargetException e) {
Throwable thrown = e.getCause();
if (thrown == null) { // probably should not happen
throw e;
}
if (expectedException == None.class){
// Convert JUnit4 AssertionError failures to JUnit3 style so
// will be treated as failure rather than error.
if (thrown instanceof AssertionError && !(thrown instanceof AssertionFailedError)){
AssertionFailedError afe = new AssertionFailedError(thrown.toString());
// copy the original stack trace
afe.setStackTrace(thrown.getStackTrace());
throw afe;
}
throw thrown;
}
if (!expectedException.isAssignableFrom(thrown.getClass())){
throw new AssertionFailedError("The wrong exception was thrown from the test case");
}
}
}