sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1515754 - in /sis/branches/JDK7: application/sis-console/src/main/java/org/apache/sis/console/ core/sis-utility/src/main/java/org/apache/sis/util/resources/ core/sis-utility/src/test/java/org/apache/sis/test/suite/ core/sis-utility/src/tes...
Date Tue, 20 Aug 2013 09:54:30 GMT
Author: desruisseaux
Date: Tue Aug 20 09:54:30 2013
New Revision: 1515754

URL: http://svn.apache.org/r1515754
Log:
Added test and more accurate detection of MIME type in the command-line application.

Added:
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ExceptionsTest.java
  (with props)
Modified:
    sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
    sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
    sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
    sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
    sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
    sis/branches/JDK7/ide-project/NetBeans/build.xml
    sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/IOUtilities.java

Modified: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java?rev=1515754&r1=1515753&r2=1515754&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
[UTF-8] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/Command.java
[UTF-8] Tue Aug 20 09:54:30 2013
@@ -24,7 +24,6 @@ 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.Exceptions;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.util.logging.MonolineFormatter;
 
@@ -177,8 +176,7 @@ public final class Command {
         } else try {
             return command.run();
         } catch (Exception e) {
-            command.out.flush();
-            command.err.println(Exceptions.formatChainedMessages(command.locale, null, e));
+            command.error(null, e);
             throw e;
         }
         return 0;
@@ -225,7 +223,8 @@ public final class Command {
      *
      * @param args Command-line options.
      */
-    public static void main(final String[] args) {
+    public static void main(String[] args) {
+        args = new String[] {"mime-type", "/Users/desruisseaux/Projets/SIS/JDK7/target/binaries/../../../../GeoAPI/trunk//geoapi-netcdf/src/test/resources/org/opengis/wrapper/netcdf/NCEP-SST.nc"};
         MonolineFormatter.install();
         final Command c;
         try {

Modified: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java?rev=1515754&r1=1515753&r2=1515754&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
[UTF-8] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
[UTF-8] Tue Aug 20 09:54:30 2013
@@ -16,8 +16,10 @@
  */
 package org.apache.sis.console;
 
+import java.net.URI;
 import java.util.EnumSet;
 import java.io.IOException;
+import java.net.URISyntaxException;
 import org.apache.sis.storage.DataStores;
 import org.apache.sis.storage.DataStoreException;
 import org.apache.sis.util.resources.Errors;
@@ -26,7 +28,7 @@ import org.apache.sis.util.resources.Err
 import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.nio.file.NoSuchFileException;
-import java.nio.file.InvalidPathException;
+import java.nio.file.FileSystemNotFoundException;
 
 
 /**
@@ -54,18 +56,29 @@ final class MimeTypeSC extends SubComman
      * @throws IOException If an error occurred while reading the file.
      */
     @Override
-    public int run() throws InvalidOptionException, IOException, DataStoreException {
+    public int run() throws InvalidOptionException, IOException, DataStoreException, URISyntaxException
{
         if (hasUnexpectedFileCount(1, 1)) {
             return Command.INVALID_ARGUMENT_EXIT_CODE;
         }
         final String file = files.get(0);
-        String type;
+        final URI uri;
         try {
-            type = Files.probeContentType(Paths.get(file));
-        } catch (InvalidPathException e) {
+            uri = new URI(file);
+        } catch (URISyntaxException e) {
+            canNotOpen(0, e);
+            return Command.IO_EXCEPTION_EXIT_CODE;
+        }
+        String type;
+        if (!uri.isAbsolute()) {
+            // If the URI is not absolute, we will not be able to convert to Path.
+            // Open as a String, leaving the conversion to DataStore implementations.
             type = DataStores.probeContentType(file);
+        } else try {
+            type = Files.probeContentType(Paths.get(uri));
+        } catch (IllegalArgumentException | FileSystemNotFoundException e) {
+            type = DataStores.probeContentType(uri);
         } catch (NoSuchFileException e) {
-            err.println(Errors.format(Errors.Keys.CanNotOpen_1, file));
+            error(Errors.format(Errors.Keys.CanNotOpen_1, uri), e);
             return Command.IO_EXCEPTION_EXIT_CODE;
         }
         if (type != null) {

Modified: sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java?rev=1515754&r1=1515753&r2=1515754&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
[UTF-8] (original)
+++ sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/SubCommand.java
[UTF-8] Tue Aug 20 09:54:30 2013
@@ -28,6 +28,7 @@ import java.io.StringWriter;
 import java.io.OutputStreamWriter;
 import java.nio.charset.Charset;
 import org.apache.sis.util.Locales;
+import org.apache.sis.util.Exceptions;
 import org.apache.sis.util.resources.Errors;
 import org.apache.sis.internal.util.X364;
 
@@ -278,6 +279,27 @@ abstract class SubCommand {
     }
 
     /**
+     * Prints the "<cite>Can not open …</cite>" error message followed by the
message in the given exception.
+     *
+     * @param fileIndex Index in the {@link #files} list of the file that can not be opened.
+     * @param e The exception which occurred.
+     */
+    final void canNotOpen(final int fileIndex, final Exception e) {
+        error(Errors.format(Errors.Keys.CanNotOpen_1, files.get(fileIndex)), e);
+    }
+
+    /**
+     * Prints the given error message followed by the message in the given exception.
+     *
+     * @param message The message to print before the exception, or {@code null}.
+     * @param e The exception which occurred.
+     */
+    final void error(final String message, final Exception e) {
+        out.flush();
+        err.println(Exceptions.formatChainedMessages(locale, message, e));
+    }
+
+    /**
      * Shows the help instructions for a specific command. This method is invoked
      * instead of {@link #run()} if the the user provided the {@code --help} option.
      *

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java?rev=1515754&r1=1515753&r2=1515754&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] Tue Aug 20 09:54:30 2013
@@ -316,6 +316,11 @@ public final class Errors extends Indexe
         public static final int MissingRequiredModule_1 = 84;
 
         /**
+         * Missing scheme in URI.
+         */
+        public static final int MissingSchemeInURI = 109;
+
+        /**
          * Missing value for option “{0}”.
          */
         public static final int MissingValueForOption_1 = 99;

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties?rev=1515754&r1=1515753&r2=1515754&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] Tue Aug 20 09:54:30 2013
@@ -74,6 +74,7 @@ MismatchedCRS                   = The co
 MismatchedDimension_2           = Mismatched object dimension: {0}D and {1}D.
 MismatchedDimension_3           = Argument \u2018{0}\u2019 has {2} dimension{2,choice,1#|2#s},
while {1} was expected.
 MissingRequiredModule_1         = This operation requires the \u201c{0}\u201d module.
+MissingSchemeInURI              = Missing scheme in URI.
 MissingValueForOption_1         = Missing value for option \u201c{0}\u201d.
 MissingValueForProperty_1       = Missing value for property \u201c{0}\u201d.
 MissingValueInColumn_1          = Missing value in the \u201c{0}\u201d column.

Modified: sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties?rev=1515754&r1=1515753&r2=1515754&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK7/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] Tue Aug 20 09:54:30 2013
@@ -64,6 +64,7 @@ MismatchedCRS                   = Le sys
 MismatchedDimension_2           = Les dimensions des objets ({0}D et {1}D) ne concordent
pas.
 MismatchedDimension_3           = L\u2019argument \u2018{0}\u2019 a {2} dimension{2,choice,1#|2#s},
alors qu\u2019on en attendait {1}.
 MissingRequiredModule_1         = Cette op\u00e9ration requiert le module \u201c{0}\u201d.
+MissingSchemeInURI              = Il manque le sch\u00e9ma d\u2019URI.
 MissingValueForOption_1         = Aucune valeur n\u2019a \u00e9t\u00e9 d\u00e9finie pour
l\u2019option \u201c{0}\u201d.
 MissingValueForProperty_1       = Aucune valeur n\u2019a \u00e9t\u00e9 d\u00e9finie pour
la propri\u00e9t\u00e9 \u201c{0}\u201d.
 MissingValueInColumn_1          = Il manque une valeur dans la colonne \u201c{0}\u201d.

Modified: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java?rev=1515754&r1=1515753&r2=1515754&view=diff
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] Tue Aug 20 09:54:30 2013
@@ -26,7 +26,7 @@ import org.junit.BeforeClass;
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
- * @version 0.3
+ * @version 0.4
  * @module
  */
 @Suite.SuiteClasses({
@@ -41,6 +41,7 @@ import org.junit.BeforeClass;
     org.apache.sis.util.CharactersTest.class,
     org.apache.sis.util.CharSequencesTest.class,
     org.apache.sis.util.StringBuildersTest.class,
+    org.apache.sis.util.ExceptionsTest.class,
     org.apache.sis.util.UtilitiesTest.class,
     org.apache.sis.util.NumbersTest.class,
     org.apache.sis.util.ClassesTest.class,

Added: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ExceptionsTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ExceptionsTest.java?rev=1515754&view=auto
==============================================================================
--- sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ExceptionsTest.java
(added)
+++ sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ExceptionsTest.java
[UTF-8] Tue Aug 20 09:54:30 2013
@@ -0,0 +1,64 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.util;
+
+import java.util.Locale;
+import java.io.FileNotFoundException;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests the {@link Exceptions} utility methods.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.4
+ * @version 0.4
+ * @module
+ */
+public final strictfp class ExceptionsTest extends TestCase {
+    /**
+     * Tests {@link Exceptions#formatChainedMessages(Locale, String, Throwable)}.
+     */
+    @Test
+    public void testFormatChainedMessages() {
+        final FileNotFoundException cause = new FileNotFoundException("MisingFile.txt");
+        cause.initCause(new Exception("Disk is not mounted."));
+        final Exception e = new Exception("Can not find “MisingFile.txt”.", cause);
+        /*
+         * The actual sequence of messages (with their cause is):
+         *
+         *    Can not find “MisingFile.txt”
+         *    MisingFile.txt
+         *    Disk is not mounted.
+         *
+         * But the second line shall be omitted because it duplicates the first line.
+         */
+        assertEquals("Can not find “MisingFile.txt”.\n" +
+                     "Disk is not mounted.",
+                     Exceptions.formatChainedMessages(Locale.ENGLISH, null, e));
+        /*
+         * Test again with a header.
+         */
+        assertEquals("Error while creating the data store.\n" +
+                     "Can not find “MisingFile.txt”.\n" +
+                     "Disk is not mounted.",
+                     Exceptions.formatChainedMessages(Locale.ENGLISH, "Error while creating
the data store.", e));
+    }
+}

Propchange: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ExceptionsTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/core/sis-utility/src/test/java/org/apache/sis/util/ExceptionsTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/ide-project/NetBeans/build.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/ide-project/NetBeans/build.xml?rev=1515754&r1=1515753&r2=1515754&view=diff
==============================================================================
--- sis/branches/JDK7/ide-project/NetBeans/build.xml (original)
+++ sis/branches/JDK7/ide-project/NetBeans/build.xml Tue Aug 20 09:54:30 2013
@@ -48,6 +48,13 @@
       </fileset>
     </copy>
 
+    <!-- FileTypeDetector implementations to be loaded by ServiceLoader. -->
+    <concat destfile="${build.classes.dir}/META-INF/services/java.nio.file.spi.FileTypeDetector"
encoding="UTF-8" fixlastline="yes">
+      <fileset dir="${project.root}">
+        <include name="*/*/src/main/resources/META-INF/services/java.nio.file.spi.FileTypeDetector"/>
+      </fileset>
+    </concat>
+
     <!-- ObjectConverter implementations to be loaded by ServiceLoader. -->
     <concat destfile="${build.classes.dir}/META-INF/services/org.apache.sis.util.ObjectConverter"
encoding="UTF-8" fixlastline="yes">
       <fileset dir="${project.root}">

Modified: sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/IOUtilities.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/IOUtilities.java?rev=1515754&r1=1515753&r2=1515754&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/IOUtilities.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/IOUtilities.java
[UTF-8] Tue Aug 20 09:54:30 2013
@@ -484,12 +484,16 @@ public final class IOUtilities extends S
              * URL uses HTTP or FTP protocols, because JDK7 does not provide file systems
for them by default.
              */
             final URI uri = (URI) input;
-            try {
+            if (!uri.isAbsolute()) {
+                // All methods invoked in this block throws IllegalArgumentException if the
URI has no scheme,
+                // so we are better to check now and provide a more appropriate exception
for this method.
+                throw new IOException(Errors.format(Errors.Keys.MissingSchemeInURI));
+            } else try {
                 input = Paths.get(uri);
             } catch (IllegalArgumentException | FileSystemNotFoundException e) {
                 try {
                     input = uri.toURL();
-                } catch (IOException ioe) {
+                } catch (MalformedURLException ioe) {
                     ioe.addSuppressed(e);
                     throw ioe;
                 }



Mime
View raw message