mirror of https://github.com/apache/lucene.git
LUCENE-4054: nested suite classes (required for tests) should not run in stand-alone mode.
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1340021 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
870d86440f
commit
6248459183
|
@ -53,9 +53,7 @@ public class TestReproduceMessage extends WithNestedTests {
|
||||||
public Statement apply(final Statement base, Description description) {
|
public Statement apply(final Statement base, Description description) {
|
||||||
return new Statement() {
|
return new Statement() {
|
||||||
public void evaluate() throws Throwable {
|
public void evaluate() throws Throwable {
|
||||||
if (isRunningNested()) {
|
triggerOn(SorePoint.RULE);
|
||||||
triggerOn(SorePoint.RULE);
|
|
||||||
}
|
|
||||||
base.evaluate();
|
base.evaluate();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
@ -69,9 +67,7 @@ public class TestReproduceMessage extends WithNestedTests {
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void before() {
|
public void before() {
|
||||||
if (isRunningNested()) {
|
triggerOn(SorePoint.BEFORE);
|
||||||
triggerOn(SorePoint.BEFORE);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
|
@ -81,9 +77,7 @@ public class TestReproduceMessage extends WithNestedTests {
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public void after() {
|
public void after() {
|
||||||
if (isRunningNested()) {
|
triggerOn(SorePoint.AFTER);
|
||||||
triggerOn(SorePoint.AFTER);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterClass
|
@AfterClass
|
||||||
|
|
|
@ -22,15 +22,18 @@ import java.io.PrintStream;
|
||||||
import java.io.UnsupportedEncodingException;
|
import java.io.UnsupportedEncodingException;
|
||||||
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
import org.apache.lucene.util.TestRuleIgnoreTestSuites;
|
||||||
import org.junit.After;
|
import org.junit.After;
|
||||||
import org.junit.Assert;
|
import org.junit.Assert;
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.ClassRule;
|
import org.junit.ClassRule;
|
||||||
|
import org.junit.Rule;
|
||||||
import org.junit.rules.TestRule;
|
import org.junit.rules.TestRule;
|
||||||
import org.junit.runner.Description;
|
import org.junit.runner.Description;
|
||||||
import org.junit.runners.model.Statement;
|
import org.junit.runners.model.Statement;
|
||||||
|
|
||||||
import com.carrotsearch.randomizedtesting.RandomizedRunner;
|
import com.carrotsearch.randomizedtesting.RandomizedRunner;
|
||||||
|
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* An abstract test class that prepares nested test classes to run.
|
* An abstract test class that prepares nested test classes to run.
|
||||||
|
@ -45,28 +48,11 @@ import com.carrotsearch.randomizedtesting.RandomizedRunner;
|
||||||
* cause havoc (static fields).
|
* cause havoc (static fields).
|
||||||
*/
|
*/
|
||||||
public abstract class WithNestedTests {
|
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() {
|
protected static boolean isRunningNested() {
|
||||||
return runsAsNested;
|
return TestRuleIgnoreTestSuites.isRunningNested();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -81,6 +67,12 @@ public abstract class WithNestedTests {
|
||||||
private ByteArrayOutputStream sysout;
|
private ByteArrayOutputStream sysout;
|
||||||
private ByteArrayOutputStream syserr;
|
private ByteArrayOutputStream syserr;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restore properties after test.
|
||||||
|
*/
|
||||||
|
@Rule
|
||||||
|
public SystemPropertiesRestoreRule restoreProperties = new SystemPropertiesRestoreRule();
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public final void before() {
|
public final void before() {
|
||||||
if (suppressOutputStreams) {
|
if (suppressOutputStreams) {
|
||||||
|
@ -97,13 +89,11 @@ public abstract class WithNestedTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
runsAsNested = true;
|
System.setProperty(TestRuleIgnoreTestSuites.PROPERTY_RUN_NESTED, "true");
|
||||||
}
|
}
|
||||||
|
|
||||||
@After
|
@After
|
||||||
public final void after() {
|
public final void after() {
|
||||||
runsAsNested = false;
|
|
||||||
|
|
||||||
if (suppressOutputStreams) {
|
if (suppressOutputStreams) {
|
||||||
System.out.flush();
|
System.out.flush();
|
||||||
System.err.flush();
|
System.err.flush();
|
||||||
|
|
|
@ -291,7 +291,8 @@ public abstract class LuceneTestCase extends Assert {
|
||||||
*/
|
*/
|
||||||
@ClassRule
|
@ClassRule
|
||||||
public static TestRule classRules = RuleChain
|
public static TestRule classRules = RuleChain
|
||||||
.outerRule(suiteFailureMarker = new TestRuleMarkFailure())
|
.outerRule(new TestRuleIgnoreTestSuites())
|
||||||
|
.around(suiteFailureMarker = new TestRuleMarkFailure())
|
||||||
.around(new TestRuleAssertionsRequired())
|
.around(new TestRuleAssertionsRequired())
|
||||||
.around(new TestRuleNoStaticHooksShadowing())
|
.around(new TestRuleNoStaticHooksShadowing())
|
||||||
.around(new TestRuleNoInstanceHooksOverrides())
|
.around(new TestRuleNoInstanceHooksOverrides())
|
||||||
|
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue