Skip to content

Apache Lucene open-source search software

LUCENE-4054: nested suite classes (required for tests) should not run in stand-alone mode.

Dawid Weiss6248459183c9
diff --git a/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestReproduceMessage.java b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestReproduceMessage.java
index a7e79ae2684..7df711f9670 100644
--- a/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestReproduceMessage.java
+++ b/lucene/core/src/test/org/apache/lucene/util/junitcompat/TestReproduceMessage.java
@@ -53,9 +53,7 @@ public class TestReproduceMessage extends WithNestedTests {
public Statement apply(final Statement base, Description description) {
return new Statement() {
public void evaluate() throws Throwable {
- if (isRunningNested()) {
- triggerOn(SorePoint.RULE);
- }
+ triggerOn(SorePoint.RULE);
base.evaluate();
}
};
@@ -69,9 +67,7 @@ public class TestReproduceMessage extends WithNestedTests {
@Before
public void before() {
- if (isRunningNested()) {
- triggerOn(SorePoint.BEFORE);
- }
+ triggerOn(SorePoint.BEFORE);
}
@Test
@@ -81,9 +77,7 @@ public class TestReproduceMessage extends WithNestedTests {
@After
public void after() {
- if (isRunningNested()) {
- triggerOn(SorePoint.AFTER);
- }
+ triggerOn(SorePoint.AFTER);
}
@AfterClass
diff --git a/lucene/core/src/test/org/apache/lucene/util/junitcompat/WithNestedTests.java b/lucene/core/src/test/org/apache/lucene/util/junitcompat/WithNestedTests.java
index af844ee9f82..adf0f94f735 100644
--- a/lucene/core/src/test/org/apache/lucene/util/junitcompat/WithNestedTests.java
+++ b/lucene/core/src/test/org/apache/lucene/util/junitcompat/WithNestedTests.java
@@ -22,15 +22,18 @@ import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import org.apache.lucene.util.LuceneTestCase;
+import org.apache.lucene.util.TestRuleIgnoreTestSuites;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.ClassRule;
+import org.junit.Rule;
import org.junit.rules.TestRule;
import org.junit.runner.Description;
import org.junit.runners.model.Statement;
import com.carrotsearch.randomizedtesting.RandomizedRunner;
+import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
/**
* An abstract test class that prepares nested test classes to run.
@@ -45,28 +48,11 @@ import com.carrotsearch.randomizedtesting.RandomizedRunner;
* cause havoc (static fields).
*/
public abstract class WithNestedTests {
- /**
- * This can no longer be thread local because {@link RandomizedRunner} runs
- * suites in an isolated threadgroup/thread.
- */
- public static volatile boolean runsAsNested;
-
- public static abstract class AbstractNestedTest extends LuceneTestCase {
- @ClassRule
- public static TestRule ignoreIfRunAsStandalone = new TestRule() {
- public Statement apply(final Statement s, Description arg1) {
- return new Statement() {
- public void evaluate() throws Throwable {
- if (isRunningNested()) {
- s.evaluate();
- }
- }
- };
- }
- };
+ public static abstract class AbstractNestedTest extends LuceneTestCase
+ implements TestRuleIgnoreTestSuites.NestedTestSuite {
protected static boolean isRunningNested() {
- return runsAsNested;
+ return TestRuleIgnoreTestSuites.isRunningNested();
}
}
@@ -81,6 +67,12 @@ public abstract class WithNestedTests {
private ByteArrayOutputStream sysout;
private ByteArrayOutputStream syserr;
+ /**
+ * Restore properties after test.
+ */
+ @Rule
+ public SystemPropertiesRestoreRule restoreProperties = new SystemPropertiesRestoreRule();
+
@Before
public final void before() {
if (suppressOutputStreams) {
@@ -97,13 +89,11 @@ public abstract class WithNestedTests {
}
}
- runsAsNested = true;
+ System.setProperty(TestRuleIgnoreTestSuites.PROPERTY_RUN_NESTED, "true");
}
@After
public final void after() {
- runsAsNested = false;
-
if (suppressOutputStreams) {
System.out.flush();
System.err.flush();
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
index 943bd6820c8..7dbb7386d44 100644
--- a/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/LuceneTestCase.java
@@ -291,7 +291,8 @@ public abstract class LuceneTestCase extends Assert {
*/
@ClassRule
public static TestRule classRules = RuleChain
- .outerRule(suiteFailureMarker = new TestRuleMarkFailure())
+ .outerRule(new TestRuleIgnoreTestSuites())
+ .around(suiteFailureMarker = new TestRuleMarkFailure())
.around(new TestRuleAssertionsRequired())
.around(new TestRuleNoStaticHooksShadowing())
.around(new TestRuleNoInstanceHooksOverrides())
diff --git a/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleIgnoreTestSuites.java b/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleIgnoreTestSuites.java
new file mode 100644
index 00000000000..58fa30cad1f
--- /dev/null
+++ b/lucene/test-framework/src/java/org/apache/lucene/util/TestRuleIgnoreTestSuites.java
@@ -0,0 +1,67 @@
+package org.apache.lucene.util;
+
+import org.junit.Assume;
+import org.junit.rules.TestRule;
+import org.junit.runner.Description;
+import org.junit.runners.model.Statement;
+
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+/**
+ * This rule will cause the suite to be assumption-ignored if
+ * the test class implements a given marker interface and a special
+ * property is not set.
+ *
+ * <p>This is a workaround for problems with certain JUnit containers (IntelliJ)
+ * which automatically discover test suites and attempt to run nested classes
+ * that we use for testing the test framework itself.
+ */
+public final class TestRuleIgnoreTestSuites implements TestRule {
+ /**
+ * Marker interface for nested suites that should be ignored
+ * if executed in stand-alone mode.
+ */
+ public static interface NestedTestSuite {}
+
+ /**
+ * A boolean system property indicating nested suites should be executed
+ * normally.
+ */
+ public final static String PROPERTY_RUN_NESTED = "tests.runnested";
+
+ @Override
+ public Statement apply(final Statement s, final Description d) {
+ return new Statement() {
+ @Override
+ public void evaluate() throws Throwable {
+ if (NestedTestSuite.class.isAssignableFrom(d.getTestClass())) {
+ LuceneTestCase.assumeTrue("Nested suite class ignored (started as stand-along).",
+ isRunningNested());
+ }
+ s.evaluate();
+ }
+ };
+ }
+
+ /**
+ * Check if a suite class is running as a nested test.
+ */
+ public static boolean isRunningNested() {
+ return Boolean.getBoolean(PROPERTY_RUN_NESTED);
+ }
+}