You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@sis.apache.org by de...@apache.org on 2023/07/13 15:22:09 UTC

[sis] branch geoapi-4.0 updated: Ensure that `InputStream` marks are supported in a test that need them. This is not always the case in a JPMS context.

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


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new c25bbe6e9c Ensure that `InputStream` marks are supported in a test that need them. This is not always the case in a JPMS context.
c25bbe6e9c is described below

commit c25bbe6e9c6114913a9bfa64bc57dbe589b6699f
Author: Martin Desruisseaux <ma...@geomatys.com>
AuthorDate: Thu Jul 13 11:08:31 2023 +0200

    Ensure that `InputStream` marks are supported in a test that need them.
    This is not always the case in a JPMS context.
---
 .../apache/sis/storage/StorageConnectorTest.java   | 23 ++++++++++++++++++++--
 1 file changed, 21 insertions(+), 2 deletions(-)

diff --git a/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java b/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java
index 4980470d1b..041294e69d 100644
--- a/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java
+++ b/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java
@@ -19,6 +19,7 @@ package org.apache.sis.storage;
 import java.net.URI;
 import java.io.DataInput;
 import java.io.InputStream;
+import java.io.BufferedInputStream;
 import java.io.Reader;
 import java.io.IOException;
 import java.nio.file.Path;
@@ -48,7 +49,7 @@ import static org.opengis.test.Assert.assertInstanceOf;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @author  Alexis Manin (Geomatys)
- * @version 1.2
+ * @version 1.4
  * @since   0.3
  */
 @DependsOn(org.apache.sis.internal.storage.io.ChannelImageInputStreamTest.class)
@@ -70,13 +71,31 @@ public final class StorageConnectorTest extends TestCase {
      */
     private static final int MAGIC_NUMBER = ('T' << 24) | ('h' << 16) | ('e' << 8) | ' ';
 
+    /**
+     * Ensures that the given input stream supports marks. This check is needed because
+     * {@link Class#getResourceAsStream(String)} may return different classes of stream
+     * depending on the context. For example on Java 20 with observed that the resource
+     * from unnamed module was returned as {@link BufferedInputStream}, while the same
+     * resource was returned as {@code sun.nio.ch.ChannelInputStream} after the module
+     * became a named one.
+     *
+     * @param  in  the input stream to check.
+     * @return the input stream with mark support.
+     */
+    private static InputStream withMarkSupport(InputStream in) {
+        if (in != null && !in.markSupported()) {
+            in = new BufferedInputStream(in);
+        }
+        return in;
+    }
+
     /**
      * Creates the instance to test. This method uses the {@code "Any.txt"} ASCII file as
      * the resource to test. The resource can be provided either as a URL or as a stream.
      */
     static StorageConnector create(final boolean asStream) {
         final Class<?> c = StorageConnectorTest.class;
-        final Object storage = asStream ? c.getResourceAsStream(FILENAME) : c.getResource(FILENAME);
+        final Object storage = asStream ? withMarkSupport(c.getResourceAsStream(FILENAME)) : c.getResource(FILENAME);
         assertNotNull(storage);
         final StorageConnector connector = new StorageConnector(storage);
         connector.setOption(OptionKey.ENCODING, StandardCharsets.US_ASCII);