Search in sources :

Example 6 with MultipleFailureException

use of org.junit.runners.model.MultipleFailureException in project sling by apache.

the class TeleporterHttpClient method runTests.

void runTests(String testSelectionPath, int testReadyTimeoutSeconds) throws MalformedURLException, IOException, MultipleFailureException {
    final String testUrl = baseUrl + "/" + testServletPath + testSelectionPath + ".junit_result";
    // Wait for non-404 response that signals that test bundle is ready
    final long timeout = System.currentTimeMillis() + (testReadyTimeoutSeconds * 1000L);
    final ExponentialBackoffDelay delay = new ExponentialBackoffDelay(25, 1000);
    while (true) {
        if (getHttpGetStatus(testUrl).getStatus() == 200) {
            break;
        }
        if (System.currentTimeMillis() > timeout) {
            fail("Timeout waiting for test at " + testUrl + " (" + testReadyTimeoutSeconds + " seconds)");
            break;
        }
        delay.waitNextDelay();
    }
    final HttpURLConnection c = (HttpURLConnection) new URL(testUrl).openConnection();
    try {
        setConnectionCredentials(c);
        c.setRequestMethod("POST");
        c.setUseCaches(false);
        c.setDoOutput(true);
        c.setDoInput(true);
        c.setInstanceFollowRedirects(false);
        final int status = c.getResponseCode();
        if (status != 200) {
            throw new IOException("Got status code " + status + " for " + testUrl);
        }
        final Result result = (Result) new ObjectInputStream(c.getInputStream()).readObject();
        if (result.getFailureCount() > 0) {
            final List<Throwable> failures = new ArrayList<Throwable>(result.getFailureCount());
            for (Failure f : result.getFailures()) {
                failures.add(f.getException());
            }
            throw new MultipleFailureException(failures);
        }
    } catch (ClassNotFoundException e) {
        throw new IOException("Exception reading test results:" + e, e);
    } finally {
        cleanup(c);
    }
}
Also used : ArrayList(java.util.ArrayList) JsonString(javax.json.JsonString) IOException(java.io.IOException) URL(java.net.URL) Result(org.junit.runner.Result) HttpURLConnection(java.net.HttpURLConnection) MultipleFailureException(org.junit.runners.model.MultipleFailureException) Failure(org.junit.runner.notification.Failure) ObjectInputStream(java.io.ObjectInputStream)

Example 7 with MultipleFailureException

use of org.junit.runners.model.MultipleFailureException in project junit4 by junit-team.

the class ExternalResourceRuleTest method shouldWrapAssumptionFailuresWhenClosingResourceFails.

@Test
public void shouldWrapAssumptionFailuresWhenClosingResourceFails() throws Throwable {
    // given
    final AtomicReference<Throwable> externalResourceException = new AtomicReference<Throwable>();
    ExternalResource resourceRule = new ExternalResource() {

        @Override
        protected void after() {
            RuntimeException runtimeException = new RuntimeException("simulating resource tear down failure");
            externalResourceException.set(runtimeException);
            throw runtimeException;
        }
    };
    final AtomicReference<Throwable> assumptionViolatedException = new AtomicReference<Throwable>();
    Statement skippedTest = new Statement() {

        @Override
        public void evaluate() throws Throwable {
            AssumptionViolatedException assumptionFailure = new AssumptionViolatedException("skip it");
            assumptionViolatedException.set(assumptionFailure);
            throw assumptionFailure;
        }
    };
    Description dummyDescription = Description.createTestDescription("dummy test class name", "dummy test name");
    try {
        resourceRule.apply(skippedTest, dummyDescription).evaluate();
        fail("ExternalResource should throw");
    } catch (MultipleFailureException e) {
        assertThat(e.getFailures(), hasItems(instanceOf(TestCouldNotBeSkippedException.class), sameInstance(externalResourceException.get())));
        assertThat(e.getFailures(), hasItems(hasCause(sameInstance(assumptionViolatedException.get())), sameInstance(externalResourceException.get())));
    }
}
Also used : Description(org.junit.runner.Description) AssumptionViolatedException(org.junit.internal.AssumptionViolatedException) MultipleFailureException(org.junit.runners.model.MultipleFailureException) Statement(org.junit.runners.model.Statement) AtomicReference(java.util.concurrent.atomic.AtomicReference) TestCouldNotBeSkippedException(org.junit.TestCouldNotBeSkippedException) Test(org.junit.Test)

Example 8 with MultipleFailureException

use of org.junit.runners.model.MultipleFailureException in project randomizedtesting by randomizedtesting.

the class RandomizedRunner method wrapBeforeAndAfters.

/**
   * Wrap before and after hooks.
   */
private Statement wrapBeforeAndAfters(Statement s, final TestCandidate c, final Object instance) {
    // Process @Before hooks. The first @Before to fail will immediately stop processing any other @Befores.
    final List<Method> befores = getShuffledMethods(Before.class);
    if (!befores.isEmpty()) {
        final Statement afterBefores = s;
        s = new Statement() {

            @Override
            public void evaluate() throws Throwable {
                for (Method m : befores) {
                    invoke(m, instance);
                }
                afterBefores.evaluate();
            }
        };
    }
    // Process @After hooks. All @After hooks are processed, regardless of their own exceptions.
    final List<Method> afters = getShuffledMethods(After.class);
    if (!afters.isEmpty()) {
        final Statement beforeAfters = s;
        s = new Statement() {

            @Override
            public void evaluate() throws Throwable {
                List<Throwable> cumulative = new ArrayList<Throwable>();
                try {
                    beforeAfters.evaluate();
                } catch (Throwable t) {
                    cumulative.add(t);
                }
                // All @Afters must be called.
                for (Method m : afters) {
                    try {
                        invoke(m, instance);
                    } catch (Throwable t) {
                        cumulative.add(t);
                    }
                }
                // At end, throw the exception or propagete.
                if (cumulative.size() == 1) {
                    throw cumulative.get(0);
                } else if (cumulative.size() > 1) {
                    throw new MultipleFailureException(cumulative);
                }
            }
        };
    }
    return s;
}
Also used : MultipleFailureException(org.junit.runners.model.MultipleFailureException) Statement(org.junit.runners.model.Statement) List(java.util.List) ArrayList(java.util.ArrayList) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod)

Example 9 with MultipleFailureException

use of org.junit.runners.model.MultipleFailureException in project neo4j by neo4j.

the class TestData method apply.

@Override
public Statement apply(final Statement base, final Description description) {
    final Title title = description.getAnnotation(Title.class);
    final Documented doc = description.getAnnotation(Documented.class);
    GraphDescription.Graph g = description.getAnnotation(GraphDescription.Graph.class);
    if (g == null) {
        g = description.getTestClass().getAnnotation(GraphDescription.Graph.class);
    }
    final GraphDescription graph = GraphDescription.create(g);
    return new Statement() {

        @Override
        public void evaluate() throws Throwable {
            product.set(create(graph, title == null ? null : title.value(), doc == null ? null : doc.value(), description.getMethodName()));
            try {
                try {
                    base.evaluate();
                } catch (Throwable err) {
                    try {
                        destroy(get(false), false);
                    } catch (Throwable sub) {
                        List<Throwable> failures = new ArrayList<Throwable>();
                        if (err instanceof MultipleFailureException) {
                            failures.addAll(((MultipleFailureException) err).getFailures());
                        } else {
                            failures.add(err);
                        }
                        failures.add(sub);
                        throw new MultipleFailureException(failures);
                    }
                    throw err;
                }
                destroy(get(false), false);
            } finally {
                product.set(null);
            }
        }
    };
}
Also used : Documented(org.neo4j.kernel.impl.annotations.Documented) MultipleFailureException(org.junit.runners.model.MultipleFailureException) Statement(org.junit.runners.model.Statement) ArrayList(java.util.ArrayList)

Example 10 with MultipleFailureException

use of org.junit.runners.model.MultipleFailureException in project spock by spockframework.

the class JUnitSupervisor method handleMultipleFailures.

// for better JUnit compatibility, e.g when a @Rule is used
private int handleMultipleFailures(ErrorInfo error) {
    MultipleFailureException multiFailure = (MultipleFailureException) error.getException();
    int runStatus = OK;
    for (Throwable failure : multiFailure.getFailures()) runStatus = error(new ErrorInfo(error.getMethod(), failure));
    return runStatus;
}
Also used : MultipleFailureException(org.junit.runners.model.MultipleFailureException)

Aggregations

MultipleFailureException (org.junit.runners.model.MultipleFailureException)11 ArrayList (java.util.ArrayList)5 Statement (org.junit.runners.model.Statement)4 Test (org.junit.Test)3 AssumptionViolatedException (org.junit.internal.AssumptionViolatedException)3 Description (org.junit.runner.Description)2 Failure (org.junit.runner.notification.Failure)2 Job (com.birbit.android.jobqueue.Job)1 JobManager (com.birbit.android.jobqueue.JobManager)1 Params (com.birbit.android.jobqueue.Params)1 IOException (java.io.IOException)1 ObjectInputStream (java.io.ObjectInputStream)1 Method (java.lang.reflect.Method)1 HttpURLConnection (java.net.HttpURLConnection)1 URL (java.net.URL)1 List (java.util.List)1 ExecutionException (java.util.concurrent.ExecutionException)1 TimeoutException (java.util.concurrent.TimeoutException)1 AtomicReference (java.util.concurrent.atomic.AtomicReference)1 JsonString (javax.json.JsonString)1