From c25dd1a99d68de6946a2f0eff3e6c65459bd6efd Mon Sep 17 00:00:00 2001 From: Alexey Andreev Date: Wed, 30 Nov 2016 00:25:26 +0300 Subject: [PATCH] Add JUnit test filtering --- .../java/org/teavm/junit/TeaVMTestRunner.java | 30 +++++++++++++++++-- 1 file changed, 27 insertions(+), 3 deletions(-) diff --git a/tools/junit/src/main/java/org/teavm/junit/TeaVMTestRunner.java b/tools/junit/src/main/java/org/teavm/junit/TeaVMTestRunner.java index 1ab3da036..f8ac3c13a 100644 --- a/tools/junit/src/main/java/org/teavm/junit/TeaVMTestRunner.java +++ b/tools/junit/src/main/java/org/teavm/junit/TeaVMTestRunner.java @@ -28,6 +28,7 @@ import java.util.ArrayList; import java.util.Arrays; import java.util.HashMap; import java.util.HashSet; +import java.util.Iterator; import java.util.List; import java.util.Map; import java.util.Properties; @@ -42,6 +43,9 @@ import java.util.stream.Stream; import org.apache.commons.io.IOUtils; 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.NoTestsRemainException; import org.junit.runner.notification.Failure; import org.junit.runner.notification.RunNotifier; import org.junit.runners.model.InitializationError; @@ -64,7 +68,7 @@ import org.teavm.vm.DirectoryBuildTarget; import org.teavm.vm.TeaVM; import org.teavm.vm.TeaVMBuilder; -public class TeaVMTestRunner extends Runner { +public class TeaVMTestRunner extends Runner implements Filterable { private static final String PATH_PARAM = "teavm.junit.target"; private static final String RUNNER = "teavm.junit.js.runner"; private static final String THREAD_COUNT = "teavm.junit.js.threads"; @@ -83,6 +87,7 @@ public class TeaVMTestRunner extends Runner { private static ScheduledThreadPoolExecutor executor = new ScheduledThreadPoolExecutor(1); private static volatile ScheduledFuture cleanupFuture; private CountDownLatch latch; + private List filteredChildren; static { Runtime.getRuntime().addShutdownHook(new Thread(() -> { @@ -129,7 +134,7 @@ public class TeaVMTestRunner extends Runner { public Description getDescription() { if (suiteDescription == null) { suiteDescription = Description.createSuiteDescription(testClass); - for (Method child : getChildren()) { + for (Method child : getFilteredChildren()) { suiteDescription.getChildren().add(describeChild(child)); } } @@ -138,7 +143,7 @@ public class TeaVMTestRunner extends Runner { @Override public void run(RunNotifier notifier) { - List children = getChildren(); + List children = getFilteredChildren(); latch = new CountDownLatch(children.size()); notifier.fireTestStarted(getDescription()); @@ -169,6 +174,13 @@ public class TeaVMTestRunner extends Runner { return children; } + private List getFilteredChildren() { + if (filteredChildren == null) { + filteredChildren = getChildren(); + } + return filteredChildren; + } + private Description describeChild(Method child) { return descriptions.computeIfAbsent(child, method -> Description.createTestDescription(testClass, method.getName())); @@ -461,6 +473,18 @@ public class TeaVMTestRunner extends Runner { new ClasspathClassHolderSource(classLoader))); } + @Override + public void filter(Filter filter) throws NoTestsRemainException { + for (Iterator iterator = getFilteredChildren().iterator(); iterator.hasNext();) { + Method method = iterator.next(); + if (filter.shouldRun(describeChild(method))) { + filter.apply(method); + } else { + iterator.remove(); + } + } + } + static class CompileResult { boolean success = true; String errorMessage;