Skip to content

Commit 7125cf7

Browse files
committed
Update some asserts in search tests
1 parent d547896 commit 7125cf7

15 files changed

+79
-57
lines changed

lucene/core/src/test/org/apache/lucene/search/BaseKnnVectorQueryTestCase.java

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,9 @@
2020
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomBoolean;
2121
import static com.carrotsearch.randomizedtesting.RandomizedTest.randomIntBetween;
2222
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
23+
import static org.hamcrest.Matchers.instanceOf;
2324
import static org.hamcrest.Matchers.lessThanOrEqualTo;
25+
import static org.hamcrest.Matchers.not;
2426

2527
import java.io.IOException;
2628
import java.util.HashSet;
@@ -915,8 +917,9 @@ public void testTimeLimitingKnnCollectorManager() throws IOException {
915917
noTimeoutManager.newCollector(Integer.MAX_VALUE, null, searcher.leafContexts.get(0));
916918

917919
// Check that a normal collector is created without timeout
918-
assertFalse(
919-
noTimeoutCollector instanceof TimeLimitingKnnCollectorManager.TimeLimitingKnnCollector);
920+
assertThat(
921+
noTimeoutCollector,
922+
not(instanceOf(TimeLimitingKnnCollectorManager.TimeLimitingKnnCollector.class)));
920923
noTimeoutCollector.collect(0, 0);
921924
assertFalse(noTimeoutCollector.earlyTerminated());
922925

@@ -932,7 +935,7 @@ public void testTimeLimitingKnnCollectorManager() throws IOException {
932935
timeoutManager.newCollector(Integer.MAX_VALUE, null, searcher.leafContexts.get(0));
933936

934937
// Check that a time limiting collector is created, which returns partial results
935-
assertFalse(timeoutCollector instanceof TopKnnCollector);
938+
assertThat(timeoutCollector, not(instanceOf(TopKnnCollector.class)));
936939
timeoutCollector.collect(0, 0);
937940
assertTrue(timeoutCollector.earlyTerminated());
938941

lucene/core/src/test/org/apache/lucene/search/BaseVectorSimilarityQueryTestCase.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
package org.apache.lucene.search;
1818

1919
import static org.apache.lucene.search.DocIdSetIterator.NO_MORE_DOCS;
20+
import static org.hamcrest.Matchers.either;
21+
import static org.hamcrest.Matchers.greaterThan;
22+
import static org.hamcrest.Matchers.lessThan;
2023

2124
import java.io.IOException;
2225
import java.util.Arrays;
@@ -314,7 +317,7 @@ public void testSomeDeletes() throws IOException {
314317
int id = getId(searcher, scoreDoc.doc);
315318

316319
// Check that returned document is not deleted
317-
assertFalse(id >= startIndex && id <= endIndex);
320+
assertThat(id, either(lessThan(startIndex)).or(greaterThan(endIndex)));
318321
}
319322
// Check that all live docs are returned
320323
assertEquals(numDocs - endIndex + startIndex - 1, scoreDocs.length);

lucene/core/src/test/org/apache/lucene/search/TestBooleanQueryVisitSubscorers.java

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
*/
1717
package org.apache.lucene.search;
1818

19+
import static org.hamcrest.Matchers.contains;
20+
1921
import java.io.IOException;
2022
import java.util.ArrayList;
2123
import java.util.Collection;
@@ -249,17 +251,16 @@ public void testGetChildrenMinShouldMatchSumScorer() throws IOException {
249251
ScoreSummary scoreSummary =
250252
searcher.search(query.build(), new ScorerSummarizingCollectorManager());
251253
assertEquals(1, scoreSummary.numHits.get());
252-
assertFalse(scoreSummary.summaries.isEmpty());
253-
for (String summary : scoreSummary.summaries) {
254-
assertEquals(
255-
"ConjunctionScorer\n"
256-
+ " MUST ConstantScoreScorer\n"
257-
+ " MUST WANDScorer\n"
258-
+ " SHOULD TermScorer\n"
259-
+ " SHOULD TermScorer\n"
260-
+ " SHOULD TermScorer",
261-
summary);
262-
}
254+
assertThat(
255+
scoreSummary.summaries,
256+
contains(
257+
"""
258+
ConjunctionScorer
259+
MUST ConstantScoreScorer
260+
MUST WANDScorer
261+
SHOULD TermScorer
262+
SHOULD TermScorer
263+
SHOULD TermScorer"""));
263264
}
264265

265266
public void testGetChildrenBoosterScorer() throws IOException {
@@ -269,10 +270,7 @@ public void testGetChildrenBoosterScorer() throws IOException {
269270
ScoreSummary scoreSummary =
270271
scorerSearcher.search(query.build(), new ScorerSummarizingCollectorManager());
271272
assertEquals(1, scoreSummary.numHits.get());
272-
assertFalse(scoreSummary.summaries.isEmpty());
273-
for (String summary : scoreSummary.summaries) {
274-
assertEquals("TermScorer", summary);
275-
}
273+
assertThat(scoreSummary.summaries, contains("TermScorer"));
276274
}
277275

278276
private static class ScoreSummary {

lucene/core/src/test/org/apache/lucene/search/TestBoostQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ public void testEquals() {
5555
}
5656
BoostQuery q3 = new BoostQuery(new MatchAllDocsQuery(), boost2);
5757
assertFalse(q1.equals(q3));
58-
assertFalse(q1.hashCode() == q3.hashCode());
58+
assertNotEquals(q3.hashCode(), q1.hashCode());
5959
}
6060

6161
public void testToString() {

lucene/core/src/test/org/apache/lucene/search/TestDocValuesQueries.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -502,12 +502,12 @@ public void testSetEquals() {
502502
assertEquals(
503503
NumericDocValuesField.newSlowSetQuery("field", 17L, 42L, 32416190071L),
504504
NumericDocValuesField.newSlowSetQuery("field", 17L, 32416190071L, 42L));
505-
assertFalse(
506-
NumericDocValuesField.newSlowSetQuery("field", 42L)
507-
.equals(NumericDocValuesField.newSlowSetQuery("field2", 42L)));
508-
assertFalse(
509-
NumericDocValuesField.newSlowSetQuery("field", 17L, 42L)
510-
.equals(NumericDocValuesField.newSlowSetQuery("field", 17L, 32416190071L)));
505+
assertNotEquals(
506+
NumericDocValuesField.newSlowSetQuery("field", 42L),
507+
NumericDocValuesField.newSlowSetQuery("field2", 42L));
508+
assertNotEquals(
509+
NumericDocValuesField.newSlowSetQuery("field", 17L, 42L),
510+
NumericDocValuesField.newSlowSetQuery("field", 17L, 32416190071L));
511511
}
512512

513513
public void testDuelSetVsTermsQuery() throws IOException {

lucene/core/src/test/org/apache/lucene/search/TestFieldExistsQuery.java

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
package org.apache.lucene.search;
1818

19+
import static org.hamcrest.Matchers.instanceOf;
20+
import static org.hamcrest.Matchers.not;
21+
1922
import java.io.IOException;
2023
import org.apache.lucene.document.BinaryPoint;
2124
import org.apache.lucene.document.Document;
@@ -127,8 +130,10 @@ public void testDocValuesNoRewrite() throws IOException {
127130
iw.close();
128131
final IndexSearcher searcher = newSearcher(reader);
129132

130-
assertFalse((new FieldExistsQuery("dim")).rewrite(searcher) instanceof MatchAllDocsQuery);
131-
assertFalse((new FieldExistsQuery("f")).rewrite(searcher) instanceof MatchAllDocsQuery);
133+
assertThat(
134+
(new FieldExistsQuery("dim")).rewrite(searcher), not(instanceOf(MatchAllDocsQuery.class)));
135+
assertThat(
136+
(new FieldExistsQuery("f")).rewrite(searcher), not(instanceOf(MatchAllDocsQuery.class)));
132137
reader.close();
133138
dir.close();
134139
}
@@ -149,9 +154,12 @@ public void testDocValuesNoRewriteWithDocValues() throws IOException {
149154
iw.close();
150155
final IndexSearcher searcher = newSearcher(reader);
151156

152-
assertFalse((new FieldExistsQuery("dv1")).rewrite(searcher) instanceof MatchAllDocsQuery);
153-
assertFalse((new FieldExistsQuery("dv2")).rewrite(searcher) instanceof MatchAllDocsQuery);
154-
assertFalse((new FieldExistsQuery("dv3")).rewrite(searcher) instanceof MatchAllDocsQuery);
157+
assertThat(
158+
(new FieldExistsQuery("dv1")).rewrite(searcher), not(instanceOf(MatchAllDocsQuery.class)));
159+
assertThat(
160+
(new FieldExistsQuery("dv2")).rewrite(searcher), not(instanceOf(MatchAllDocsQuery.class)));
161+
assertThat(
162+
(new FieldExistsQuery("dv3")).rewrite(searcher), not(instanceOf(MatchAllDocsQuery.class)));
155163
reader.close();
156164
dir.close();
157165
}

lucene/core/src/test/org/apache/lucene/search/TestMaxClauseLimit.java

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717

1818
package org.apache.lucene.search;
1919

20+
import static org.hamcrest.Matchers.instanceOf;
21+
import static org.hamcrest.Matchers.not;
22+
2023
import java.io.IOException;
2124
import java.util.Arrays;
2225
import org.apache.lucene.index.MultiReader;
@@ -57,9 +60,10 @@ public void testFlattenInnerDisjunctionsWithMoreThan1024Terms() throws IOExcepti
5760
() -> {
5861
searcher.rewrite(query);
5962
});
60-
assertFalse(
63+
assertThat(
6164
"Should have been caught during flattening and not required full nested walk",
62-
e instanceof IndexSearcher.TooManyNestedClauses);
65+
e,
66+
not(instanceOf(IndexSearcher.TooManyNestedClauses.class)));
6367
}
6468

6569
public void testLargeTermsNestedFirst() throws IOException {

lucene/core/src/test/org/apache/lucene/search/TestMultiPhraseQuery.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ public void testHashCodeAndEquals() {
305305
query1builder.add(term2);
306306
query1 = query1builder.build();
307307

308-
assertFalse(query1.hashCode() == query2.hashCode());
308+
assertNotEquals(query1.hashCode(), query2.hashCode());
309309
assertFalse(query1.equals(query2));
310310

311311
query2builder.add(term2);

lucene/core/src/test/org/apache/lucene/search/TestPointQueries.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2159,7 +2159,7 @@ public void testRangeOptimizesIfAllPointsMatch() throws IOException {
21592159
searcher.setQueryCache(null);
21602160
weight = searcher.createWeight(query, ScoreMode.COMPLETE_NO_SCORES, 1);
21612161
scorer = weight.scorer(searcher.getIndexReader().leaves().get(0));
2162-
assertFalse(DocIdSetIterator.all(1).getClass().equals(scorer.iterator().getClass()));
2162+
assertNotEquals(DocIdSetIterator.all(1).getClass(), scorer.iterator().getClass());
21632163

21642164
reader.close();
21652165
w.close();

lucene/core/src/test/org/apache/lucene/search/TestQueryRescorer.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@
1616
*/
1717
package org.apache.lucene.search;
1818

19+
import static org.hamcrest.Matchers.containsString;
20+
import static org.hamcrest.Matchers.not;
21+
1922
import com.carrotsearch.randomizedtesting.generators.RandomPicks;
2023
import java.io.IOException;
2124
import java.util.Arrays;
@@ -333,20 +336,20 @@ protected float combine(
333336
int docID = hits2.scoreDocs[0].doc;
334337
Explanation explain = rescorer.explain(searcher, searcher.explain(bq.build(), docID), docID);
335338
String s = explain.toString();
336-
assertTrue(s.contains("TestQueryRescorer$"));
337-
assertTrue(s.contains("combined first and second pass score"));
338-
assertTrue(s.contains("first pass score"));
339-
assertTrue(s.contains("= second pass score"));
339+
assertThat(s, containsString("TestQueryRescorer$"));
340+
assertThat(s, containsString("combined first and second pass score"));
341+
assertThat(s, containsString("first pass score"));
342+
assertThat(s, containsString("= second pass score"));
340343
assertEquals(hits2.scoreDocs[0].score, explain.getValue().doubleValue(), 0.0f);
341344

342345
docID = hits2.scoreDocs[1].doc;
343346
explain = rescorer.explain(searcher, searcher.explain(bq.build(), docID), docID);
344347
s = explain.toString();
345-
assertTrue(s.contains("TestQueryRescorer$"));
346-
assertTrue(s.contains("combined first and second pass score"));
347-
assertTrue(s.contains("first pass score"));
348-
assertTrue(s.contains("no second pass score"));
349-
assertFalse(s.contains("= second pass score"));
348+
assertThat(s, containsString("TestQueryRescorer$"));
349+
assertThat(s, containsString("combined first and second pass score"));
350+
assertThat(s, containsString("first pass score"));
351+
assertThat(s, containsString("no second pass score"));
352+
assertThat(s, not(containsString("= second pass score")));
350353
assertEquals(hits2.scoreDocs[1].score, explain.getValue().doubleValue(), 0.0f);
351354

352355
r.close();

0 commit comments

Comments
 (0)