sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1732179 - in /sis/branches/JDK8: application/sis-console/src/main/java/org/apache/sis/console/ application/sis-console/src/main/resources/org/apache/sis/console/ core/sis-metadata/src/main/java/org/apache/sis/io/wkt/ storage/sis-storage/sr...
Date Wed, 24 Feb 2016 15:56:22 GMT
Author: desruisseaux
Date: Wed Feb 24 15:56:22 2016
New Revision: 1732179

URL: http://svn.apache.org/viewvc?rev=1732179&view=rev
Log:
StorageConnector.getStorageAs(InputStream.class) needs to call InputStream.reset() if the
InputStream has been used previously (SIS-308).
As a side effect of this bug fix, add a "--debug" option to the command-line tools (which
is the place where the problem manifested).

Modified:
    sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/AboutSC.java
    sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/Command.java
    sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
    sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MetadataSC.java
    sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
    sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/Option.java
    sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
    sis/branches/JDK8/application/sis-console/src/main/resources/org/apache/sis/console/Options.properties
    sis/branches/JDK8/application/sis-console/src/main/resources/org/apache/sis/console/Options_fr.properties
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/AbstractParser.java
    sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Element.java
    sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java
    sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreProviderTest.java
    sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreTest.java
    sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreProviderTest.java
    sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java
    sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java

Modified: sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/AboutSC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/AboutSC.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/AboutSC.java
[UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/AboutSC.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -68,7 +68,7 @@ final class AboutSC extends SubCommand {
      */
     AboutSC(final int commandIndex, final String... args) throws InvalidOptionException {
         super(commandIndex, args, EnumSet.of(Option.LOCALE, Option.TIMEZONE, Option.ENCODING,
-                Option.BRIEF, Option.VERBOSE, Option.HELP));
+                Option.BRIEF, Option.VERBOSE, Option.HELP, Option.DEBUG));
     }
 
     /**

Modified: sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/Command.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/Command.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/Command.java
[UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/Command.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -26,6 +26,7 @@ import java.io.IOException;
 import java.sql.SQLException;
 import org.opengis.referencing.operation.TransformException;
 import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.util.ArraysExt;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.logging.MonolineFormatter;
 
@@ -51,6 +52,7 @@ import org.apache.sis.util.logging.Monol
  * <tr><td>{@code --colors}   </td><td>Whether colorized output shall
be enabled.</td></tr>
  * <tr><td>{@code --brief}    </td><td>Whether the output should
contains only brief information.</td></tr>
  * <tr><td>{@code --verbose}  </td><td>Whether the output should
contains more detailed information.</td></tr>
+ * <tr><td>{@code --debug}    </td><td>Prints full stack trace in
case of failure.</td></tr>
  * <tr><td>{@code --help}     </td><td>Lists the options available
for a specific command.</td></tr>
  * </table></blockquote>
  *
@@ -212,16 +214,28 @@ public final class Command {
     /**
      * Prints the message of the given exception. This method is invoked only when the error
occurred before
      * the {@link SubCommand} has been built, otherwise the {@link SubCommand#err} printer
shall be used.
+     *
+     * @param args The command line arguments, used only for detecting if the {@code --debug}
option was present.
      */
-    private static void error(final Exception e) {
+    private static void error(final String[] args, final Exception e) {
+        final boolean debug = ArraysExt.containsIgnoreCase(args, Option.PREFIX + "debug");
         final Console console = System.console();
         if (console != null) {
             final PrintWriter err = console.writer();
-            err.println(e.getLocalizedMessage());
+            if (debug) {
+                e.printStackTrace(err);
+            } else {
+                err.println(e.getLocalizedMessage());
+            }
             err.flush();
         } else {
+            @SuppressWarnings("UseOfSystemOutOrSystemErr")
             final PrintStream err = System.err;
-            err.println(e.getLocalizedMessage());
+            if (debug) {
+                e.printStackTrace(err);
+            } else {
+                err.println(e.getLocalizedMessage());
+            }
             err.flush();
         }
     }
@@ -254,11 +268,11 @@ public final class Command {
         try {
             c = new Command(args);
         } catch (InvalidCommandException e) {
-            error(e);
+            error(args, e);
             System.exit(INVALID_COMMAND_EXIT_CODE);
             return;
         } catch (InvalidOptionException e) {
-            error(e);
+            error(args, e);
             System.exit(INVALID_OPTION_EXIT_CODE);
             return;
         }

Modified: sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
[UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/HelpSC.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -56,7 +56,7 @@ final class HelpSC extends SubCommand {
      * Creates the {@code "help"} sub-command.
      */
     HelpSC(final int commandIndex, final String... args) throws InvalidOptionException {
-        super(commandIndex, args, EnumSet.of(Option.LOCALE, Option.ENCODING, Option.HELP));
+        super(commandIndex, args, EnumSet.of(Option.LOCALE, Option.ENCODING, Option.HELP,
Option.DEBUG));
     }
 
     /**

Modified: sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MetadataSC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MetadataSC.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MetadataSC.java
[UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MetadataSC.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -71,7 +71,7 @@ final class MetadataSC extends SubComman
      */
     MetadataSC(final boolean isCRS, final int commandIndex, final String... args) throws
InvalidOptionException {
         super(commandIndex, args, EnumSet.of(Option.FORMAT, Option.LOCALE, Option.TIMEZONE,
Option.ENCODING,
-                Option.COLORS, Option.HELP));
+                Option.COLORS, Option.HELP, Option.DEBUG));
         this.isCRS = isCRS;
     }
 

Modified: sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
[UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -54,7 +54,7 @@ final class MimeTypeSC extends SubComman
      * Creates the {@code "mime-type"} sub-command.
      */
     MimeTypeSC(final int commandIndex, final String... args) throws InvalidOptionException
{
-        super(commandIndex, args, EnumSet.of(Option.ENCODING, Option.HELP));
+        super(commandIndex, args, EnumSet.of(Option.ENCODING, Option.HELP, Option.DEBUG));
     }
 
     /**

Modified: sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/Option.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/Option.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/Option.java
[UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/Option.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -25,7 +25,7 @@ import org.apache.sis.util.resources.Err
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.3
+ * @version 0.7
  * @module
  */
 enum Option {
@@ -67,6 +67,12 @@ enum Option {
     VERBOSE(false),
 
     /**
+     * Whether to print the full stack trace in case of error.
+     * This option expects no value.
+     */
+    DEBUG(false),
+
+    /**
      * Lists the options accepted by a command.
      */
     HELP(false);

Modified: sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
[UTF-8] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -88,6 +88,11 @@ abstract class SubCommand {
     protected final boolean colors;
 
     /**
+     * {@code true} for printing the full stack trace in case of failure.
+     */
+    protected final boolean debug;
+
+    /**
      * Output stream to the console. This output stream uses the encoding
      * specified by the {@code "--encoding"} argument, if presents.
      */
@@ -124,6 +129,7 @@ abstract class SubCommand {
         this.timezone     = parent.timezone;
         this.encoding     = parent.encoding;
         this.colors       = parent.colors;
+        this.debug        = parent.debug;
         this.out          = parent.out;
         this.err          = parent.err;
         this.outputBuffer = parent.outputBuffer;
@@ -141,6 +147,7 @@ abstract class SubCommand {
      * @param  validOptions The command-line options allowed by this sub-command.
      * @throws InvalidOptionException If an illegal option has been provided, or the option
has an illegal value.
      */
+    @SuppressWarnings("UseOfSystemOutOrSystemErr")
     protected SubCommand(final int commandIndex, final String[] arguments, final EnumSet<Option>
validOptions)
             throws InvalidOptionException
     {
@@ -188,6 +195,8 @@ abstract class SubCommand {
         final Console console;
         final boolean explicitEncoding;
         try {
+            debug = options.containsKey(option = Option.DEBUG);
+
             value = options.get(option = Option.LOCALE);
             locale = (value != null) ? Locales.parse(value) : Locale.getDefault(Locale.Category.DISPLAY);
 
@@ -202,7 +211,7 @@ abstract class SubCommand {
             console = System.console();
             colors = (value != null) ? Option.COLORS.parseBoolean(value) : (console != null)
&& X364.isAnsiSupported();
         } catch (RuntimeException e) {
-            @SuppressWarnings("null") // 'option' has been assigned in 'get' argument.
+            @SuppressWarnings("null")                                   // 'option' has been
assigned in 'get' argument.
             final String name = option.name().toLowerCase(Locale.US);
             throw new InvalidOptionException(Errors.format(Errors.Keys.IllegalOptionValue_2,
name, value), name);
         }
@@ -305,7 +314,11 @@ abstract class SubCommand {
      */
     final void error(final String message, final Exception e) {
         out.flush();
-        err.println(Exceptions.formatChainedMessages(locale, message, e));
+        if (debug) {
+            e.printStackTrace(err);
+        } else {
+            err.println(Exceptions.formatChainedMessages(locale, message, e));
+        }
     }
 
     /**

Modified: sis/branches/JDK8/application/sis-console/src/main/resources/org/apache/sis/console/Options.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/resources/org/apache/sis/console/Options.properties?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/resources/org/apache/sis/console/Options.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/resources/org/apache/sis/console/Options.properties
[ISO-8859-1] Wed Feb 24 15:56:22 2016
@@ -6,4 +6,5 @@ encoding=The encoding to use for the com
 colors=Whether colorized output shall be enabled.
 brief=Reduce the output to only brief information.
 verbose=Request the output to contain more detailed information.
+debug=Prints full stack trace in case of failure.
 help=Lists the options available for a specific command.

Modified: sis/branches/JDK8/application/sis-console/src/main/resources/org/apache/sis/console/Options_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/application/sis-console/src/main/resources/org/apache/sis/console/Options_fr.properties?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/application/sis-console/src/main/resources/org/apache/sis/console/Options_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/application/sis-console/src/main/resources/org/apache/sis/console/Options_fr.properties
[ISO-8859-1] Wed Feb 24 15:56:22 2016
@@ -6,4 +6,5 @@ encoding=L\u2019encodage des caractères
 colors=Indique si l\u2019affichage peut être en couleurs.
 brief=Indique que la sortie de la commande ne doit contenir que de brèves informations.
 verbose=Indique que la sortie de la commande doit contenir des informations plus détaillées.
+debug=Affiche la trace complète de l\u2019exception en cas d\u2019échec.
 help=Liste les options disponibles pour une commande spécifique.

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/AbstractParser.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/AbstractParser.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/AbstractParser.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/AbstractParser.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -36,6 +36,7 @@ import org.opengis.util.InternationalStr
 import org.apache.sis.internal.system.Loggers;
 import org.apache.sis.internal.util.StandardDateFormat;
 import org.apache.sis.measure.Units;
+import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.logging.Logging;
 
 import static org.apache.sis.util.ArgumentChecks.ensureNonNull;
@@ -165,7 +166,7 @@ abstract class AbstractParser implements
         this.unitFormat  = unitFormat;
         this.errorLocale = errorLocale;
         if (Symbols.SCIENTIFIC_NOTATION && numberFormat instanceof DecimalFormat)
{
-            final DecimalFormat decimalFormat = (DecimalFormat) ((DecimalFormat) numberFormat).clone();
+            final DecimalFormat decimalFormat = (DecimalFormat) numberFormat.clone();
             exponentSymbol = decimalFormat.getDecimalFormatSymbols().getExponentSeparator();
             String pattern = decimalFormat.toPattern();
             if (!pattern.contains("E0")) {
@@ -236,6 +237,7 @@ abstract class AbstractParser implements
     public Object parseObject(final String text, final ParsePosition position) throws ParseException
{
         warnings = null;
         ignoredElements.clear();
+        ArgumentChecks.ensureNonEmpty("text", text);
         final Element element = new Element("<root>", new Element(this, text, position,
null));
         final Object object = parseObject(element);
         element.close(ignoredElements);

Modified: sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Element.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Element.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Element.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-metadata/src/main/java/org/apache/sis/io/wkt/Element.java [UTF-8]
Wed Feb 24 15:56:22 2016
@@ -175,7 +175,7 @@ final class Element implements Serializa
         final int length = text.length();
         int lower = skipLeadingWhitespaces(text, offset, length);
         { // This block is for keeping some variables local.
-            int c = text.codePointAt(lower);
+            int c = (lower < length) ? text.codePointAt(lower) : 0;
             if (!Character.isUnicodeIdentifierStart(c)) {
                 keyword = text;
                 position.setErrorIndex(lower);

Modified: sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -369,6 +369,11 @@ public class StorageConnector implements
         if (views != null) {
             final Object view = views.get(type);
             if (view != null) {
+                if (view == storage && view instanceof InputStream) try {
+                    resetInputStream();
+                } catch (IOException e) {
+                    throw new DataStoreException(Errors.format(Errors.Keys.CanNotRead_1,
getStorageName()), e);
+                }
                 return (view != Void.TYPE) ? type.cast(view) : null;
             }
         } else {
@@ -388,7 +393,7 @@ public class StorageConnector implements
             } else if (type == DataInput.class) {
                 createDataInput();
                 done = true;
-            } else if (type == ChannelDataInput.class) { // Undocumented case (SIS internal)
+            } else if (type == ChannelDataInput.class) {                // Undocumented case
(SIS internal)
                 createChannelDataInput(false);
                 done = true;
             }
@@ -416,6 +421,19 @@ public class StorageConnector implements
     }
 
     /**
+     * Assuming that {@link #storage} is an instance of {@link InputStream}, resets its position.
This method
+     * is the converse of the marks performed at the beginning of {@link #createChannelDataInput(boolean)}.
+     */
+    private void resetInputStream() throws IOException {
+        final ChannelDataInput channel = getView(ChannelDataInput.class);
+        if (channel != null) {
+            ((InputStream) storage).reset();        // May throw an exception if mark is
unsupported.
+            channel.buffer.limit(0);                // Must be after storage.reset().
+            channel.setStreamPosition(0);           // Must be after buffer.limit(0).
+        }
+    }
+
+    /**
      * Creates a view for the input as a {@link ChannelDataInput} if possible.
      * If the view can not be created, remember that fact in order to avoid new attempts.
      *
@@ -423,8 +441,22 @@ public class StorageConnector implements
      * @throws IOException If an error occurred while opening a channel for the input.
      */
     private void createChannelDataInput(final boolean asImageInputStream) throws IOException
{
+        /*
+         * Before to try to open an InputStream, mark its position so we can rewind if the
user asks for
+         * the InputStream directly. We need to reset because ChannelDataInput may have read
some bytes.
+         * Note that if mark is unsupported, the default InputStream.mark() implementation
does nothing.
+         * See above 'resetInputStream()' method.
+         */
+        if (storage instanceof InputStream) {
+            ((InputStream) storage).mark(DEFAULT_BUFFER_SIZE);
+        }
+        /*
+         * Following method call recognizes ReadableByteChannel, InputStream (with special
case for FileInputStream),
+         * URL, URI, File, Path or other types that may be added in future SIS versions.
+         */
         final ReadableByteChannel channel = IOUtilities.open(storage,
                 getOption(OptionKey.URL_ENCODING), getOption(OptionKey.OPEN_OPTIONS));
+
         ChannelDataInput asDataInput = null;
         if (channel != null) {
             addViewToClose(channel, storage);
@@ -475,7 +507,7 @@ public class StorageConnector implements
                 // No call to 'addViewToClose' because the instance already exists.
             } else {
                 asDataInput = new ChannelImageInputStream(c);
-                if (views.put(ChannelDataInput.class, asDataInput) != c) { // Replace the
previous instance.
+                if (views.put(ChannelDataInput.class, asDataInput) != c) {          // Replace
the previous instance.
                     throw new ConcurrentModificationException();
                 }
                 addViewToClose(asDataInput, c.channel);
@@ -595,13 +627,19 @@ public class StorageConnector implements
             final DataInput input = getStorageAs(DataInput.class);
             return (input instanceof ImageInputStream) ? input : null;
         }
+        /*
+         * If the user asked an InputStream, we may return the storage as-is if it was already
an InputStream.
+         * However before doing so, we may need to reset the InputStream position if the
stream has been used
+         * by a ChannelDataInput.
+         */
         if (type == InputStream.class) {
             if (storage instanceof InputStream) {
+                resetInputStream();
                 return storage;
             }
             final DataInput input = getStorageAs(DataInput.class);
             if (input instanceof InputStream) {
-                return (InputStream) input;
+                return input;
             }
             if (input instanceof ImageInputStream) {
                 final InputStream c = new InputStreamAdapter((ImageInputStream) input);

Modified: sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreProviderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreProviderTest.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreProviderTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreProviderTest.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -20,6 +20,7 @@ import java.io.StringReader;
 import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.ProbeResult;
+import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -34,6 +35,7 @@ import static org.junit.Assert.*;
  * @version 0.7
  * @module
  */
+@DependsOn(org.apache.sis.storage.StorageConnectorTest.class)
 public final strictfp class StoreProviderTest extends TestCase {
     /**
      * Verifies validity of {@code StoreProvider.MIN_LENGTH} and {@code StoreProvider.MAX_LENGTH}
constants
@@ -55,7 +57,7 @@ public final strictfp class StoreProvide
     /**
      * Tests {@link StoreProvider#probeContent(StorageConnector)} method from a {@link Reader}
object.
      *
-     * @throws DataStoreException Should never happen.
+     * @throws DataStoreException if en error occurred while reading the WKT.
      */
     @Test
     public void testProbeContentFromReader() throws DataStoreException {

Modified: sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreTest.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreTest.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -17,17 +17,22 @@
 package org.apache.sis.internal.storage.wkt;
 
 import java.io.StringReader;
+import java.io.ByteArrayInputStream;
 import org.opengis.metadata.Metadata;
 import org.opengis.referencing.crs.GeographicCRS;
 import org.opengis.referencing.cs.AxisDirection;
 import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.test.TestUtilities;
+import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
 import static org.opengis.test.Assert.*;
 
+// Branch-dependent imports
+import java.nio.charset.StandardCharsets;
+
 
 /**
  * Tests the WKT {@link Store}.
@@ -37,34 +42,61 @@ import static org.opengis.test.Assert.*;
  * @version 0.7
  * @module
  */
+@DependsOn(StoreProviderTest.class)
 public final strictfp class StoreTest extends TestCase {
     /**
-     * The WKT to parse.
+     * The WKT to parse. This WKT uses US-ASCII characters only.
      */
     public static final String WKT =
-            "GEOGCS[“NTF (Paris)”,\n" +
-            "  DATUM[“Nouvelle Triangulation Française (Paris)”,\n" +
-            "    SPHEROID[“Clarke 1880 (IGN)”, 6378249.2, 293.4660212936269]],\n" +
-            "    PRIMEM[“Paris”, 2.5969213, AUTHORITY[“EPSG”, “8903”]],\n" +
-            "  UNIT[“grade”, 0.015707963267948967],\n" +
-            "  AXIS[“Latitude”, NORTH],\n" +
-            "  AXIS[“Longitude”, EAST]]";
+            "GEOGCS[\"NTF (Paris)\",\n" +
+            "  DATUM[\"Nouvelle Triangulation Francaise (Paris)\",\n" +
+            "    SPHEROID[\"Clarke 1880 (IGN)\", 6378249.2, 293.4660212936269]],\n" +
+            "    PRIMEM[\"Paris\", 2.5969213, AUTHORITY[\"EPSG\", \"8903\"]],\n" +
+            "  UNIT[\"grade\", 0.015707963267948967],\n" +
+            "  AXIS[\"Latitude\", NORTH],\n" +
+            "  AXIS[\"Longitude\", EAST]]";
+
+    /**
+     * Validates the parsed CRS.
+     */
+    private static void validate(final GeographicCRS crs) {
+        assertEquals("NTF (Paris)", crs.getName().getCode());
+        assertEquals("Nouvelle Triangulation Francaise (Paris)", crs.getDatum().getName().getCode());
+        assertAxisDirectionsEqual("EllipsoidalCS", crs.getCoordinateSystem(), AxisDirection.NORTH,
AxisDirection.EAST);
+    }
 
     /**
-     * Tests {@link Store#getMetadata()}.
+     * Tests {@link Store#getMetadata()} reading from a {@link Reader}.
      *
      * @throws DataStoreException If an error occurred while reading the metadata.
      */
     @Test
-    public void testMetadata() throws DataStoreException {
+    public void testFromReader() throws DataStoreException {
         final Metadata metadata;
         try (Store store = new Store(new StorageConnector(new StringReader(WKT)))) {
             metadata = store.getMetadata();
             assertSame("Expected cached value.", metadata, store.getMetadata());
         }
-        final GeographicCRS crs = (GeographicCRS) TestUtilities.getSingleton(metadata.getReferenceSystemInfo());
-        assertEquals("NTF (Paris)", crs.getName().getCode());
-        assertEquals("Nouvelle Triangulation Française (Paris)", crs.getDatum().getName().getCode());
-        assertAxisDirectionsEqual("EllipsoidalCS", crs.getCoordinateSystem(), AxisDirection.NORTH,
AxisDirection.EAST);
+        validate((GeographicCRS) TestUtilities.getSingleton(metadata.getReferenceSystemInfo()));
+    }
+
+    /**
+     * Tests {@link StoreProvider#probeContent(StorageConnector)} followed by {@link Store#getMetadata()}
+     * reading from an {@link InputStream}. This method tests indirectly {@link StorageConnector}
capability
+     * to reset the {@code InputStream} to its original position after {@code probeContent(…)}.
+     *
+     * @throws DataStoreException if en error occurred while reading the WKT.
+     */
+    @Test
+    public void testFromInputStream() throws DataStoreException {
+        final Metadata metadata;
+        final StoreProvider p = new StoreProvider();
+        final StorageConnector c = new StorageConnector(new ByteArrayInputStream(StoreTest.WKT.getBytes(StandardCharsets.US_ASCII)));
+        assertTrue("isSupported", p.probeContent(c).isSupported());
+        try (Store store = new Store(c)) {
+            metadata = store.getMetadata();
+            assertSame("Expected cached value.", metadata, store.getMetadata());
+        }
+        validate((GeographicCRS) TestUtilities.getSingleton(metadata.getReferenceSystemInfo()));
     }
 }

Modified: sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreProviderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreProviderTest.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreProviderTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreProviderTest.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -20,6 +20,7 @@ import java.io.StringReader;
 import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.storage.ProbeResult;
+import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -34,11 +35,12 @@ import static org.junit.Assert.*;
  * @version 0.7
  * @module
  */
+@DependsOn(org.apache.sis.storage.StorageConnectorTest.class)
 public final strictfp class StoreProviderTest extends TestCase {
     /**
      * Tests {@link StoreProvider#probeContent(StorageConnector)} method from a {@link Reader}
object.
      *
-     * @throws DataStoreException Should never happen.
+     * @throws DataStoreException if en error occurred while reading the XML.
      */
     @Test
     public void testProbeContentFromReader() throws DataStoreException {

Modified: sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -23,6 +23,7 @@ import org.opengis.metadata.citation.*;
 import org.apache.sis.xml.Namespaces;
 import org.apache.sis.storage.StorageConnector;
 import org.apache.sis.storage.DataStoreException;
+import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -41,6 +42,7 @@ import java.nio.charset.StandardCharsets
  * @version 0.7
  * @module
  */
+@DependsOn(org.apache.sis.storage.StorageConnectorTest.class)
 public final strictfp class StoreTest extends TestCase {
     /**
      * The metadata to unmarshal.
@@ -86,7 +88,7 @@ public final strictfp class StoreTest ex
     /**
      * Tests {@link Store#getMetadata()}.
      *
-     * @throws DataStoreException If an error occurred while reading the metadata.
+     * @throws DataStoreException if en error occurred while reading the XML.
      */
     @Test
     public void testMetadata() throws DataStoreException {

Modified: sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java?rev=1732179&r1=1732178&r2=1732179&view=diff
==============================================================================
--- sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java
[UTF-8] Wed Feb 24 15:56:22 2016
@@ -41,9 +41,10 @@ import static org.opengis.test.Assert.*;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.4
+ * @version 0.7
  * @module
  */
+@SuppressWarnings("OverlyStrongTypeCast")
 @DependsOn(org.apache.sis.internal.storage.ChannelImageInputStreamTest.class)
 public final strictfp class StorageConnectorTest extends TestCase {
     /**
@@ -55,7 +56,7 @@ public final strictfp class StorageConne
      * Creates the instance to test. This method uses the {@code StorageConnectorTest} compiled
      * class file as the resource to test. The resource can be provided either as a URL or
as a stream.
      */
-    private StorageConnector create(final boolean asStream) {
+    private static StorageConnector create(final boolean asStream) {
         final Class<?> c = StorageConnectorTest.class;
         final String name = c.getSimpleName() + ".class";
         final Object storage = asStream ? c.getResourceAsStream(name) : c.getResource(name);
@@ -86,7 +87,7 @@ public final strictfp class StorageConne
     /**
      * Tests the {@link StorageConnector#getStorageAs(Class)} method for the {@link String}
type.
      *
-     * @throws DataStoreException Should never happen.
+     * @throws DataStoreException if an error occurred while using the storage connector.
      * @throws IOException Should never happen.
      */
     @Test
@@ -99,8 +100,8 @@ public final strictfp class StorageConne
      * Tests the {@link StorageConnector#getStorageAs(Class)} method for the I/O types.
      * The initial storage object is a {@link java.net.URL}.
      *
-     * @throws DataStoreException Should never happen.
-     * @throws IOException If an error occurred while reading the test file.
+     * @throws DataStoreException if an error occurred while using the storage connector.
+     * @throws IOException if an error occurred while reading the test file.
      */
     @Test
     public void testGetAsDataInputFromURL() throws DataStoreException, IOException {
@@ -111,8 +112,8 @@ public final strictfp class StorageConne
      * Tests the {@link StorageConnector#getStorageAs(Class)} method for the I/O types.
      * The initial storage object is an {@link java.io.InputStream}.
      *
-     * @throws DataStoreException Should never happen.
-     * @throws IOException If an error occurred while reading the test file.
+     * @throws DataStoreException if an error occurred while using the storage connector.
+     * @throws IOException if an error occurred while reading the test file.
      */
     @Test
     public void testGetAsDataInputFromStream() throws DataStoreException, IOException {
@@ -143,8 +144,8 @@ public final strictfp class StorageConne
      * Tests the {@link StorageConnector#getStorageAs(Class)} method for the {@link ImageInputStream}
type.
      * This is basically a synonymous of {@code getStorageAs(DataInput.class)}.
      *
-     * @throws DataStoreException Should never happen.
-     * @throws IOException If an error occurred while reading the test file.
+     * @throws DataStoreException if an error occurred while using the storage connector.
+     * @throws IOException if an error occurred while reading the test file.
      */
     @Test
     public void testGetAsImageInputStream() throws DataStoreException, IOException {
@@ -156,20 +157,47 @@ public final strictfp class StorageConne
 
     /**
      * Tests the {@link StorageConnector#getStorageAs(Class)} method for the {@link InputStream}
type.
+     * The {@code InputStream} was specified directly to the {@link StorageConnector} constructor.
      *
-     * @throws DataStoreException Should never happen.
-     * @throws IOException If an error occurred while reading the test file.
+     * @throws DataStoreException if an error occurred while using the storage connector.
+     * @throws IOException if an error occurred while reading the test file.
      */
     @Test
     @DependsOnMethod("testGetAsImageInputStream")
-    public void testGetAsInputStream() throws DataStoreException, IOException {
-        StorageConnector connection = create(true);
-        InputStream in = connection.getStorageAs(InputStream.class);
-        assertSame(connection.getStorage(), in);
+    public void testGetOriginalInputStream() throws DataStoreException, IOException {
+        final StorageConnector connection = create(true);
+        final InputStream in = connection.getStorageAs(InputStream.class);
+        assertSame("The InputStream shall be the one specified to the constructor.", connection.getStorage(),
in);
+        /*
+         * Ask a different type and request a few bytes. We do not test the ImageInputStream
type here as this is
+         * not the purpose of this method. But we need a different type before to request
again the InputStream.
+         */
+        final ImageInputStream data = connection.getStorageAs(ImageInputStream.class);
+        final byte[] sample = new byte[32];
+        data.readFully(sample);
+        /*
+         * Request again the InputStream and read the same amount of bytes than above. The
intend of this test
+         * is to verify that StorageConnector has reseted the InputStream position before
to return it.
+         */
+        assertSame(in, connection.getStorageAs(InputStream.class));
+        final byte[] actual = new byte[sample.length];
+        assertEquals("Should read all requested bytes.", actual.length, in.read(actual));
+        assertArrayEquals("InputStream shall be reseted to the beginning of the stream.",
sample, actual);
         connection.closeAllExcept(null);
+    }
 
-        connection = create(false);
-        in = connection.getStorageAs(InputStream.class);
+    /**
+     * Tests the {@link StorageConnector#getStorageAs(Class)} method for the {@link InputStream}
type.
+     * The {@code InputStream} was specified as a URL.
+     *
+     * @throws DataStoreException if an error occurred while using the storage connector.
+     * @throws IOException if an error occurred while reading the test file.
+     */
+    @Test
+    @DependsOnMethod("testGetAsImageInputStream")
+    public void testGetAsInputStream() throws DataStoreException, IOException {
+        final StorageConnector connection = create(false);
+        final InputStream in = connection.getStorageAs(InputStream.class);
         assertNotSame(connection.getStorage(), in);
         assertSame("Expected cached value.", in, connection.getStorageAs(InputStream.class));
         assertInstanceOf("Expected Channel backend", InputStreamAdapter.class, in);
@@ -186,13 +214,13 @@ public final strictfp class StorageConne
     /**
      * Tests the {@link StorageConnector#getStorageAs(Class)} method for the {@link Reader}
type.
      *
-     * @throws DataStoreException Should never happen.
-     * @throws IOException If an error occurred while reading the test file.
+     * @throws DataStoreException if an error occurred while using the storage connector.
+     * @throws IOException if an error occurred while reading the test file.
      */
     @Test
     @DependsOnMethod("testGetAsInputStream")
     public void testGetAsReader() throws DataStoreException, IOException {
-        StorageConnector connection = create(true);
+        final StorageConnector connection = create(true);
         final Reader in = connection.getStorageAs(Reader.class);
         assertSame("Expected cached value.", in, connection.getStorageAs(Reader.class));
         connection.closeAllExcept(null);
@@ -204,8 +232,8 @@ public final strictfp class StorageConne
      * the Image I/O classes. However after a call to {@code getStorageAt(ChannelImageInputStream.class)},
the type
      * should have been promoted.
      *
-     * @throws DataStoreException Should never happen.
-     * @throws IOException If an error occurred while reading the test file.
+     * @throws DataStoreException if an error occurred while using the storage connector.
+     * @throws IOException if an error occurred while reading the test file.
      */
     @Test
     public void testGetAsChannelDataInput() throws DataStoreException, IOException {
@@ -229,8 +257,8 @@ public final strictfp class StorageConne
      * Tests the {@link StorageConnector#getStorageAs(Class)} method for the {@link ByteBuffer}
type.
      * This method uses the same test file than {@link #testGetAsDataInputFromURL()}.
      *
-     * @throws DataStoreException Should never happen.
-     * @throws IOException If an error occurred while reading the test file.
+     * @throws DataStoreException if an error occurred while using the storage connector.
+     * @throws IOException if an error occurred while reading the test file.
      */
     @Test
     @DependsOnMethod("testGetAsDataInputFromURL")
@@ -249,8 +277,8 @@ public final strictfp class StorageConne
      * that the buffer created in this test will not be used for the "real" reading process
in the data store.
      * Consequently, it should be a smaller, only temporary, buffer.
      *
-     * @throws DataStoreException Should never happen.
-     * @throws IOException If an error occurred while reading the test file.
+     * @throws DataStoreException if an error occurred while using the storage connector.
+     * @throws IOException if an error occurred while reading the test file.
      */
     @Test
     @DependsOnMethod("testGetAsDataInputFromStream")
@@ -271,7 +299,7 @@ public final strictfp class StorageConne
     /**
      * Tests the {@link StorageConnector#getStorageAs(Class)} method for the {@link Connection}
type.
      *
-     * @throws DataStoreException Should never happen.
+     * @throws DataStoreException if an error occurred while using the storage connector.
      * @throws IOException Should never happen.
      */
     public void testGetAsConnection() throws DataStoreException, IOException {
@@ -283,8 +311,8 @@ public final strictfp class StorageConne
     /**
      * Tests the {@link StorageConnector#closeAllExcept(Object)} method.
      *
-     * @throws DataStoreException Should never happen.
-     * @throws IOException If an error occurred while reading the test file.
+     * @throws DataStoreException if an error occurred while using the storage connector.
+     * @throws IOException if an error occurred while reading the test file.
      */
     @Test
     @DependsOnMethod("testGetAsDataInputFromStream")




Mime
View raw message