Java Code Examples for junit.framework.AssertionFailedError#initCause()

The following examples show how to use junit.framework.AssertionFailedError#initCause() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JSR166TestCase.java    From Chronicle-Map with Apache License 2.0 6 votes vote down vote up
public void assertThrows(Class<? extends Throwable> expectedExceptionClass,
                         Runnable... throwingActions) {
    for (Runnable throwingAction : throwingActions) {
        boolean threw = false;
        try {
            throwingAction.run();
        } catch (Throwable t) {
            threw = true;
            if (!expectedExceptionClass.isInstance(t)) {
                AssertionFailedError afe =
                        new AssertionFailedError
                                ("Expected " + expectedExceptionClass.getName() +
                                        ", got " + t.getClass().getName());
                afe.initCause(t);
                threadUnexpectedException(afe);
            }
        }
        if (!threw)
            shouldThrow(expectedExceptionClass.getName());
    }
}
 
Example 2
Source File: JSR166TestCase.java    From j2objc with Apache License 2.0 6 votes vote down vote up
/**
 * Extra checks that get done for all test cases.
 *
 * Triggers test case failure if any thread assertions have failed,
 * by rethrowing, in the test harness thread, any exception recorded
 * earlier by threadRecordFailure.
 *
 * 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())
        tearDownFail("interrupt status set in main thread");

    checkForkJoinPoolThreadLeaks();
}
 
Example 3
Source File: TestConfiguration.java    From gemfirexd-oss with Apache License 2.0 6 votes vote down vote up
public static String getNetProtocol(String hostName, int port) {
  if (USE_ODBC_BRIDGE) {
    // create an odbc.ini with the host/port, if required
    if (!odbcIni.exists()) {
      try {
        PrintWriter pw = new PrintWriter(odbcIni);
        pw.println("[gemfirexd]");
        pw.println("Description         = GemFireXD local data source");
        pw.println("Driver              = GemFireXD");
        pw.println("Server              = " + hostName);
        pw.println("Port                = " + port);
        pw.close();
      } catch (FileNotFoundException fnfe) {
        AssertionFailedError ae = new AssertionFailedError(
            "failed to create odbc.ini");
        ae.initCause(fnfe);
        throw ae;
      }
    }
    return odbcProtocol;
  }
  else {
    return netProtocol + hostName + '[' + port + "]/";
  }
}
 
Example 4
Source File: TestCase.java    From linked-blocking-multi-queue with BSD 2-Clause "Simplified" License 6 votes vote down vote up
/**
 * 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.
 */
@After
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");
}
 
Example 5
Source File: ParserTest.java    From caja with Apache License 2.0 6 votes vote down vote up
private void assertParseSucceeds(String code) {
  mq.getMessages().clear();
  try {
    js(fromString(code));
  } catch (ParseException ex) {
    AssertionFailedError afe = new AssertionFailedError(code);
    afe.initCause(ex);
    throw afe;
  }
  try {
    assertNoErrors();
  } catch (AssertionFailedError e) {
    log("assertParseSucceeds", code);
    throw e;
  }
}
 
Example 6
Source File: JSR166TestCase.java    From j2objc with Apache License 2.0 6 votes vote down vote up
public void assertThrows(Class<? extends Throwable> expectedExceptionClass,
                         Runnable... throwingActions) {
    for (Runnable throwingAction : throwingActions) {
        boolean threw = false;
        try { throwingAction.run(); }
        catch (Throwable t) {
            threw = true;
            if (!expectedExceptionClass.isInstance(t)) {
                AssertionFailedError afe =
                    new AssertionFailedError
                    ("Expected " + expectedExceptionClass.getName() +
                     ", got " + t.getClass().getName());
                afe.initCause(t);
                threadUnexpectedException(afe);
            }
        }
        if (!threw)
            shouldThrow(expectedExceptionClass.getName());
    }
}
 
Example 7
Source File: JSR166TestCase.java    From caffeine with Apache License 2.0 6 votes vote down vote up
/**
 * Extra checks that get done for all test cases.
 *
 * Triggers test case failure if any thread assertions have failed,
 * by rethrowing, in the test harness thread, any exception recorded
 * earlier by threadRecordFailure.
 *
 * Triggers test case failure if interrupt status is set in the main thread.
 */
@Override
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");
    }

    checkForkJoinPoolThreadLeaks();
}
 
Example 8
Source File: JSR166TestCase.java    From j2objc with Apache License 2.0 5 votes vote down vote up
public int await() {
    try {
        return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
    } catch (TimeoutException timedOut) {
        throw new AssertionFailedError("timed out");
    } catch (Exception fail) {
        AssertionFailedError afe =
            new AssertionFailedError("Unexpected exception: " + fail);
        afe.initCause(fail);
        throw afe;
    }
}
 
Example 9
Source File: JSR166TestCase.java    From Chronicle-Map with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 10
Source File: DeploymentTest.java    From vertx-lang-groovy with Apache License 2.0 5 votes vote down vote up
private Class assertScript(String script) {
  GroovyClassLoader loader = new GroovyClassLoader(getClass().getClassLoader());
  try {
    return loader.loadClass(getClass().getPackage().getName() + "." + script);
  } catch (ClassNotFoundException e) {
    AssertionFailedError afe = new AssertionFailedError();
    afe.initCause(e);
    throw afe;
  }
}
 
Example 11
Source File: PositionsBagTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void assertMarks(String message, PositionsBag expected, PositionsBag actual) {
    try {
        GapList<Position> expectedPositions = expected.getMarks();
        GapList<Position> actualPositions = actual.getMarks();
        GapList<AttributeSet> expectedAttributes = expected.getAttributes();
        GapList<AttributeSet> actualAttributes = actual.getAttributes();
        assertEquals("Different number of marks", expectedPositions.size(), actualPositions.size());
        for(int i = 0; i < expectedPositions.size(); i++) {
            Position expectedPos = expectedPositions.get(i);
            Position actualPos = actualPositions.get(i);
            assertEquals("Different offset at the " + i + "-th mark", expectedPos.getOffset(), actualPos.getOffset());

            AttributeSet expectedAttrib = expectedAttributes.get(i);
            AttributeSet actualAttrib = actualAttributes.get(i);
            assertSame("Different attributes at the " + i + "-th mark", expectedAttrib, actualAttrib);
        }
    } catch (AssertionFailedError afe) {
        StringBuilder sb = new StringBuilder();
        sb.append(message);
        sb.append('\n');
        sb.append("Expected marks (size=");
        sb.append(expected.getMarks().size());
        sb.append("):\n");
        dumpMarks(expected, sb);
        sb.append('\n');
        sb.append("Actual marks (size=");
        sb.append(actual.getMarks().size());
        sb.append("):\n");
        dumpMarks(actual, sb);
        sb.append('\n');

        AssertionFailedError afe2 = new AssertionFailedError(sb.toString());
        afe2.initCause(afe);
        throw afe2;
    }
}
 
Example 12
Source File: OffsetsBagTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void assertMarks(String message, OffsetsBag expected, OffsetsBag actual) {
    try {
        OffsetGapList<OffsetsBag.Mark> expectedMarks = expected.getMarks();
        OffsetGapList<OffsetsBag.Mark> actualMarks = actual.getMarks();
        assertEquals("Different number of marks", expectedMarks.size(), actualMarks.size());
        for(int i = 0; i < expectedMarks.size(); i++) {
            OffsetsBag.Mark expectedMark = expectedMarks.get(i);
            OffsetsBag.Mark actualMark = actualMarks.get(i);
            assertEquals("Different offset at the " + i + "-th mark", expectedMark.getOffset(), actualMark.getOffset());
            assertSame("Different attributes at the " + i + "-th mark", expectedMark.getAttributes(), actualMark.getAttributes());
        }
    } catch (AssertionFailedError afe) {
        StringBuilder sb = new StringBuilder();
        sb.append(message);
        sb.append('\n');
        sb.append("Expected marks (size=");
        sb.append(expected.getMarks().size());
        sb.append("):\n");
        dumpMarks(expected, sb);
        sb.append('\n');
        sb.append("Actual marks (size=");
        sb.append(actual.getMarks().size());
        sb.append("):\n");
        dumpMarks(actual, sb);
        sb.append('\n');

        AssertionFailedError afe2 = new AssertionFailedError(sb.toString());
        afe2.initCause(afe);
        throw afe2;
    }
}
 
Example 13
Source File: LuceneTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Checks a specific exception class is thrown by the given runnable, and returns it. */
public static <T extends Throwable> T expectThrows(Class<T> expectedType, String noExceptionMessage, ThrowingRunnable runnable) {
  final Throwable thrown = _expectThrows(Collections.singletonList(expectedType), runnable);
  if (expectedType.isInstance(thrown)) {
    return expectedType.cast(thrown);
  }
  if (null == thrown) {
    throw new AssertionFailedError(noExceptionMessage);
  }
  AssertionFailedError assertion = new AssertionFailedError("Unexpected exception type, expected " + expectedType.getSimpleName() + " but got " + thrown);
  assertion.initCause(thrown);
  throw assertion;
}
 
Example 14
Source File: ChildrenKeysTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void assertNoEvents (String msg) {
    if (events.size() > 0) {
        AssertionFailedError err = new AssertionFailedError(msg + ":\n" + events);
        err.initCause(when);
        err.setStackTrace(when.getStackTrace());
        throw err;
    }
}
 
Example 15
Source File: JSR166TestCase.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public int await() {
    try {
        return super.await(2 * LONG_DELAY_MS, MILLISECONDS);
    } catch (TimeoutException timedOut) {
        throw new AssertionFailedError("timed out");
    } catch (Exception fail) {
        AssertionFailedError afe =
            new AssertionFailedError("Unexpected exception: " + fail);
        afe.initCause(fail);
        throw afe;
    }
}
 
Example 16
Source File: JSR166TestCase.java    From caffeine with Apache License 2.0 5 votes vote down vote up
/**
 * 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;
    }
}
 
Example 17
Source File: JSR166TestCase.java    From streamsupport with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Sleeps until the given time has elapsed.
 * Throws AssertionFailedError if interrupted.
 */
static void sleep(long millis) {
    try {
        delay(millis);
    } catch (InterruptedException fail) {
        AssertionFailedError afe =
            new AssertionFailedError("Unexpected InterruptedException");
        afe.initCause(fail);
        throw afe;
    }
}
 
Example 18
Source File: EvaluatorTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
private void runInstanceEvaluation(int bpNo) throws Exception {
    try {
        Utils.BreakPositions bp = Utils.getBreakPositions(System.getProperty ("test.dir.src")+
                                  "org/netbeans/api/debugger/jpda/testapps/EvaluatorApp.java");
        LineBreakpoint lb = bp.getLineBreakpoints().get(bpNo);
        DebuggerManager.getDebuggerManager ().addBreakpoint (lb);
        support.doContinue();
        support.waitState (JPDADebugger.STATE_STOPPED);
        
        List<Method> methods = getMethods(false);
        AssertionFailedError te = null;
        AssertionFailedError ex = null;
        for (Method m : methods) {
            try {
                checkEval (m);
            } catch (AssertionFailedError e) {
                if (te == null) {
                    te = ex = e;
                } else {
                    ex.initCause(e);
                    ex = e;
                }
            }
        }
        if (te != null) {
            throw te;
        }
    } finally {
        support.doFinish ();
    }
}
 
Example 19
Source File: LuceneTestCase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/** Checks a specific exception class is thrown by the given runnable, and returns it. */
public static <T extends Throwable> T expectThrowsAnyOf(List<Class<? extends T>> expectedTypes, ThrowingRunnable runnable) {
  if (expectedTypes.isEmpty()) {
    throw new AssertionError("At least one expected exception type is required?");
  }

  final Throwable thrown = _expectThrows(expectedTypes, runnable);
  if (null != thrown) {
    for (Class<? extends T> expectedType : expectedTypes) {
      if (expectedType.isInstance(thrown)) {
        return expectedType.cast(thrown);
      }
    }
  }

  List<String> exceptionTypes = expectedTypes.stream().map(c -> c.getSimpleName()).collect(Collectors.toList());

  if (thrown != null) {
    AssertionFailedError assertion = new AssertionFailedError("Unexpected exception type, expected any of " +
        exceptionTypes +
        " but got: " + thrown);
    assertion.initCause(thrown);
    throw assertion;
  } else {
    throw new AssertionFailedError("Expected any of the following exception types: " +
        exceptionTypes+ " but no exception was thrown.");
  }
}
 
Example 20
Source File: ReactorTest.java    From qpid-proton-j with Apache License 2.0 5 votes vote down vote up
@Test
public void connectionRefused() throws IOException {
    final ServerSocket serverSocket = new ServerSocket(0, 0);

    class ConnectionHandler extends TestHandler {
        @Override
        public void onConnectionInit(Event event) {
            super.onConnectionInit(event);
            Connection connection = event.getConnection();
            connection.open();
            try {
                serverSocket.close();
            } catch(IOException e) {
                AssertionFailedError afe = new AssertionFailedError();
                afe.initCause(e);
                throw afe;
            }
        }
    }
    TestHandler connectionHandler = new ConnectionHandler();
    reactor.connectionToHost("127.0.0.1", serverSocket.getLocalPort(), connectionHandler);
    reactor.run();
    reactor.free();
    serverSocket.close();
    connectionHandler.assertEvents(Type.CONNECTION_INIT, Type.CONNECTION_LOCAL_OPEN, Type.CONNECTION_BOUND, Type.TRANSPORT_ERROR, Type.TRANSPORT_TAIL_CLOSED,
            Type.TRANSPORT_HEAD_CLOSED, Type.TRANSPORT_CLOSED, Type.CONNECTION_UNBOUND, Type.TRANSPORT);
}