Search in sources :

Example 1 with Failure

use of org.junit.runner.notification.Failure in project buck by facebook.

the class DelegateRunNotifier method fireTestStarted.

@Override
public void fireTestStarted(final Description description) throws StoppedByUserException {
    delegate.fireTestStarted(description);
    // Do not do apply the default timeout if the test has its own @Test(timeout).
    if (hasJunitTimeout(description)) {
        return;
    }
    // Schedule a timer that verifies that the test completed within the specified timeout.
    TimerTask task = new TimerTask() {

        @Override
        public void run() {
            synchronized (finishedTests) {
                // If the test already finished, then do nothing.
                if (finishedTests.contains(description)) {
                    return;
                }
                hasTestThatExceededTimeout.set(true);
                // Should report the failure. The Exception is modeled after the one created by
                // org.junit.internal.runners.statements.FailOnTimeout#createTimeoutException(Thread).
                Exception exception = new Exception(String.format("test timed out after %d milliseconds", defaultTestTimeoutMillis));
                Failure failure = new Failure(description, exception);
                fireTestFailure(failure);
                fireTestFinished(description);
                if (!finishedTests.contains(description)) {
                    throw new IllegalStateException("fireTestFinished() should update finishedTests.");
                }
                onTestRunFinished();
            }
        }
    };
    timer.schedule(task, defaultTestTimeoutMillis);
}
Also used : TimerTask(java.util.TimerTask) StoppedByUserException(org.junit.runner.notification.StoppedByUserException) Failure(org.junit.runner.notification.Failure)

Example 2 with Failure

use of org.junit.runner.notification.Failure in project jersey by jersey.

the class ConcurrentParameterizedRunner method runChild.

@Override
protected void runChild(final FrameworkMethod method, final RunNotifier notifier) {
    notifier.fireTestStarted(describeChild(method));
    final Object testInstance;
    try {
        // get the test parameter iterator first
        final List<FrameworkMethod> parameterMethods = getTestClass().getAnnotatedMethods(Parameterized.Parameters.class);
        final Iterable<Object[]> parameters = (Iterable<Object[]>) parameterMethods.get(0).getMethod().invoke(null);
        // then create the test instance
        testInstance = super.createTest();
        // now run the before methods
        List<FrameworkMethod> befores = getTestClass().getAnnotatedMethods(Before.class);
        for (FrameworkMethod before : befores) {
            before.getMethod().invoke(testInstance);
        }
        // and launch as meny test method invocations as many parameters is available
        final Iterator<Object[]> paramIterator = parameters.iterator();
        final Method javaTestMethod = method.getMethod();
        final AtomicInteger submitted = new AtomicInteger(0);
        while (paramIterator.hasNext()) {
            final Object[] javaMethodArgs = paramIterator.next();
            submitted.incrementAndGet();
            executor.submit(new Runnable() {

                @Override
                public void run() {
                    try {
                        javaTestMethod.invoke(testInstance, javaMethodArgs);
                    } catch (IllegalAccessException ex) {
                        notifier.fireTestFailure(new Failure(describeChild(method), ex));
                    } catch (IllegalArgumentException ex) {
                        notifier.fireTestFailure(new Failure(describeChild(method), ex));
                    } catch (InvocationTargetException ex) {
                        notifier.fireTestFailure(new Failure(describeChild(method), ex));
                    } finally {
                        submitted.decrementAndGet();
                    }
                }
            });
        }
        // wait until everything is done
        while (submitted.intValue() > 0) {
            LOGGER.info(String.format("Waiting for %d requests to finish...%n", submitted.intValue()));
            try {
                Thread.sleep(FINISH_WAIT_CYCLE_MS);
            } catch (InterruptedException e) {
            }
        }
        // and launch the after party..
        List<FrameworkMethod> afters = getTestClass().getAnnotatedMethods(After.class);
        for (FrameworkMethod after : afters) {
            after.getMethod().invoke(testInstance);
        }
    } catch (Exception ex) {
        notifier.fireTestFailure(new Failure(describeChild(method), ex));
        return;
    }
    notifier.fireTestFinished(describeChild(method));
}
Also used : FrameworkMethod(org.junit.runners.model.FrameworkMethod) Method(java.lang.reflect.Method) InvocationTargetException(java.lang.reflect.InvocationTargetException) InvocationTargetException(java.lang.reflect.InvocationTargetException) Parameterized(org.junit.runners.Parameterized) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Failure(org.junit.runner.notification.Failure)

Example 3 with Failure

use of org.junit.runner.notification.Failure in project jersey by jersey.

the class ConcurrentRunner method runThemAll.

private void runThemAll(final List<FrameworkMethod> methods, final RunNotifier notifier) {
    final Object testInstance;
    try {
        testInstance = super.createTest();
    } catch (Exception ex) {
        throw new RuntimeException(ex);
    }
    // run the before methods
    List<FrameworkMethod> befores = getTestClass().getAnnotatedMethods(Before.class);
    for (FrameworkMethod before : befores) {
        try {
            before.getMethod().invoke(testInstance);
        } catch (Exception ex) {
            LOGGER.log(java.util.logging.Level.SEVERE, null, ex);
        }
        final AtomicInteger submitted = new AtomicInteger(0);
        for (final FrameworkMethod method : methods) {
            try {
                notifier.fireTestStarted(describeChild(method));
                final Method javaTestMethod = method.getMethod();
                final Object[] javaMethodArgs = new Object[] {};
                submitted.incrementAndGet();
                executor.submit(new Runnable() {

                    @Override
                    public void run() {
                        try {
                            javaTestMethod.invoke(testInstance, javaMethodArgs);
                        } catch (Exception ex) {
                            notifier.fireTestFailure(new Failure(describeChild(method), ex));
                        } finally {
                            submitted.decrementAndGet();
                        }
                    }
                });
            } catch (Exception ex) {
                notifier.fireTestFailure(new Failure(describeChild(method), ex));
                return;
            }
            notifier.fireTestFinished(describeChild(method));
        }
        // wait until everything is done
        while (submitted.intValue() > 0) {
            LOGGER.info(String.format("Waiting for %d requests to finish...%n", submitted.intValue()));
            try {
                Thread.sleep(FINISH_WAIT_CYCLE_MS);
            } catch (InterruptedException e) {
            }
        }
        // and launch the after party..
        List<FrameworkMethod> afters = getTestClass().getAnnotatedMethods(After.class);
        for (FrameworkMethod after : afters) {
            try {
                after.getMethod().invoke(testInstance);
            } catch (Exception ex) {
                LOGGER.log(Level.SEVERE, null, ex);
            }
        }
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Method(java.lang.reflect.Method) FrameworkMethod(org.junit.runners.model.FrameworkMethod) Failure(org.junit.runner.notification.Failure)

Example 4 with Failure

use of org.junit.runner.notification.Failure in project junit4 by junit-team.

the class EventCollector method hasSingleFailureWithMessage.

static Matcher<EventCollector> hasSingleFailureWithMessage(final Matcher<String> messageMatcher) {
    return new TypeSafeMatcher<EventCollector>() {

        @Override
        public boolean matchesSafely(EventCollector item) {
            return hasSingleFailure().matches(item) && messageMatcher.matches(item.fFailures.get(0).getMessage());
        }

        public void describeTo(org.hamcrest.Description description) {
            description.appendText("has single failure with message ");
            messageMatcher.describeTo(description);
        }

        @Override
        protected void describeMismatchSafely(EventCollector item, org.hamcrest.Description description) {
            description.appendText("was ");
            hasSingleFailure().describeMismatch(item, description);
            description.appendText(": ");
            boolean first = true;
            for (Failure f : item.fFailures) {
                if (!first) {
                    description.appendText(" ,");
                }
                description.appendText("'");
                description.appendText(f.getMessage());
                description.appendText("'");
                first = false;
            }
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.junit.runner.Description) Failure(org.junit.runner.notification.Failure)

Example 5 with Failure

use of org.junit.runner.notification.Failure in project junit4 by junit-team.

the class EventCollector method failureIs.

static Matcher<EventCollector> failureIs(final Matcher<? super Throwable> exceptionMatcher) {
    return new TypeSafeMatcher<EventCollector>() {

        @Override
        public boolean matchesSafely(EventCollector item) {
            for (Failure f : item.fFailures) {
                return exceptionMatcher.matches(f.getException());
            }
            return false;
        }

        public void describeTo(org.hamcrest.Description description) {
            description.appendText("failure is ");
            exceptionMatcher.describeTo(description);
        }
    };
}
Also used : TypeSafeMatcher(org.hamcrest.TypeSafeMatcher) Description(org.junit.runner.Description) Failure(org.junit.runner.notification.Failure)

Aggregations

Failure (org.junit.runner.notification.Failure)190 Test (org.junit.Test)101 Result (org.junit.runner.Result)88 Description (org.junit.runner.Description)38 IOException (java.io.IOException)32 UnitTest (org.apache.geode.test.junit.categories.UnitTest)27 JUnitCore (org.junit.runner.JUnitCore)21 FileInputStream (java.io.FileInputStream)20 InputStream (java.io.InputStream)20 RunListener (org.junit.runner.notification.RunListener)18 ArrayList (java.util.ArrayList)10 ComparisonFailure (org.junit.ComparisonFailure)10 ByteArrayInputStream (java.io.ByteArrayInputStream)9 JUnit4TestListener (com.intellij.junit4.JUnit4TestListener)7 RunNotifier (org.junit.runner.notification.RunNotifier)7 ResourceImpl (ddf.catalog.resource.impl.ResourceImpl)6 BinaryContentImpl (ddf.catalog.data.impl.BinaryContentImpl)5 Before (org.junit.Before)5 Request (org.junit.runner.Request)5 Runner (org.junit.runner.Runner)5