diff options
author | Robert Newson <rnewson@apache.org> | 2023-03-21 14:23:25 +0000 |
---|---|---|
committer | Robert Newson <rnewson@apache.org> | 2023-03-27 17:39:45 +0100 |
commit | 3c2ab78dade55fb239eb2014110548620f33cc3e (patch) | |
tree | ef4fb29c8c384b94bb402cb3ffd54575f7c8d0bd | |
parent | d950fbac9dd4d05602fcade15b64ec01c9584b2d (diff) | |
download | couchdb-3c2ab78dade55fb239eb2014110548620f33cc3e.tar.gz |
use TempDir correctly?
3 files changed, 102 insertions, 89 deletions
diff --git a/java/nouveau/base/src/test/java/org/apache/couchdb/nouveau/core/BaseIndexTest.java b/java/nouveau/base/src/test/java/org/apache/couchdb/nouveau/core/BaseIndexTest.java index 589c34d11..98933d982 100644 --- a/java/nouveau/base/src/test/java/org/apache/couchdb/nouveau/core/BaseIndexTest.java +++ b/java/nouveau/base/src/test/java/org/apache/couchdb/nouveau/core/BaseIndexTest.java @@ -14,7 +14,6 @@ package org.apache.couchdb.nouveau.core; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.condition.OS.WINDOWS; import java.io.IOException; import java.nio.file.Path; @@ -25,56 +24,50 @@ import org.apache.couchdb.nouveau.api.DocumentUpdateRequest; import org.apache.couchdb.nouveau.api.IndexDefinition; import org.apache.couchdb.nouveau.api.SearchRequest; import org.apache.couchdb.nouveau.api.SearchResults; -import org.junit.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledOnOs; import org.junit.jupiter.api.io.TempDir; public abstract class BaseIndexTest<T> { - @TempDir - Path path; - - protected Index<T> index; - protected abstract IndexLoader<T> indexLoader(); protected abstract T stringField(final String name, final String value); - @BeforeEach - public void setup() throws IOException { + protected final Index<T> setup(final Path path) throws IOException { final IndexDefinition indexDefinition = new IndexDefinition(); indexDefinition.setDefaultAnalyzer("standard"); final Index<T> index = indexLoader().apply(path, indexDefinition); index.setDeleteOnClose(true); - this.index = index; + return index; } - @AfterEach - public void cleanup() throws IOException { + protected final void cleanup(final Index<T> index) throws IOException { index.close(); } @Test - @DisabledOnOs(WINDOWS) - public void testOpenClose() throws IOException { - // do nothing + public void testOpenClose(@TempDir Path path) throws IOException { + final Index<T> index = setup(path); + cleanup(index); } @Test - @DisabledOnOs(WINDOWS) - public void testSearching() throws IOException { - final int count = 100; - for (int i = 1; i <= count; i++) { - final Collection<T> fields = List.of(stringField("foo", "bar")); - final DocumentUpdateRequest<T> request = new DocumentUpdateRequest<T>(i, null, fields); - index.update("doc" + i, request); + public void testSearching(@TempDir Path path) throws IOException { + final Index<T> index = setup(path); + try { + final int count = 100; + for (int i = 1; i <= count; i++) { + final Collection<T> fields = List.of(stringField("foo", "bar")); + final DocumentUpdateRequest<T> request = new DocumentUpdateRequest<T>(i, null, fields); + index.update("doc" + i, request); + } + final SearchRequest request = new SearchRequest(); + request.setQuery("*:*"); + final SearchResults<T> results = index.search(request); + assertThat(results.getTotalHits()).isEqualTo(count); + } finally { + cleanup(index); } - final SearchRequest request = new SearchRequest(); - request.setQuery("*:*"); - final SearchResults<T> results = index.search(request); - assertThat(results.getTotalHits()).isEqualTo(count); } } diff --git a/java/nouveau/lucene4/src/test/java/org/apache/couchdb/nouveau/lucene4/core/Lucene4IndexTest.java b/java/nouveau/lucene4/src/test/java/org/apache/couchdb/nouveau/lucene4/core/Lucene4IndexTest.java index b18276f43..519c6411d 100644 --- a/java/nouveau/lucene4/src/test/java/org/apache/couchdb/nouveau/lucene4/core/Lucene4IndexTest.java +++ b/java/nouveau/lucene4/src/test/java/org/apache/couchdb/nouveau/lucene4/core/Lucene4IndexTest.java @@ -14,9 +14,9 @@ package org.apache.couchdb.nouveau.lucene4.core; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.condition.OS.WINDOWS; import java.io.IOException; +import java.nio.file.Path; import java.util.Collection; import java.util.List; import java.util.Map; @@ -26,6 +26,7 @@ import org.apache.couchdb.nouveau.api.DoubleRange; import org.apache.couchdb.nouveau.api.SearchRequest; import org.apache.couchdb.nouveau.api.SearchResults; import org.apache.couchdb.nouveau.core.BaseIndexTest; +import org.apache.couchdb.nouveau.core.Index; import org.apache.couchdb.nouveau.core.IndexLoader; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.DoubleDocValuesField; @@ -40,7 +41,7 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.BytesRef; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.io.TempDir; public class Lucene4IndexTest extends BaseIndexTest<IndexableField> { @@ -62,39 +63,49 @@ public class Lucene4IndexTest extends BaseIndexTest<IndexableField> { } @Test - @DisabledOnOs(WINDOWS) - public void testCounts() throws IOException { - final int count = 100; - for (int i = 1; i <= count; i++) { - final Collection<IndexableField> fields = List.of(new SortedSetDocValuesField("$facets_sorted_doc_values",new BytesRef("bar\u001Fbaz"))); - final DocumentUpdateRequest<IndexableField> request = new DocumentUpdateRequest<IndexableField>(i, null, fields); - index.update("doc" + i, request); + public void testCounts(@TempDir Path path) throws IOException { + Index<IndexableField> index = setup(path); + try { + final int count = 100; + for (int i = 1; i <= count; i++) { + final Collection<IndexableField> fields = List + .of(new SortedSetDocValuesField("$facets_sorted_doc_values", new BytesRef("bar\u001Fbaz"))); + final DocumentUpdateRequest<IndexableField> request = new DocumentUpdateRequest<IndexableField>(i, null, + fields); + index.update("doc" + i, request); + } + final SearchRequest request = new SearchRequest(); + request.setQuery("*:*"); + request.setCounts(List.of("bar")); + final SearchResults<IndexableField> results = index.search(request); + assertThat(results.getCounts()).isEqualTo(Map.of("bar", Map.of("baz", (double) count))); + } finally { + cleanup(index); } - final SearchRequest request = new SearchRequest(); - request.setQuery("*:*"); - request.setCounts(List.of("bar")); - final SearchResults<IndexableField> results = index.search(request); - assertThat(results.getCounts()).isEqualTo(Map.of("bar", Map.of("baz", (double) count))); } @Test - @DisabledOnOs(WINDOWS) - public void testRanges() throws IOException { - final int count = 100; - for (int i = 1; i <= count; i++) { - final Collection<IndexableField> fields = List.of(new DoubleDocValuesField("bar", i)); - final DocumentUpdateRequest<IndexableField> request = new DocumentUpdateRequest<IndexableField>(i, null, - fields); - index.update("doc" + i, request); + public void testRanges(@TempDir Path path) throws IOException { + Index<IndexableField> index = setup(path); + try { + final int count = 100; + for (int i = 1; i <= count; i++) { + final Collection<IndexableField> fields = List.of(new DoubleDocValuesField("bar", i)); + final DocumentUpdateRequest<IndexableField> request = new DocumentUpdateRequest<IndexableField>(i, null, + fields); + index.update("doc" + i, request); + } + final SearchRequest request = new SearchRequest(); + request.setQuery("*:*"); + request.setRanges(Map.of("bar", + List.of(new DoubleRange("low", 0.0, true, (double) count / 2, true), + new DoubleRange("high", (double) count / 2, true, (double) count, true)))); + final SearchResults<IndexableField> results = index.search(request); + assertThat(results.getRanges()).isEqualTo( + Map.of("bar", Map.of("low", (double) count / 2, "high", (double) count / 2 + 1))); + } finally { + cleanup(index); } - final SearchRequest request = new SearchRequest(); - request.setQuery("*:*"); - request.setRanges(Map.of("bar", - List.of(new DoubleRange("low", 0.0, true, (double) count / 2, true), - new DoubleRange("high", (double) count / 2, true, (double) count, true)))); - final SearchResults<IndexableField> results = index.search(request); - assertThat(results.getRanges()).isEqualTo( - Map.of("bar", Map.of("low", (double) count / 2, "high", (double) count / 2 + 1))); } } diff --git a/java/nouveau/lucene9/src/test/java/org/apache/couchdb/nouveau/lucene9/core/Lucene9IndexTest.java b/java/nouveau/lucene9/src/test/java/org/apache/couchdb/nouveau/lucene9/core/Lucene9IndexTest.java index 4621fe82f..7f1333154 100644 --- a/java/nouveau/lucene9/src/test/java/org/apache/couchdb/nouveau/lucene9/core/Lucene9IndexTest.java +++ b/java/nouveau/lucene9/src/test/java/org/apache/couchdb/nouveau/lucene9/core/Lucene9IndexTest.java @@ -14,9 +14,9 @@ package org.apache.couchdb.nouveau.lucene9.core; import static org.assertj.core.api.Assertions.assertThat; -import static org.junit.jupiter.api.condition.OS.WINDOWS; import java.io.IOException; +import java.nio.file.Path; import java.util.Collection; import java.util.List; import java.util.Map; @@ -26,6 +26,7 @@ import org.apache.couchdb.nouveau.api.DoubleRange; import org.apache.couchdb.nouveau.api.SearchRequest; import org.apache.couchdb.nouveau.api.SearchResults; import org.apache.couchdb.nouveau.core.BaseIndexTest; +import org.apache.couchdb.nouveau.core.Index; import org.apache.couchdb.nouveau.core.IndexLoader; import org.apache.lucene.analysis.Analyzer; import org.apache.lucene.document.DoubleDocValuesField; @@ -41,7 +42,7 @@ import org.apache.lucene.store.Directory; import org.apache.lucene.store.FSDirectory; import org.apache.lucene.util.BytesRef; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.condition.DisabledOnOs; +import org.junit.jupiter.api.io.TempDir; public class Lucene9IndexTest extends BaseIndexTest<IndexableField> { @@ -63,40 +64,48 @@ public class Lucene9IndexTest extends BaseIndexTest<IndexableField> { } @Test - @DisabledOnOs(WINDOWS) - public void testCounts() throws IOException { - final int count = 100; - for (int i = 1; i <= count; i++) { - final Collection<IndexableField> fields = List.of(new SortedDocValuesField("bar", new BytesRef("baz"))); - final DocumentUpdateRequest<IndexableField> request = new DocumentUpdateRequest<IndexableField>(i, null, - fields); - index.update("doc" + i, request); + public void testCounts(@TempDir Path path) throws IOException { + Index<IndexableField> index = setup(path); + try { + final int count = 100; + for (int i = 1; i <= count; i++) { + final Collection<IndexableField> fields = List.of(new SortedDocValuesField("bar", new BytesRef("baz"))); + final DocumentUpdateRequest<IndexableField> request = new DocumentUpdateRequest<IndexableField>(i, null, + fields); + index.update("doc" + i, request); + } + final SearchRequest request = new SearchRequest(); + request.setQuery("*:*"); + request.setCounts(List.of("bar")); + final SearchResults<IndexableField> results = index.search(request); + assertThat(results.getCounts()).isEqualTo(Map.of("bar", Map.of("baz", count))); + } finally { + cleanup(index); } - final SearchRequest request = new SearchRequest(); - request.setQuery("*:*"); - request.setCounts(List.of("bar")); - final SearchResults<IndexableField> results = index.search(request); - assertThat(results.getCounts()).isEqualTo(Map.of("bar", Map.of("baz", count))); } @Test - @DisabledOnOs(WINDOWS) - public void testRanges() throws IOException { - final int count = 100; - for (int i = 1; i <= count; i++) { - final Collection<IndexableField> fields = List.of(new DoubleDocValuesField("bar", i)); - final DocumentUpdateRequest<IndexableField> request = new DocumentUpdateRequest<IndexableField>(i, null, - fields); - index.update("doc" + i, request); + public void testRanges(@TempDir Path path) throws IOException { + Index<IndexableField> index = setup(path); + try { + final int count = 100; + for (int i = 1; i <= count; i++) { + final Collection<IndexableField> fields = List.of(new DoubleDocValuesField("bar", i)); + final DocumentUpdateRequest<IndexableField> request = new DocumentUpdateRequest<IndexableField>(i, null, + fields); + index.update("doc" + i, request); + } + final SearchRequest request = new SearchRequest(); + request.setQuery("*:*"); + request.setRanges(Map.of("bar", + List.of(new DoubleRange("low", 0.0, true, (double) count / 2, true), + new DoubleRange("high", (double) count / 2, true, (double) count, true)))); + final SearchResults<IndexableField> results = index.search(request); + assertThat(results.getRanges()).isEqualTo( + Map.of("bar", Map.of("low", count / 2, "high", count / 2 + 1))); + } finally { + cleanup(index); } - final SearchRequest request = new SearchRequest(); - request.setQuery("*:*"); - request.setRanges(Map.of("bar", - List.of(new DoubleRange("low", 0.0, true, (double) count / 2, true), - new DoubleRange("high", (double) count / 2, true, (double) count, true)))); - final SearchResults<IndexableField> results = index.search(request); - assertThat(results.getRanges()).isEqualTo( - Map.of("bar", Map.of("low", count / 2, "high", count / 2 + 1))); } } |