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 2013/08/28 12:16:26 UTC
svn commit: r1518142 - in /sis/branches/JDK7: application/sis-console/
application/sis-console/src/main/java/org/apache/sis/console/
application/sis-console/src/test/java/org/apache/sis/console/
application/sis-console/src/test/java/org/apache/sis/test...
Author: desruisseaux
Date: Wed Aug 28 10:16:25 2013
New Revision: 1518142
URL: http://svn.apache.org/r1518142
Log:
Added tests and fixed a NullPointerException identified by those tests.
Added:
sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeSCTest.java (with props)
Modified:
sis/branches/JDK7/application/sis-console/pom.xml
sis/branches/JDK7/application/sis-console/src/main/java/org/apache/sis/console/MimeTypeSC.java
sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/test/suite/ConsoleTestSuite.java
sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/IOUtilities.java
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/IOUtilitiesTest.java
Modified: sis/branches/JDK7/application/sis-console/pom.xml
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/pom.xml?rev=1518142&r1=1518141&r2=1518142&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/pom.xml (original)
+++ sis/branches/JDK7/application/sis-console/pom.xml Wed Aug 28 10:16:25 2013
@@ -120,6 +120,13 @@ Console application.
<type>test-jar</type>
<scope>test</scope>
</dependency>
+ <dependency>
+ <groupId>org.apache.sis.core</groupId>
+ <artifactId>sis-metadata</artifactId>
+ <version>${project.version}</version>
+ <type>test-jar</type>
+ <scope>test</scope>
+ </dependency>
</dependencies>
</project>
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=1518142&r1=1518141&r2=1518142&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] Wed Aug 28 10:16:25 2013
@@ -23,6 +23,7 @@ import java.net.URISyntaxException;
import org.apache.sis.storage.DataStores;
import org.apache.sis.storage.DataStoreException;
import org.apache.sis.util.resources.Errors;
+import org.apache.sis.util.CharSequences;
// Related to JDK7.
import java.nio.file.Files;
@@ -33,6 +34,12 @@ import java.nio.file.FileSystemNotFoundE
/**
* The "mime-type" subcommand.
+ * This sub-command reproduces the functionality of the following Unix command,
+ * except that {@code MimeTypeSC} uses the SIS detection mechanism instead than the OS one.
+ *
+ * {@preformat shell
+ * file --mime-type <files>
+ * }
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.4
@@ -57,33 +64,55 @@ final class MimeTypeSC extends SubComman
*/
@Override
public int run() throws InvalidOptionException, IOException, DataStoreException, URISyntaxException {
- if (hasUnexpectedFileCount(1, 1)) {
+ if (hasUnexpectedFileCount(1, Integer.MAX_VALUE)) {
return Command.INVALID_ARGUMENT_EXIT_CODE;
}
- final String file = files.get(0);
- final URI uri;
- try {
- 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) {
- error(Errors.format(Errors.Keys.CanNotOpen_1, uri), e);
- return Command.IO_EXCEPTION_EXIT_CODE;
+ /*
+ * Computes the width of the first column, which will contain file names.
+ */
+ int width = 0;
+ for (final String file : files) {
+ final int length = file.length() + 1;
+ if (length > width) {
+ width = length;
+ }
}
- if (type != null) {
- out.println(type);
- out.flush();
+ /*
+ * Now detect and print MIME type.
+ */
+ for (final String file : files) {
+ final URI uri;
+ try {
+ 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) {
+ error(Errors.format(Errors.Keys.CanNotOpen_1, uri), e);
+ return Command.IO_EXCEPTION_EXIT_CODE;
+ }
+ /*
+ * Output of Unix "file --mime-type" Unix command is of the form:
+ *
+ * file: type
+ */
+ if (type != null) {
+ out.print(file);
+ out.print(':');
+ out.print(CharSequences.spaces(width - file.length()));
+ out.println(type);
+ out.flush();
+ }
}
return 0;
}
Added: sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeSCTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeSCTest.java?rev=1518142&view=auto
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeSCTest.java (added)
+++ sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeSCTest.java [UTF-8] Wed Aug 28 10:16:25 2013
@@ -0,0 +1,52 @@
+/*
+ * 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.console;
+
+import java.net.URL;
+import org.apache.sis.xml.MetadataMarshallingTest;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Tests the {@link MimeTypeSC} sub-command.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.4
+ * @version 0.4
+ * @module
+ */
+@DependsOn(SubCommandTest.class)
+public final strictfp class MimeTypeSCTest extends TestCase {
+ /**
+ * Tests the sub-command on a metadata file.
+ *
+ * @throws Exception Should never happen.
+ */
+ @Test
+ public void testWithMetadataXML() throws Exception {
+ final URL url = MetadataMarshallingTest.class.getResource("Extent.xml");
+ assertNotNull("Extent.xml", url);
+ final MimeTypeSC test = new MimeTypeSC(0, SubCommand.TEST, url.toString());
+ test.run();
+ final String output = test.outputBuffer.toString().trim();
+ assertTrue(output, output.endsWith("org/apache/sis/xml/Extent.xml: application/vnd.iso.19139+xml"));
+ }
+}
Propchange: sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeSCTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange: sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/console/MimeTypeSCTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Modified: sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/test/suite/ConsoleTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/test/suite/ConsoleTestSuite.java?rev=1518142&r1=1518141&r2=1518142&view=diff
==============================================================================
--- sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/test/suite/ConsoleTestSuite.java [UTF-8] (original)
+++ sis/branches/JDK7/application/sis-console/src/test/java/org/apache/sis/test/suite/ConsoleTestSuite.java [UTF-8] Wed Aug 28 10:16:25 2013
@@ -26,13 +26,14 @@ import org.junit.BeforeClass;
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3
- * @version 0.3
+ * @version 0.4
* @module
*/
@Suite.SuiteClasses({
org.apache.sis.console.SubCommandTest.class,
org.apache.sis.console.HelpSCTest.class,
org.apache.sis.console.AboutSCTest.class,
+ org.apache.sis.console.MimeTypeSCTest.class,
org.apache.sis.console.MetadataSCTest.class
})
public final strictfp class ConsoleTestSuite extends TestSuite {
Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java?rev=1518142&r1=1518141&r2=1518142&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java [UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/storage/netcdf/NetcdfStoreProvider.java [UTF-8] Wed Aug 28 10:16:25 2013
@@ -16,6 +16,7 @@
*/
package org.apache.sis.storage.netcdf;
+import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.lang.reflect.Method;
@@ -34,6 +35,7 @@ import org.apache.sis.storage.StorageCon
import org.apache.sis.storage.DataStoreException;
import org.apache.sis.storage.ProbeResult;
import org.apache.sis.util.logging.WarningListeners;
+import org.apache.sis.util.logging.Logging;
import org.apache.sis.util.ThreadSafe;
import org.apache.sis.util.Version;
@@ -139,6 +141,9 @@ public class NetcdfStoreProvider extends
* The UCAR library is an optional dependency. If that library is present and the
* input is a String, then the following code may trigs a large amount of classes
* loading.
+ *
+ * Note that the UCAR library expects a String argument, not a File, because it
+ * has special cases for "file:", "http:", "nodods:" and "slurp:" protocols.
*/
if (!isSupported) {
final String path = storage.getStorageAs(String.class);
@@ -155,6 +160,16 @@ public class NetcdfStoreProvider extends
if (cause instanceof DataStoreException) throw (DataStoreException) cause;
if (cause instanceof RuntimeException) throw (RuntimeException) cause;
if (cause instanceof Error) throw (Error) cause;
+ if (cause instanceof FileNotFoundException) {
+ /*
+ * Happen if the String argument uses any protocol not recognized by the UCAR library,
+ * in which case UCAR tries to open it as a file even if it is not a file. For example
+ * we get this exception for "jar:file:/file.jar!/entry.nc".
+ */
+ Logging.recoverableException(
+ Logging.getLogger("org.apache.sis.storage"), netcdfFileClass, "canOpen", cause);
+ return ProbeResult.UNSUPPORTED_STORAGE;
+ }
throw new DataStoreException(e); // The cause may be IOException.
}
} else {
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=1518142&r1=1518141&r2=1518142&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] Wed Aug 28 10:16:25 2013
@@ -119,7 +119,8 @@ public final class IOUtilities extends S
if (path instanceof URL) {
name = ((URL) path).getPath();
} else if (path instanceof URI) {
- name = ((URI) path).getPath();
+ final URI uri = (URI) path;
+ name = uri.isOpaque() ? uri.getSchemeSpecificPart() : uri.getPath();
} else if (path instanceof CharSequence) {
name = path.toString();
separator = File.separatorChar;
Modified: sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/IOUtilitiesTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/IOUtilitiesTest.java?rev=1518142&r1=1518141&r2=1518142&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/IOUtilitiesTest.java [UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/IOUtilitiesTest.java [UTF-8] Wed Aug 28 10:16:25 2013
@@ -35,7 +35,7 @@ import static org.junit.Assert.*;
* @author Martin Desruisseaux (Geomatys)
* @author Johann Sorel (Geomatys)
* @since 0.3 (derived from geotk-3.00)
- * @version 0.3
+ * @version 0.4
* @module
*/
public final strictfp class IOUtilitiesTest extends TestCase {
@@ -75,6 +75,26 @@ public final strictfp class IOUtilitiesT
}
/**
+ * Tests again {@link IOUtilities#filename(Object)} and {@link IOUtilities#extension(Object)}, but with a URI
+ * that point to a JAR entry. Such URI are opaque, in which case {@link URI#getPath()} returns {@code null}.
+ *
+ * @throws URISyntaxException Should never happen.
+ * @throws MalformedURLException Should never happen.
+ */
+ @Test
+ @DependsOnMethod({"testFilename", "testExtension"})
+ public void testWithOpaqueURI() throws URISyntaxException, MalformedURLException {
+ final URI uri = new URI("jar:file:/sis-storage-tests.jar!/org/apache/sis/Any.xml");
+ assertTrue(uri.isOpaque()); // This test would be useless if this condition is false.
+ assertEquals("Any.xml", IOUtilities.filename (uri));
+ assertEquals( "xml", IOUtilities.extension(uri));
+
+ final URL url = new URL("jar:file:/sis-storage-tests.jar!/org/apache/sis/Any.xml");
+ assertEquals("Any.xml", IOUtilities.filename (url));
+ assertEquals( "xml", IOUtilities.extension(url));
+ }
+
+ /**
* Tests {@link IOUtilities#toString(Object)}.
*
* @throws URISyntaxException Should never happen.