This is an automated email from the ASF dual-hosted git repository.

desruisseaux pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git

commit 3f8d19d715c8bd5fbb586c91f8aea2c5d1ff0d8a
Author: Martin Desruisseaux <martin.desruisse...@geomatys.com>
AuthorDate: Wed Feb 21 10:45:27 2024 +0100

    Let JUnit manage temporary directories.
---
 .../sis/storage/image/WorldFileStoreTest.java      | 93 ++++++++++------------
 .../sis/storage/shapefile/ShapefileStoreTest.java  | 33 ++------
 2 files changed, 47 insertions(+), 79 deletions(-)

diff --git 
a/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/image/WorldFileStoreTest.java
 
b/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/image/WorldFileStoreTest.java
index 0f0a6b69ad..a146fc4eac 100644
--- 
a/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/image/WorldFileStoreTest.java
+++ 
b/endorsed/src/org.apache.sis.storage/test/org/apache/sis/storage/image/WorldFileStoreTest.java
@@ -19,7 +19,6 @@ package org.apache.sis.storage.image;
 import java.io.IOException;
 import java.nio.file.Path;
 import java.nio.file.Files;
-import java.nio.file.DirectoryStream;
 import java.nio.file.StandardOpenOption;
 import org.opengis.metadata.Metadata;
 import org.opengis.metadata.extent.GeographicBoundingBox;
@@ -33,6 +32,7 @@ import org.apache.sis.setup.OptionKey;
 import org.apache.sis.util.ArraysExt;
 
 // Test dependencies
+import org.junit.jupiter.api.io.TempDir;
 import org.junit.jupiter.api.Test;
 import static org.junit.jupiter.api.Assertions.*;
 import static org.apache.sis.test.Assertions.assertMessageContains;
@@ -122,65 +122,56 @@ public final class WorldFileStoreTest extends TestCase {
      * This test unconditionally open the data store as an aggregate,
      * i.e. it bypasses the simplification of PNG files as {@link 
SingleImageStore} view.
      *
+     * @param  directory  a temporary directory where to write the image.
      * @throws DataStoreException if an error occurred during Image I/O or 
data store operations.
      * @throws IOException if an error occurred when creating, reading or 
deleting temporary files.
      */
     @Test
-    public void testReadWrite() throws DataStoreException, IOException {
-        final Path directory = Files.createTempDirectory("SIS-");
-        try {
-            final WorldFileStoreProvider provider = new 
WorldFileStoreProvider(false);
-            try (WorldFileStore source = provider.open(testData())) {
-                assertFalse(source instanceof WritableStore);
-                final GridCoverageResource resource = 
getSingleton(source.components());
-                assertEquals("gradient:1", 
resource.getIdentifier().get().toString());
+    public void testReadWrite(@TempDir final Path directory) throws 
DataStoreException, IOException {
+        final WorldFileStoreProvider provider = new 
WorldFileStoreProvider(false);
+        try (WorldFileStore source = provider.open(testData())) {
+            assertFalse(source instanceof WritableStore);
+            final GridCoverageResource resource = 
getSingleton(source.components());
+            assertEquals("gradient:1", 
resource.getIdentifier().get().toString());
+            /*
+             * Above `resource` is the content of "gradient.png" file.
+             * Write the resource in a new file using a different format.
+             */
+            final Path targetPath = directory.resolve("copy.jpg");
+            final StorageConnector connector = new 
StorageConnector(targetPath);
+            connector.setOption(OptionKey.OPEN_OPTIONS, new 
StandardOpenOption[] {
+                StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE
+            });
+            try (WritableStore target = (WritableStore) 
provider.open(connector)) {
+                assertEquals(0, target.isMultiImages());
+                final WritableResource copy = (WritableResource) 
target.add(resource);
+                assertEquals(1, target.isMultiImages());
+                assertNotSame(resource, copy);
+                assertEquals (resource.getGridGeometry(),     
copy.getGridGeometry());
+                assertEquals (resource.getSampleDimensions(), 
copy.getSampleDimensions());
                 /*
-                 * Above `resource` is the content of "gradient.png" file.
-                 * Write the resource in a new file using a different format.
+                 * Verify that attempt to write again without `REPLACE` mode 
fails.
                  */
-                final Path targetPath = directory.resolve("copy.jpg");
-                final StorageConnector connector = new 
StorageConnector(targetPath);
-                connector.setOption(OptionKey.OPEN_OPTIONS, new 
StandardOpenOption[] {
-                    StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE
-                });
-                try (WritableStore target = (WritableStore) 
provider.open(connector)) {
-                    assertEquals(0, target.isMultiImages());
-                    final WritableResource copy = (WritableResource) 
target.add(resource);
-                    assertEquals(1, target.isMultiImages());
-                    assertNotSame(resource, copy);
-                    assertEquals (resource.getGridGeometry(),     
copy.getGridGeometry());
-                    assertEquals (resource.getSampleDimensions(), 
copy.getSampleDimensions());
-                    /*
-                     * Verify that attempt to write again without `REPLACE` 
mode fails.
-                     */
-                    final GridCoverage coverage = resource.read(null, null);
-                    var e = assertThrows(ResourceAlreadyExistsException.class, 
() -> copy.write(coverage),
-                                         "Should not have replaced existing 
resource.");
-                    assertMessageContains(e, "1");      // "1" is the image 
identifier.
-                    /*
-                     * Try to write again in `REPLACE` mode.
-                     */
-                    copy.write(coverage, 
WritableResource.CommonOption.REPLACE);
-                    assertEquals(1, target.isMultiImages());
-                }
+                final GridCoverage coverage = resource.read(null, null);
+                var e = assertThrows(ResourceAlreadyExistsException.class, () 
-> copy.write(coverage),
+                                     "Should not have replaced existing 
resource.");
+                assertMessageContains(e, "1");      // "1" is the image 
identifier.
                 /*
-                 * Verify that the 3 files have been written. The JGW file 
content is verified,
-                 * but the PRJ file content is not fully verified because it 
may vary.
+                 * Try to write again in `REPLACE` mode.
                  */
-                assertTrue(Files.size(targetPath) > 0);
-                assertTrue(Files.readAllLines(directory.resolve("copy.prj"))
-                                .stream().anyMatch((line) -> 
line.contains("WGS 84")));
-                assertArrayEquals(new String[] {
-                    "2.8125", "0.0", "0.0", "-2.8125", "-178.59375", "88.59375"
-                }, 
Files.readAllLines(directory.resolve("copy.jgw")).toArray());
-            }
-        } finally {
-            try (DirectoryStream<Path> entries = 
Files.newDirectoryStream(directory)) {
-                for (Path entry : entries) {
-                    Files.delete(entry);
-                }
+                copy.write(coverage, WritableResource.CommonOption.REPLACE);
+                assertEquals(1, target.isMultiImages());
             }
-            Files.delete(directory);
+            /*
+             * Verify that the 3 files have been written. The JGW file content 
is verified,
+             * but the PRJ file content is not fully verified because it may 
vary.
+             */
+            assertTrue(Files.size(targetPath) > 0);
+            assertTrue(Files.readAllLines(directory.resolve("copy.prj"))
+                            .stream().anyMatch((line) -> line.contains("WGS 
84")));
+            assertArrayEquals(new String[] {
+                "2.8125", "0.0", "0.0", "-2.8125", "-178.59375", "88.59375"
+            }, Files.readAllLines(directory.resolve("copy.jgw")).toArray());
         }
     }
 }
diff --git 
a/incubator/src/org.apache.sis.storage.shapefile/test/org/apache/sis/storage/shapefile/ShapefileStoreTest.java
 
b/incubator/src/org.apache.sis.storage.shapefile/test/org/apache/sis/storage/shapefile/ShapefileStoreTest.java
index 1aba1cbb2d..c6c3df712a 100644
--- 
a/incubator/src/org.apache.sis.storage.shapefile/test/org/apache/sis/storage/shapefile/ShapefileStoreTest.java
+++ 
b/incubator/src/org.apache.sis.storage.shapefile/test/org/apache/sis/storage/shapefile/ShapefileStoreTest.java
@@ -16,15 +16,12 @@
  */
 package org.apache.sis.storage.shapefile;
 
-import java.io.File;
 import java.io.IOException;
 import java.net.URISyntaxException;
 import java.net.URL;
-import java.nio.file.Files;
 import java.nio.file.Path;
 import java.nio.file.Paths;
 import java.time.LocalDate;
-import java.util.Comparator;
 import java.util.Iterator;
 import java.util.List;
 import java.util.function.UnaryOperator;
@@ -45,6 +42,7 @@ import org.apache.sis.feature.internal.AttributeConvention;
 // Test dependencies
 import static org.junit.jupiter.api.Assertions.*;
 import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.io.TempDir;
 
 // Specific to the geoapi-3.1 and geoapi-4.0 branches:
 import org.opengis.feature.AttributeType;
@@ -196,8 +194,7 @@ public class ShapefileStoreTest {
      * Test creating a new shapefile.
      */
     @Test
-    public void testCreate() throws URISyntaxException, DataStoreException, 
IOException {
-        final Path folder = Files.createTempDirectory("shapefileTest");
+    public void testCreate(@TempDir final Path folder) throws 
URISyntaxException, DataStoreException, IOException {
         final Path temp = folder.resolve("test.shp");
         final String name = temp.getFileName().toString().split("\\.")[0];
         try (final ShapefileStore store = new ShapefileStore(temp)) {
@@ -241,8 +238,6 @@ public class ShapefileStoreTest {
                 assertEquals(Double.class, floatProp.getValueClass());
                 assertEquals(LocalDate.class, dateProp.getValueClass());
             }
-        } finally {
-            deleteDirectory(folder);
         }
     }
 
@@ -250,8 +245,7 @@ public class ShapefileStoreTest {
      * Test adding features to a shapefile.
      */
     @Test
-    public void testAddFeatures() throws URISyntaxException, 
DataStoreException, IOException {
-        final Path folder = Files.createTempDirectory("shapefileTest");
+    public void testAddFeatures(@TempDir final Path folder) throws 
URISyntaxException, DataStoreException, IOException {
         final Path temp = folder.resolve("test.shp");
         try (final ShapefileStore store = new ShapefileStore(temp)) {
             FeatureType type = createType();
@@ -266,8 +260,6 @@ public class ShapefileStoreTest {
             assertEquals(2, result.length);
             assertEquals(feature1, result[0]);
             assertEquals(feature2, result[1]);
-        } finally {
-            deleteDirectory(folder);
         }
     }
 
@@ -275,8 +267,7 @@ public class ShapefileStoreTest {
      * Test remove features from a shapefile.
      */
     @Test
-    public void testRemoveFeatures() throws DataStoreException, IOException {
-        final Path folder = Files.createTempDirectory("shapefileTest");
+    public void testRemoveFeatures(@TempDir final Path folder) throws 
DataStoreException, IOException {
         final Path temp = folder.resolve("test.shp");
         try (final ShapefileStore store = new ShapefileStore(temp)) {
             FeatureType type = createType();
@@ -294,8 +285,6 @@ public class ShapefileStoreTest {
             Object[] result = store.features(false).toArray();
             assertEquals(1, result.length);
             assertEquals(feature2, result[0]);
-        } finally {
-            deleteDirectory(folder);
         }
     }
 
@@ -303,8 +292,7 @@ public class ShapefileStoreTest {
      * Test replacing features in a shapefile.
      */
     @Test
-    public void testReplaceFeatures() throws DataStoreException, IOException {
-        final Path folder = Files.createTempDirectory("shapefileTest");
+    public void testReplaceFeatures(@TempDir final Path folder) throws 
DataStoreException, IOException {
         final Path temp = folder.resolve("test.shp");
         try (final ShapefileStore store = new ShapefileStore(temp)) {
             FeatureType type = createType();
@@ -330,8 +318,6 @@ public class ShapefileStoreTest {
             Feature f1 = (Feature) result[0];
             assertEquals(45, f1.getPropertyValue("id"));
             assertEquals(feature2, result[1]);
-        } finally {
-            deleteDirectory(folder);
         }
     }
 
@@ -368,13 +354,4 @@ public class ShapefileStoreTest {
         feature.setPropertyValue("date", LocalDate.of(2030, 6, 21));
         return feature;
     }
-
-    private static void deleteDirectory(Path path) throws IOException{
-        try (Stream<Path> stream = Files.walk(path)) {
-            stream.sorted(Comparator.reverseOrder())
-                    .map(Path::toFile)
-                    .forEach(File::delete);
-        }
-    }
-
 }

Reply via email to