diff options
author | David Srbecky <dsrbecky@google.com> | 2021-02-24 15:52:50 +0000 |
---|---|---|
committer | David Srbecky <dsrbecky@google.com> | 2021-02-24 15:52:50 +0000 |
commit | 08a6d4b74555db6d01048fc7065eb1e2bfaf33bc (patch) | |
tree | 81124de095a6b4a53b223d0f70cadde9744ee44a /src/main/java/org/junit/internal/runners | |
parent | 6a658e7a4df0cc8ea6465da46fcf1a823cb0d491 (diff) | |
download | junit-08a6d4b74555db6d01048fc7065eb1e2bfaf33bc.tar.gz |
Revert "Upgrade external/junit to 4.13.2"
Revert submission 1601635
Reason for revert: b/181123058
Reverted Changes:
I8f5cd1266:Remove support for stuck threads
Ifdb59336d:Remove DisableOnDebug (new in 4.12) as it is not s...
I6abae5aed:Extra generic type information to aid certain java...
I5ec909df6:Upgrade external/junit to 4.13.2
Change-Id: Idaddfc2039816a8d7b12c91fdd540b801ab854ff
Diffstat (limited to 'src/main/java/org/junit/internal/runners')
11 files changed, 24 insertions, 257 deletions
diff --git a/src/main/java/org/junit/internal/runners/ErrorReportingRunner.java b/src/main/java/org/junit/internal/runners/ErrorReportingRunner.java index f52abab..1d32beb 100644 --- a/src/main/java/org/junit/internal/runners/ErrorReportingRunner.java +++ b/src/main/java/org/junit/internal/runners/ErrorReportingRunner.java @@ -1,44 +1,33 @@ package org.junit.internal.runners; import java.lang.reflect.InvocationTargetException; +import java.util.Arrays; import java.util.List; import org.junit.runner.Description; import org.junit.runner.Runner; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; -import org.junit.runners.model.InvalidTestClassError; import org.junit.runners.model.InitializationError; -import static java.util.Collections.singletonList; - public class ErrorReportingRunner extends Runner { private final List<Throwable> causes; - private final String classNames; + private final Class<?> testClass; public ErrorReportingRunner(Class<?> testClass, Throwable cause) { - this(cause, testClass); - } - - public ErrorReportingRunner(Throwable cause, Class<?>... testClasses) { - if (testClasses == null || testClasses.length == 0) { - throw new NullPointerException("Test classes cannot be null or empty"); + if (testClass == null) { + throw new NullPointerException("Test class cannot be null"); } - for (Class<?> testClass : testClasses) { - if (testClass == null) { - throw new NullPointerException("Test class cannot be null"); - } - } - classNames = getClassNames(testClasses); + this.testClass = testClass; causes = getCauses(cause); } - + @Override public Description getDescription() { - Description description = Description.createSuiteDescription(classNames); + Description description = Description.createSuiteDescription(testClass); for (Throwable each : causes) { - description.addChild(describeCause()); + description.addChild(describeCause(each)); } return description; } @@ -50,25 +39,11 @@ public class ErrorReportingRunner extends Runner { } } - private String getClassNames(Class<?>... testClasses) { - final StringBuilder builder = new StringBuilder(); - for (Class<?> testClass : testClasses) { - if (builder.length() != 0) { - builder.append(", "); - } - builder.append(testClass.getName()); - } - return builder.toString(); - } - @SuppressWarnings("deprecation") private List<Throwable> getCauses(Throwable cause) { if (cause instanceof InvocationTargetException) { return getCauses(cause.getCause()); } - if (cause instanceof InvalidTestClassError) { - return singletonList(cause); - } if (cause instanceof InitializationError) { return ((InitializationError) cause).getCauses(); } @@ -76,15 +51,16 @@ public class ErrorReportingRunner extends Runner { return ((org.junit.internal.runners.InitializationError) cause) .getCauses(); } - return singletonList(cause); + return Arrays.asList(cause); } - private Description describeCause() { - return Description.createTestDescription(classNames, "initializationError"); + private Description describeCause(Throwable child) { + return Description.createTestDescription(testClass, + "initializationError"); } private void runCause(Throwable child, RunNotifier notifier) { - Description description = describeCause(); + Description description = describeCause(child); notifier.fireTestStarted(description); notifier.fireTestFailure(new Failure(description, child)); notifier.fireTestFinished(description); diff --git a/src/main/java/org/junit/internal/runners/InitializationError.java b/src/main/java/org/junit/internal/runners/InitializationError.java index 484f58d..52065ec 100644 --- a/src/main/java/org/junit/internal/runners/InitializationError.java +++ b/src/main/java/org/junit/internal/runners/InitializationError.java @@ -15,7 +15,7 @@ public class InitializationError extends Exception { /* * We have to use the f prefix until the next major release to ensure * serialization compatibility. - * See https://github.com/junit-team/junit4/issues/976 + * See https://github.com/junit-team/junit/issues/976 */ private final List<Throwable> fErrors; diff --git a/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java b/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java index 0d51541..631fcf2 100644 --- a/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java +++ b/src/main/java/org/junit/internal/runners/JUnit38ClassRunner.java @@ -1,8 +1,5 @@ package org.junit.internal.runners; -import java.lang.annotation.Annotation; -import java.lang.reflect.Method; - import junit.extensions.TestDecorator; import junit.framework.AssertionFailedError; import junit.framework.Test; @@ -15,16 +12,15 @@ import org.junit.runner.Description; import org.junit.runner.Runner; import org.junit.runner.manipulation.Filter; import org.junit.runner.manipulation.Filterable; -import org.junit.runner.manipulation.Orderer; -import org.junit.runner.manipulation.InvalidOrderingException; import org.junit.runner.manipulation.NoTestsRemainException; -import org.junit.runner.manipulation.Orderable; import org.junit.runner.manipulation.Sortable; import org.junit.runner.manipulation.Sorter; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; +import java.lang.annotation.Annotation; +import java.lang.reflect.Method; -public class JUnit38ClassRunner extends Runner implements Filterable, Orderable { +public class JUnit38ClassRunner extends Runner implements Filterable, Sortable { private static final class OldTestClassAdaptingListener implements TestListener { private final RunNotifier notifier; @@ -174,18 +170,6 @@ public class JUnit38ClassRunner extends Runner implements Filterable, Orderable } } - /** - * {@inheritDoc} - * - * @since 4.13 - */ - public void order(Orderer orderer) throws InvalidOrderingException { - if (getTest() instanceof Orderable) { - Orderable adapter = (Orderable) getTest(); - adapter.order(orderer); - } - } - private void setTest(Test test) { this.test = test; } diff --git a/src/main/java/org/junit/internal/runners/MethodValidator.java b/src/main/java/org/junit/internal/runners/MethodValidator.java index e656ee5..ba9c9d1 100644 --- a/src/main/java/org/junit/internal/runners/MethodValidator.java +++ b/src/main/java/org/junit/internal/runners/MethodValidator.java @@ -86,7 +86,7 @@ public class MethodValidator { } if (each.getReturnType() != Void.TYPE) { errors.add(new Exception("Method " + each.getName() - + "should have a return type of void")); + + " should be void")); } if (each.getParameterTypes().length != 0) { errors.add(new Exception("Method " + each.getName() diff --git a/src/main/java/org/junit/internal/runners/TestClass.java b/src/main/java/org/junit/internal/runners/TestClass.java index 6d24f4f..1abaeea 100644 --- a/src/main/java/org/junit/internal/runners/TestClass.java +++ b/src/main/java/org/junit/internal/runners/TestClass.java @@ -85,7 +85,7 @@ public class TestClass { } private List<Class<?>> getSuperClasses(Class<?> testClass) { - List<Class<?>> results = new ArrayList<Class<?>>(); + ArrayList<Class<?>> results = new ArrayList<Class<?>>(); Class<?> current = testClass; while (current != null) { results.add(current); diff --git a/src/main/java/org/junit/internal/runners/model/EachTestNotifier.java b/src/main/java/org/junit/internal/runners/model/EachTestNotifier.java index c5a0764..e094809 100644 --- a/src/main/java/org/junit/internal/runners/model/EachTestNotifier.java +++ b/src/main/java/org/junit/internal/runners/model/EachTestNotifier.java @@ -45,27 +45,4 @@ public class EachTestNotifier { public void fireTestIgnored() { notifier.fireTestIgnored(description); } - - /** - * Calls {@link RunNotifier#fireTestSuiteStarted(Description)}, passing the - * {@link Description} that was passed to the {@code EachTestNotifier} constructor. - * This should be called when a test suite is about to be started. - * @see RunNotifier#fireTestSuiteStarted(Description) - * @since 4.13 - */ - public void fireTestSuiteStarted() { - notifier.fireTestSuiteStarted(description); - } - - /** - * Calls {@link RunNotifier#fireTestSuiteFinished(Description)}, passing the - * {@link Description} that was passed to the {@code EachTestNotifier} constructor. - * This should be called when a test suite has finished, whether the test suite succeeds - * or fails. - * @see RunNotifier#fireTestSuiteFinished(Description) - * @since 4.13 - */ - public void fireTestSuiteFinished() { - notifier.fireTestSuiteFinished(description); - } }
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/rules/ValidationError.java b/src/main/java/org/junit/internal/runners/rules/ValidationError.java index 31bd660..d1af8ae 100644 --- a/src/main/java/org/junit/internal/runners/rules/ValidationError.java +++ b/src/main/java/org/junit/internal/runners/rules/ValidationError.java @@ -5,9 +5,6 @@ import org.junit.runners.model.FrameworkMember; import java.lang.annotation.Annotation; class ValidationError extends Exception { - - private static final long serialVersionUID = 3176511008672645574L; - public ValidationError(FrameworkMember<?> member, Class<? extends Annotation> annotation, String suffix) { super(String.format("The @%s '%s' %s", annotation.getSimpleName(), member.getName(), suffix)); } diff --git a/src/main/java/org/junit/internal/runners/statements/ExpectException.java b/src/main/java/org/junit/internal/runners/statements/ExpectException.java index 9a2a952..d0636bd 100644 --- a/src/main/java/org/junit/internal/runners/statements/ExpectException.java +++ b/src/main/java/org/junit/internal/runners/statements/ExpectException.java @@ -19,9 +19,7 @@ public class ExpectException extends Statement { next.evaluate(); complete = true; } catch (AssumptionViolatedException e) { - if (!expected.isAssignableFrom(e.getClass())) { - throw e; - } + throw e; } catch (Throwable e) { if (!expected.isAssignableFrom(e.getClass())) { String message = "Unexpected exception, expected<" diff --git a/src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java b/src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java index 9362cc1..9fad35b 100644 --- a/src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java +++ b/src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java @@ -1,8 +1,5 @@ package org.junit.internal.runners.statements; -import java.util.Arrays; -import java.util.Collections; -import java.util.List; import java.util.concurrent.Callable; import java.util.concurrent.CountDownLatch; import java.util.concurrent.ExecutionException; @@ -10,9 +7,6 @@ import java.util.concurrent.FutureTask; import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeoutException; -import org.junit.internal.management.ManagementFactory; -import org.junit.internal.management.ThreadMXBean; -import org.junit.runners.model.MultipleFailureException; import org.junit.runners.model.Statement; import org.junit.runners.model.TestTimedOutException; @@ -20,7 +14,6 @@ public class FailOnTimeout extends Statement { private final Statement originalStatement; private final TimeUnit timeUnit; private final long timeout; - private final boolean lookForStuckThread; /** * Returns a new builder for building an instance. @@ -47,7 +40,6 @@ public class FailOnTimeout extends Statement { originalStatement = statement; timeout = builder.timeout; timeUnit = builder.unit; - lookForStuckThread = builder.lookForStuckThread; } /** @@ -56,7 +48,6 @@ public class FailOnTimeout extends Statement { * @since 4.12 */ public static class Builder { - private boolean lookForStuckThread = false; private long timeout = 0; private TimeUnit unit = TimeUnit.SECONDS; @@ -89,20 +80,6 @@ public class FailOnTimeout extends Statement { } /** - * Specifies whether to look for a stuck thread. If a timeout occurs and this - * feature is enabled, the test will look for a thread that appears to be stuck - * and dump its backtrace. This feature is experimental. Behavior may change - * after the 4.12 release in response to feedback. - * - * @param enable {@code true} to enable the feature - * @return {@code this} for method chaining. - */ - public Builder withLookingForStuckThread(boolean enable) { - this.lookForStuckThread = enable; - return this; - } - - /** * Builds a {@link FailOnTimeout} instance using the values in this builder, * wrapping the given statement. * @@ -120,8 +97,7 @@ public class FailOnTimeout extends Statement { public void evaluate() throws Throwable { CallableStatement callable = new CallableStatement(); FutureTask<Throwable> task = new FutureTask<Throwable>(callable); - ThreadGroup threadGroup = threadGroupForNewThread(); - Thread thread = new Thread(threadGroup, task, "Time-limited test"); + Thread thread = new Thread(task, "Time-limited test"); thread.setDaemon(true); thread.start(); callable.awaitStarted(); @@ -131,31 +107,6 @@ public class FailOnTimeout extends Statement { } } - private ThreadGroup threadGroupForNewThread() { - if (!lookForStuckThread) { - // Use the default ThreadGroup (usually the one from the current - // thread). - return null; - } - - // Create the thread in a new ThreadGroup, so if the time-limited thread - // becomes stuck, getStuckThread() can find the thread likely to be the - // culprit. - ThreadGroup threadGroup = new ThreadGroup("FailOnTimeoutGroup"); - if (!threadGroup.isDaemon()) { - // Mark the new ThreadGroup as a daemon thread group, so it will be - // destroyed after the time-limited thread completes. By ensuring the - // ThreadGroup is destroyed, any data associated with the ThreadGroup - // (ex: via java.beans.ThreadGroupContext) is destroyed. - try { - threadGroup.setDaemon(true); - } catch (SecurityException e) { - // Swallow the exception to keep the same behavior as in JUnit 4.12. - } - } - return threadGroup; - } - /** * Wait for the test task, returning the exception thrown by the test if the * test failed, an exception indicating a timeout if the test timed out, or @@ -180,114 +131,12 @@ public class FailOnTimeout extends Statement { private Exception createTimeoutException(Thread thread) { StackTraceElement[] stackTrace = thread.getStackTrace(); - final Thread stuckThread = lookForStuckThread ? getStuckThread(thread) : null; Exception currThreadException = new TestTimedOutException(timeout, timeUnit); if (stackTrace != null) { currThreadException.setStackTrace(stackTrace); thread.interrupt(); } - if (stuckThread != null) { - Exception stuckThreadException = - new Exception("Appears to be stuck in thread " + - stuckThread.getName()); - stuckThreadException.setStackTrace(getStackTrace(stuckThread)); - return new MultipleFailureException( - Arrays.<Throwable>asList(currThreadException, stuckThreadException)); - } else { - return currThreadException; - } - } - - /** - * Retrieves the stack trace for a given thread. - * @param thread The thread whose stack is to be retrieved. - * @return The stack trace; returns a zero-length array if the thread has - * terminated or the stack cannot be retrieved for some other reason. - */ - private StackTraceElement[] getStackTrace(Thread thread) { - try { - return thread.getStackTrace(); - } catch (SecurityException e) { - return new StackTraceElement[0]; - } - } - - /** - * Determines whether the test appears to be stuck in some thread other than - * the "main thread" (the one created to run the test). This feature is experimental. - * Behavior may change after the 4.12 release in response to feedback. - * @param mainThread The main thread created by {@code evaluate()} - * @return The thread which appears to be causing the problem, if different from - * {@code mainThread}, or {@code null} if the main thread appears to be the - * problem or if the thread cannot be determined. The return value is never equal - * to {@code mainThread}. - */ - private Thread getStuckThread(Thread mainThread) { - List<Thread> threadsInGroup = getThreadsInGroup(mainThread.getThreadGroup()); - if (threadsInGroup.isEmpty()) { - return null; - } - - // Now that we have all the threads in the test's thread group: Assume that - // any thread we're "stuck" in is RUNNABLE. Look for all RUNNABLE threads. - // If just one, we return that (unless it equals threadMain). If there's more - // than one, pick the one that's using the most CPU time, if this feature is - // supported. - Thread stuckThread = null; - long maxCpuTime = 0; - for (Thread thread : threadsInGroup) { - if (thread.getState() == Thread.State.RUNNABLE) { - long threadCpuTime = cpuTime(thread); - if (stuckThread == null || threadCpuTime > maxCpuTime) { - stuckThread = thread; - maxCpuTime = threadCpuTime; - } - } - } - return (stuckThread == mainThread) ? null : stuckThread; - } - - /** - * Returns all active threads belonging to a thread group. - * @param group The thread group. - * @return The active threads in the thread group. The result should be a - * complete list of the active threads at some point in time. Returns an empty list - * if this cannot be determined, e.g. because new threads are being created at an - * extremely fast rate. - */ - private List<Thread> getThreadsInGroup(ThreadGroup group) { - final int activeThreadCount = group.activeCount(); // this is just an estimate - int threadArraySize = Math.max(activeThreadCount * 2, 100); - for (int loopCount = 0; loopCount < 5; loopCount++) { - Thread[] threads = new Thread[threadArraySize]; - int enumCount = group.enumerate(threads); - if (enumCount < threadArraySize) { - return Arrays.asList(threads).subList(0, enumCount); - } - // if there are too many threads to fit into the array, enumerate's result - // is >= the array's length; therefore we can't trust that it returned all - // the threads. Try again. - threadArraySize += 100; - } - // threads are proliferating too fast for us. Bail before we get into - // trouble. - return Collections.emptyList(); - } - - /** - * Returns the CPU time used by a thread, if possible. - * @param thr The thread to query. - * @return The CPU time used by {@code thr}, or 0 if it cannot be determined. - */ - private long cpuTime(Thread thr) { - ThreadMXBean mxBean = ManagementFactory.getThreadMXBean(); - if (mxBean.isThreadCpuTimeSupported()) { - try { - return mxBean.getThreadCpuTime(thr.getId()); - } catch (UnsupportedOperationException e) { - } - } - return 0; + return currThreadException; } private class CallableStatement implements Callable<Throwable> { diff --git a/src/main/java/org/junit/internal/runners/statements/RunAfters.java b/src/main/java/org/junit/internal/runners/statements/RunAfters.java index 5e56c33..7512a7d 100644 --- a/src/main/java/org/junit/internal/runners/statements/RunAfters.java +++ b/src/main/java/org/junit/internal/runners/statements/RunAfters.java @@ -30,7 +30,7 @@ public class RunAfters extends Statement { } finally { for (FrameworkMethod each : afters) { try { - invokeMethod(each); + each.invokeExplosively(target); } catch (Throwable e) { errors.add(e); } @@ -38,11 +38,4 @@ public class RunAfters extends Statement { } MultipleFailureException.assertEmpty(errors); } - - /** - * @since 4.13 - */ - protected void invokeMethod(FrameworkMethod method) throws Throwable { - method.invokeExplosively(target); - } }
\ No newline at end of file diff --git a/src/main/java/org/junit/internal/runners/statements/RunBefores.java b/src/main/java/org/junit/internal/runners/statements/RunBefores.java index bd835c7..238fbe7 100644 --- a/src/main/java/org/junit/internal/runners/statements/RunBefores.java +++ b/src/main/java/org/junit/internal/runners/statements/RunBefores.java @@ -21,15 +21,8 @@ public class RunBefores extends Statement { @Override public void evaluate() throws Throwable { for (FrameworkMethod before : befores) { - invokeMethod(before); + before.invokeExplosively(target); } next.evaluate(); } - - /** - * @since 4.13 - */ - protected void invokeMethod(FrameworkMethod method) throws Throwable { - method.invokeExplosively(target); - } }
\ No newline at end of file |