/* Run a single test and decide whether the test was * successful, meaningless, or a failure. This is the * Template Method pattern abstraction of the inner loop in a * JML/JUnit test. */ public void runTest() throws java.lang.Throwable { try { // The call being tested! doCall(); } catch (org.jmlspecs.jmlrac.runtime.JMLEntryPreconditionError e) { // meaningless test input addMeaningless(); } catch (org.jmlspecs.jmlrac.runtime.JMLAssertionError e) { // test failure int l = org.jmlspecs.jmlrac.runtime.JMLChecker.getLevel(); org.jmlspecs.jmlrac.runtime.JMLChecker.setLevel(org.jmlspecs.jmlrac.runtime.JMLOption.NONE); try { java.lang.String failmsg = this.failMessage(e); junit.framework.AssertionFailedError err = new junit.framework.AssertionFailedError(failmsg); err.setStackTrace(new java.lang.StackTraceElement[] {}); err.initCause(e); result.addFailure(this, err); } finally { org.jmlspecs.jmlrac.runtime.JMLChecker.setLevel(l); } } catch (java.lang.Throwable e) { // test success } }
static Throwable wrapWithAddendum(Throwable ex, String addendum, boolean after) { if (ex instanceof AssertionFailedError) { AssertionFailedError ne = new AssertionFailedError(combineMessages(ex, addendum, after)); if (ex.getCause() != null) { ne.initCause(ex.getCause()); } ne.setStackTrace(ex.getStackTrace()); return ne; } if (ex instanceof AssertionError) { // preferred in JUnit 4 AssertionError ne = new AssertionError(combineMessages(ex, addendum, after)); if (ex.getCause() != null) { ne.initCause(ex.getCause()); } ne.setStackTrace(ex.getStackTrace()); return ne; } if (ex instanceof IOException) { // #66208 IOException ne = new IOException(combineMessages(ex, addendum, after)); if (ex.getCause() != null) { ne.initCause(ex.getCause()); } ne.setStackTrace(ex.getStackTrace()); return ne; } if (ex instanceof Exception) { return new InvocationTargetException(ex, combineMessages(ex, addendum, after)); } return ex; }
public void testExceptionContainsFileNameUnmarshalResourceWithBadResource() throws MarshalException, ValidationException, FileNotFoundException, IOException { /* * We are going to attempt to unmarshal groups.xml with the wrong * class so we get a MarshalException and we can then test to see if the * file name is embedded in the exception. */ boolean gotException = false; File file = ConfigurationTestUtils.getFileForConfigFile("groups.xml"); try { CastorUtils.unmarshal(Userinfo.class, new FileSystemResource(file)); } catch (MarshalException e) { String matchString = file.getAbsolutePath().replace('\\', '/'); if (e.toString().contains(matchString)) { gotException = true; } else { AssertionFailedError ae = new AssertionFailedError( "Got an exception, but not one containing the message we were expecting ('" + matchString + "'): " + e); ae.initCause(e); throw ae; } } if (!gotException) { fail("Did not get a MarshalException, but we were expecting one."); } }
protected final <A extends Annotation> void assertAnnotation( PropertyInfo<?, ?> property, Class<A> annotationClass, Map<String, Object> expectedAnnotation) { A ann1 = property.getAnnotation(annotationClass); assertNotNull(ann1); Map<String, Object> values = new HashMap<String, Object>(); for (Method m : ann1.getClass().getDeclaredMethods()) { if (m.getName().equals("equals") && m.getParameterTypes().length == 1 && m.getParameterTypes()[0] == Object.class) { continue; } if (m.getName().equals("hashCode") && m.getParameterTypes().length == 0) { continue; } if (m.getName().equals("toString") && m.getParameterTypes().length == 0) { continue; } if (m.getName().equals("annotationType") && m.getParameterTypes().length == 0) { continue; } try { Object value = m.invoke(ann1); values.put(m.getName(), value); } catch (Exception e) { AssertionFailedError afe = new AssertionFailedError("Could not invoke annotation value " + m); afe.initCause(e); throw afe; } } assertEquals(expectedAnnotation, values); }
/** Sleeps until the given time has elapsed. Throws AssertionFailedError if interrupted. */ void sleep(long millis) { try { delay(millis); } catch (InterruptedException ie) { AssertionFailedError afe = new AssertionFailedError("Unexpected InterruptedException"); afe.initCause(ie); throw afe; } }
protected void enableAutoCommit() { try { netConn.setAutoCommit(false); } catch (SQLException se) { junit.framework.AssertionFailedError ase = new junit.framework.AssertionFailedError(se.getMessage()); ase.initCause(se); throw ase; } }
/** * Records the given exception using {@link #threadRecordFailure}, then rethrows the exception, * wrapping it in an AssertionFailedError if necessary. */ public void threadUnexpectedException(Throwable t) { threadRecordFailure(t); t.printStackTrace(); if (t instanceof RuntimeException) throw (RuntimeException) t; else if (t instanceof Error) throw (Error) t; else { AssertionFailedError afe = new AssertionFailedError("unexpected exception: " + t); afe.initCause(t); throw afe; } }
public int await() { try { return super.await(2 * LONG_DELAY_MS, MILLISECONDS); } catch (TimeoutException e) { throw new AssertionFailedError("timed out"); } catch (Exception e) { AssertionFailedError afe = new AssertionFailedError("Unexpected exception: " + e); afe.initCause(e); throw afe; } }
/** * Extra checks that get done for all test cases. * * <p>Triggers test case failure if any thread assertions have failed, by rethrowing, in the test * harness thread, any exception recorded earlier by threadRecordFailure. * * <p>Triggers test case failure if interrupt status is set in the main thread. */ public void tearDown() throws Exception { Throwable t = threadFailure.getAndSet(null); if (t != null) { if (t instanceof Error) throw (Error) t; else if (t instanceof RuntimeException) throw (RuntimeException) t; else if (t instanceof Exception) throw (Exception) t; else { AssertionFailedError afe = new AssertionFailedError(t.toString()); afe.initCause(t); throw afe; } } if (Thread.interrupted()) throw new AssertionFailedError("interrupt status set in main thread"); }
@BeforeClass public static void setupOnce() { // Start the Application new Thread(() -> Application.launch(MyApp.class, (String[]) null)).start(); try { if (!launchLatch.await(TIMEOUT, TimeUnit.MILLISECONDS)) { throw new AssertionFailedError("Timeout waiting for Application to launch"); } } catch (InterruptedException ex) { AssertionFailedError err = new AssertionFailedError("Unexpected exception"); err.initCause(ex); throw err; } }
static void fail(Throwable cause, Object message) { AssertionFailedError assertionFailedError = new AssertionFailedError(String.valueOf(message)); assertionFailedError.initCause(cause); throw assertionFailedError; }