You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@lucene.apache.org by rm...@apache.org on 2016/02/19 18:13:22 UTC
[2/8] lucene-solr git commit: LUCENE-7037: Switch all exceptions
tests to expectThrows()
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java
----------------------------------------------------------------------
diff --git a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java
index 423188d..565578c 100644
--- a/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java
+++ b/lucene/join/src/test/org/apache/lucene/search/join/TestBlockJoinValidation.java
@@ -81,12 +81,10 @@ public class TestBlockJoinValidation extends LuceneTestCase {
public void testNextDocValidationForToParentBjq() throws Exception {
Query parentQueryWithRandomChild = createChildrenQueryWithOneParent(getRandomChildNumber(0));
ToParentBlockJoinQuery blockJoinQuery = new ToParentBlockJoinQuery(parentQueryWithRandomChild, parentsFilter, ScoreMode.None);
- try {
+ IllegalStateException expected = expectThrows(IllegalStateException.class, () -> {
indexSearcher.search(blockJoinQuery, 1);
- fail("didn't get expected exception");
- } catch (IllegalStateException expected) {
- assertTrue(expected.getMessage() != null && expected.getMessage().contains("child query must only match non-parent docs"));
- }
+ });
+ assertTrue(expected.getMessage() != null && expected.getMessage().contains("child query must only match non-parent docs"));
}
public void testAdvanceValidationForToParentBjq() throws Exception {
@@ -102,12 +100,10 @@ public class TestBlockJoinValidation extends LuceneTestCase {
conjunctionQuery.add(new BooleanClause(childQuery, BooleanClause.Occur.MUST));
conjunctionQuery.add(new BooleanClause(blockJoinQuery, BooleanClause.Occur.MUST));
- try {
+ IllegalStateException expected = expectThrows(IllegalStateException.class, () -> {
indexSearcher.search(conjunctionQuery.build(), 1);
- fail("didn't get expected exception");
- } catch (IllegalStateException expected) {
- assertTrue(expected.getMessage() != null && expected.getMessage().contains("child query must only match non-parent docs"));
- }
+ });
+ assertTrue(expected.getMessage() != null && expected.getMessage().contains("child query must only match non-parent docs"));
}
public void testNextDocValidationForToChildBjq() throws Exception {
@@ -115,12 +111,10 @@ public class TestBlockJoinValidation extends LuceneTestCase {
ToChildBlockJoinQuery blockJoinQuery = new ToChildBlockJoinQuery(parentQueryWithRandomChild, parentsFilter);
- try {
+ IllegalStateException expected = expectThrows(IllegalStateException.class, () -> {
indexSearcher.search(blockJoinQuery, 1);
- fail("didn't get expected exception");
- } catch (IllegalStateException expected) {
- assertTrue(expected.getMessage() != null && expected.getMessage().contains(ToChildBlockJoinQuery.INVALID_QUERY_MESSAGE));
- }
+ });
+ assertTrue(expected.getMessage() != null && expected.getMessage().contains(ToChildBlockJoinQuery.INVALID_QUERY_MESSAGE));
}
public void testAdvanceValidationForToChildBjq() throws Exception {
@@ -138,12 +132,11 @@ public class TestBlockJoinValidation extends LuceneTestCase {
target = TestUtil.nextInt(random(), 0, context.reader().maxDoc() - 2);
} while (parentDocs.get(target + 1));
- try {
- scorer.iterator().advance(target);
- fail();
- } catch (IllegalStateException expected) {
- assertTrue(expected.getMessage() != null && expected.getMessage().contains(ToChildBlockJoinQuery.INVALID_QUERY_MESSAGE));
- }
+ final int illegalTarget = target;
+ IllegalStateException expected = expectThrows(IllegalStateException.class, () -> {
+ scorer.iterator().advance(illegalTarget);
+ });
+ assertTrue(expected.getMessage() != null && expected.getMessage().contains(ToChildBlockJoinQuery.INVALID_QUERY_MESSAGE));
}
private static List<Document> createDocsForSegment(int segmentNumber) {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/memory/src/test/org/apache/lucene/index/memory/TestMemoryIndex.java
----------------------------------------------------------------------
diff --git a/lucene/memory/src/test/org/apache/lucene/index/memory/TestMemoryIndex.java b/lucene/memory/src/test/org/apache/lucene/index/memory/TestMemoryIndex.java
index c8b352a..b150ea3 100644
--- a/lucene/memory/src/test/org/apache/lucene/index/memory/TestMemoryIndex.java
+++ b/lucene/memory/src/test/org/apache/lucene/index/memory/TestMemoryIndex.java
@@ -70,21 +70,15 @@ public class TestMemoryIndex extends LuceneTestCase {
// freeze!
mi.freeze();
- try {
+ RuntimeException expected = expectThrows(RuntimeException.class, () -> {
mi.addField("f3", "and yet more", analyzer);
- fail("Expected an IllegalArgumentException when adding a field after calling freeze()");
- }
- catch (RuntimeException e) {
- assertThat(e.getMessage(), containsString("frozen"));
- }
+ });
+ assertThat(expected.getMessage(), containsString("frozen"));
- try {
+ expected = expectThrows(RuntimeException.class, () -> {
mi.setSimilarity(new BM25Similarity(1, 1));
- fail("Expected an IllegalArgumentException when setting the Similarity after calling freeze()");
- }
- catch (RuntimeException e) {
- assertThat(e.getMessage(), containsString("frozen"));
- }
+ });
+ assertThat(expected.getMessage(), containsString("frozen"));
assertThat(mi.search(new TermQuery(new Term("f1", "some"))), not(is(0.0f)));
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/misc/src/test/org/apache/lucene/index/SortingLeafReaderTest.java
----------------------------------------------------------------------
diff --git a/lucene/misc/src/test/org/apache/lucene/index/SortingLeafReaderTest.java b/lucene/misc/src/test/org/apache/lucene/index/SortingLeafReaderTest.java
index 6052ab5..3e8cb99 100644
--- a/lucene/misc/src/test/org/apache/lucene/index/SortingLeafReaderTest.java
+++ b/lucene/misc/src/test/org/apache/lucene/index/SortingLeafReaderTest.java
@@ -64,12 +64,10 @@ public class SortingLeafReaderTest extends SorterTestBase {
}
public void testBadSort() throws Exception {
- try {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
SortingLeafReader.wrap(sortedReader, Sort.RELEVANCE);
- fail("Didn't get expected exception");
- } catch (IllegalArgumentException e) {
- assertEquals("Cannot sort an index with a Sort that refers to the relevance score", e.getMessage());
- }
+ });
+ assertEquals("Cannot sort an index with a Sort that refers to the relevance score", expected.getMessage());
}
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/misc/src/test/org/apache/lucene/index/TestSortingMergePolicy.java
----------------------------------------------------------------------
diff --git a/lucene/misc/src/test/org/apache/lucene/index/TestSortingMergePolicy.java b/lucene/misc/src/test/org/apache/lucene/index/TestSortingMergePolicy.java
index a0c3fb1..a5486f4 100644
--- a/lucene/misc/src/test/org/apache/lucene/index/TestSortingMergePolicy.java
+++ b/lucene/misc/src/test/org/apache/lucene/index/TestSortingMergePolicy.java
@@ -192,12 +192,10 @@ public class TestSortingMergePolicy extends BaseMergePolicyTestCase {
}
public void testBadSort() throws Exception {
- try {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
new SortingMergePolicy(newMergePolicy(), Sort.RELEVANCE);
- fail("Didn't get expected exception");
- } catch (IllegalArgumentException e) {
- assertEquals("Cannot sort an index with a Sort that refers to the relevance score", e.getMessage());
- }
+ });
+ assertEquals("Cannot sort an index with a Sort that refers to the relevance score", expected.getMessage());
}
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCache.java
----------------------------------------------------------------------
diff --git a/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCache.java b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCache.java
index 215e9a3..fb05875 100644
--- a/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCache.java
+++ b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCache.java
@@ -440,43 +440,37 @@ public class TestFieldCache extends LuceneTestCase {
LeafReader ar = getOnlySegmentReader(ir);
// Binary type: can be retrieved via getTerms()
- try {
+ expectThrows(IllegalStateException.class, () -> {
FieldCache.DEFAULT.getNumerics(ar, "binary", FieldCache.NUMERIC_UTILS_INT_PARSER, false);
- fail();
- } catch (IllegalStateException expected) {}
+ });
BinaryDocValues binary = FieldCache.DEFAULT.getTerms(ar, "binary", true);
final BytesRef term = binary.get(0);
assertEquals("binary value", term.utf8ToString());
- try {
+ expectThrows(IllegalStateException.class, () -> {
FieldCache.DEFAULT.getTermsIndex(ar, "binary");
- fail();
- } catch (IllegalStateException expected) {}
+ });
- try {
+ expectThrows(IllegalStateException.class, () -> {
FieldCache.DEFAULT.getDocTermOrds(ar, "binary", null);
- fail();
- } catch (IllegalStateException expected) {}
+ });
- try {
+ expectThrows(IllegalStateException.class, () -> {
new DocTermOrds(ar, null, "binary");
- fail();
- } catch (IllegalStateException expected) {}
+ });
Bits bits = FieldCache.DEFAULT.getDocsWithField(ar, "binary");
assertTrue(bits.get(0));
// Sorted type: can be retrieved via getTerms(), getTermsIndex(), getDocTermOrds()
- try {
+ expectThrows(IllegalStateException.class, () -> {
FieldCache.DEFAULT.getNumerics(ar, "sorted", FieldCache.NUMERIC_UTILS_INT_PARSER, false);
- fail();
- } catch (IllegalStateException expected) {}
+ });
- try {
+ expectThrows(IllegalStateException.class, () -> {
new DocTermOrds(ar, null, "sorted");
- fail();
- } catch (IllegalStateException expected) {}
+ });
binary = FieldCache.DEFAULT.getTerms(ar, "sorted", true);
BytesRef scratch = binary.get(0);
@@ -501,49 +495,41 @@ public class TestFieldCache extends LuceneTestCase {
NumericDocValues numeric = FieldCache.DEFAULT.getNumerics(ar, "numeric", FieldCache.NUMERIC_UTILS_INT_PARSER, false);
assertEquals(42, numeric.get(0));
- try {
+ expectThrows(IllegalStateException.class, () -> {
FieldCache.DEFAULT.getTerms(ar, "numeric", true);
- fail();
- } catch (IllegalStateException expected) {}
+ });
- try {
+ expectThrows(IllegalStateException.class, () -> {
FieldCache.DEFAULT.getTermsIndex(ar, "numeric");
- fail();
- } catch (IllegalStateException expected) {}
+ });
- try {
+ expectThrows(IllegalStateException.class, () -> {
FieldCache.DEFAULT.getDocTermOrds(ar, "numeric", null);
- fail();
- } catch (IllegalStateException expected) {}
+ });
- try {
+ expectThrows(IllegalStateException.class, () -> {
new DocTermOrds(ar, null, "numeric");
- fail();
- } catch (IllegalStateException expected) {}
+ });
bits = FieldCache.DEFAULT.getDocsWithField(ar, "numeric");
assertTrue(bits.get(0));
// SortedSet type: can be retrieved via getDocTermOrds()
- try {
+ expectThrows(IllegalStateException.class, () -> {
FieldCache.DEFAULT.getNumerics(ar, "sortedset", FieldCache.NUMERIC_UTILS_INT_PARSER, false);
- fail();
- } catch (IllegalStateException expected) {}
+ });
- try {
+ expectThrows(IllegalStateException.class, () -> {
FieldCache.DEFAULT.getTerms(ar, "sortedset", true);
- fail();
- } catch (IllegalStateException expected) {}
+ });
- try {
+ expectThrows(IllegalStateException.class, () -> {
FieldCache.DEFAULT.getTermsIndex(ar, "sortedset");
- fail();
- } catch (IllegalStateException expected) {}
+ });
- try {
+ expectThrows(IllegalStateException.class, () -> {
new DocTermOrds(ar, null, "sortedset");
- fail();
- } catch (IllegalStateException expected) {}
+ });
sortedSet = FieldCache.DEFAULT.getDocTermOrds(ar, "sortedset", null);
sortedSet.setDocument(0);
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheSort.java
----------------------------------------------------------------------
diff --git a/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheSort.java b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheSort.java
index 55d6714..717d364 100644
--- a/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheSort.java
+++ b/lucene/misc/src/test/org/apache/lucene/uninverting/TestFieldCacheSort.java
@@ -1033,10 +1033,9 @@ public class TestFieldCacheSort extends LuceneTestCase {
IndexReader reader = UninvertingReader.wrap(DirectoryReader.open(indexStore),
Collections.singletonMap("string", Type.SORTED));
IndexSearcher searcher = new IndexSearcher(reader);
- try {
+ expectThrows(IllegalStateException.class, () -> {
searcher.search(new MatchAllDocsQuery(), 500, sort);
- fail("didn't get expected exception");
- } catch (IllegalStateException expected) {}
+ });
reader.close();
indexStore.close();
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queries/src/test/org/apache/lucene/queries/CommonTermsQueryTest.java
----------------------------------------------------------------------
diff --git a/lucene/queries/src/test/org/apache/lucene/queries/CommonTermsQueryTest.java b/lucene/queries/src/test/org/apache/lucene/queries/CommonTermsQueryTest.java
index 640089a..a7bfffd 100644
--- a/lucene/queries/src/test/org/apache/lucene/queries/CommonTermsQueryTest.java
+++ b/lucene/queries/src/test/org/apache/lucene/queries/CommonTermsQueryTest.java
@@ -179,12 +179,10 @@ public class CommonTermsQueryTest extends LuceneTestCase {
Random random = random();
CommonTermsQuery query = new CommonTermsQuery(randomOccur(random),
randomOccur(random), random().nextFloat());
- try {
+ // null values are not supported
+ expectThrows(IllegalArgumentException.class, () -> {
query.add(null);
- fail("null values are not supported");
- } catch (IllegalArgumentException ex) {
-
- }
+ });
}
public void testMinShouldMatch() throws IOException {
@@ -324,23 +322,19 @@ public class CommonTermsQueryTest extends LuceneTestCase {
IOUtils.close(r, w, dir, analyzer);
}
+ /** MUST_NOT is not supported */
public void testIllegalOccur() {
Random random = random();
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
new CommonTermsQuery(Occur.MUST_NOT, randomOccur(random), random()
.nextFloat());
- fail("MUST_NOT is not supproted");
- } catch (IllegalArgumentException ex) {
+ });
- }
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
new CommonTermsQuery(randomOccur(random), Occur.MUST_NOT, random()
.nextFloat());
- fail("MUST_NOT is not supproted");
- } catch (IllegalArgumentException ex) {
-
- }
+ });
}
@Test
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/analyzing/TestAnalyzingQueryParser.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/analyzing/TestAnalyzingQueryParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/analyzing/TestAnalyzingQueryParser.java
index 2f72e07..bf5f69f 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/analyzing/TestAnalyzingQueryParser.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/analyzing/TestAnalyzingQueryParser.java
@@ -102,52 +102,31 @@ public class TestAnalyzingQueryParser extends LuceneTestCase {
}
public void testSingleChunkExceptions() {
- boolean ex = false;
String termStr = "the*tre";
Analyzer stopsAnalyzer = new MockAnalyzer
(random(), MockTokenizer.WHITESPACE, true, MockTokenFilter.ENGLISH_STOPSET);
- try {
- String q = parseWithAnalyzingQueryParser(termStr, stopsAnalyzer, true);
- } catch (ParseException e){
- if (e.getMessage().contains("returned nothing")){
- ex = true;
- }
- }
- assertEquals("Should have returned nothing", true, ex);
- ex = false;
+
+ ParseException expected = expectThrows(ParseException.class, () -> {
+ parseWithAnalyzingQueryParser(termStr, stopsAnalyzer, true);
+ });
+ assertTrue(expected.getMessage().contains("returned nothing"));
AnalyzingQueryParser qp = new AnalyzingQueryParser(FIELD, a);
- try{
+ expected = expectThrows(ParseException.class, () -> {
qp.analyzeSingleChunk(FIELD, "", "not a single chunk");
- } catch (ParseException e){
- if (e.getMessage().contains("multiple terms")){
- ex = true;
- }
- }
- assertEquals("Should have produced multiple terms", true, ex);
+ });
+ assertTrue(expected.getMessage().contains("multiple terms"));
}
public void testWildcardAlone() throws ParseException {
//seems like crazy edge case, but can be useful in concordance
- boolean pex = false;
- try{
- Query q = getAnalyzedQuery("*", a, false);
- } catch (ParseException e){
- pex = true;
- }
- assertEquals("Wildcard alone with allowWildcard=false", true, pex);
+ expectThrows(ParseException.class, () -> {
+ getAnalyzedQuery("*", a, false);
+ });
- pex = false;
- try {
- String qString = parseWithAnalyzingQueryParser("*", a, true);
- assertEquals("Every word", "*", qString);
- } catch (ParseException e){
- pex = true;
- }
-
- assertEquals("Wildcard alone with allowWildcard=true", false, pex);
-
+ String qString = parseWithAnalyzingQueryParser("*", a, true);
+ assertEquals("Every word", "*", qString);
}
public void testWildCardEscapes() throws ParseException, IOException {
@@ -162,15 +141,9 @@ public class TestAnalyzingQueryParser extends LuceneTestCase {
}
public void testWildCardQueryNoLeadingAllowed() {
- boolean ex = false;
- try{
- String q = parseWithAnalyzingQueryParser(wildcardInput[0], a, false);
-
- } catch (ParseException e){
- ex = true;
- }
- assertEquals("Testing initial wildcard not allowed",
- true, ex);
+ expectThrows(ParseException.class, () -> {
+ parseWithAnalyzingQueryParser(wildcardInput[0], a, false);
+ });
}
public void testWildCardQuery() throws ParseException {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiFieldQueryParser.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiFieldQueryParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiFieldQueryParser.java
index 4e7cb54..d4d8b93 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiFieldQueryParser.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestMultiFieldQueryParser.java
@@ -177,12 +177,10 @@ public class TestMultiFieldQueryParser extends LuceneTestCase {
assertEquals("(b:one +b:more) (+t:two)", q.toString());
String[] queries5 = {"blah"};
- try {
- q = MultiFieldQueryParser.parse(queries5, fields, new MockAnalyzer(random()));
- fail();
- } catch(IllegalArgumentException e) {
- // expected exception, array length differs
- }
+ // expected exception, array length differs
+ expectThrows(IllegalArgumentException.class, () -> {
+ MultiFieldQueryParser.parse(queries5, fields, new MockAnalyzer(random()));
+ });
// check also with stop words for this static form (qtxts[], fields[]).
TestQueryParser.QPTestAnalyzer stopA = new TestQueryParser.QPTestAnalyzer();
@@ -206,13 +204,11 @@ public class TestMultiFieldQueryParser extends LuceneTestCase {
q = MultiFieldQueryParser.parse("one two", fields, flags, new MockAnalyzer(random()));
assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());
- try {
+ // expected exception, array length differs
+ expectThrows(IllegalArgumentException.class, () -> {
BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
- q = MultiFieldQueryParser.parse("blah", fields, flags2, new MockAnalyzer(random()));
- fail();
- } catch(IllegalArgumentException e) {
- // expected exception, array length differs
- }
+ MultiFieldQueryParser.parse("blah", fields, flags2, new MockAnalyzer(random()));
+ });
}
public void testStaticMethod2Old() throws ParseException {
@@ -226,13 +222,11 @@ public class TestMultiFieldQueryParser extends LuceneTestCase {
q = MultiFieldQueryParser.parse("one two", fields, flags, new MockAnalyzer(random()));
assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());
- try {
+ // expected exception, array length differs
+ expectThrows(IllegalArgumentException.class, () -> {
BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
- q = MultiFieldQueryParser.parse("blah", fields, flags2, new MockAnalyzer(random()));
- fail();
- } catch(IllegalArgumentException e) {
- // expected exception, array length differs
- }
+ MultiFieldQueryParser.parse("blah", fields, flags2, new MockAnalyzer(random()));
+ });
}
public void testStaticMethod3() throws ParseException {
@@ -243,13 +237,11 @@ public class TestMultiFieldQueryParser extends LuceneTestCase {
Query q = MultiFieldQueryParser.parse(queries, fields, flags, new MockAnalyzer(random()));
assertEquals("+f1:one -f2:two f3:three", q.toString());
- try {
+ // expected exception, array length differs
+ expectThrows(IllegalArgumentException.class, () -> {
BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
- q = MultiFieldQueryParser.parse(queries, fields, flags2, new MockAnalyzer(random()));
- fail();
- } catch(IllegalArgumentException e) {
- // expected exception, array length differs
- }
+ MultiFieldQueryParser.parse(queries, fields, flags2, new MockAnalyzer(random()));
+ });
}
public void testStaticMethod3Old() throws ParseException {
@@ -259,13 +251,11 @@ public class TestMultiFieldQueryParser extends LuceneTestCase {
Query q = MultiFieldQueryParser.parse(queries, fields, flags, new MockAnalyzer(random()));
assertEquals("+b:one -t:two", q.toString());
- try {
+ // expected exception, array length differs
+ expectThrows(IllegalArgumentException.class, () -> {
BooleanClause.Occur[] flags2 = {BooleanClause.Occur.MUST};
- q = MultiFieldQueryParser.parse(queries, fields, flags2, new MockAnalyzer(random()));
- fail();
- } catch(IllegalArgumentException e) {
- // expected exception, array length differs
- }
+ MultiFieldQueryParser.parse(queries, fields, flags2, new MockAnalyzer(random()));
+ });
}
public void testAnalyzerReturningNull() throws ParseException {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestQueryParser.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestQueryParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestQueryParser.java
index 5d3b7ec..2457d3c 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestQueryParser.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/classic/TestQueryParser.java
@@ -259,24 +259,20 @@ public class TestQueryParser extends QueryParserTestBase {
}
+ // Wildcard queries should not be allowed
public void testCustomQueryParserWildcard() {
- try {
+ expectThrows(ParseException.class, () -> {
new QPTestParser("contents", new MockAnalyzer(random(),
MockTokenizer.WHITESPACE, false)).parse("a?t");
- fail("Wildcard queries should not be allowed");
- } catch (ParseException expected) {
- // expected exception
- }
+ });
}
+ // Fuzzy queries should not be allowed
public void testCustomQueryParserFuzzy() throws Exception {
- try {
+ expectThrows(ParseException.class, () -> {
new QPTestParser("contents", new MockAnalyzer(random(),
MockTokenizer.WHITESPACE, false)).parse("xunit~");
- fail("Fuzzy queries should not be allowed");
- } catch (ParseException expected) {
- // expected exception
- }
+ });
}
/** query parser that doesn't expand synonyms when users use double quotes */
@@ -481,11 +477,8 @@ public class TestQueryParser extends QueryParserTestBase {
public void testWildcardMaxDeterminizedStates() throws Exception {
QueryParser qp = new QueryParser("field", new MockAnalyzer(random()));
qp.setMaxDeterminizedStates(10);
- try {
+ expectThrows(TooComplexToDeterminizeException.class, () -> {
qp.parse("a*aaaaaaa");
- fail("should have hit exception");
- } catch (TooComplexToDeterminizeException tctde) {
- // expected
- }
+ });
}
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
index 45d13de..66078b0 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/complexPhrase/TestComplexPhraseQuery.java
@@ -86,14 +86,9 @@ public class TestComplexPhraseQuery extends LuceneTestCase {
private void checkBadQuery(String qString) {
ComplexPhraseQueryParser qp = new ComplexPhraseQueryParser(defaultFieldName, analyzer);
qp.setInOrder(inOrder);
- Throwable expected = null;
- try {
+ expectThrows(Throwable.class, () -> {
qp.parse(qString);
- } catch (Throwable e) {
- expected = e;
- }
- assertNotNull("Expected parse error in " + qString, expected);
-
+ });
}
private void checkMatches(String qString, String expectedVals)
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/ext/TestExtendableQueryParser.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/ext/TestExtendableQueryParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/ext/TestExtendableQueryParser.java
index ac46930..785dd1c 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/ext/TestExtendableQueryParser.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/ext/TestExtendableQueryParser.java
@@ -57,11 +57,9 @@ public class TestExtendableQueryParser extends TestQueryParser {
Extensions ext = newExtensions(':');
ext.add("testExt", new ExtensionStub());
ExtendableQueryParser parser = (ExtendableQueryParser) getParser(null, ext);
- try {
+ expectThrows(ParseException.class, () -> {
parser.parse("aField:testExt:\"foo \\& bar\"");
- fail("extension field delimiter is not escaped");
- } catch (ParseException e) {
- }
+ });
}
public void testExtFieldUnqoted() throws Exception {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/ext/TestExtensions.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/ext/TestExtensions.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/ext/TestExtensions.java
index fde11bb..7899d50 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/ext/TestExtensions.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/ext/TestExtensions.java
@@ -68,11 +68,9 @@ public class TestExtensions extends LuceneTestCase {
public void testEscapeExtension() {
assertEquals("abc\\:\\?\\{\\}\\[\\]\\\\\\(\\)\\+\\-\\!\\~", ext
.escapeExtensionField("abc:?{}[]\\()+-!~"));
- try {
+ // should throw NPE - escape string is null
+ expectThrows(NullPointerException.class, () -> {
ext.escapeExtensionField(null);
- fail("should throw NPE - escape string is null");
- } catch (NullPointerException e) {
- //
- }
+ });
}
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/precedence/TestPrecedenceQueryParser.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/precedence/TestPrecedenceQueryParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/precedence/TestPrecedenceQueryParser.java
index 4dbe3f9..b58cede 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/precedence/TestPrecedenceQueryParser.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/precedence/TestPrecedenceQueryParser.java
@@ -314,12 +314,9 @@ public class TestPrecedenceQueryParser extends LuceneTestCase {
fq = (FuzzyQuery) getQuery("term~", null);
assertEquals(2, fq.getMaxEdits());
assertEquals(FuzzyQuery.defaultPrefixLength, fq.getPrefixLength());
- try {
+ expectThrows(ParseException.class, () -> {
getQuery("term~1.1", null); // value > 1, throws exception
- fail();
- } catch (ParseException pe) {
- // expected exception
- }
+ });
assertTrue(getQuery("term*germ", null) instanceof WildcardQuery);
/*
@@ -580,21 +577,17 @@ public class TestPrecedenceQueryParser extends LuceneTestCase {
}
public void testException() throws Exception {
- try {
+ expectThrows(QueryNodeParseException.class, () -> {
assertQueryEquals("\"some phrase", null, "abc");
- fail("ParseException expected, not thrown");
- } catch (QueryNodeParseException expected) {
- }
+ });
}
+ // ParseException expected due to too many boolean clauses
public void testBooleanQuery() throws Exception {
BooleanQuery.setMaxClauseCount(2);
- try {
+ expectThrows(QueryNodeException.class, () -> {
getParser(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)).parse("one two three", "field");
- fail("ParseException expected due to too many boolean clauses");
- } catch (QueryNodeException expected) {
- // too many boolean clauses, so ParseException is expected
- }
+ });
}
// LUCENE-792
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/spans/TestSpanQueryParser.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/spans/TestSpanQueryParser.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/spans/TestSpanQueryParser.java
index 192cd3b..0bcfedf 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/spans/TestSpanQueryParser.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/spans/TestSpanQueryParser.java
@@ -168,62 +168,33 @@ public class TestSpanQueryParser extends LuceneTestCase {
public void testQueryValidator() throws QueryNodeException {
- try {
- getSpanQuery("term*");
- fail("QueryNodeException was expected, wildcard queries should not be supported");
+ expectThrows(QueryNodeException.class, () -> {
+ getSpanQuery("term*"); // wildcard queries should not be supported
+ });
- } catch (QueryNodeException ex) {
- // expected exception
- }
+ expectThrows(QueryNodeException.class, () -> {
+ getSpanQuery("[a TO z]"); // range queries should not be supported
+ });
- try {
- getSpanQuery("[a TO z]");
- fail("QueryNodeException was expected, range queries should not be supported");
+ expectThrows(QueryNodeException.class, () -> {
+ getSpanQuery("a~0.5"); // boost queries should not be supported
+ });
- } catch (QueryNodeException ex) {
- // expected exception
- }
+ expectThrows(QueryNodeException.class, () -> {
+ getSpanQuery("a^0.5"); // fuzzy queries should not be supported
+ });
- try {
- getSpanQuery("a~0.5");
- fail("QueryNodeException was expected, boost queries should not be supported");
+ expectThrows(QueryNodeException.class, () -> {
+ getSpanQuery("\"a b\""); // quoted queries should not be supported
+ });
- } catch (QueryNodeException ex) {
- // expected exception
- }
-
- try {
- getSpanQuery("a^0.5");
- fail("QueryNodeException was expected, fuzzy queries should not be supported");
-
- } catch (QueryNodeException ex) {
- // expected exception
- }
-
- try {
- getSpanQuery("\"a b\"");
- fail("QueryNodeException was expected, quoted queries should not be supported");
-
- } catch (QueryNodeException ex) {
- // expected exception
- }
-
- try {
- getSpanQuery("(a b)");
- fail("QueryNodeException was expected, parenthesized queries should not be supported");
-
- } catch (QueryNodeException ex) {
- // expected exception
- }
-
- try {
- getSpanQuery("a AND b");
- fail("QueryNodeException was expected, and queries should not be supported");
-
- } catch (QueryNodeException ex) {
- // expected exception
- }
+ expectThrows(QueryNodeException.class, () -> {
+ getSpanQuery("(a b)"); // parenthesized queries should not be supported
+ });
+ expectThrows(QueryNodeException.class, () -> {
+ getSpanQuery("a AND b"); // AND queries should not be supported
+ });
}
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestMultiFieldQPHelper.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestMultiFieldQPHelper.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestMultiFieldQPHelper.java
index 0988e56..28e815d 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestMultiFieldQPHelper.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestMultiFieldQPHelper.java
@@ -195,12 +195,10 @@ public class TestMultiFieldQPHelper extends LuceneTestCase {
assertEquals("(b:one +b:more) t:two", q.toString());
String[] queries5 = { "blah" };
- try {
- q = QueryParserUtil.parse(queries5, fields, new MockAnalyzer(random()));
- fail();
- } catch (IllegalArgumentException e) {
- // expected exception, array length differs
- }
+ // expected exception, array length differs
+ expectThrows(IllegalArgumentException.class, () -> {
+ QueryParserUtil.parse(queries5, fields, new MockAnalyzer(random()));
+ });
// check also with stop words for this static form (qtxts[], fields[]).
TestQPHelper.QPTestAnalyzer stopA = new TestQPHelper.QPTestAnalyzer();
@@ -226,13 +224,11 @@ public class TestMultiFieldQPHelper extends LuceneTestCase {
q = QueryParserUtil.parse("one two", fields, flags, new MockAnalyzer(random()));
assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());
- try {
+ // expected exception, array length differs
+ expectThrows(IllegalArgumentException.class, () -> {
BooleanClause.Occur[] flags2 = { BooleanClause.Occur.MUST };
- q = QueryParserUtil.parse("blah", fields, flags2, new MockAnalyzer(random()));
- fail();
- } catch (IllegalArgumentException e) {
- // expected exception, array length differs
- }
+ QueryParserUtil.parse("blah", fields, flags2, new MockAnalyzer(random()));
+ });
}
public void testStaticMethod2Old() throws QueryNodeException {
@@ -251,13 +247,11 @@ public class TestMultiFieldQPHelper extends LuceneTestCase {
q = QueryParserUtil.parse("one two", fields, flags, new MockAnalyzer(random()));
assertEquals("+(b:one b:two) -(t:one t:two)", q.toString());
- try {
+ // expected exception, array length differs
+ expectThrows(IllegalArgumentException.class, () -> {
BooleanClause.Occur[] flags2 = { BooleanClause.Occur.MUST };
- q = QueryParserUtil.parse("blah", fields, flags2, new MockAnalyzer(random()));
- fail();
- } catch (IllegalArgumentException e) {
- // expected exception, array length differs
- }
+ QueryParserUtil.parse("blah", fields, flags2, new MockAnalyzer(random()));
+ });
}
public void testStaticMethod3() throws QueryNodeException {
@@ -269,14 +263,12 @@ public class TestMultiFieldQPHelper extends LuceneTestCase {
new MockAnalyzer(random()));
assertEquals("+f1:one -f2:two f3:three", q.toString());
- try {
+ // expected exception, array length differs
+ expectThrows(IllegalArgumentException.class, () -> {
BooleanClause.Occur[] flags2 = { BooleanClause.Occur.MUST };
- q = QueryParserUtil
+ QueryParserUtil
.parse(queries, fields, flags2, new MockAnalyzer(random()));
- fail();
- } catch (IllegalArgumentException e) {
- // expected exception, array length differs
- }
+ });
}
public void testStaticMethod3Old() throws QueryNodeException {
@@ -288,14 +280,12 @@ public class TestMultiFieldQPHelper extends LuceneTestCase {
new MockAnalyzer(random()));
assertEquals("+b:one -t:two", q.toString());
- try {
+ // expected exception, array length differs
+ expectThrows(IllegalArgumentException.class, () -> {
BooleanClause.Occur[] flags2 = { BooleanClause.Occur.MUST };
- q = QueryParserUtil
+ QueryParserUtil
.parse(queries, fields, flags2, new MockAnalyzer(random()));
- fail();
- } catch (IllegalArgumentException e) {
- // expected exception, array length differs
- }
+ });
}
public void testAnalyzerReturningNull() throws QueryNodeException {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java
index 1c53726..e7d0c89 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestQPHelper.java
@@ -619,18 +619,14 @@ public class TestQPHelper extends LuceneTestCase {
assertWildcardQueryEquals("[A TO C]", true, "[a TO c]");
assertWildcardQueryEquals("[A TO C]", false, "[A TO C]");
// Test suffix queries: first disallow
- try {
+ expectThrows(QueryNodeException.class, () -> {
assertWildcardQueryEquals("*Term", true, "*term");
- fail();
- } catch (QueryNodeException pe) {
- // expected exception
- }
- try {
+ });
+
+ expectThrows(QueryNodeException.class, () -> {
assertWildcardQueryEquals("?Term", true, "?term");
- fail();
- } catch (QueryNodeException pe) {
- // expected exception
- }
+ });
+
// Test suffix queries: then allow
assertWildcardQueryEquals("*Term", true, "*term", true);
assertWildcardQueryEquals("?Term", true, "?term", true);
@@ -1001,12 +997,9 @@ public class TestQPHelper extends LuceneTestCase {
}
public void assertQueryNodeException(String queryString) throws Exception {
- try {
+ expectThrows(QueryNodeException.class, () -> {
getQuery(queryString, null);
- } catch (QueryNodeException expected) {
- return;
- }
- fail("ParseException expected, not thrown");
+ });
}
public void testException() throws Exception {
@@ -1019,35 +1012,29 @@ public class TestQPHelper extends LuceneTestCase {
assertQueryNodeException("secret AND illegal) AND access:confidential");
}
+ // Wildcard queries should not be allowed
public void testCustomQueryParserWildcard() {
- try {
+ expectThrows(QueryNodeException.class, () -> {
new QPTestParser(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)).parse("a?t", "contents");
- fail("Wildcard queries should not be allowed");
- } catch (QueryNodeException expected) {
- // expected exception
- }
+ });
}
+ // Fuzzy queries should not be allowed"
public void testCustomQueryParserFuzzy() throws Exception {
- try {
+ expectThrows(QueryNodeException.class, () -> {
new QPTestParser(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false)).parse("xunit~", "contents");
- fail("Fuzzy queries should not be allowed");
- } catch (QueryNodeException expected) {
- // expected exception
- }
+ });
}
+ // too many boolean clauses, so ParseException is expected
public void testBooleanQuery() throws Exception {
BooleanQuery.setMaxClauseCount(2);
- try {
+ expectThrows(QueryNodeException.class, () -> {
StandardQueryParser qp = new StandardQueryParser();
qp.setAnalyzer(new MockAnalyzer(random(), MockTokenizer.WHITESPACE, false));
qp.parse("one two three", "field");
- fail("ParseException expected due to too many boolean clauses");
- } catch (QueryNodeException expected) {
- // too many boolean clauses, so ParseException is expected
- }
+ });
}
/**
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestStandardQP.java
----------------------------------------------------------------------
diff --git a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestStandardQP.java b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestStandardQP.java
index 1bbdb53..cc2ac12 100644
--- a/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestStandardQP.java
+++ b/lucene/queryparser/src/test/org/apache/lucene/queryparser/flexible/standard/TestStandardQP.java
@@ -155,22 +155,18 @@ public class TestStandardQP extends QueryParserTestBase {
@Override
public void testCollatedRange() throws Exception {
- try {
+ expectThrows(UnsupportedOperationException.class, () -> {
setAnalyzeRangeTerms(getParser(null), true);
super.testCollatedRange();
- } catch (UnsupportedOperationException e) {
- // expected
- }
+ });
}
@Override
public void testAutoGeneratePhraseQueriesOn() throws Exception {
- try {
+ expectThrows(UnsupportedOperationException.class, () -> {
setAutoGeneratePhraseQueries(getParser(null), true);
super.testAutoGeneratePhraseQueriesOn();
- } catch (UnsupportedOperationException e) {
- // expected
- }
+ });
}
@Override
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java
----------------------------------------------------------------------
diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java
index 8804004..022298b 100644
--- a/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java
+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexAndTaxonomyRevisionTest.java
@@ -56,15 +56,13 @@ public class IndexAndTaxonomyRevisionTest extends ReplicatorTestCase {
Directory taxoDir = newDirectory();
SnapshotDirectoryTaxonomyWriter taxoWriter = new SnapshotDirectoryTaxonomyWriter(taxoDir);
- try {
- assertNotNull(new IndexAndTaxonomyRevision(indexWriter, taxoWriter));
- fail("should have failed when there are no commits to snapshot");
- } catch (IllegalStateException e) {
- // expected
- } finally {
- indexWriter.close();
- IOUtils.close(taxoWriter, taxoDir, indexDir);
- }
+ // should fail when there are no commits to snapshot
+ expectThrows(IllegalStateException.class, () -> {
+ new IndexAndTaxonomyRevision(indexWriter, taxoWriter);
+ });
+
+ indexWriter.close();
+ IOUtils.close(taxoWriter, taxoDir, indexDir);
}
@Test
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexRevisionTest.java
----------------------------------------------------------------------
diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/IndexRevisionTest.java b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexRevisionTest.java
index d5f3588..77f323d 100644
--- a/lucene/replicator/src/test/org/apache/lucene/replicator/IndexRevisionTest.java
+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/IndexRevisionTest.java
@@ -41,15 +41,13 @@ public class IndexRevisionTest extends ReplicatorTestCase {
IndexWriterConfig conf = new IndexWriterConfig(null);
conf.setIndexDeletionPolicy(new KeepOnlyLastCommitDeletionPolicy());
IndexWriter writer = new IndexWriter(dir, conf);
- try {
- assertNotNull(new IndexRevision(writer));
- fail("should have failed when IndexDeletionPolicy is not Snapshot");
- } catch (IllegalArgumentException e) {
- // expected
- } finally {
- writer.close();
- IOUtils.close(dir);
- }
+ // should fail when IndexDeletionPolicy is not Snapshot
+ expectThrows(IllegalArgumentException.class, () -> {
+ new IndexRevision(writer);
+ });
+
+ writer.close();
+ IOUtils.close(dir);
}
@Test
@@ -58,15 +56,13 @@ public class IndexRevisionTest extends ReplicatorTestCase {
IndexWriterConfig conf = new IndexWriterConfig(null);
conf.setIndexDeletionPolicy(new SnapshotDeletionPolicy(conf.getIndexDeletionPolicy()));
IndexWriter writer = new IndexWriter(dir, conf);
- try {
- assertNotNull(new IndexRevision(writer));
- fail("should have failed when there are no commits to snapshot");
- } catch (IllegalStateException e) {
- // expected
- } finally {
- writer.close();
- IOUtils.close(dir);
- }
+ // should fail when there are no commits to snapshot"
+ expectThrows(IllegalStateException.class, () -> {
+ new IndexRevision(writer);
+ });
+
+ writer.close();
+ IOUtils.close(dir);
}
@Test
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java
----------------------------------------------------------------------
diff --git a/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java b/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java
index 2d32878..af23da1 100644
--- a/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java
+++ b/lucene/replicator/src/test/org/apache/lucene/replicator/LocalReplicatorTest.java
@@ -86,34 +86,25 @@ public class LocalReplicatorTest extends ReplicatorTestCase {
assertEquals(1, res.sourceFiles.size());
Entry<String,List<RevisionFile>> entry = res.sourceFiles.entrySet().iterator().next();
replicator.close();
- try {
+ expectThrows(AlreadyClosedException.class, () -> {
replicator.obtainFile(res.id, entry.getKey(), entry.getValue().get(0).fileName);
- fail("should have failed on AlreadyClosedException");
- } catch (AlreadyClosedException e) {
- // expected
- }
+ });
}
@Test
public void testPublishAlreadyClosed() throws IOException {
replicator.close();
- try {
+ expectThrows(AlreadyClosedException.class, () -> {
replicator.publish(createRevision(2));
- fail("should have failed on AlreadyClosedException");
- } catch (AlreadyClosedException e) {
- // expected
- }
+ });
}
@Test
public void testUpdateAlreadyClosed() throws IOException {
replicator.close();
- try {
+ expectThrows(AlreadyClosedException.class, () -> {
replicator.checkForUpdate(null);
- fail("should have failed on AlreadyClosedException");
- } catch (AlreadyClosedException e) {
- // expected
- }
+ });
}
@Test
@@ -140,12 +131,10 @@ public class LocalReplicatorTest extends ReplicatorTestCase {
replicator.publish(createRevision(1));
Revision old = new IndexRevision(sourceWriter);
replicator.publish(createRevision(2));
- try {
+ // should fail to publish an older revision
+ expectThrows(IllegalArgumentException.class, () -> {
replicator.publish(old);
- fail("should have failed to publish an older revision");
- } catch (IllegalArgumentException e) {
- // expected
- }
+ });
assertEquals(1, DirectoryReader.listCommits(sourceDir).size());
}
@@ -167,12 +156,10 @@ public class LocalReplicatorTest extends ReplicatorTestCase {
SessionToken session = replicator.checkForUpdate(null);
replicator.setExpirationThreshold(5); // expire quickly
Thread.sleep(50); // sufficient for expiration
- try {
+ // should fail to obtain a file for an expired session
+ expectThrows(SessionExpiredException.class, () -> {
replicator.obtainFile(session.id, session.sourceFiles.keySet().iterator().next(), session.sourceFiles.values().iterator().next().get(0).fileName);
- fail("should have failed to obtain a file for an expired session");
- } catch (SessionExpiredException e) {
- // expected
- }
+ });
}
@Test
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/sandbox/src/test/org/apache/lucene/codecs/idversion/TestIDVersionPostingsFormat.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/codecs/idversion/TestIDVersionPostingsFormat.java b/lucene/sandbox/src/test/org/apache/lucene/codecs/idversion/TestIDVersionPostingsFormat.java
index efbd6ad..d173762 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/codecs/idversion/TestIDVersionPostingsFormat.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/codecs/idversion/TestIDVersionPostingsFormat.java
@@ -363,15 +363,14 @@ public class TestIDVersionPostingsFormat extends LuceneTestCase {
Document doc = new Document();
doc.add(makeIDField("id", 17));
w.addDocument(doc);
- doc = new Document();
- doc.add(makeIDField("id", 17));
- try {
- w.addDocument(doc);
+
+ Document duplicate = new Document();
+ duplicate.add(makeIDField("id", 17));
+ expectThrows(IllegalArgumentException.class, () -> {
+ w.addDocument(duplicate);
w.commit();
- fail("didn't hit expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
+ });
+
w.close();
dir.close();
}
@@ -464,13 +463,10 @@ public class TestIDVersionPostingsFormat extends LuceneTestCase {
RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
Document doc = new Document();
doc.add(newTextField("id", "id", Field.Store.NO));
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
w.addDocument(doc);
w.commit();
- fail("didn't hit expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
+ });
w.close();
dir.close();
@@ -483,13 +479,11 @@ public class TestIDVersionPostingsFormat extends LuceneTestCase {
RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
Document doc = new Document();
doc.add(newStringField("id", "id", Field.Store.NO));
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
w.addDocument(doc);
w.commit();
- fail("didn't hit expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
+ });
+
w.close();
dir.close();
@@ -502,13 +496,10 @@ public class TestIDVersionPostingsFormat extends LuceneTestCase {
RandomIndexWriter w = new RandomIndexWriter(random(), dir, iwc);
Document doc = new Document();
doc.add(new StringAndPayloadField("id", "id", new BytesRef("foo")));
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
w.addDocument(doc);
w.commit();
- fail("didn't hit expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
+ });
w.close();
dir.close();
@@ -550,14 +541,12 @@ public class TestIDVersionPostingsFormat extends LuceneTestCase {
ts.setValue("foo", payload);
Field field = new Field("id", ts, ft);
doc.add(new Field("id", ts, ft));
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
w.addDocument(doc);
w.commit();
fail("didn't hit expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- // iae.printStackTrace(System.out);
- }
+ });
+
w.close();
dir.close();
}
@@ -570,13 +559,11 @@ public class TestIDVersionPostingsFormat extends LuceneTestCase {
Document doc = new Document();
doc.add(makeIDField("id", 17));
doc.add(makeIDField("id", 17));
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
w.addDocument(doc);
w.commit();
- fail("didn't hit expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
+ });
+
w.close();
dir.close();
}
@@ -589,19 +576,13 @@ public class TestIDVersionPostingsFormat extends LuceneTestCase {
Document doc = new Document();
// -1
doc.add(new StringAndPayloadField("id", "id", new BytesRef(new byte[] {(byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff})));
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
w.addDocument(doc);
w.commit();
- fail("didn't hit expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
- try {
+ });
+ expectThrows(AlreadyClosedException.class, () -> {
w.addDocument(doc);
- fail("should have hit exc");
- } catch (AlreadyClosedException ace) {
- // expected
- }
+ });
dir.close();
}
@@ -613,19 +594,14 @@ public class TestIDVersionPostingsFormat extends LuceneTestCase {
Document doc = new Document();
// Long.MAX_VALUE:
doc.add(new StringAndPayloadField("id", "id", new BytesRef(new byte[] {(byte)0x7f, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff, (byte)0xff})));
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
w.addDocument(doc);
w.commit();
- fail("didn't hit expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
- try {
+ });
+ expectThrows(AlreadyClosedException.class, () -> {
w.addDocument(doc);
- fail("should have hit exc");
- } catch (AlreadyClosedException ace) {
- // expected
- }
+ });
+
dir.close();
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery.java b/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery.java
index 4b4aa38..922213f 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/sandbox/queries/TestSlowFuzzyQuery.java
@@ -291,18 +291,13 @@ public class TestSlowFuzzyQuery extends LuceneTestCase {
hits = searcher.search(query, 1000).scoreDocs;
assertEquals(0, hits.length);
- try {
- query = new SlowFuzzyQuery(new Term("field", "student"), 1.1f);
- fail("Expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expecting exception
- }
- try {
- query = new SlowFuzzyQuery(new Term("field", "student"), -0.1f);
- fail("Expected IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expecting exception
- }
+ expectThrows(IllegalArgumentException.class, () -> {
+ new SlowFuzzyQuery(new Term("field", "student"), 1.1f);
+ });
+
+ expectThrows(IllegalArgumentException.class, () -> {
+ new SlowFuzzyQuery(new Term("field", "student"), -0.1f);
+ });
reader.close();
directory.close();
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java
----------------------------------------------------------------------
diff --git a/lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java b/lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java
index b5e784e..392941d 100644
--- a/lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java
+++ b/lucene/sandbox/src/test/org/apache/lucene/search/TestTermAutomatonQuery.java
@@ -357,12 +357,9 @@ public class TestTermAutomatonQuery extends LuceneTestCase {
q.setAccept(s2, true);
q.addAnyTransition(s0, s1);
q.addTransition(s1, s2, "b");
- try {
+ expectThrows(IllegalStateException.class, () -> {
q.finish();
- fail("did not hit expected exception");
- } catch (IllegalStateException ise) {
- // expected
- }
+ });
}
public void testInvalidTrailWithAny() throws Exception {
@@ -373,12 +370,9 @@ public class TestTermAutomatonQuery extends LuceneTestCase {
q.setAccept(s2, true);
q.addTransition(s0, s1, "b");
q.addAnyTransition(s1, s2);
- try {
+ expectThrows(IllegalStateException.class, () -> {
q.finish();
- fail("did not hit expected exception");
- } catch (IllegalStateException ise) {
- // expected
- }
+ });
}
public void testAnyFromTokenStream() throws Exception {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/TestGeoPointQuery.java
----------------------------------------------------------------------
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/TestGeoPointQuery.java b/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/TestGeoPointQuery.java
index bdc4d86..13ce235 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/TestGeoPointQuery.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/geopoint/search/TestGeoPointQuery.java
@@ -300,13 +300,11 @@ public class TestGeoPointQuery extends BaseGeoPointTestCase {
assertEquals("smallTest failed", 2, td.totalHits);
}
+ // GeoBoundingBox should not accept invalid lat/lon
public void testInvalidBBox() throws Exception {
- try {
+ expectThrows(Exception.class, () -> {
bboxQuery(179.0, -92.0, 181.0, -91.0, 20);
- } catch(Exception e) {
- return;
- }
- throw new Exception("GeoBoundingBox should not accept invalid lat/lon");
+ });
}
public void testGeoDistanceQuery() throws Exception {
@@ -327,11 +325,10 @@ public class TestGeoPointQuery extends BaseGeoPointTestCase {
}
public void testTooBigRadius() throws Exception {
- try {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
geoDistanceQuery(0.0, 85.0, 4000000, 20);
- } catch (IllegalArgumentException e) {
- e.getMessage().contains("exceeds maxRadius");
- }
+ });
+ assertTrue(expected.getMessage().contains("exceeds maxRadius"));
}
/**
@@ -347,13 +344,11 @@ public class TestGeoPointQuery extends BaseGeoPointTestCase {
assertEquals("GeoDistanceQuery failed", 3, td.totalHits);
}
+ // GeoDistanceQuery should not accept invalid lat/lon as origin
public void testInvalidGeoDistanceQuery() throws Exception {
- try {
+ expectThrows(Exception.class, () -> {
geoDistanceQuery(181.0, 92.0, 120000, 20);
- } catch (Exception e) {
- return;
- }
- throw new Exception("GeoDistanceQuery should not accept invalid lat/lon as origin");
+ });
}
public void testMaxDistanceRangeQuery() throws Exception {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/spatial/src/test/org/apache/lucene/spatial/query/SpatialArgsParserTest.java
----------------------------------------------------------------------
diff --git a/lucene/spatial/src/test/org/apache/lucene/spatial/query/SpatialArgsParserTest.java b/lucene/spatial/src/test/org/apache/lucene/spatial/query/SpatialArgsParserTest.java
index b7bd50d..93b95f3 100644
--- a/lucene/spatial/src/test/org/apache/lucene/spatial/query/SpatialArgsParserTest.java
+++ b/lucene/spatial/src/test/org/apache/lucene/spatial/query/SpatialArgsParserTest.java
@@ -47,19 +47,15 @@ public class SpatialArgsParserTest extends LuceneTestCase {
out = parser.parse(arg, ctx);
assertEquals(SpatialOperation.IsDisjointTo, out.getOperation());
- try {
+ // spatial operations need args
+ expectThrows(Exception.class, () -> {
parser.parse(SpatialOperation.IsDisjointTo + "[ ]", ctx);
- fail("spatial operations need args");
- }
- catch (Exception ex) {//expected
- }
+ });
- try {
+ // unknown operation
+ expectThrows(Exception.class, () -> {
parser.parse("XXXX(Envelope(-10, 10, 20, -20))", ctx);
- fail("unknown operation!");
- }
- catch (Exception ex) {//expected
- }
+ });
assertAlias(SpatialOperation.IsWithin, "CoveredBy");
assertAlias(SpatialOperation.IsWithin, "COVEREDBY");
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/suggest/src/test/org/apache/lucene/search/spell/TestSpellChecker.java
----------------------------------------------------------------------
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/spell/TestSpellChecker.java b/lucene/suggest/src/test/org/apache/lucene/search/spell/TestSpellChecker.java
index 02484ad..8428d84 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/spell/TestSpellChecker.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/spell/TestSpellChecker.java
@@ -310,12 +310,8 @@ public class TestSpellChecker extends LuceneTestCase {
assertEquals(2, similar.length);
assertEquals(similar[0], "ninety");
assertEquals(similar[1], "one");
- try {
- similar = spellChecker.suggestSimilar("tousand", 10, r, null,
- SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX);
- } catch (NullPointerException e) {
- assertTrue("threw an NPE, and it shouldn't have", false);
- }
+ // should not throw exception
+ spellChecker.suggestSimilar("tousand", 10, r, null, SuggestMode.SUGGEST_WHEN_NOT_IN_INDEX);
}
private void checkJaroWinklerSuggestions() throws IOException {
@@ -361,39 +357,27 @@ public class TestSpellChecker extends LuceneTestCase {
assertLastSearcherOpen(4);
spellChecker.close();
assertSearchersClosed();
- try {
+
+ expectThrows(AlreadyClosedException.class, () -> {
spellChecker.close();
- fail("spellchecker was already closed");
- } catch (AlreadyClosedException e) {
- // expected
- }
- try {
+ });
+
+ expectThrows(AlreadyClosedException.class, () -> {
checkCommonSuggestions(r);
- fail("spellchecker was already closed");
- } catch (AlreadyClosedException e) {
- // expected
- }
+ });
- try {
+ expectThrows(AlreadyClosedException.class, () -> {
spellChecker.clearIndex();
- fail("spellchecker was already closed");
- } catch (AlreadyClosedException e) {
- // expected
- }
+ });
- try {
+ expectThrows(AlreadyClosedException.class, () -> {
spellChecker.indexDictionary(new LuceneDictionary(r, field), newIndexWriterConfig(null), false);
- fail("spellchecker was already closed");
- } catch (AlreadyClosedException e) {
- // expected
- }
+ });
- try {
+ expectThrows(AlreadyClosedException.class, () -> {
spellChecker.setSpellIndex(spellindex);
- fail("spellchecker was already closed");
- } catch (AlreadyClosedException e) {
- // expected
- }
+ });
+
assertEquals(4, searchers.size());
assertSearchersClosed();
r.close();
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggesterTest.java
----------------------------------------------------------------------
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggesterTest.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggesterTest.java
index 092eb95..44ecf88 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggesterTest.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingInfixSuggesterTest.java
@@ -41,6 +41,7 @@ import org.apache.lucene.search.BooleanQuery;
import org.apache.lucene.search.suggest.Input;
import org.apache.lucene.search.suggest.InputArrayIterator;
import org.apache.lucene.search.suggest.Lookup.LookupResult;
+import org.apache.lucene.store.AlreadyClosedException;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BytesRef;
import org.apache.lucene.util.IOUtils;
@@ -877,12 +878,11 @@ public class AnalyzingInfixSuggesterTest extends LuceneTestCase {
Path tempDir = createTempDir("AIS_NRT_PERSIST_TEST");
AnalyzingInfixSuggester suggester = new AnalyzingInfixSuggester(newFSDirectory(tempDir), a, a, 3, false);
Thread[] multiAddThreads = new Thread[10];
- try {
+ // Cannot call refresh on an suggester when no docs are added to the index
+ expectThrows(IllegalStateException.class, () -> {
suggester.refresh();
- fail("Cannot call refresh on an suggester when no docs are added to the index");
- } catch(IllegalStateException e) {
- //Expected
- }
+ });
+
for(int i=0; i<10; i++) {
multiAddThreads[i] = new Thread(new IndexDocument(suggester, keys[i]));
}
@@ -903,12 +903,12 @@ public class AnalyzingInfixSuggesterTest extends LuceneTestCase {
suggester.commit();
suggester.close();
- suggester = new AnalyzingInfixSuggester(newFSDirectory(tempDir), a, a, 3, false);
- results = suggester.lookup(TestUtil.stringToCharSequence("python", random()), 10, true, false);
+ AnalyzingInfixSuggester suggester2 = new AnalyzingInfixSuggester(newFSDirectory(tempDir), a, a, 3, false);
+ results = suggester2.lookup(TestUtil.stringToCharSequence("python", random()), 10, true, false);
assertEquals(1, results.size());
assertEquals("python", results.get(0).key);
- suggester.close();
+ suggester2.close();
a.close();
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggesterTest.java
----------------------------------------------------------------------
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggesterTest.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggesterTest.java
index 8890a82..7baa73c 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggesterTest.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/AnalyzingSuggesterTest.java
@@ -1224,18 +1224,13 @@ public class AnalyzingSuggesterTest extends LuceneTestCase {
suggester.build(new InputArrayIterator(new Input[] {
new Input("а где Люси?", 7),
}));
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
suggester.lookup("а\u001E", false, 3);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
- try {
+ });
+ expectThrows(IllegalArgumentException.class, () -> {
suggester.lookup("а\u001F", false, 3);
- fail("should throw IllegalArgumentException");
- } catch (IllegalArgumentException e) {
- // expected
- }
+ });
+
IOUtils.close(a, tempDir);
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestFreeTextSuggester.java
----------------------------------------------------------------------
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestFreeTextSuggester.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestFreeTextSuggester.java
index 45a6855..4fd7773 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestFreeTextSuggester.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestFreeTextSuggester.java
@@ -105,12 +105,10 @@ public class TestFreeTextSuggester extends LuceneTestCase {
);
Analyzer analyzer = new MockAnalyzer(random());
FreeTextSuggester sug = new FreeTextSuggester(analyzer);
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
sug.build(new InputArrayIterator(keys));
- fail("did not hit expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
+ });
+
analyzer.close();
}
@@ -124,12 +122,10 @@ public class TestFreeTextSuggester extends LuceneTestCase {
FreeTextSuggester sug = new FreeTextSuggester(analyzer);
sug.build(new InputArrayIterator(keys));
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
sug.lookup("foo\u001eb", 10);
- fail("did not hit expected exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
+ });
+
analyzer.close();
}
@@ -234,12 +230,10 @@ public class TestFreeTextSuggester extends LuceneTestCase {
Analyzer a = new MockAnalyzer(random());
FreeTextSuggester sug = new FreeTextSuggester(a, a, 2, (byte) 0x20);
sug.build(new InputArrayIterator(keys));
- try {
+ expectThrows(IllegalArgumentException.class, () -> {
sug.lookup("", 10);
- fail("did not hit exception");
- } catch (IllegalArgumentException iae) {
- // expected
- }
+ });
+
a.close();
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestSuggestStopFilterFactory.java
----------------------------------------------------------------------
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestSuggestStopFilterFactory.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestSuggestStopFilterFactory.java
index b901c21..58b1892 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestSuggestStopFilterFactory.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/analyzing/TestSuggestStopFilterFactory.java
@@ -69,37 +69,33 @@ public class TestSuggestStopFilterFactory extends BaseTokenStreamTestCase {
/** Test that bogus arguments result in exception */
public void testBogusArguments() throws Exception {
- try {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
createFactory("bogusArg", "bogusValue");
- fail();
- } catch (IllegalArgumentException expected) {
- assertTrue(expected.getMessage().contains("Unknown parameters"));
- }
+ });
+ assertTrue(expected.getMessage().contains("Unknown parameters"));
}
/** Test that bogus arguments result in exception */
public void testBogusFormats() throws Exception {
- try {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
createFactory("words", "stop-snowball.txt",
"format", "bogus");
- fail();
- } catch (IllegalArgumentException expected) {
- String msg = expected.getMessage();
- assertTrue(msg, msg.contains("Unknown"));
- assertTrue(msg, msg.contains("format"));
- assertTrue(msg, msg.contains("bogus"));
- }
- try {
+ });
+
+ String msg = expected.getMessage();
+ assertTrue(msg, msg.contains("Unknown"));
+ assertTrue(msg, msg.contains("format"));
+ assertTrue(msg, msg.contains("bogus"));
+
+ expected = expectThrows(IllegalArgumentException.class, () -> {
createFactory(
// implicit default words file
"format", "bogus");
- fail();
- } catch (IllegalArgumentException expected) {
- String msg = expected.getMessage();
- assertTrue(msg, msg.contains("can not be specified"));
- assertTrue(msg, msg.contains("format"));
- assertTrue(msg, msg.contains("bogus"));
- }
+ });
+ msg = expected.getMessage();
+ assertTrue(msg, msg.contains("can not be specified"));
+ assertTrue(msg, msg.contains("format"));
+ assertTrue(msg, msg.contains("bogus"));
}
private SuggestStopFilterFactory createFactory(String ... params) throws IOException {
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java
----------------------------------------------------------------------
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java
index 1be3f37..35661ee 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextQuery.java
@@ -56,13 +56,11 @@ public class TestContextQuery extends LuceneTestCase {
@Test
public void testIllegalInnerQuery() throws Exception {
- try {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
new ContextQuery(new ContextQuery(
new PrefixCompletionQuery(new MockAnalyzer(random()), new Term("suggest_field", "sugg"))));
- fail("should error out trying to nest a Context query within another Context query");
- } catch (IllegalArgumentException expected) {
- assertTrue(expected.getMessage().contains(ContextQuery.class.getSimpleName()));
- }
+ });
+ assertTrue(expected.getMessage().contains(ContextQuery.class.getSimpleName()));
}
@Test
@@ -125,11 +123,11 @@ public class TestContextQuery extends LuceneTestCase {
DirectoryReader reader = iw.getReader();
SuggestIndexSearcher suggestIndexSearcher = new SuggestIndexSearcher(reader);
ContextQuery query = new ContextQuery(new PrefixCompletionQuery(analyzer, new Term("suggest_field", "ab")));
- try {
+ IllegalStateException expected = expectThrows(IllegalStateException.class, () -> {
suggestIndexSearcher.suggest(query, 4);
- } catch (IllegalStateException expected) {
- assertTrue(expected.getMessage().contains("SuggestField"));
- }
+ });
+ assertTrue(expected.getMessage().contains("SuggestField"));
+
reader.close();
iw.close();
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextSuggestField.java
----------------------------------------------------------------------
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextSuggestField.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextSuggestField.java
index 4f23679..9f207f8 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextSuggestField.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestContextSuggestField.java
@@ -56,12 +56,10 @@ public class TestContextSuggestField extends LuceneTestCase {
@Test
public void testEmptySuggestion() throws Exception {
- try {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
new ContextSuggestField("suggest_field", "", 1, "type1");
- fail("no exception thrown when indexing zero length suggestion");
- } catch (IllegalArgumentException expected) {
- assertTrue(expected.getMessage().contains("value"));
- }
+ });
+ assertTrue(expected.getMessage().contains("value"));
}
@Test
@@ -73,22 +71,24 @@ public class TestContextSuggestField extends LuceneTestCase {
Analyzer analyzer = new MockAnalyzer(random());
Document document = new Document();
try (RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwcWithSuggestField(analyzer, "name"))) {
- document.add(new ContextSuggestField("name", "sugg", 1, charsRefBuilder.toString()));
- iw.addDocument(document);
- iw.commit();
- fail("no exception thrown for context value containing CONTEXT_SEPARATOR:" + ContextSuggestField.CONTEXT_SEPARATOR);
- } catch (IllegalArgumentException e) {
- assertTrue(e.getMessage().contains("[0x1d]"));
+ // exception should be thrown for context value containing CONTEXT_SEPARATOR
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
+ document.add(new ContextSuggestField("name", "sugg", 1, charsRefBuilder.toString()));
+ iw.addDocument(document);
+ iw.commit();
+ });
+ assertTrue(expected.getMessage().contains("[0x1d]"));
}
document.clear();
try (RandomIndexWriter iw = new RandomIndexWriter(random(), dir, iwcWithSuggestField(analyzer, "name"))) {
- document.add(new ContextSuggestField("name", charsRefBuilder.toString(), 1, "sugg"));
- iw.addDocument(document);
- iw.commit();
- fail("no exception thrown for value containing CONTEXT_SEPARATOR:" + ContextSuggestField.CONTEXT_SEPARATOR);
- } catch (IllegalArgumentException e) {
- assertTrue(e.getMessage().contains("[0x1d]"));
+ // exception should be thrown for context value containing CONTEXT_SEPARATOR
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
+ document.add(new ContextSuggestField("name", charsRefBuilder.toString(), 1, "sugg"));
+ iw.addDocument(document);
+ iw.commit();
+ });
+ assertTrue(expected.getMessage().contains("[0x1d]"));
}
}
@@ -135,10 +135,11 @@ public class TestContextSuggestField extends LuceneTestCase {
try (RandomIndexWriter iw = new RandomIndexWriter(random(), dir,
iwcWithSuggestField(analyzer, "suggest_field"))) {
- iw.addDocument(document);
- iw.commit();
- fail("mixing suggest field types for same field name should error out");
- } catch (IllegalArgumentException expected) {
+ // mixing suggest field types for same field name should error out
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
+ iw.addDocument(document);
+ iw.commit();
+ });
assertTrue(expected.getMessage().contains("mixed types"));
}
}
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestSuggestField.java
----------------------------------------------------------------------
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestSuggestField.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestSuggestField.java
index 1e2b946..3d2759d 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestSuggestField.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/document/TestSuggestField.java
@@ -79,22 +79,18 @@ public class TestSuggestField extends LuceneTestCase {
@Test
public void testEmptySuggestion() throws Exception {
- try {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
new SuggestField("suggest_field", "", 3);
- fail("no exception thrown when indexing zero length suggestion");
- } catch (IllegalArgumentException expected) {
- assertTrue(expected.getMessage().contains("value"));
- }
+ });
+ assertTrue(expected.getMessage().contains("value"));
}
@Test
public void testNegativeWeight() throws Exception {
- try {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
new SuggestField("suggest_field", "sugg", -1);
- fail("no exception thrown when indexing suggestion with negative weight");
- } catch (IllegalArgumentException expected) {
- assertTrue(expected.getMessage().contains("weight"));
- }
+ });
+ assertTrue(expected.getMessage().contains("weight"));
}
@Test
@@ -102,28 +98,22 @@ public class TestSuggestField extends LuceneTestCase {
CharsRefBuilder charsRefBuilder = new CharsRefBuilder();
charsRefBuilder.append("sugg");
charsRefBuilder.setCharAt(2, (char) CompletionAnalyzer.SEP_LABEL);
- try {
+ IllegalArgumentException expected = expectThrows(IllegalArgumentException.class, () -> {
new SuggestField("name", charsRefBuilder.toString(), 1);
- fail("no exception thrown for suggestion value containing SEP_LABEL:" + CompletionAnalyzer.SEP_LABEL);
- } catch (IllegalArgumentException e) {
- assertTrue(e.getMessage().contains("[0x1f]"));
- }
+ });
+ assertTrue(expected.getMessage().contains("[0x1f]"));
charsRefBuilder.setCharAt(2, (char) CompletionAnalyzer.HOLE_CHARACTER);
- try {
+ expected = expectThrows(IllegalArgumentException.class, () -> {
new SuggestField("name", charsRefBuilder.toString(), 1);
- fail("no exception thrown for suggestion value containing HOLE_CHARACTER:" + CompletionAnalyzer.HOLE_CHARACTER);
- } catch (IllegalArgumentException e) {
- assertTrue(e.getMessage().contains("[0x1e]"));
- }
+ });
+ assertTrue(expected.getMessage().contains("[0x1e]"));
charsRefBuilder.setCharAt(2, (char) NRTSuggesterBuilder.END_BYTE);
- try {
+ expected = expectThrows(IllegalArgumentException.class, () -> {
new SuggestField("name", charsRefBuilder.toString(), 1);
- fail("no exception thrown for suggestion value containing END_BYTE:" + NRTSuggesterBuilder.END_BYTE);
- } catch (IllegalArgumentException e) {
- assertTrue(e.getMessage().contains("[0x0]"));
- }
+ });
+ assertTrue(expected.getMessage().contains("[0x0]"));
}
@Test
http://git-wip-us.apache.org/repos/asf/lucene-solr/blob/189e985b/lucene/suggest/src/test/org/apache/lucene/search/suggest/fst/BytesRefSortersTest.java
----------------------------------------------------------------------
diff --git a/lucene/suggest/src/test/org/apache/lucene/search/suggest/fst/BytesRefSortersTest.java b/lucene/suggest/src/test/org/apache/lucene/search/suggest/fst/BytesRefSortersTest.java
index 4f4a126..1006700 100644
--- a/lucene/suggest/src/test/org/apache/lucene/search/suggest/fst/BytesRefSortersTest.java
+++ b/lucene/suggest/src/test/org/apache/lucene/search/suggest/fst/BytesRefSortersTest.java
@@ -52,12 +52,10 @@ public class BytesRefSortersTest extends LuceneTestCase {
BytesRefIterator i2 = sorter.iterator();
// Verify sorter contract.
- try {
+ expectThrows(IllegalStateException.class, () -> {
sorter.add(new BytesRef(new byte [1]));
- fail("expected contract violation.");
- } catch (IllegalStateException e) {
- // Expected.
- }
+ });
+
BytesRef spare1;
BytesRef spare2;
while ((spare1 = i1.next()) != null && (spare2 = i2.next()) != null) {