Search in sources :

Example 1 with TestFailure

use of junit.framework.TestFailure in project groovy by apache.

the class SecurityTestSupport method executeTest.

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)");
            }
        }
    }
}
Also used : Enumeration(java.util.Enumeration) TestSuite(junit.framework.TestSuite) TestFailure(junit.framework.TestFailure) TestResult(junit.framework.TestResult)

Example 2 with TestFailure

use of junit.framework.TestFailure in project junit4 by junit-team.

the class TestCaseTest method testExceptionRunningAndTearDown.

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());
}
Also used : Test(junit.framework.Test) TestFailure(junit.framework.TestFailure) TestResult(junit.framework.TestResult)

Example 3 with TestFailure

use of junit.framework.TestFailure in project nutz by nutzam.

the class AdvancedTestAll method printResult.

public static void printResult(TestResult result) {
    System.out.printf("Run %d , Fail %d , Error %d \n", result.runCount(), result.failureCount(), result.errorCount());
    if (result.failureCount() > 0) {
        Enumeration<TestFailure> enu = result.failures();
        while (enu.hasMoreElements()) {
            TestFailure testFailure = (TestFailure) enu.nextElement();
            System.out.println("--Fail------------------------------------------------");
            System.out.println(testFailure.trace());
            testFailure.thrownException().printStackTrace(System.out);
        }
    }
    if (result.errorCount() > 0) {
        Enumeration<TestFailure> enu = result.errors();
        while (enu.hasMoreElements()) {
            TestFailure testFailure = (TestFailure) enu.nextElement();
            System.out.println("--ERROR------------------------------------------------");
            System.out.println(testFailure.trace());
            testFailure.thrownException().printStackTrace(System.out);
        }
    }
}
Also used : TestFailure(junit.framework.TestFailure)

Example 4 with TestFailure

use of junit.framework.TestFailure in project jena by apache.

the class SimpleTestRunner method runAndReport.

public static void runAndReport(Test ts, PrintStream out) {
    if (out == null)
        out = System.out;
    TestResult result = runNoReport(ts);
    if (result.errorCount() > 0 || result.failureCount() > 0) {
        out.println();
        out.println("===========================================");
    }
    int goodCount = result.runCount() - result.errorCount() - result.failureCount();
    out.println("Tests = " + result.runCount() + " : Successes = " + goodCount + " : Errors = " + result.errorCount() + " : Failures = " + result.failureCount());
    for (Enumeration<?> e = result.errors(); e.hasMoreElements(); ) {
        out.println();
        TestFailure failure = (TestFailure) e.nextElement();
        out.println("Error:    " + failure.toString());
    }
    for (Enumeration<?> e = result.failures(); e.hasMoreElements(); ) {
        out.println();
        TestFailure failure = (TestFailure) e.nextElement();
        out.println("Failure:  " + failure.toString());
    }
}
Also used : TestFailure(junit.framework.TestFailure) TestResult(junit.framework.TestResult)

Example 5 with TestFailure

use of junit.framework.TestFailure in project jmeter by apache.

the class JUnitSampler method sample.

/** {@inheritDoc} */
@Override
public SampleResult sample(Entry entry) {
    if (getCreateOneInstancePerSample()) {
        initializeTestObject();
    }
    SampleResult sresult = new SampleResult();
    // Bug 41522 - don't use rlabel here
    sresult.setSampleLabel(getName());
    sresult.setSamplerData(className + "." + methodName);
    sresult.setDataType(SampleResult.TEXT);
    // Assume success
    sresult.setSuccessful(true);
    sresult.setResponseMessage(getSuccess());
    sresult.setResponseCode(getSuccessCode());
    if (this.testCase != null) {
        // create a new TestResult
        TestResult tr = new TestResult();
        final TestCase theClazz = this.testCase;
        try {
            if (setUpMethod != null) {
                setUpMethod.invoke(this.testObject, new Object[0]);
            }
            sresult.sampleStart();
            tr.startTest(this.testCase);
            // Do not use TestCase.run(TestResult) method, since it will
            // call setUp and tearDown. Doing that will result in calling
            // the setUp and tearDown method twice and the elapsed time
            // will include setup and teardown.
            tr.runProtected(theClazz, protectable);
            tr.endTest(this.testCase);
            sresult.sampleEnd();
            if (tearDownMethod != null) {
                tearDownMethod.invoke(testObject, new Object[0]);
            }
        } catch (InvocationTargetException e) {
            Throwable cause = e.getCause();
            if (cause instanceof AssertionFailedError) {
                tr.addFailure(theClazz, (AssertionFailedError) cause);
            } else if (cause instanceof AssertionError) {
                // Convert JUnit4 failure to Junit3 style
                AssertionFailedError afe = new AssertionFailedError(cause.toString());
                // copy the original stack trace
                afe.setStackTrace(cause.getStackTrace());
                tr.addFailure(theClazz, afe);
            } else if (cause != null) {
                tr.addError(theClazz, cause);
            } else {
                tr.addError(theClazz, e);
            }
        } catch (IllegalAccessException | IllegalArgumentException e) {
            tr.addError(theClazz, e);
        }
        if (!tr.wasSuccessful()) {
            sresult.setSuccessful(false);
            StringBuilder buf = new StringBuilder();
            StringBuilder buftrace = new StringBuilder();
            Enumeration<TestFailure> en;
            if (getAppendError()) {
                en = tr.failures();
                if (en.hasMoreElements()) {
                    sresult.setResponseCode(getFailureCode());
                    buf.append(getFailure());
                    buf.append("\n");
                }
                while (en.hasMoreElements()) {
                    TestFailure item = en.nextElement();
                    buf.append("Failure -- ");
                    buf.append(item.toString());
                    buf.append("\n");
                    buftrace.append("Failure -- ");
                    buftrace.append(item.toString());
                    buftrace.append("\n");
                    buftrace.append("Trace -- ");
                    buftrace.append(item.trace());
                }
                en = tr.errors();
                if (en.hasMoreElements()) {
                    sresult.setResponseCode(getErrorCode());
                    buf.append(getError());
                    buf.append("\n");
                }
                while (en.hasMoreElements()) {
                    TestFailure item = en.nextElement();
                    buf.append("Error -- ");
                    buf.append(item.toString());
                    buf.append("\n");
                    buftrace.append("Error -- ");
                    buftrace.append(item.toString());
                    buftrace.append("\n");
                    buftrace.append("Trace -- ");
                    buftrace.append(item.trace());
                }
            }
            sresult.setResponseMessage(buf.toString());
            sresult.setResponseData(buftrace.toString(), null);
        }
    } else {
        // we should log a warning, but allow the test to keep running
        sresult.setSuccessful(false);
        // this should be externalized to the properties
        sresult.setResponseMessage("Failed to create an instance of the class:" + getClassname() + ", reasons may be missing both empty constructor and one " + "String constructor or failure to instantiate constructor," + " check warning messages in jmeter log file");
        sresult.setResponseCode(getErrorCode());
    }
    return sresult;
}
Also used : TestFailure(junit.framework.TestFailure) TestResult(junit.framework.TestResult) InvocationTargetException(java.lang.reflect.InvocationTargetException) TestCase(junit.framework.TestCase) SampleResult(org.apache.jmeter.samplers.SampleResult) AssertionFailedError(junit.framework.AssertionFailedError)

Aggregations

TestFailure (junit.framework.TestFailure)11 TestResult (junit.framework.TestResult)8 Enumeration (java.util.Enumeration)5 TestCase (junit.framework.TestCase)2 TestSuite (junit.framework.TestSuite)2 FileWriter (java.io.FileWriter)1 IOException (java.io.IOException)1 InvocationTargetException (java.lang.reflect.InvocationTargetException)1 AssertionFailedError (junit.framework.AssertionFailedError)1 JUnit4TestAdapter (junit.framework.JUnit4TestAdapter)1 Test (junit.framework.Test)1 SampleResult (org.apache.jmeter.samplers.SampleResult)1 Test (org.junit.Test)1 Description (org.junit.runner.Description)1 Request (org.junit.runner.Request)1 Failure (org.junit.runner.notification.Failure)1 RunListener (org.junit.runner.notification.RunListener)1 RunNotifier (org.junit.runner.notification.RunNotifier)1