LUCENE-4102: refactored support for SuppressCodec annotation (assumption thrown at suite level rather than method level.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1348475 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Dawid Weiss 2012-06-09 18:33:48 +00:00
parent 0063ec8cf8
commit 9efa2ad629
4 changed files with 48 additions and 46 deletions

View File

@ -38,10 +38,10 @@ abstract class AbstractBeforeAfterRule implements TestRule {
public Statement apply(final Statement s, final Description d) { public Statement apply(final Statement s, final Description d) {
return new Statement() { return new Statement() {
public void evaluate() throws Throwable { public void evaluate() throws Throwable {
before();
final ArrayList<Throwable> errors = new ArrayList<Throwable>(); final ArrayList<Throwable> errors = new ArrayList<Throwable>();
try { try {
before();
s.evaluate(); s.evaluate();
} catch (Throwable t) { } catch (Throwable t) {
errors.add(t); errors.add(t);

View File

@ -1101,11 +1101,4 @@ public abstract class LuceneTestCase extends Assert {
throw new IOException("Cannot find resource: " + name); throw new IOException("Cannot find resource: " + name);
} }
} }
/**
* @see SuppressCodecs
*/
static boolean shouldAvoidCodec(String codec) {
return classEnvRule.shouldAvoidCodec(codec);
}
} }

View File

@ -37,6 +37,8 @@ import org.apache.lucene.search.RandomSimilarityProvider;
import org.apache.lucene.search.similarities.DefaultSimilarity; import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.apache.lucene.search.similarities.Similarity; import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs; import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
import org.junit.internal.AssumptionViolatedException;
import com.carrotsearch.randomizedtesting.RandomizedContext; import com.carrotsearch.randomizedtesting.RandomizedContext;
import static org.apache.lucene.util.LuceneTestCase.*; import static org.apache.lucene.util.LuceneTestCase.*;
@ -78,7 +80,11 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
if (System.getProperty("solr.directoryFactory") == null) { if (System.getProperty("solr.directoryFactory") == null) {
System.setProperty("solr.directoryFactory", "org.apache.solr.core.MockDirectoryFactory"); 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"));
// enable the Lucene 3.x PreflexRW codec explicitly, to work around bugs in IBM J9 / Harmony ServiceLoader: // enable the Lucene 3.x PreflexRW codec explicitly, to work around bugs in IBM J9 / Harmony ServiceLoader:
try { try {
final java.lang.reflect.Field spiLoaderField = Codec.class.getDeclaredField("loader"); final java.lang.reflect.Field spiLoaderField = Codec.class.getDeclaredField("loader");
@ -106,7 +112,7 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
throw new RuntimeException("Cannot access internals of Codec and NamedSPILoader classes", e); throw new RuntimeException("Cannot access internals of Codec and NamedSPILoader classes", e);
} }
// if verbose: print some debugging stuff about which codecs are loaded // if verbose: print some debugging stuff about which codecs are loaded.
if (VERBOSE) { if (VERBOSE) {
Set<String> codecs = Codec.availableCodecs(); Set<String> codecs = Codec.availableCodecs();
for (String codec : codecs) { for (String codec : codecs) {
@ -129,7 +135,7 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
final String name; final String name;
if (Thread.currentThread().getName().startsWith("TEST-")) { if (Thread.currentThread().getName().startsWith("TEST-")) {
// The name of the main thread is way too // The name of the main thread is way too
// long when looking at IW verbose output...: // long when looking at IW verbose output...
name = "main"; name = "main";
} else { } else {
name = Thread.currentThread().getName(); name = Thread.currentThread().getName();
@ -146,8 +152,6 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
if (targetClass.isAnnotationPresent(SuppressCodecs.class)) { if (targetClass.isAnnotationPresent(SuppressCodecs.class)) {
SuppressCodecs a = targetClass.getAnnotation(SuppressCodecs.class); SuppressCodecs a = targetClass.getAnnotation(SuppressCodecs.class);
avoidCodecs.addAll(Arrays.asList(a.value())); avoidCodecs.addAll(Arrays.asList(a.value()));
System.err.println("NOTE: Suppressing codecs " + Arrays.toString(a.value())
+ " for " + targetClass.getSimpleName() + ".");
} }
PREFLEX_IMPERSONATION_IS_ACTIVE = false; PREFLEX_IMPERSONATION_IS_ACTIVE = false;
@ -206,7 +210,40 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
TimeZone randomTimeZone = randomTimeZone(random()); TimeZone randomTimeZone = randomTimeZone(random());
timeZone = testTimeZone.equals("random") ? randomTimeZone : TimeZone.getTimeZone(testTimeZone); timeZone = testTimeZone.equals("random") ? randomTimeZone : TimeZone.getTimeZone(testTimeZone);
TimeZone.setDefault(timeZone); TimeZone.setDefault(timeZone);
similarity = random().nextBoolean() ? new DefaultSimilarity() : new RandomSimilarityProvider(random()); similarity = random().nextBoolean() ? new DefaultSimilarity() : new RandomSimilarityProvider(random());
// Check codec restrictions once at class level.
try {
checkCodecRestrictions(codec);
} catch (AssumptionViolatedException e) {
System.err.println("NOTE: " + e.getMessage() + " Suppressed codecs: " +
Arrays.toString(avoidCodecs.toArray()));
throw e;
}
}
/**
* Check codec restrictions.
*
* @throws AssumptionViolatedException if the class does not work with a given codec.
*/
private void checkCodecRestrictions(Codec codec) {
assumeFalse("Class not allowed to use codec: " + codec.getName() + ".",
shouldAvoidCodec(codec.getName()));
if (codec instanceof RandomCodec && !avoidCodecs.isEmpty()) {
for (String name : ((RandomCodec)codec).formatNames) {
assumeFalse("Class not allowed to use postings format: " + name + ".",
shouldAvoidCodec(name));
}
}
PostingsFormat pf = codec.postingsFormat();
assumeFalse("Class not allowed to use postings format: " + pf.getName() + ".",
shouldAvoidCodec(pf.getName()));
assumeFalse("Class not allowed to use postings format: " + LuceneTestCase.TEST_POSTINGSFORMAT + ".",
shouldAvoidCodec(LuceneTestCase.TEST_POSTINGSFORMAT));
} }
/** /**
@ -225,17 +262,14 @@ final class TestRuleSetupAndRestoreClassEnv extends AbstractBeforeAfterRule {
Codec.setDefault(savedCodec); Codec.setDefault(savedCodec);
InfoStream.setDefault(savedInfoStream); InfoStream.setDefault(savedInfoStream);
Locale.setDefault(savedLocale); if (savedLocale != null) Locale.setDefault(savedLocale);
TimeZone.setDefault(savedTimeZone); if (savedTimeZone != null) TimeZone.setDefault(savedTimeZone);
System.clearProperty("solr.solr.home");
System.clearProperty("solr.data.dir");
} }
/** /**
* Should a given codec be avoided for the currently executing suite? * Should a given codec be avoided for the currently executing suite?
*/ */
public boolean shouldAvoidCodec(String codec) { private boolean shouldAvoidCodec(String codec) {
return !avoidCodecs.isEmpty() && avoidCodecs.contains(codec); return !avoidCodecs.isEmpty() && avoidCodecs.contains(codec);
} }
} }

View File

@ -1,10 +1,6 @@
package org.apache.lucene.util; package org.apache.lucene.util;
import org.apache.lucene.codecs.Codec;
import org.apache.lucene.codecs.PostingsFormat;
import org.apache.lucene.index.RandomCodec;
import org.apache.lucene.search.BooleanQuery; import org.apache.lucene.search.BooleanQuery;
import org.junit.internal.AssumptionViolatedException;
/** /**
* Licensed to the Apache Software Foundation (ASF) under one or more * Licensed to the Apache Software Foundation (ASF) under one or more
@ -32,27 +28,6 @@ final class TestRuleSetupAndRestoreInstanceEnv extends AbstractBeforeAfterRule {
protected void before() { protected void before() {
savedBoolMaxClauseCount = BooleanQuery.getMaxClauseCount(); savedBoolMaxClauseCount = BooleanQuery.getMaxClauseCount();
Codec codec = Codec.getDefault();
if (LuceneTestCase.shouldAvoidCodec(codec.getName())) {
throw new AssumptionViolatedException(
"Method not allowed to use codec: " + codec.getName() + ".");
}
// TODO: make this more efficient
if (codec instanceof RandomCodec) {
for (String name : ((RandomCodec)codec).formatNames) {
if (LuceneTestCase.shouldAvoidCodec(name)) {
throw new AssumptionViolatedException(
"Method not allowed to use postings format: " + name + ".");
}
}
}
PostingsFormat pf = codec.postingsFormat();
if (LuceneTestCase.shouldAvoidCodec(pf.getName())) {
throw new AssumptionViolatedException(
"Method not allowed to use postings format: " + pf.getName() + ".");
}
} }
protected void after() { protected void after() {