Cleanup temporary directories after tests
This patch uses Junit's `@TempDir` to replace usages of `Files.createTempDirectory(Path)` where possible. There's still one unmanaged directory in the Util class that I'll have to manually add exit handlers if I care enough.
- Id
- 9adfde293499f5d2d5ae0953aa2560c6283608bb
- Author
- Caio
- Commit time
- 2019-05-21T09:54:58+02:00
Modified src/test/java/co/caio/cerberus/db/SimpleRecipeMetadataDatabaseTest.java
package co.caio.cerberus.db;
import static org.junit.jupiter.api.Assertions.*;
import co.caio.cerberus.Util;
import co.caio.cerberus.db.RecipeMetadataDatabase.RecipeMetadataDbException;
-import java.io.IOException;
-import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
class SimpleRecipeMetadataDatabaseTest {
@Test
- void canSaveAndReadSamples() throws IOException {
+ void canSaveAndReadSamples(@TempDir Path dbPath) {
var numSamples = 10;
- var dbPath = Files.createTempDirectory("sdb-");
var writer = new SimpleRecipeMetadataDatabase.Writer(dbPath);
}
@Test
- void canCreateEmptyDb() throws IOException {
- var dbPath = Files.createTempDirectory("sdb-empty-");
+ void canCreateEmptyDb(@TempDir Path dbPath) {
new SimpleRecipeMetadataDatabase.Writer(dbPath).close();
assertDoesNotThrow(() -> new SimpleRecipeMetadataDatabase(dbPath));
assertEquals(0, new SimpleRecipeMetadataDatabase(dbPath).size());
}
@Test
- void cannotWriteToExistingDb() throws IOException {
- var dbPath = Files.createTempDirectory("sdb-empty-");
+ void cannotWriteToExistingDb(@TempDir Path dbPath) {
// First open+close should work
assertDoesNotThrow(() -> new SimpleRecipeMetadataDatabase.Writer(dbPath).close());
}
@Test
- void saveAllIsNotAllowed() throws IOException {
- var dbPath = Files.createTempDirectory("sdb-empty-");
+ void saveAllIsNotAllowed(@TempDir Path dbPath) {
new SimpleRecipeMetadataDatabase.Writer(dbPath).close();
var db = new SimpleRecipeMetadataDatabase(dbPath);
assertThrows(
Modified src/test/java/co/caio/cerberus/lucene/FloatAssociationsThresholdCountTest.java
package co.caio.cerberus.lucene;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.IOException;
-import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Map;
import org.apache.lucene.document.Document;
import org.apache.lucene.facet.FacetsCollector;
import org.apache.lucene.store.FSDirectory;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
class FloatAssociationsThresholdCountTest {
}
@BeforeAll
- static void setUp() throws IOException {
- final var indexDir = FSDirectory.open(Files.createTempDirectory("lucene-test"));
- final var taxoDir = FSDirectory.open(Files.createTempDirectory("lucene-test-taxo"));
+ static void setUp(@TempDir Path tmpDir) throws IOException {
+ final var indexDir = FSDirectory.open(tmpDir.resolve("index"));
+ final var taxoDir = FSDirectory.open(tmpDir.resolve("taxo"));
config = new FacetsConfig();
config.setMultiValued("score", true);
Modified src/test/java/co/caio/cerberus/search/CategoryExtractorTest.java
import co.caio.cerberus.model.Recipe;
import co.caio.cerberus.model.SearchQuery;
import java.io.IOException;
-import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.HashSet;
import java.util.Map;
import java.util.Random;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
class CategoryExtractorTest {
@Test
- void basicFunctionality() throws IOException {
+ void basicFunctionality(@TempDir Path dataDir) throws IOException {
var ce =
new CategoryExtractor.Builder()
})
.build();
- var dataDir = Files.createTempDirectory("extractor-");
var indexer =
new Indexer.Builder().dataDirectory(dataDir).categoryExtractor(ce).createMode().build();
Modified src/test/java/co/caio/cerberus/search/IndexConfigurationTest.java
package co.caio.cerberus.search;
import static org.junit.jupiter.api.Assertions.*;
import co.caio.cerberus.search.IndexConfiguration.IndexConfigurationException;
import java.io.IOException;
-import java.nio.file.Files;
+import java.nio.file.Path;
import java.util.Map.Entry;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.lucene.facet.FacetsConfig;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
class IndexConfigurationTest {
@Test
- void canCreateEmpty() {
- assertDoesNotThrow(
- () -> new IndexConfiguration(Files.createTempDirectory("indexconfig-"), Set.of()));
+ void canCreateEmpty(@TempDir Path tempDir) {
+ assertDoesNotThrow(() -> new IndexConfiguration(tempDir, Set.of()));
}
@Test
- void multiValuedDimensionsConfiguresFacetConfig() throws IOException {
+ void multiValuedDimensionsConfiguresFacetConfig(@TempDir Path tempDir) throws IOException {
var originalMv = Set.of("a", "b", "c");
- var config = new IndexConfiguration(Files.createTempDirectory("indexconfig-"), originalMv);
+ var config = new IndexConfiguration(tempDir, originalMv);
var configuredMv = extractMultiValued(config.getFacetsConfig());
}
@Test
- void cantLoadFromConfigIfItDoesNotExist() {
+ void cantLoadFromConfigIfItDoesNotExist(@TempDir Path tempDir) {
assertThrows(
- IndexConfigurationException.class,
- () -> IndexConfiguration.fromBaseDirectory(Files.createTempDirectory("indexconfig-")));
+ IndexConfigurationException.class, () -> IndexConfiguration.fromBaseDirectory(tempDir));
}
@Test
- void loadFromConfigWorks() throws IOException {
- var base = Files.createTempDirectory("indexconfig-");
+ void loadFromConfigWorks(@TempDir Path base) {
var multiValued = Set.of("a", "b", "c", "d");
var config = new IndexConfiguration(base, multiValued);
Modified src/test/java/co/caio/cerberus/search/IndexerTest.java
package co.caio.cerberus.search;
import static org.junit.jupiter.api.Assertions.*;
import co.caio.cerberus.Util;
import java.io.IOException;
-import java.nio.file.Files;
import java.nio.file.Path;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
class IndexerTest {
@Test
}
@Test
- void simpleLocalIndexer() throws IOException {
- var tempDir = Files.createTempDirectory("cerberus-test");
+ void simpleLocalIndexer(@TempDir Path tempDir) throws IOException {
var index = new Indexer.Builder().dataDirectory(tempDir).createMode().build();
assertEquals(0, index.numDocs());
index.addRecipe(Util.getBasicRecipe());
Modified src/test/java/co/caio/cerberus/search/SearcherTest.java
import co.caio.cerberus.model.SearchQuery.SortOrder;
import co.caio.cerberus.search.IndexConfiguration.IndexConfigurationException;
import co.caio.cerberus.search.Searcher.Builder.SearcherBuilderException;
-import java.nio.file.Files;
import java.nio.file.Path;
import java.util.List;
import java.util.OptionalInt;
import org.apache.lucene.search.MatchNoDocsQuery;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
class SearcherTest {
private static Searcher searcher;
}
@Test
- void dietThreshold() throws Exception {
- var indexer =
- new Indexer.Builder()
- .dataDirectory(Files.createTempDirectory("threshold-test"))
- .createMode()
- .build();
+ void dietThreshold(@TempDir Path tmpDir) throws Exception {
+ var indexer = new Indexer.Builder().dataDirectory(tmpDir).createMode().build();
var recipeBuilder =
new Recipe.Builder()