Skip to content

Commit

Permalink
LUCENE-4102: refactored support for SuppressCodec annotation (assumpt…
Browse files Browse the repository at this point in the history
…ion thrown at suite level rather than method level.

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/branches/branch_4x@1348476 13f79535-47bb-0310-9956-ffa450edef68
  • Loading branch information
dweiss committed Jun 9, 2012
1 parent 45569cd commit 87321f5
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ abstract class AbstractBeforeAfterRule implements TestRule {
public Statement apply(final Statement s, final Description d) {
return new Statement() {
public void evaluate() throws Throwable {
before();

final ArrayList<Throwable> errors = new ArrayList<Throwable>();

try {
before();
s.evaluate();
} catch (Throwable t) {
errors.add(t);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1106,11 +1106,4 @@ protected File getDataFile(String name) throws IOException {
throw new IOException("Cannot find resource: " + name);
}
}

/**
* @see SuppressCodecs
*/
static boolean shouldAvoidCodec(String codec) {
return classEnvRule.shouldAvoidCodec(codec);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,8 @@
import org.apache.lucene.search.similarities.DefaultSimilarity;
import org.apache.lucene.search.similarities.Similarity;
import org.apache.lucene.util.LuceneTestCase.SuppressCodecs;
import org.junit.internal.AssumptionViolatedException;

import com.carrotsearch.randomizedtesting.RandomizedContext;

import static org.apache.lucene.util.LuceneTestCase.*;
Expand Down Expand Up @@ -79,7 +81,11 @@ protected void before() throws Exception {
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"));

// enable the Lucene 3.x PreflexRW codec explicitly, to work around bugs in IBM J9 / Harmony ServiceLoader:
try {
final java.lang.reflect.Field spiLoaderField = Codec.class.getDeclaredField("loader");
Expand All @@ -106,7 +112,7 @@ protected void before() throws Exception {
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) {
Set<String> codecs = Codec.availableCodecs();
for (String codec : codecs) {
Expand All @@ -129,7 +135,7 @@ public void message(String component, String message) {
final String name;
if (Thread.currentThread().getName().startsWith("TEST-")) {
// 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";
} else {
name = Thread.currentThread().getName();
Expand All @@ -146,8 +152,6 @@ public void message(String component, String message) {
if (targetClass.isAnnotationPresent(SuppressCodecs.class)) {
SuppressCodecs a = targetClass.getAnnotation(SuppressCodecs.class);
avoidCodecs.addAll(Arrays.asList(a.value()));
System.err.println("NOTE: Suppressing codecs " + Arrays.toString(a.value())
+ " for " + targetClass.getSimpleName() + ".");
}

PREFLEX_IMPERSONATION_IS_ACTIVE = false;
Expand Down Expand Up @@ -204,7 +208,40 @@ public String toString() {
TimeZone randomTimeZone = randomTimeZone(random());
timeZone = testTimeZone.equals("random") ? randomTimeZone : TimeZone.getTimeZone(testTimeZone);
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));
}

/**
Expand All @@ -223,17 +260,14 @@ protected void after() throws Exception {

Codec.setDefault(savedCodec);
InfoStream.setDefault(savedInfoStream);
Locale.setDefault(savedLocale);
TimeZone.setDefault(savedTimeZone);

System.clearProperty("solr.solr.home");
System.clearProperty("solr.data.dir");
if (savedLocale != null) Locale.setDefault(savedLocale);
if (savedTimeZone != null) TimeZone.setDefault(savedTimeZone);
}

/**
* 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);
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
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.junit.internal.AssumptionViolatedException;

/**
* Licensed to the Apache Software Foundation (ASF) under one or more
Expand Down Expand Up @@ -32,27 +28,6 @@ final class TestRuleSetupAndRestoreInstanceEnv extends AbstractBeforeAfterRule {

protected void before() {
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() {
Expand Down

0 comments on commit 87321f5

Please sign in to comment.