diff options
-rw-r--r-- | Android.bp | 28 | ||||
-rw-r--r-- | METADATA | 1 | ||||
-rw-r--r-- | OWNERS | 3 | ||||
-rw-r--r-- | README.version | 3 | ||||
-rw-r--r-- | src/main/java/org/junit/internal/runners/statements/FailOnTimeout.java | 155 | ||||
-rw-r--r-- | src/main/java/org/junit/rules/DisableOnDebug.java | 125 | ||||
-rw-r--r-- | src/main/java/org/junit/rules/ErrorCollector.java | 6 | ||||
-rw-r--r-- | src/main/java/org/junit/rules/Timeout.java | 33 |
8 files changed, 37 insertions, 317 deletions
@@ -14,6 +14,29 @@ // // +package { + default_applicable_licenses: ["external_junit_license"], +} + +// Added automatically by a large-scale-change +// +// large-scale-change included anything that looked like it might be a license +// text as a license_text. e.g. LICENSE, NOTICE, COPYING etc. +// +// Please consider removing redundant or irrelevant files from 'license_text:'. +// See: http://go/android-license-faq +license { + name: "external_junit_license", + visibility: [":__subpackages__"], + license_kinds: [ + "SPDX-license-identifier-EPL", + ], + license_text: [ + "LICENSE-junit.txt", + "NOTICE.txt", + ], +} + java_library_static { name: "junit", host_supported: true, @@ -21,11 +44,6 @@ java_library_static { srcs: ["src/main/java/**/*.java"], sdk_version: "core_current", static_libs: ["hamcrest"], - - // Pin to Java language level 8. It is loaded by an OpenJDK 8 runtime from - // prebuilts/studio/jdk in frameworks/layoutlib/bridge/tests/run_tests.sh. - // See b/141970724. - java_version: "1.8", } // Legacy junit-host jar until references are updated @@ -10,6 +10,7 @@ third_party { value: "https://github.com/junit-team/junit4/archive/r4.12.tar.gz" } version: "r4.12" + license_type: RECIPROCAL last_upgrade_date { year: 2016 month: 12 @@ -1,2 +1,3 @@ paulduffin@google.com -android-libcore-team+review@google.com +jgaillard@google.com +prb@google.com diff --git a/README.version b/README.version index e525ad8..430e5e8 100644 --- a/README.version +++ b/README.version @@ -4,3 +4,6 @@ BugComponent: 40416 Local Changes: Extra generic type information to aid certain javacs. + Remove DisableOnDebug (new in 4.12) as it is not supported on Android + Remove support for stuck threads + Don't convert assumption failures into errors in rules/ErrorCollector 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/rules/DisableOnDebug.java b/src/main/java/org/junit/rules/DisableOnDebug.java deleted file mode 100644 index 3bca103..0000000 --- a/src/main/java/org/junit/rules/DisableOnDebug.java +++ /dev/null @@ -1,125 +0,0 @@ -package org.junit.rules; - -import java.util.List; - -import org.junit.internal.management.ManagementFactory; -import org.junit.internal.management.RuntimeMXBean; -import org.junit.runner.Description; -import org.junit.runners.model.Statement; - -/** - * The {@code DisableOnDebug} Rule allows you to label certain rules to be - * disabled when debugging. - * <p> - * The most illustrative use case is for tests that make use of the - * {@link Timeout} rule, when ran in debug mode the test may terminate on - * timeout abruptly during debugging. Developers may disable the timeout, or - * increase the timeout by making a code change on tests that need debugging and - * remember revert the change afterwards or rules such as {@link Timeout} that - * may be disabled during debugging may be wrapped in a {@code DisableOnDebug}. - * <p> - * The important benefit of this feature is that you can disable such rules - * without any making any modifications to your test class to remove them during - * debugging. - * <p> - * This does nothing to tackle timeouts or time sensitive code under test when - * debugging and may make this less useful in such circumstances. - * <p> - * Example usage: - * - * <pre> - * public static class DisableTimeoutOnDebugSampleTest { - * - * @Rule - * public TestRule timeout = new DisableOnDebug(new Timeout(20)); - * - * @Test - * public void myTest() { - * int i = 0; - * assertEquals(0, i); // suppose you had a break point here to inspect i - * } - * } - * </pre> - * - * @since 4.12 - */ -public class DisableOnDebug implements TestRule { - private final TestRule rule; - private final boolean debugging; - - /** - * Create a {@code DisableOnDebug} instance with the timeout specified in - * milliseconds. - * - * @param rule to disable during debugging - */ - public DisableOnDebug(TestRule rule) { - this(rule, ManagementFactory.getRuntimeMXBean() - .getInputArguments()); - } - - /** - * Visible for testing purposes only. - * - * @param rule the rule to disable during debugging - * @param inputArguments - * arguments provided to the Java runtime - */ - DisableOnDebug(TestRule rule, List<String> inputArguments) { - this.rule = rule; - debugging = isDebugging(inputArguments); - } - - /** - * @see TestRule#apply(Statement, Description) - */ - public Statement apply(Statement base, Description description) { - if (debugging) { - return base; - } else { - return rule.apply(base, description); - } - } - - /** - * Parses arguments passed to the runtime environment for debug flags - * <p> - * Options specified in: - * <ul> - * <li> - * <a href="http://docs.oracle.com/javase/6/docs/technotes/guides/jpda/conninv.html#Invocation" - * >javase-6</a></li> - * <li><a href="http://docs.oracle.com/javase/7/docs/technotes/guides/jpda/conninv.html#Invocation" - * >javase-7</a></li> - * <li><a href="http://docs.oracle.com/javase/8/docs/technotes/guides/jpda/conninv.html#Invocation" - * >javase-8</a></li> - * - * - * @param arguments - * the arguments passed to the runtime environment, usually this - * will be {@link RuntimeMXBean#getInputArguments()} - * @return true if the current JVM was started in debug mode, false - * otherwise. - */ - private static boolean isDebugging(List<String> arguments) { - for (final String argument : arguments) { - if ("-Xdebug".equals(argument) || argument.startsWith("-agentlib:jdwp")) { - return true; - } - } - return false; - } - - /** - * Returns {@code true} if the JVM is in debug mode. This method may be used - * by test classes to take additional action to disable code paths that - * interfere with debugging if required. - * - * @return {@code true} if the current JVM is in debug mode, {@code false} - * otherwise - */ - public boolean isDebugging() { - return debugging; - } - -} diff --git a/src/main/java/org/junit/rules/ErrorCollector.java b/src/main/java/org/junit/rules/ErrorCollector.java index 9711e50..18d94b8 100644 --- a/src/main/java/org/junit/rules/ErrorCollector.java +++ b/src/main/java/org/junit/rules/ErrorCollector.java @@ -49,6 +49,9 @@ public class ErrorCollector extends Verifier { if (error == null) { throw new NullPointerException("Error cannot be null"); } + // BEGIN Android-changed: Don't convert assumption failures to errors. b/181123057 + // Submitted upstream: https://github.com/junit-team/junit4/issues/1703 + /* if (error instanceof AssumptionViolatedException) { AssertionError e = new AssertionError(error.getMessage()); e.initCause(error); @@ -56,6 +59,9 @@ public class ErrorCollector extends Verifier { } else { errors.add(error); } + */ + // END Android-changed: Don't convert assumption failures to errors. b/181123057 + errors.add(error); } /** diff --git a/src/main/java/org/junit/rules/Timeout.java b/src/main/java/org/junit/rules/Timeout.java index 334a923..5cf905a 100644 --- a/src/main/java/org/junit/rules/Timeout.java +++ b/src/main/java/org/junit/rules/Timeout.java @@ -40,7 +40,6 @@ import java.util.concurrent.TimeUnit; public class Timeout implements TestRule { private final long timeout; private final TimeUnit timeUnit; - private final boolean lookForStuckThread; /** * Returns a new builder for building an instance. @@ -80,7 +79,6 @@ public class Timeout implements TestRule { public Timeout(long timeout, TimeUnit timeUnit) { this.timeout = timeout; this.timeUnit = timeUnit; - lookForStuckThread = false; } /** @@ -92,7 +90,6 @@ public class Timeout implements TestRule { protected Timeout(Builder builder) { timeout = builder.getTimeout(); timeUnit = builder.getTimeUnit(); - lookForStuckThread = builder.getLookingForStuckThread(); } /** @@ -125,16 +122,6 @@ public class Timeout implements TestRule { } /** - * Gets whether this {@code Timeout} will look for a stuck thread - * when the test times out. - * - * @since 4.12 - */ - protected final boolean getLookingForStuckThread() { - return lookForStuckThread; - } - - /** * Creates a {@link Statement} that will run the given * {@code statement}, and timeout the operation based * on the values configured in this rule. Subclasses @@ -146,7 +133,6 @@ public class Timeout implements TestRule { Statement statement) throws Exception { return FailOnTimeout.builder() .withTimeout(timeout, timeUnit) - .withLookingForStuckThread(lookForStuckThread) .build(statement); } @@ -205,25 +191,6 @@ public class Timeout implements TestRule { } /** - * Specifies whether to look for a stuck thread. If a timeout occurs and this - * feature is enabled, the rule 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; - } - - protected boolean getLookingForStuckThread() { - return lookForStuckThread; - } - - - /** * Builds a {@link Timeout} instance using the values in this builder., */ public Timeout build() { |