junit.framework.TestFailure Java Examples

The following examples show how to use junit.framework.TestFailure. 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: FlowControlTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testRuntimeExceptionsAlsoGenerateLog() throws Exception {
    if (throwIt != null) {
        Logger.getLogger("").info("Ahoj");
        throw throwIt;
    }
    
    FlowControlTest l = new FlowControlTest("testRuntimeExceptionsAlsoGenerateLog");
    l.throwIt = new NullPointerException();
    TestResult res = l.run();
    assertEquals("No failures", 0, res.failureCount());
    assertEquals("One error", 1, res.errorCount());
    
    Object o = res.errors().nextElement();
    TestFailure f = (TestFailure)o;
    
    if (f.exceptionMessage() == null || f.exceptionMessage().indexOf("Ahoj") == -1) {
        fail("Logged messages shall be in exception message: " + f.exceptionMessage());
    }
}
 
Example #2
Source File: TimeOutHasToPrintLogTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testThatTheTimeOutStillPrintsTheWarning() throws Exception {
    TimeOutHasToPrintLogTest t = new TimeOutHasToPrintLogTest("printAhojAndTimeOut");
    
    CharSequence seq = Log.enable(LOG.getName(), Level.FINE);
    
    TestResult res = t.run();
    
    assertEquals("One test has been run", 1, res.runCount());
    
    String s = seq.toString();
    
    if (s.indexOf("Ahoj") == -1) {
        fail("Ahoj has to be logged:\n" + s);
    }
    
    assertEquals("No error", 0, res.errorCount());
    assertEquals("One failure", 1, res.failureCount());
    
    TestFailure f = (TestFailure)res.failures().nextElement();
    s = f.exceptionMessage();
    if (s.indexOf("Ahoj") == -1) {
        fail("Ahoj has to be part of the message:\n" + s);
    }
}
 
Example #3
Source File: TimeOutHasToPrintLogTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testThreadDumpPrinted() throws Exception {
    TimeOutHasToPrintLogTest t = new TimeOutHasToPrintLogTest("justTimeOutInOneOfMyMethods");
    
    TestResult res = t.run();
    
    assertEquals("One test has been run", 1, res.runCount());
    TestFailure failure = (TestFailure)res.failures().nextElement();
    String s = failure.exceptionMessage();

    if (s.indexOf("justTimeOutInOneOfMyMethods") == -1) {
        fail("There should be thread dump reported in case of timeout:\n" + s);
    }
    
    assertEquals("No error", 0, res.errorCount());
    assertEquals("One failure", 1, res.failureCount());
}
 
Example #4
Source File: LoggingTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testMyExceptionIsWrappedWithLogMsg() throws Exception {
    LoggingTest inner = new LoggingTest("throwMyThrowable");
    
    class MyEx extends Exception {
    }
    
    inner.toThrow = new MyEx();
    
    TestResult res = inner.run();
    assertEquals("One error", 1, res.errorCount());
    assertEquals("No failure", 0, res.failureCount());
    
    TestFailure f = (TestFailure)res.errors().nextElement();
    
    if (f.exceptionMessage() == null || f.exceptionMessage().indexOf("Going to throw") == -1) {
        fail("There should be output of the log:\n" + f.exceptionMessage());
    }
}
 
Example #5
Source File: LoggingTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testMyExceptionWithStackTrace() throws Exception {
    LoggingTest inner = new LoggingTest("throwMyThrowable");
    
    class MyEx extends Exception {
    }
    
    inner.toThrow = new MyEx();
    inner.toMsg = new MyEx();
    
    TestResult res = inner.run();
    assertEquals("One error", 1, res.errorCount());
    assertEquals("No failure", 0, res.failureCount());
    
    TestFailure f = (TestFailure)res.errors().nextElement();
    
    if (
        f.exceptionMessage() == null || 
        f.exceptionMessage().indexOf("Going to throw") == -1 ||
        f.exceptionMessage().indexOf("testMyExceptionWithStackTrace") == -1
   ) {
        fail("There should be output of the log:\n" + f.exceptionMessage());
    }
}
 
Example #6
Source File: JUnitServlet.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void displayProblems( PrintWriter writer, String kind, int count, Enumeration enumeration ) {
    if (count != 0) {
        displayProblemTitle( writer, getFormatted( count, kind ) );
        Enumeration e = enumeration;
        for (int i = 1; e.hasMoreElements(); i++) {
            TestFailure failure = (TestFailure) e.nextElement();
            displayProblemDetailHeader( writer, i, failure.failedTest().toString() );
            if (failure.thrownException() instanceof AssertionFailedError) {
                displayProblemDetail( writer, failure.thrownException().getMessage() );
            } else {
                displayProblemDetail( writer, BaseTestRunner.getFilteredTrace( failure.thrownException() ) );
            }
            displayProblemDetailFooter( writer );
        }
    }
}
 
Example #7
Source File: JUnitServlet.java    From lams with GNU General Public License v2.0 6 votes vote down vote up
private void displayResults( PrintWriter writer, String failureNodeName, Enumeration resultsEnumeration ) {
    for (Enumeration e = resultsEnumeration; e.hasMoreElements();) {
        TestFailure failure = (TestFailure) e.nextElement();
        writer.println( "  <testcase name=" + asAttribute( failure.failedTest().toString() ) + ">" );
        writer.print( "    <" + failureNodeName + " type=" + asAttribute( failure.thrownException().getClass().getName() ) +
                                              " message=" + asAttribute( failure.exceptionMessage() ) );
        if (!displayException( failure )) {
            writer.println( "/>" );
        } else {
            writer.println( ">" );
            writer.print( sgmlEscape( BaseTestRunner.getFilteredTrace( failure.thrownException() ) ) );
            writer.println( "    </" + failureNodeName + ">" );
        }
        writer.println( "  </testcase>" );
    }
}
 
Example #8
Source File: LoggingControlTest.java    From netbeans with Apache License 2.0 6 votes vote down vote up
public void testRuntimeExceptionsAlsoGenerateLog() throws Exception {
    if (throwIt != null) {
        Logger.getLogger("global").info("Ahoj");
        throw throwIt;
    }
    
    LoggingControlTest l = new LoggingControlTest("testRuntimeExceptionsAlsoGenerateLog");
    l.throwIt = new NullPointerException();
    TestResult res = l.run();
    assertEquals("No failures", 0, res.failureCount());
    assertEquals("One error", 1, res.errorCount());
    
    Object o = res.errors().nextElement();
    TestFailure f = (TestFailure)o;
    
    if (f.exceptionMessage() == null || f.exceptionMessage().indexOf("Ahoj") == -1) {
        fail("Logged messages shall be in exception message: " + f.exceptionMessage());
    }
}
 
Example #9
Source File: NullableUniqueConstraintTest.java    From spliceengine with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void main(String [] args) {
    TestResult tr = new TestResult();
    Test t = suite();
    t.run(tr);
    System.out.println(tr.errorCount());
    Enumeration e = tr.failures();
    while (e.hasMoreElements()) {
        ((TestFailure)e.nextElement ()).thrownException().printStackTrace();
    }
    System.out.println(tr.failureCount());
}
 
Example #10
Source File: UIMAResultPrinter.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * @see junit.textui.ResultPrinter#printDefects(java.util.Enumeration, int, java.lang.String)
 */
protected void printDefects(Enumeration booBoos, int count, String type) {
  if (count == 0)
    return;
  if (count == 1)
    getWriter().println("There was " + count + " " + type + ":");
  else
    getWriter().println("There were " + count + " " + type + "s:");
  for (int i = 1; booBoos.hasMoreElements(); i++) {
    printDefect((TestFailure) booBoos.nextElement(), i);
  }
}
 
Example #11
Source File: SecurityTestSupport.java    From groovy with Apache License 2.0 5 votes vote down vote up
protected void executeTest(Class test, Permission missingPermission) {
    TestSuite suite = new TestSuite();
    suite.addTestSuite(test);
    TestResult result = new TestResult();
    suite.run(result);
    if (result.wasSuccessful()) {
        if (missingPermission == null) {
            return;
        } else {
            fail("Security test expected an AccessControlException on " + missingPermission + ", but did not receive one");
        }
    } else {
        if (missingPermission == null) {
            new SecurityTestResultPrinter(System.out).print(result);
            fail("Security test was expected to run successfully, but failed (results on System.out)");
        } else {
            //There may be more than 1 failure:  iterate to ensure that they all match the missingPermission.
            boolean otherFailure = false;
            for (Enumeration e = result.errors(); e.hasMoreElements();) {
                TestFailure failure = (TestFailure) e.nextElement();
                if (failure.thrownException() instanceof AccessControlException) {
                    AccessControlException ace = (AccessControlException) failure.thrownException();
                    if (missingPermission.implies(ace.getPermission())) {
                        continue;
                    }
                }
                otherFailure = true;
                break;
            }
            if (otherFailure) {
                new SecurityTestResultPrinter(System.out).print(result);
                fail("Security test expected an AccessControlException on " + missingPermission + ", but failed for other reasons (results on System.out)");
            }
        }
    }
}
 
Example #12
Source File: NullableUniqueConstraintTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static void main(String [] args) {
    TestResult tr = new TestResult();
    Test t = suite();
    t.run(tr);
    System.out.println(tr.errorCount());
    Enumeration e = tr.failures();
    while (e.hasMoreElements()) {
        ((TestFailure)e.nextElement ()).thrownException().printStackTrace();
    }
    System.out.println(tr.failureCount());
}
 
Example #13
Source File: TestCaseTest.java    From cacheonix-core with GNU Lesser General Public License v2.1 5 votes vote down vote up
public void testExceptionRunningAndTearDown() {
	// With 1.4, we should
	// wrap the exception thrown while running with the exception thrown
	// while tearing down
	Test t= new TornDown() {
		public void tearDown() {
			throw new Error("tearingDown");
		}
	};
	TestResult result= new TestResult();
	t.run(result);
	TestFailure failure= (TestFailure) result.errors().nextElement();
	assertEquals("running", failure.thrownException().getMessage());
}
 
Example #14
Source File: NullableUniqueConstraintTest.java    From gemfirexd-oss with Apache License 2.0 5 votes vote down vote up
public static void main(String [] args) {
    TestResult tr = new TestResult();
    Test t = suite();
    t.run(tr);
    System.out.println(tr.errorCount());
    Enumeration e = tr.failures();
    while (e.hasMoreElements()) {
        ((TestFailure)e.nextElement ()).thrownException().printStackTrace();
    }
    System.out.println(tr.failureCount());
}
 
Example #15
Source File: UIMAResultPrinter.java    From uima-uimaj with Apache License 2.0 5 votes vote down vote up
/**
 * @see junit.textui.ResultPrinter#printDefectHeader(junit.framework.TestFailure, int)
 */
protected void printDefectHeader(TestFailure booBoo, int count) {
  // I feel like making this a println, then adding a line giving the throwable a chance to print
  // something
  // before we get to the stack trace.
  getWriter().print(count + ") " + booBoo.failedTest());
}
 
Example #16
Source File: LoggingTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testIOExceptionIsWrappedWithLogMsg() throws Exception {
    
    LoggingTest inner = new LoggingTest("throwIOThrowable");
    inner.toThrow = new IOException("Ahoj");
    
    TestResult res = inner.run();
    assertEquals("One error", 1, res.errorCount());
    assertEquals("No failure", 0, res.failureCount());
    
    TestFailure f = (TestFailure)res.errors().nextElement();
    
    if (f.exceptionMessage().indexOf("Going to throw") == -1) {
        fail("There should be output of the log:\n" + f.exceptionMessage());
    }
}
 
Example #17
Source File: TimeOutTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void run(TestResult result) {
    if (!canRun()) {
        return;
    }
    this.main = Thread.currentThread();
    
    TestResult mine = new TestResult();
    result.startTest(this);
    super.run(mine);
    
    if (mine.errorCount() != 0) {
        Enumeration en = mine.errors();
        while(en.hasMoreElements()) {
            TestFailure f = (TestFailure)en.nextElement();
            result.addError(this, f.thrownException());
        }
        return;
    }
    if (expectedResult != (mine.failureCount() == 0)) {
        result.addFailure(this, 
            new AssertionFailedError(
                "expectedResult: " + expectedResult + "failureCount: " + mine.failureCount() + " for " + getName()
            )
        );
        return;
    }
    
    result.endTest(this);
}
 
Example #18
Source File: NbModuleSuiteExceptionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testFailOnException() {
    System.setProperty("generate.msg", "false");
    System.setProperty("generate.exc", "true");

    Test instance =
        NbModuleSuite.createConfiguration(NbModuleSuiteException.class).
        gui(false).
        failOnException(Level.INFO)
    .suite();
    TestResult r = junit.textui.TestRunner.run(instance);
    assertEquals("One failure", 1, r.failureCount());
    assertEquals("No errors", 0, r.errorCount());
    TestFailure f = r.failures().nextElement();
    assertEquals("Failure name", "testGenerateMsgOrException", ((TestCase)f.failedTest()).getName());
}
 
Example #19
Source File: NbModuleSuiteExceptionTest.java    From netbeans with Apache License 2.0 5 votes vote down vote up
public void testFailOnMessage() {
    System.setProperty("generate.msg", "true");
    System.setProperty("generate.exc", "false");

    Test instance =
        NbModuleSuite.createConfiguration(NbModuleSuiteException.class).
        gui(false).
        failOnMessage(Level.WARNING)
    .suite();
    TestResult r = junit.textui.TestRunner.run(instance);
    assertEquals("One failure", 1, r.failureCount());
    assertEquals("No errors", 0, r.errorCount());
    TestFailure f = r.failures().nextElement();
    assertEquals("Failure name", "testGenerateMsgOrException", ((TestCase)f.failedTest()).getName());
}
 
Example #20
Source File: UIMAResultPrinter.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * @see junit.textui.ResultPrinter#printDefect(junit.framework.TestFailure, int)
 */
public void printDefect(TestFailure booBoo, int count) { // only public for testing purposes
  printDefectHeader(booBoo, count);
  printDefectTrace(booBoo);
}
 
Example #21
Source File: UIMAResultPrinter.java    From uima-uimaj with Apache License 2.0 4 votes vote down vote up
/**
 * @see junit.textui.ResultPrinter#printDefectTrace(junit.framework.TestFailure)
 */
protected void printDefectTrace(TestFailure booBoo) {
  getWriter().print(BaseTestRunner.getFilteredTrace(booBoo.trace()));
}
 
Example #22
Source File: DelegatingTestResult.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public Enumeration<TestFailure> failures() {
  return wrappedResult.failures();
}
 
Example #23
Source File: DelegatingTestResult.java    From android-test with Apache License 2.0 4 votes vote down vote up
@Override
public Enumeration<TestFailure> errors() {
  return wrappedResult.errors();
}
 
Example #24
Source File: TestRunContainer.java    From scipio-erp with Apache License 2.0 4 votes vote down vote up
public boolean start() throws ContainerException {
    // configure log4j output logging
    if (logLevel != null) {
        int llevel = Debug.getLevelFromString(logLevel);

        for (int v = 0; v < 9; v++) {
            if (v < llevel) {
                Debug.set(v, false);
            } else {
                Debug.set(v, true);
            }
        }
    }

    // get the tests to run
    JunitSuiteWrapper jsWrapper = new JunitSuiteWrapper(component, suiteName, testCase);
    if (jsWrapper.getAllTestList().size() == 0) {
        throw new ContainerException("No tests found (" + component + " / " + suiteName + " / " + testCase + ")");
    }

    boolean failedRun = false;
    for (ModelTestSuite modelSuite: jsWrapper.getModelTestSuites()) {
        Delegator testDelegator = modelSuite.getDelegator();
        TestSuite suite = modelSuite.makeTestSuite();
        JUnitTest test = new JUnitTest();
        test.setName(suite.getName());

        // create the XML logger
        JunitXmlListener xml;
        try {
            xml = new JunitXmlListener(new FileOutputStream(logDir + suite.getName() + ".xml"));
        } catch (FileNotFoundException e) {
            throw new ContainerException(e);
        }

        // per-suite results
        TestResult results = new TestResult();
        results.addListener(new JunitListener());
        results.addListener(xml);

        // add the suite to the xml listener
        xml.startTestSuite(test);
        // run the tests
        suite.run(results);
        test.setCounts(results.runCount(), results.failureCount(), results.errorCount());
        // rollback all entity operations performed by the delegator
        testDelegator.rollback();
        xml.endTestSuite(test);

        if (!results.wasSuccessful()) {
            failedRun = true;
        }

        // display the results
        Debug.logInfo("[JUNIT] Results for test suite: " + suite.getName(), module);
        Debug.logInfo("[JUNIT] Pass: " + results.wasSuccessful() + " | # Tests: " + results.runCount() + " | # Failed: " +
                results.failureCount() + " # Errors: " + results.errorCount(), module);
        if (Debug.importantOn() && !results.wasSuccessful()) {
            Debug.logInfo("[JUNIT] ----------------------------- ERRORS ----------------------------- [JUNIT]", module);
            Enumeration<?> err = results.errors();
            if (!err.hasMoreElements()) {
                Debug.logInfo("None", module);
            } else {
                while (err.hasMoreElements()) {
                    Object error = err.nextElement();
                    Debug.logInfo("--> " + error, module);
                    if (error instanceof TestFailure) {
                        Debug.logInfo(((TestFailure) error).trace(), module);
                    }
                }
            }
            Debug.logInfo("[JUNIT] ------------------------------------------------------------------ [JUNIT]", module);
            Debug.logInfo("[JUNIT] ---------------------------- FAILURES ---------------------------- [JUNIT]", module);
            Enumeration<?> fail = results.failures();
            if (!fail.hasMoreElements()) {
                Debug.logInfo("None", module);
            } else {
                while (fail.hasMoreElements()) {
                    Object failure = fail.nextElement();
                    Debug.logInfo("--> " + failure, module);
                    if (failure instanceof TestFailure) {
                        Debug.logInfo(((TestFailure) failure).trace(), module);
                    }
                }
            }
            Debug.logInfo("[JUNIT] ------------------------------------------------------------------ [JUNIT]", module);
        }
    }

    if (failedRun) {
        throw new ContainerException("Test run was unsuccessful");
    }
    return true;
}
 
Example #25
Source File: JUnitServlet.java    From lams with GNU General Public License v2.0 4 votes vote down vote up
private boolean displayException( TestFailure failure ) {
    return true;
}