LUCENE-6238: minimize tests.policy (upgrade of randomizedtesting, system properties are read-only).

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1663431 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dawid Weiss 2015-03-02 20:55:14 +00:00
commit c3fd88d10f
24 changed files with 123 additions and 311 deletions

View File

@ -2,7 +2,7 @@
<library name="JUnit">
<CLASSES>
<root url="jar://$PROJECT_DIR$/lucene/test-framework/lib/junit-4.10.jar!/" />
<root url="jar://$PROJECT_DIR$/lucene/test-framework/lib/randomizedtesting-runner-2.1.9.jar!/" />
<root url="jar://$PROJECT_DIR$/lucene/test-framework/lib/randomizedtesting-runner-2.1.12.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />

View File

@ -24,7 +24,10 @@
</description>
<property name="tests.userdir" value="src/test-files"/>
<!-- TODO: why is this limited to one JVM? -->
<property name="tests.jvms.override" value="1" />
<!-- TODO: go fix this in uima, its stupid -->
<property name="tests.policy" location="../../tools/junit4/solr-tests.policy"/>
<path id="uimajars">
<fileset dir="lib"/>

View File

@ -5,11 +5,6 @@ import java.text.SimpleDateFormat;
import java.util.*;
import org.apache.lucene.util.LuceneTestCase;
import org.junit.Rule;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
@ -28,10 +23,6 @@ import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
* limitations under the License.
*/
public class TestDateTools extends LuceneTestCase {
@Rule
public TestRule testRules =
RuleChain.outerRule(new SystemPropertiesRestoreRule());
public void testStringToDate() throws ParseException {
Date d = null;

View File

@ -22,7 +22,6 @@ import java.util.concurrent.CountDownLatch;
import org.apache.lucene.util.junitcompat.WithNestedTests;
import org.junit.Assert;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.Description;
import org.junit.runner.JUnitCore;
@ -37,17 +36,11 @@ import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakScope.Scope;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakZombies;
import com.carrotsearch.randomizedtesting.annotations.ThreadLeakZombies.Consequence;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesInvariantRule;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
/**
* @see TestRuleIgnoreAfterMaxFailures
* @see SystemPropertiesInvariantRule
*/
public class TestMaxFailuresRule extends WithNestedTests {
@Rule
public SystemPropertiesRestoreRule restoreSysProps = new SystemPropertiesRestoreRule();
public TestMaxFailuresRule() {
super(true);
}

View File

@ -1,83 +0,0 @@
package org.apache.lucene.util.junitcompat;
import java.util.*;
import org.apache.lucene.util.TestUtil;
import org.apache.lucene.util.LuceneTestCase.SuppressSysoutChecks;
import org.junit.*;
import org.junit.rules.RuleChain;
import org.junit.rules.TestRule;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import com.carrotsearch.randomizedtesting.RandomizedContext;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
/*
* 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.
*/
public class TestSameRandomnessLocalePassedOrNot extends WithNestedTests {
@ClassRule
public static TestRule solrClassRules =
RuleChain.outerRule(new SystemPropertiesRestoreRule());
@Rule
public TestRule solrTestRules =
RuleChain.outerRule(new SystemPropertiesRestoreRule());
public TestSameRandomnessLocalePassedOrNot() {
super(true);
}
public static class Nested extends WithNestedTests.AbstractNestedTest {
public static String pickString;
public static Locale defaultLocale;
public static TimeZone defaultTimeZone;
public static String seed;
@BeforeClass
public static void setup() {
seed = RandomizedContext.current().getRunnerSeedAsString();
Random rnd = random();
pickString = TestUtil.randomSimpleString(rnd);
defaultLocale = Locale.getDefault();
defaultTimeZone = TimeZone.getDefault();
}
public void testPassed() {
System.out.println("Picked locale: " + defaultLocale);
System.out.println("Picked timezone: " + defaultTimeZone.getID());
}
}
@Test
public void testSetupWithoutLocale() {
Result runClasses = JUnitCore.runClasses(Nested.class);
Assert.assertEquals(0, runClasses.getFailureCount());
String s1 = Nested.pickString;
System.setProperty("tests.seed", Nested.seed);
System.setProperty("tests.timezone", Nested.defaultTimeZone.getID());
System.setProperty("tests.locale", Nested.defaultLocale.toString());
JUnitCore.runClasses(Nested.class);
String s2 = Nested.pickString;
Assert.assertEquals(s1, s2);
}
}

View File

@ -18,7 +18,6 @@ package org.apache.lucene.util.junitcompat;
*/
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.LuceneTestCase.SuppressSysoutChecks;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.JUnitCore;

View File

@ -1,158 +0,0 @@
package org.apache.lucene.util.junitcompat;
/*
* 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.
*/
import java.util.Properties;
import org.junit.*;
import org.junit.rules.TestRule;
import org.junit.runner.JUnitCore;
import org.junit.runner.Result;
import org.junit.runner.notification.Failure;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesInvariantRule;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
/**
* @see SystemPropertiesRestoreRule
* @see SystemPropertiesInvariantRule
*/
public class TestSystemPropertiesInvariantRule extends WithNestedTests {
public static final String PROP_KEY1 = "new-property-1";
public static final String VALUE1 = "new-value-1";
public TestSystemPropertiesInvariantRule() {
super(true);
}
public static class Base extends WithNestedTests.AbstractNestedTest {
public void testEmpty() {}
}
public static class InBeforeClass extends Base {
@BeforeClass
public static void beforeClass() {
System.setProperty(PROP_KEY1, VALUE1);
}
}
public static class InAfterClass extends Base {
@AfterClass
public static void afterClass() {
System.setProperty(PROP_KEY1, VALUE1);
}
}
public static class InTestMethod extends Base {
public void testMethod1() {
if (System.getProperty(PROP_KEY1) != null) {
throw new RuntimeException("Shouldn't be here.");
}
System.setProperty(PROP_KEY1, VALUE1);
}
public void testMethod2() {
testMethod1();
}
}
public static class NonStringProperties extends Base {
public void testMethod1() {
if (System.getProperties().get(PROP_KEY1) != null) {
throw new RuntimeException("Will pass.");
}
Properties properties = System.getProperties();
properties.put(PROP_KEY1, new Object());
Assert.assertTrue(System.getProperties().get(PROP_KEY1) != null);
}
public void testMethod2() {
testMethod1();
}
@AfterClass
public static void cleanup() {
System.getProperties().remove(PROP_KEY1);
}
}
public static class IgnoredProperty {
@Rule
public TestRule invariant = new SystemPropertiesInvariantRule(PROP_KEY1);
@Test
public void testMethod1() {
System.setProperty(PROP_KEY1, VALUE1);
}
}
@Before
@After
public void cleanup() {
System.clearProperty(PROP_KEY1);
}
@Test
public void testRuleInvariantBeforeClass() {
Result runClasses = JUnitCore.runClasses(InBeforeClass.class);
Assert.assertEquals(1, runClasses.getFailureCount());
Assert.assertTrue(runClasses.getFailures().get(0).getMessage()
.contains(PROP_KEY1));
Assert.assertNull(System.getProperty(PROP_KEY1));
}
@Test
public void testRuleInvariantAfterClass() {
Result runClasses = JUnitCore.runClasses(InAfterClass.class);
Assert.assertEquals(1, runClasses.getFailureCount());
Assert.assertTrue(runClasses.getFailures().get(0).getMessage()
.contains(PROP_KEY1));
Assert.assertNull(System.getProperty(PROP_KEY1));
}
@Test
public void testRuleInvariantInTestMethod() {
Result runClasses = JUnitCore.runClasses(InTestMethod.class);
Assert.assertEquals(2, runClasses.getFailureCount());
for (Failure f : runClasses.getFailures()) {
Assert.assertTrue(f.getMessage().contains(PROP_KEY1));
}
Assert.assertNull(System.getProperty(PROP_KEY1));
}
@Test
public void testNonStringProperties() {
Result runClasses = JUnitCore.runClasses(NonStringProperties.class);
Assert.assertEquals(1, runClasses.getFailureCount());
Assert.assertTrue(runClasses.getFailures().get(0).getMessage().contains("Will pass"));
Assert.assertEquals(3, runClasses.getRunCount());
}
@Test
public void testIgnoredProperty() {
System.clearProperty(PROP_KEY1);
try {
Result runClasses = JUnitCore.runClasses(IgnoredProperty.class);
Assert.assertEquals(0, runClasses.getFailureCount());
Assert.assertEquals(VALUE1, System.getProperty(PROP_KEY1));
} finally {
System.clearProperty(PROP_KEY1);
}
}
}

View File

@ -29,6 +29,7 @@ import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.TestRuleIgnoreAfterMaxFailures;
import org.apache.lucene.util.TestRuleIgnoreTestSuites;
import org.apache.lucene.util.TestRuleMarkFailure;
import org.apache.lucene.util.TestRuleRestoreSystemProperties;
import org.apache.lucene.util.LuceneTestCase.SuppressSysoutChecks;
import org.junit.After;
import org.junit.Assert;
@ -44,7 +45,6 @@ import org.junit.runner.notification.Failure;
import com.carrotsearch.randomizedtesting.RandomizedRunner;
import com.carrotsearch.randomizedtesting.RandomizedTest;
import com.carrotsearch.randomizedtesting.SysGlobals;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
import com.carrotsearch.randomizedtesting.rules.TestRuleAdapter;
/**
@ -124,7 +124,7 @@ public abstract class WithNestedTests {
{
final TestRuleMarkFailure marker = new TestRuleMarkFailure();
rules = RuleChain
.outerRule(new SystemPropertiesRestoreRule())
.outerRule(new TestRuleRestoreSystemProperties(TestRuleIgnoreTestSuites.PROPERTY_RUN_NESTED))
.around(new TestRuleAdapter() {
@Override
protected void afterAlways(List<Throwable> errors) throws Throwable {

View File

@ -40,7 +40,9 @@
<!-- you might need to tweak this from china so it works -->
<ibiblio name="working-chinese-mirror" root="http://uk.maven.org/maven2" m2compatible="true" />
<ibiblio name="rr-snapshot" root="https://oss.sonatype.org/content/repositories/comcarrotsearch-1039" m2compatible="true" />
<!--
<filesystem name="local-maven-2" m2compatible="true" local="true">
<artifact
@ -53,6 +55,7 @@
<chain name="default" returnFirst="true" checkmodified="true" changingPattern=".*SNAPSHOT">
<resolver ref="local"/>
<!-- <resolver ref="local-maven-2" /> -->
<resolver ref="rr-snapshot"/>
<resolver ref="main"/>
<resolver ref="maven.restlet.org" />
<resolver ref="sonatype-releases" />

View File

@ -7,7 +7,7 @@
/cglib/cglib-nodep = 2.2
/com.adobe.xmp/xmpcore = 5.1.2
com.carrotsearch.randomizedtesting.version = 2.1.9
com.carrotsearch.randomizedtesting.version = 2.1.12
/com.carrotsearch.randomizedtesting/junit4-ant = ${com.carrotsearch.randomizedtesting.version}
/com.carrotsearch.randomizedtesting/randomizedtesting-runner = ${com.carrotsearch.randomizedtesting.version}

View File

@ -0,0 +1 @@
09847aac214a15628aaa49f45daedcc14b856d1b

View File

@ -1 +0,0 @@
ce61b54b0c33e45811e823441f8322c23de57842

View File

@ -0,0 +1 @@
a31b5efc13b29ad7cbb2ca0f35980f2e152d5845

View File

@ -1 +0,0 @@
2d7283d02387a23434ef553d5e5e4e94f45b007a

View File

@ -21,6 +21,9 @@
Files replication utility
</description>
<!-- TODO: go fix this in jetty, its stupid -->
<property name="tests.policy" location="../tools/junit4/solr-tests.policy"/>
<import file="../module-build.xml"/>
<path id="classpath">

View File

@ -48,10 +48,6 @@ import org.junit.rules.TestRule;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
public class HttpReplicatorTest extends ReplicatorTestCase {
@Rule
public TestRule testRules =
RuleChain.outerRule(new SystemPropertiesRestoreRule());
private Path clientWorkDir;
private Replicator serverReplicator;
private IndexWriter writer;

View File

@ -81,7 +81,6 @@ import com.carrotsearch.randomizedtesting.generators.RandomPicks;
import com.carrotsearch.randomizedtesting.rules.NoClassHooksShadowingRule;
import com.carrotsearch.randomizedtesting.rules.NoInstanceHooksOverridesRule;
import com.carrotsearch.randomizedtesting.rules.StaticFieldsInvariantRule;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesInvariantRule;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.MockAnalyzer;
@ -418,16 +417,6 @@ public abstract class LuceneTestCase extends Assert {
LEAVE_TEMPORARY = defaultValue;
}
/**
* These property keys will be ignored in verification of altered properties.
* @see SystemPropertiesInvariantRule
* @see #ruleChain
* @see #classRules
*/
private static final String [] IGNORED_INVARIANT_PROPERTIES = {
"user.timezone", "java.rmi.server.randomIDs"
};
/** Filesystem-based {@link Directory} implementations. */
private static final List<String> FS_DIRECTORIES = Arrays.asList(
"SimpleFSDirectory",
@ -582,8 +571,20 @@ public abstract class LuceneTestCase extends Assert {
return !(name.equals("setUp") || name.equals("tearDown"));
}
})
.around(new SystemPropertiesInvariantRule(IGNORED_INVARIANT_PROPERTIES))
.around(classNameRule = new TestRuleStoreClassName())
.around(new TestRuleRestoreSystemProperties(
// Enlist all properties to which we have write access (security manager);
// these should be restored to previous state, no matter what the outcome of the test.
// We reset the default locale and timezone; these properties change as a side-effect
"user.language",
"user.timezone",
// TODO: these should, ideally, be moved to Solr's base class.
"solr.directoryFactory",
"solr.solr.home",
"solr.data.dir"
))
.around(classEnvRule = new TestRuleSetupAndRestoreClassEnv());
@ -610,7 +611,6 @@ public abstract class LuceneTestCase extends Assert {
.outerRule(testFailureMarker)
.around(ignoreAfterMaxFailures)
.around(threadAndTestNameRule)
.around(new SystemPropertiesInvariantRule(IGNORED_INVARIANT_PROPERTIES))
.around(new TestRuleSetupAndRestoreInstanceEnv())
.around(parentChainCallRule);

View File

@ -0,0 +1,78 @@
package org.apache.lucene.util;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.carrotsearch.randomizedtesting.rules.SystemPropertiesRestoreRule;
import com.carrotsearch.randomizedtesting.rules.TestRuleAdapter;
/*
* 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.
*/
/**
* Restore a given set of system properties to a snapshot taken at the beginning
* of the rule.
*
* This is semantically similar to {@link SystemPropertiesRestoreRule} but
* the list of properties to restore must be provided explicitly (because the security
* manager prevents us from accessing the whole set of properties).
*
* All properties to be restored must have r/w property permission.
*/
public class TestRuleRestoreSystemProperties extends TestRuleAdapter {
private final String[] propertyNames;
private final Map<String, String> restore = new HashMap<String, String>();
public TestRuleRestoreSystemProperties(String... propertyNames) {
this.propertyNames = propertyNames;
if (propertyNames.length == 0) {
throw new IllegalArgumentException("No properties to restore? Odd.");
}
}
@Override
protected void before() throws Throwable {
super.before();
assert restore.isEmpty();
for (String key : propertyNames) {
restore.put(key, System.getProperty(key));
}
}
@Override
protected void afterAlways(List<Throwable> errors) throws Throwable {
for (String key : propertyNames) {
try {
String value = restore.get(key);
if (value == null) {
System.clearProperty(key);
} else {
System.setProperty(key, value);
}
} catch (SecurityException e) {
// We should have permission to write but if we don't, record the error
errors.add(e);
}
}
restore.clear();
super.afterAlways(errors);
}
}

View File

@ -20,10 +20,8 @@ package org.apache.lucene.util;
import java.io.PrintStream;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Locale;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.TimeZone;
@ -38,7 +36,6 @@ import org.apache.lucene.codecs.cheapbastard.CheapBastardCodec;
import org.apache.lucene.codecs.compressing.CompressingCodec;
import org.apache.lucene.codecs.lucene50.Lucene50Codec;
import org.apache.lucene.codecs.lucene50.Lucene50StoredFieldsFormat;
import org.apache.lucene.codecs.lucene50.Lucene50StoredFieldsFormat.Mode;
import org.apache.lucene.codecs.mockrandom.MockRandomPostingsFormat;
import org.apache.lucene.codecs.simpletext.SimpleTextCodec;
import org.apache.lucene.index.RandomCodec;
@ -68,11 +65,6 @@ import static org.apache.lucene.util.LuceneTestCase.randomTimeZone;
* doesn't fit anywhere else).
*/
final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
/**
* Restore these system property values.
*/
private HashMap<String, String> restoreProperties = new HashMap<>();
private Codec savedCodec;
private Locale savedLocale;
private TimeZone savedTimeZone;
@ -116,15 +108,10 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
// enable this by default, for IDE consistency with ant tests (as it's the default from ant)
// TODO: really should be in solr base classes, but some extend LTC directly.
// we do this in beforeClass, because some tests currently disable it
restoreProperties.put("solr.directoryFactory", System.getProperty("solr.directoryFactory"));
if (System.getProperty("solr.directoryFactory") == null) {
System.setProperty("solr.directoryFactory", "org.apache.solr.core.MockDirectoryFactory");
}
// Restore more Solr properties.
restoreProperties.put("solr.solr.home", System.getProperty("solr.solr.home"));
restoreProperties.put("solr.data.dir", System.getProperty("solr.data.dir"));
// if verbose: print some debugging stuff about which codecs are loaded.
if (VERBOSE) {
Set<String> codecs = Codec.availableCodecs();
@ -224,9 +211,6 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
locale = testLocale.equals("random") ? randomLocale : localeForName(testLocale);
Locale.setDefault(locale);
// TimeZone.getDefault will set user.timezone to the default timezone of the user's locale.
// So store the original property value and restore it at end.
restoreProperties.put("user.timezone", System.getProperty("user.timezone"));
savedTimeZone = TimeZone.getDefault();
TimeZone randomTimeZone = randomTimeZone(random());
timeZone = testTimeZone.equals("random") ? randomTimeZone : TimeZone.getTimeZone(testTimeZone);
@ -291,15 +275,6 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
*/
@Override
protected void after() throws Exception {
for (Map.Entry<String,String> e : restoreProperties.entrySet()) {
if (e.getValue() == null) {
System.clearProperty(e.getKey());
} else {
System.setProperty(e.getKey(), e.getValue());
}
}
restoreProperties.clear();
Codec.setDefault(savedCodec);
InfoStream.setDefault(savedInfoStream);
if (savedLocale != null) Locale.setDefault(savedLocale);

View File

@ -74,8 +74,20 @@ grant {
permission java.lang.RuntimePermission "accessClassInPackage.sun.misc";
// read access to all system properties:
// needed by junit4 BootstrapEvent (it calls System.getProperties, used by ant xml reporting?)
permission java.util.PropertyPermission "*", "read,write";
permission java.util.PropertyPermission "*", "read";
// write access to only these:
// locale randomization
permission java.util.PropertyPermission "user.language", "write";
// timezone randomization
permission java.util.PropertyPermission "user.timezone", "write";
// used by nested tests? (e.g. TestLeaveFilesIfTestFails). TODO: look into this
permission java.util.PropertyPermission "tests.runnested", "write";
// solr properties. TODO: move these out to SolrTestCase
permission java.util.PropertyPermission "solr.data.dir", "write";
permission java.util.PropertyPermission "solr.solr.home", "write";
permission java.util.PropertyPermission "solr.directoryFactory", "write";
// replicator: jetty tests require some network permissions:
// all possibilities of accepting/binding/connecting on localhost with ports >= 1024:

View File

@ -0,0 +1 @@
09847aac214a15628aaa49f45daedcc14b856d1b

View File

@ -1 +0,0 @@
ce61b54b0c33e45811e823441f8322c23de57842

View File

@ -0,0 +1 @@
a31b5efc13b29ad7cbb2ca0f35980f2e152d5845

View File

@ -1 +0,0 @@
2d7283d02387a23434ef553d5e5e4e94f45b007a