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()); } }
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); } }
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()); }
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()); } }
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()); } }
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()); } }
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 ); } } }
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>" ); } }
/** * Populate the list of failures in each of the suites. */ private void populateFailures(boolean errors, Enumeration<TestFailure> failures) { while (failures.hasMoreElements()) { TestFailure failure = failures.nextElement(); TestId test = new TestId(failure.failedTest()); Suite suite = suites.get(test.className); if (suite == null) { throw new IllegalStateException( "received a failure for a " + "test that wasn't in the original test suite!"); } if (errors) { suite.errors.put(test, failure); } else { suite.failures.put(test, failure); } } }
protected static void outputErrorInfo(Enumeration e, boolean quiet) { while (e.hasMoreElements()) { TestFailure tf = (TestFailure) e.nextElement(); if (! quiet) { System.out.println(tf.trace()); Throwable t = tf.thrownException().getCause(); while (t != null) { t.printStackTrace(); t = t.getCause(); } } else { System.out.println(tf.toString()); System.out.println("run without -quiet for more information"); } } }
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()); }
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()); }
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); }
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()); } }
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()); }
private void dumpList(String header, Enumeration<TestFailure> failures) { System.err.println(header); while (failures.hasMoreElements()) { TestFailure failure = failures.nextElement(); System.err.print(" "); System.err.println(failure.toString()); } }
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()); }
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)"); } } } }
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); } }
void print(KXmlSerializer serializer, String timestamp) throws IOException { serializer.startTag(ns, TESTSUITE); serializer.attribute(ns, ATTR_NAME, name); serializer.attribute(ns, ATTR_TESTS, Integer.toString(tests.size())); serializer.attribute(ns, ATTR_FAILURES, Integer.toString(failures.size())); serializer.attribute(ns, ATTR_ERRORS, Integer.toString(errors.size())); serializer.attribute(ns, ATTR_TIME, "0"); serializer.attribute(ns, TIMESTAMP, timestamp); serializer.attribute(ns, HOSTNAME, "localhost"); serializer.startTag(ns, PROPERTIES); serializer.endTag(ns, PROPERTIES); for (TestId testId : tests) { TestFailure error = errors.get(testId); TestFailure failure = failures.get(testId); if (error != null) { testId.printFailure(serializer, ERROR, error.thrownException()); } else if (failure != null) { testId.printFailure(serializer, FAILURE, failure.thrownException()); } else { testId.printSuccess(serializer); } } serializer.endTag(ns, TESTSUITE); }
/** * Provides the main entry point. First and second argument are class and * method name, respectively. Third argument is the temporary file name for * the result. Exits with one of the usual JUnit exit values. */ public static void main(String args[]) { Logger.global.setLevel(Level.OFF); CoreTestIsolator testRunner = new CoreTestIsolator(); try { TestResult r = testRunner.start(args); if (!r.wasSuccessful()) { // Store failure or error - we know there must be one Throwable failure = r.failureCount() != 0 ? ((TestFailure)r.failures().nextElement()). thrownException() : ((TestFailure)r.errors().nextElement()). thrownException(); saveStackTrace(failure, args[2]); System.exit(FAILURE_EXIT); } else { // Nothing to see here, please get along System.exit(SUCCESS_EXIT); } } catch(Exception e) { // Let main TestRunner know about execution problem saveStackTrace(e, args[2]); System.exit(EXCEPTION_EXIT); } }
private void writeClassNames(Enumeration<TestFailure> failures, Writer writer) throws IOException { while (failures.hasMoreElements()) { TestFailure failure = failures.nextElement(); Test test = failure.failedTest(); writeTest(writer, test); } }
protected void printDefects(Enumeration<TestFailure> 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(booBoos.nextElement(), i); } }
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() { @Override public void tearDown() { throw new Error("tearingDown"); } }; TestResult result = new TestResult(); t.run(result); TestFailure failure = result.errors().nextElement(); assertEquals("running", failure.thrownException().getMessage()); }
public void testInvalidMethod() { TestResult result = new TestResult(); junit.framework.Test adapter = new JUnit4TestAdapter(InvalidMethodTest.class); adapter.run(result); assertEquals(1, result.errorCount()); TestFailure failure = result.errors().nextElement(); assertTrue(failure.exceptionMessage().contains("Method shouldBeStatic() should be static")); }
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); } }
public void printFailures(TestResult result) { if (result.failureCount() != 0) { if (result.failureCount() == 1) getWriter().println("There was " + result.failureCount() + " failure:"); else getWriter().println("There were " + result.failureCount() + " failures:"); int i = 1; for (Enumeration e = result.failures(); e.hasMoreElements(); i++) { TestFailure failure = (TestFailure) e.nextElement(); getWriter().print(i + ") " + failure.failedTest()); Throwable t = failure.thrownException(); getWriter().print(getFilteredTrace2(failure.thrownException())); } } }
public void printErrors(TestResult result) { if (result.errorCount() != 0) { if (result.errorCount() == 1) getWriter().println("There was " + result.errorCount() + " error:"); else getWriter().println("There were " + result.errorCount() + " errors:"); int i = 1; for (Enumeration e = result.errors(); e.hasMoreElements(); i++) { TestFailure failure = (TestFailure) e.nextElement(); getWriter().println(i + ") " + failure.failedTest()); getWriter().print(getFilteredTrace2(failure.thrownException())); } } }