Skip to content

Commit

Permalink
GH-5149 Use ParameterizedTest and Objects.requireNonNull
Browse files Browse the repository at this point in the history
  • Loading branch information
ate47 committed Nov 5, 2024
1 parent 41d4869 commit ffa582e
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 90 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;

Expand Down Expand Up @@ -577,19 +578,11 @@ protected Iterable<? extends DocumentScore> query(Resource subject, QuerySpec sp
}

SearchHits hits;
Integer numDocs = spec.getNumDocs();
int numDocs = Objects.requireNonNullElse(spec.getNumDocs(), -1);
if (subject != null) {
if (numDocs != null) {
hits = search(subject, request, qb, numDocs);
} else {
hits = search(subject, request, qb);
}
hits = search(subject, request, qb, numDocs);
} else {
if (numDocs != null) {
hits = search(request, qb, numDocs);
} else {
hits = search(request, qb);
}
hits = search(request, qb, numDocs);
}
return Iterables.transform(hits, new Function<>() {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;
import java.util.concurrent.atomic.AtomicBoolean;
Expand Down Expand Up @@ -751,21 +752,13 @@ protected Iterable<? extends DocumentScore> query(Resource subject, QuerySpec sp
highlighter = null;
}

Integer numDocs = spec.getNumDocs();
int numDocs = Objects.requireNonNullElse(spec.getNumDocs(), -1);

TopDocs docs;
if (subject != null) {
if (numDocs != null) {
docs = search(subject, q, numDocs);
} else {
docs = search(subject, q);
}
docs = search(subject, q, numDocs);
} else {
if (numDocs != null) {
docs = search(q, numDocs);
} else {
docs = search(q);
}
docs = search(q, numDocs);
}
return Iterables.transform(Arrays.asList(docs.scoreDocs),
(ScoreDoc doc) -> new LuceneDocumentScore(doc, highlighter, LuceneIndex.this));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Properties;
import java.util.Set;

Expand Down Expand Up @@ -317,20 +318,12 @@ protected Iterable<? extends DocumentScore> query(Resource subject, QuerySpec sp
q.addField(SearchFields.URI_FIELD_NAME);
}
q.addField("score");
Integer numDocs = spec.getNumDocs();
int numDocs = Objects.requireNonNullElse(spec.getNumDocs(), -1);
try {
if (subject != null) {
if (numDocs != null) {
response = search(subject, q, numDocs);
} else {
response = search(subject, q);
}
response = search(subject, q, numDocs);
} else {
if (numDocs != null) {
response = search(q, numDocs);
} else {
response = search(q);
}
response = search(q, numDocs);
}
} catch (SolrServerException e) {
throw new IOException(e);
Expand Down
5 changes: 5 additions & 0 deletions testsuites/lucene/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,10 @@
<artifactId>junit-vintage-engine</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<scope>compile</scope>
</dependency>
</dependencies>
</project>
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.junit.jupiter.api.Assertions.fail;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
Expand Down Expand Up @@ -64,6 +63,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;

@Timeout(value = 10, unit = TimeUnit.MINUTES)
public abstract class AbstractLuceneSailTest {
Expand Down Expand Up @@ -111,7 +112,7 @@ public abstract class AbstractLuceneSailTest {

protected abstract void configure(LuceneSail sail);

private void createTestSail(Consumer<LuceneSail> config) throws IOException {
private void createTestSail(Consumer<LuceneSail> config) {
if (repository != null) {
repository.shutDown();
repository = null;
Expand Down Expand Up @@ -1096,65 +1097,59 @@ public void run() {
assertEquals(0, exceptions.size(), "Exceptions occurred during testMultithreadedAdd, see stacktraces above");
}

@Test
public void testMaxNumDocsResult() throws IOException {
for (int i = 1; i <= 3; i++) {
final int j = i;
createTestSail(lc -> lc.setParameter(LuceneSail.MAX_QUERY_DOCUMENTS_KEY, String.valueOf(j)));
Repositories.consumeNoTransaction(repository, conn -> {
try (TupleQueryResult res = conn.prepareTupleQuery(
"SELECT ?Resource {\n"
+ " ?Resource <" + MATCHES + "> [\n "
+ " <" + QUERY + "> \"one\";\n "
+ " <" + NUM_DOCS + "> 3;\n "
+ " ]. } "
).evaluate()) {
for (int k = 0; k < j; k++) {
assertTrue(res.hasNext(), "missing result #" + k);
res.next();
}
if (res.hasNext()) {
StringBuilder b = new StringBuilder();
int r = 0;
do {
b.append("\n#").append(r++).append(res.next());
} while (res.hasNext());
fail("can't have more than " + j + " result(s)" + b);
}
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
public void testMaxNumDocsResult(int numDoc) {
createTestSail(lc -> lc.setParameter(LuceneSail.MAX_QUERY_DOCUMENTS_KEY, String.valueOf(numDoc)));
Repositories.consumeNoTransaction(repository, conn -> {
try (TupleQueryResult res = conn.prepareTupleQuery(
"SELECT ?Resource {\n"
+ " ?Resource <" + MATCHES + "> [\n "
+ " <" + QUERY + "> \"one\";\n "
+ " <" + NUM_DOCS + "> 3;\n "
+ " ]. } "
).evaluate()) {
for (int k = 0; k < numDoc; k++) {
assertTrue(res.hasNext(), "missing result #" + k);
res.next();
}
;
});
}
if (res.hasNext()) {
StringBuilder b = new StringBuilder();
int r = 0;
do {
b.append("\n#").append(r++).append(res.next());
} while (res.hasNext());
fail("can't have more than " + numDoc + " result(s)" + b);
}
}
});
}

@Test
public void testNumDocsResult() {
for (int i = 1; i <= 3; i++) {
final int j = i;
Repositories.consumeNoTransaction(repository, conn -> {
try (TupleQueryResult res = conn.prepareTupleQuery(
"SELECT ?Resource {\n"
+ " ?Resource <" + MATCHES + "> [\n "
+ " <" + QUERY + "> \"one\";\n "
+ " <" + NUM_DOCS + "> " + j + ";\n "
+ " ]. } "
).evaluate()) {
for (int k = 0; k < j; k++) {
assertTrue(res.hasNext(), "missing result #" + k);
res.next();
}
if (res.hasNext()) {
StringBuilder b = new StringBuilder();
int r = 0;
do {
b.append("\n#").append(r++).append(res.next());
} while (res.hasNext());
fail("can't have more than " + j + " result(s)" + b);
}
@ParameterizedTest
@ValueSource(ints = {1, 2, 3})
public void testNumDocsResult(int numDoc) {
Repositories.consumeNoTransaction(repository, conn -> {
try (TupleQueryResult res = conn.prepareTupleQuery(
"SELECT ?Resource {\n"
+ " ?Resource <" + MATCHES + "> [\n "
+ " <" + QUERY + "> \"one\";\n "
+ " <" + NUM_DOCS + "> " + numDoc + ";\n "
+ " ]. } "
).evaluate()) {
for (int k = 0; k < numDoc; k++) {
assertTrue(res.hasNext(), "missing result #" + k);
res.next();
}
;
});
}
if (res.hasNext()) {
StringBuilder b = new StringBuilder();
int r = 0;
do {
b.append("\n#").append(r++).append(res.next());
} while (res.hasNext());
fail("can't have more than " + numDoc + " result(s)" + b);
}
}
});
}

protected void assertQueryResult(String literal, IRI predicate, Resource resultUri) {
Expand Down

0 comments on commit ffa582e

Please sign in to comment.