Return-Path: X-Original-To: apmail-sis-commits-archive@www.apache.org Delivered-To: apmail-sis-commits-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id C6FB810C2F for ; Tue, 20 Aug 2013 09:55:01 +0000 (UTC) Received: (qmail 66030 invoked by uid 500); 20 Aug 2013 09:55:01 -0000 Delivered-To: apmail-sis-commits-archive@sis.apache.org Received: (qmail 65978 invoked by uid 500); 20 Aug 2013 09:54:58 -0000 Mailing-List: contact commits-help@sis.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: sis-dev@sis.apache.org Delivered-To: mailing list commits@sis.apache.org Received: (qmail 65971 invoked by uid 99); 20 Aug 2013 09:54:56 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 20 Aug 2013 09:54:56 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Tue, 20 Aug 2013 09:54:52 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 2E87B238890D; Tue, 20 Aug 2013 09:54:31 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit 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 -0000 To: commits@sis.apache.org From: desruisseaux@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20130820095431.2E87B238890D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 "Can not open …" 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 @@ + + + + + + + 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; }