mirror of https://github.com/apache/lucene.git
LUCENE-3847: ignore user.timezone because it is set by java logging system and this is hard to predict.
git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1304019 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
parent
a78109cdf2
commit
c9189ed8c9
|
@ -20,11 +20,18 @@ package org.apache.lucene.util.junitcompat;
|
||||||
import java.util.Properties;
|
import java.util.Properties;
|
||||||
|
|
||||||
import org.apache.lucene.util.LuceneTestCase;
|
import org.apache.lucene.util.LuceneTestCase;
|
||||||
|
import org.apache.lucene.util.SystemPropertiesInvariantRule;
|
||||||
|
import org.apache.lucene.util.SystemPropertiesRestoreRule;
|
||||||
import org.junit.*;
|
import org.junit.*;
|
||||||
|
import org.junit.rules.TestRule;
|
||||||
import org.junit.runner.JUnitCore;
|
import org.junit.runner.JUnitCore;
|
||||||
import org.junit.runner.Result;
|
import org.junit.runner.Result;
|
||||||
import org.junit.runner.notification.Failure;
|
import org.junit.runner.notification.Failure;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @see SystemPropertiesRestoreRule
|
||||||
|
* @see SystemPropertiesInvariantRule
|
||||||
|
*/
|
||||||
public class TestSystemPropertiesInvariantRule extends WithNestedTests {
|
public class TestSystemPropertiesInvariantRule extends WithNestedTests {
|
||||||
public static final String PROP_KEY1 = "new-property-1";
|
public static final String PROP_KEY1 = "new-property-1";
|
||||||
public static final String VALUE1 = "new-value-1";
|
public static final String VALUE1 = "new-value-1";
|
||||||
|
@ -85,6 +92,16 @@ public class TestSystemPropertiesInvariantRule extends WithNestedTests {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static class IgnoredProperty {
|
||||||
|
@Rule
|
||||||
|
public TestRule invariant = new SystemPropertiesInvariantRule(PROP_KEY1);
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testMethod1() {
|
||||||
|
System.setProperty(PROP_KEY1, VALUE1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRuleInvariantBeforeClass() {
|
public void testRuleInvariantBeforeClass() {
|
||||||
Result runClasses = JUnitCore.runClasses(InBeforeClass.class);
|
Result runClasses = JUnitCore.runClasses(InBeforeClass.class);
|
||||||
|
@ -120,4 +137,16 @@ public class TestSystemPropertiesInvariantRule extends WithNestedTests {
|
||||||
Assert.assertTrue(runClasses.getFailures().get(0).getMessage().contains("Will pass"));
|
Assert.assertTrue(runClasses.getFailures().get(0).getMessage().contains("Will pass"));
|
||||||
Assert.assertEquals(3, runClasses.getRunCount());
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -276,6 +276,16 @@ public abstract class LuceneTestCase extends Assert {
|
||||||
*/
|
*/
|
||||||
private static final UncaughtExceptionsRule uncaughtExceptionsRule = new UncaughtExceptionsRule(null);
|
private static final UncaughtExceptionsRule uncaughtExceptionsRule = new UncaughtExceptionsRule(null);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* These property keys will be ignored in verification of altered properties.
|
||||||
|
* @see SystemPropertiesInvariantRule
|
||||||
|
* @see #ruleChain
|
||||||
|
* @see #classRules
|
||||||
|
*/
|
||||||
|
private static final String [] ignoredInvariantProperties = {
|
||||||
|
"user.timezone"
|
||||||
|
};
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This controls how suite-level rules are nested. It is important that _all_ rules declared
|
* This controls how suite-level rules are nested. It is important that _all_ rules declared
|
||||||
* in {@link LuceneTestCase} are executed in proper order if they depend on each
|
* in {@link LuceneTestCase} are executed in proper order if they depend on each
|
||||||
|
@ -283,7 +293,7 @@ public abstract class LuceneTestCase extends Assert {
|
||||||
*/
|
*/
|
||||||
@ClassRule
|
@ClassRule
|
||||||
public static TestRule classRules = RuleChain
|
public static TestRule classRules = RuleChain
|
||||||
.outerRule(new SystemPropertiesInvariantRule())
|
.outerRule(new SystemPropertiesInvariantRule(ignoredInvariantProperties))
|
||||||
.around(classNameRule)
|
.around(classNameRule)
|
||||||
.around(uncaughtExceptionsRule);
|
.around(uncaughtExceptionsRule);
|
||||||
|
|
||||||
|
@ -297,7 +307,7 @@ public abstract class LuceneTestCase extends Assert {
|
||||||
.outerRule(new RememberThreadRule())
|
.outerRule(new RememberThreadRule())
|
||||||
.around(new UncaughtExceptionsRule(this))
|
.around(new UncaughtExceptionsRule(this))
|
||||||
.around(new TestResultInterceptorRule())
|
.around(new TestResultInterceptorRule())
|
||||||
.around(new SystemPropertiesInvariantRule())
|
.around(new SystemPropertiesInvariantRule(ignoredInvariantProperties))
|
||||||
.around(new InternalSetupTeardownRule())
|
.around(new InternalSetupTeardownRule())
|
||||||
.around(new SubclassSetupTeardownRule());
|
.around(new SubclassSetupTeardownRule());
|
||||||
|
|
||||||
|
|
|
@ -18,7 +18,11 @@ package org.apache.lucene.util;
|
||||||
*/
|
*/
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
import java.util.Set;
|
||||||
import java.util.TreeMap;
|
import java.util.TreeMap;
|
||||||
import java.util.TreeSet;
|
import java.util.TreeSet;
|
||||||
|
|
||||||
|
@ -28,6 +32,32 @@ import org.junit.runners.model.MultipleFailureException;
|
||||||
import org.junit.runners.model.Statement;
|
import org.junit.runners.model.Statement;
|
||||||
|
|
||||||
public class SystemPropertiesInvariantRule implements TestRule {
|
public class SystemPropertiesInvariantRule implements TestRule {
|
||||||
|
/**
|
||||||
|
* Ignored property keys.
|
||||||
|
*/
|
||||||
|
private final HashSet<String> ignoredProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Cares about all properties.
|
||||||
|
*/
|
||||||
|
public SystemPropertiesInvariantRule() {
|
||||||
|
this(Collections.<String>emptySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Don't care about the given set of properties.
|
||||||
|
*/
|
||||||
|
public SystemPropertiesInvariantRule(String... ignoredProperties) {
|
||||||
|
this.ignoredProperties = new HashSet<String>(Arrays.asList(ignoredProperties));
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Don't care about the given set of properties.
|
||||||
|
*/
|
||||||
|
public SystemPropertiesInvariantRule(Set<String> ignoredProperties) {
|
||||||
|
this.ignoredProperties = new HashSet<String>(ignoredProperties);
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement apply(final Statement s, Description d) {
|
public Statement apply(final Statement s, Description d) {
|
||||||
return new Statement() {
|
return new Statement() {
|
||||||
|
@ -40,7 +70,12 @@ public class SystemPropertiesInvariantRule implements TestRule {
|
||||||
} catch (Throwable t) {
|
} catch (Throwable t) {
|
||||||
errors.add(t);
|
errors.add(t);
|
||||||
} finally {
|
} finally {
|
||||||
TreeMap<String,String> after = SystemPropertiesRestoreRule.cloneAsMap(System.getProperties());
|
final TreeMap<String,String> after = SystemPropertiesRestoreRule.cloneAsMap(System.getProperties());
|
||||||
|
|
||||||
|
// Remove ignored if they exist.
|
||||||
|
before.keySet().removeAll(ignoredProperties);
|
||||||
|
after.keySet().removeAll(ignoredProperties);
|
||||||
|
|
||||||
if (!after.equals(before)) {
|
if (!after.equals(before)) {
|
||||||
errors.add(
|
errors.add(
|
||||||
new AssertionError("System properties invariant violated.\n" +
|
new AssertionError("System properties invariant violated.\n" +
|
||||||
|
@ -48,7 +83,7 @@ public class SystemPropertiesInvariantRule implements TestRule {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Restore original properties.
|
// Restore original properties.
|
||||||
SystemPropertiesRestoreRule.restore(before, after);
|
SystemPropertiesRestoreRule.restore(before, after, ignoredProperties);
|
||||||
}
|
}
|
||||||
|
|
||||||
MultipleFailureException.assertEmpty(errors);
|
MultipleFailureException.assertEmpty(errors);
|
||||||
|
|
|
@ -27,6 +27,32 @@ import org.junit.runners.model.Statement;
|
||||||
* Restore system properties from before the nested {@link Statement}.
|
* Restore system properties from before the nested {@link Statement}.
|
||||||
*/
|
*/
|
||||||
public class SystemPropertiesRestoreRule implements TestRule {
|
public class SystemPropertiesRestoreRule implements TestRule {
|
||||||
|
/**
|
||||||
|
* Ignored property keys.
|
||||||
|
*/
|
||||||
|
private final HashSet<String> ignoredProperties;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Restores all properties.
|
||||||
|
*/
|
||||||
|
public SystemPropertiesRestoreRule() {
|
||||||
|
this(Collections.<String>emptySet());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ignoredProperties Properties that will be ignored (and will not be restored).
|
||||||
|
*/
|
||||||
|
public SystemPropertiesRestoreRule(Set<String> ignoredProperties) {
|
||||||
|
this.ignoredProperties = new HashSet<String>(this.ignoredProperties);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param ignoredProperties Properties that will be ignored (and will not be restored).
|
||||||
|
*/
|
||||||
|
public SystemPropertiesRestoreRule(String... ignoredProperties) {
|
||||||
|
this.ignoredProperties = new HashSet<String>(Arrays.asList(ignoredProperties));
|
||||||
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Statement apply(final Statement s, Description d) {
|
public Statement apply(final Statement s, Description d) {
|
||||||
return new Statement() {
|
return new Statement() {
|
||||||
|
@ -39,7 +65,7 @@ public class SystemPropertiesRestoreRule implements TestRule {
|
||||||
TreeMap<String,String> after = cloneAsMap(System.getProperties());
|
TreeMap<String,String> after = cloneAsMap(System.getProperties());
|
||||||
if (!after.equals(before)) {
|
if (!after.equals(before)) {
|
||||||
// Restore original properties.
|
// Restore original properties.
|
||||||
restore(before, after);
|
restore(before, after, ignoredProperties);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -69,16 +95,25 @@ public class SystemPropertiesRestoreRule implements TestRule {
|
||||||
|
|
||||||
static void restore(
|
static void restore(
|
||||||
TreeMap<String,String> before,
|
TreeMap<String,String> before,
|
||||||
TreeMap<String,String> after) {
|
TreeMap<String,String> after,
|
||||||
|
Set<String> ignoredKeys) {
|
||||||
|
|
||||||
|
// Clear anything that is present after but wasn't before.
|
||||||
after.keySet().removeAll(before.keySet());
|
after.keySet().removeAll(before.keySet());
|
||||||
for (String key : after.keySet()) {
|
for (String key : after.keySet()) {
|
||||||
|
if (!ignoredKeys.contains(key))
|
||||||
System.clearProperty(key);
|
System.clearProperty(key);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Restore original property values unless they are ignored (then leave).
|
||||||
for (Map.Entry<String,String> e : before.entrySet()) {
|
for (Map.Entry<String,String> e : before.entrySet()) {
|
||||||
if (e.getValue() == null) {
|
String key = e.getValue();
|
||||||
|
if (!ignoredKeys.contains(key)) {
|
||||||
|
if (key == null) {
|
||||||
System.clearProperty(e.getKey()); // Can this happen?
|
System.clearProperty(e.getKey()); // Can this happen?
|
||||||
} else {
|
} else {
|
||||||
System.setProperty(e.getKey(), e.getValue());
|
System.setProperty(e.getKey(), key);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue