LUCENE-5869: Added restriction to positive values for maxExpansions in FuzzyQuery

git-svn-id: https://svn.apache.org/repos/asf/lucene/dev/trunk@1615945 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Ryan Ernst 2014-08-05 15:39:20 +00:00
parent a7b25d28da
commit cabef7b282
3 changed files with 43 additions and 2 deletions

View File

@ -226,6 +226,9 @@ Bug Fixes
documents will now throw IllegalStateException if the max count
would be exceeded, instead of silently creating an unusable
index. (Mike McCandless)
* LUCENE-5869: Added restriction to positive values for maxExpansions in FuzzyQuery.
(Ryan Ernst)
Test Framework

View File

@ -89,8 +89,8 @@ public class FuzzyQuery extends MultiTermQuery {
if (prefixLength < 0) {
throw new IllegalArgumentException("prefixLength cannot be negative.");
}
if (maxExpansions < 0) {
throw new IllegalArgumentException("maxExpansions cannot be negative.");
if (maxExpansions <= 0) {
throw new IllegalArgumentException("maxExpansions must be positive.");
}
this.term = term;

View File

@ -31,6 +31,7 @@ import org.apache.lucene.index.RandomIndexWriter;
import org.apache.lucene.index.Term;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.LuceneTestCase;
import org.apache.lucene.util.automaton.LevenshteinAutomata;
/**
* Tests {@link FuzzyQuery}.
@ -372,6 +373,43 @@ public class TestFuzzyQuery extends LuceneTestCase {
reader.close();
index.close();
}
public void testValidation() {
try {
new FuzzyQuery(new Term("field", "foo"), -1, 0, 1, false);
fail("Expected error for illegal max edits value");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("maxEdits"));
}
try {
new FuzzyQuery(new Term("field", "foo"), LevenshteinAutomata.MAXIMUM_SUPPORTED_DISTANCE + 1, 0, 1, false);
fail("Expected error for illegal max edits value");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("maxEdits must be between"));
}
try {
new FuzzyQuery(new Term("field", "foo"), 1, -1, 1, false);
fail("Expected error for illegal prefix length value");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("prefixLength cannot be negative"));
}
try {
new FuzzyQuery(new Term("field", "foo"), 1, 0, -1, false);
fail("Expected error for illegal max expansions value");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("maxExpansions must be positive"));
}
try {
new FuzzyQuery(new Term("field", "foo"), 1, 0, -1, false);
fail("Expected error for illegal max expansions value");
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("maxExpansions must be positive"));
}
}
private void addDoc(String text, RandomIndexWriter writer) throws IOException {
Document doc = new Document();