From ffa582e7868fdc9589c64307178fc8c47acedbf7 Mon Sep 17 00:00:00 2001 From: qaate47 Date: Tue, 5 Nov 2024 14:05:30 +0100 Subject: [PATCH] GH-5149 Use ParameterizedTest and Objects.requireNonNull --- .../elasticsearch/ElasticsearchIndex.java | 15 +-- .../rdf4j/sail/lucene/impl/LuceneIndex.java | 15 +-- .../eclipse/rdf4j/sail/solr/SolrIndex.java | 15 +-- testsuites/lucene/pom.xml | 5 + .../sail/lucene/AbstractLuceneSailTest.java | 109 +++++++++--------- 5 files changed, 69 insertions(+), 90 deletions(-) diff --git a/core/sail/elasticsearch/src/main/java/org/eclipse/rdf4j/sail/elasticsearch/ElasticsearchIndex.java b/core/sail/elasticsearch/src/main/java/org/eclipse/rdf4j/sail/elasticsearch/ElasticsearchIndex.java index 29a2d1e74c5..65e5e8a1502 100644 --- a/core/sail/elasticsearch/src/main/java/org/eclipse/rdf4j/sail/elasticsearch/ElasticsearchIndex.java +++ b/core/sail/elasticsearch/src/main/java/org/eclipse/rdf4j/sail/elasticsearch/ElasticsearchIndex.java @@ -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; @@ -577,19 +578,11 @@ protected Iterable 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<>() { diff --git a/core/sail/lucene/src/main/java/org/eclipse/rdf4j/sail/lucene/impl/LuceneIndex.java b/core/sail/lucene/src/main/java/org/eclipse/rdf4j/sail/lucene/impl/LuceneIndex.java index 5a4965b088d..16885229230 100644 --- a/core/sail/lucene/src/main/java/org/eclipse/rdf4j/sail/lucene/impl/LuceneIndex.java +++ b/core/sail/lucene/src/main/java/org/eclipse/rdf4j/sail/lucene/impl/LuceneIndex.java @@ -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; @@ -751,21 +752,13 @@ protected Iterable 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)); diff --git a/core/sail/solr/src/main/java/org/eclipse/rdf4j/sail/solr/SolrIndex.java b/core/sail/solr/src/main/java/org/eclipse/rdf4j/sail/solr/SolrIndex.java index 34ea7b799a8..ab9e7282324 100644 --- a/core/sail/solr/src/main/java/org/eclipse/rdf4j/sail/solr/SolrIndex.java +++ b/core/sail/solr/src/main/java/org/eclipse/rdf4j/sail/solr/SolrIndex.java @@ -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; @@ -317,20 +318,12 @@ protected Iterable 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); diff --git a/testsuites/lucene/pom.xml b/testsuites/lucene/pom.xml index cf0c02226d8..66488c8e819 100644 --- a/testsuites/lucene/pom.xml +++ b/testsuites/lucene/pom.xml @@ -35,5 +35,10 @@ junit-vintage-engine compile + + org.junit.jupiter + junit-jupiter-params + compile + diff --git a/testsuites/lucene/src/main/java/org/eclipse/testsuite/rdf4j/sail/lucene/AbstractLuceneSailTest.java b/testsuites/lucene/src/main/java/org/eclipse/testsuite/rdf4j/sail/lucene/AbstractLuceneSailTest.java index 84391587d15..8d9a23dc4f8 100644 --- a/testsuites/lucene/src/main/java/org/eclipse/testsuite/rdf4j/sail/lucene/AbstractLuceneSailTest.java +++ b/testsuites/lucene/src/main/java/org/eclipse/testsuite/rdf4j/sail/lucene/AbstractLuceneSailTest.java @@ -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; @@ -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 { @@ -111,7 +112,7 @@ public abstract class AbstractLuceneSailTest { protected abstract void configure(LuceneSail sail); - private void createTestSail(Consumer config) throws IOException { + private void createTestSail(Consumer config) { if (repository != null) { repository.shutDown(); repository = null; @@ -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) {