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 2016/02/24 23:57:21 UTC
svn commit: r1732239 [2/2] - in /sis/branches/JDK6: ./
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/internal/metadata/ co...
Modified: sis/branches/JDK6/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java?rev=1732239&r1=1732238&r2=1732239&view=diff
==============================================================================
--- sis/branches/JDK6/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java [UTF-8] (original)
+++ sis/branches/JDK6/storage/sis-storage/src/main/java/org/apache/sis/storage/StorageConnector.java [UTF-8] Wed Feb 24 22:57:20 2016
@@ -372,6 +372,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 {
@@ -391,7 +396,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;
}
@@ -419,6 +424,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.
*
@@ -426,8 +444,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);
@@ -478,7 +510,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);
@@ -598,13 +630,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/JDK6/storage/sis-storage/src/main/resources/META-INF/services/org.apache.sis.storage.DataStoreProvider
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/storage/sis-storage/src/main/resources/META-INF/services/org.apache.sis.storage.DataStoreProvider?rev=1732239&r1=1732238&r2=1732239&view=diff
==============================================================================
--- sis/branches/JDK6/storage/sis-storage/src/main/resources/META-INF/services/org.apache.sis.storage.DataStoreProvider [UTF-8] (original)
+++ sis/branches/JDK6/storage/sis-storage/src/main/resources/META-INF/services/org.apache.sis.storage.DataStoreProvider [UTF-8] Wed Feb 24 22:57:20 2016
@@ -1 +1,2 @@
-org.apache.sis.internal.storage.xml.XMLStoreProvider
+org.apache.sis.internal.storage.xml.StoreProvider
+org.apache.sis.internal.storage.wkt.StoreProvider
Modified: sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreTest.java?rev=1732239&r1=1732235&r2=1732239&view=diff
==============================================================================
--- sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreTest.java [UTF-8] (original)
+++ sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/wkt/StoreTest.java [UTF-8] Wed Feb 24 22:57:20 2016
@@ -31,7 +31,7 @@ import org.junit.Test;
import static org.opengis.test.Assert.*;
// Branch-dependent imports
-import java.nio.charset.StandardCharsets;
+import org.apache.sis.internal.jdk7.StandardCharsets;
/**
@@ -73,9 +73,12 @@ public final strictfp class StoreTest ex
@Test
public void testFromReader() throws DataStoreException {
final Metadata metadata;
- try (Store store = new Store(new StorageConnector(new StringReader(WKT)))) {
+ final Store store = new Store(new StorageConnector(new StringReader(WKT)));
+ try {
metadata = store.getMetadata();
assertSame("Expected cached value.", metadata, store.getMetadata());
+ } finally {
+ store.close();
}
validate((GeographicCRS) TestUtilities.getSingleton(metadata.getReferenceSystemInfo()));
}
@@ -93,9 +96,12 @@ public final strictfp class StoreTest ex
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)) {
+ final Store store = new Store(c);
+ try {
metadata = store.getMetadata();
assertSame("Expected cached value.", metadata, store.getMetadata());
+ } finally {
+ store.close();
}
validate((GeographicCRS) TestUtilities.getSingleton(metadata.getReferenceSystemInfo()));
}
Modified: sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java?rev=1732239&r1=1732238&r2=1732239&view=diff
==============================================================================
--- sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java [UTF-8] (original)
+++ sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/MimeTypeDetectorTest.java [UTF-8] Wed Feb 24 22:57:20 2016
@@ -44,7 +44,7 @@ public final strictfp class MimeTypeDete
*/
@Test
public void testGMDFromString() throws IOException {
- final StringReader in = new StringReader(XMLStoreTest.XML);
+ final StringReader in = new StringReader(StoreTest.XML);
assertEquals('<', in.read());
assertEquals('?', in.read());
final MimeTypeDetector detector = new MimeTypeDetector() {
Copied: sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java (from r1732235, sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java?p2=sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java&p1=sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java&r1=1732235&r2=1732239&rev=1732239&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java [UTF-8] (original)
+++ sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/xml/StoreTest.java [UTF-8] Wed Feb 24 22:57:20 2016
@@ -31,7 +31,7 @@ import static org.opengis.test.Assert.*;
import static org.apache.sis.test.TestUtilities.getSingleton;
// Branch-dependent imports
-import java.nio.charset.StandardCharsets;
+import org.apache.sis.internal.jdk7.StandardCharsets;
/**
@@ -93,9 +93,12 @@ public final strictfp class StoreTest ex
@Test
public void testMetadata() throws DataStoreException {
final Metadata metadata;
- try (Store store = new Store(new StorageConnector(new StringReader(XML)))) {
+ final Store store = new Store(new StorageConnector(new StringReader(XML)));
+ try {
metadata = store.getMetadata();
assertSame("Expected cached value.", metadata, store.getMetadata());
+ } finally {
+ store.close();
}
final Responsibility resp = getSingleton(metadata.getContacts());
final Party party = getSingleton(resp.getParties());
Modified: sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java?rev=1732239&r1=1732238&r2=1732239&view=diff
==============================================================================
--- sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java [UTF-8] (original)
+++ sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoresTest.java [UTF-8] Wed Feb 24 22:57:20 2016
@@ -17,7 +17,7 @@
package org.apache.sis.storage;
import java.io.StringReader;
-import org.apache.sis.internal.storage.xml.XMLStoreTest;
+import org.apache.sis.internal.storage.xml.StoreTest;
import org.apache.sis.test.DependsOn;
import org.apache.sis.test.TestCase;
import org.junit.Test;
@@ -30,10 +30,10 @@ import static org.junit.Assert.*;
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.4
- * @version 0.4
+ * @version 0.7
* @module
*/
-@DependsOn(XMLStoreTest.class)
+@DependsOn(StoreTest.class)
public final strictfp class DataStoresTest extends TestCase {
/**
* Tests {@link DataStores#probeContentType(Object)}.
@@ -42,7 +42,7 @@ public final strictfp class DataStoresTe
*/
@Test
public void testProbeContentType() throws DataStoreException {
- final String type = DataStores.probeContentType(new StringReader(XMLStoreTest.XML));
+ final String type = DataStores.probeContentType(new StringReader(StoreTest.XML));
assertEquals("application/vnd.iso.19139+xml", type);
}
@@ -53,7 +53,7 @@ public final strictfp class DataStoresTe
*/
@Test
public void testOpen() throws DataStoreException {
- final DataStore store = DataStores.open(new StringReader(XMLStoreTest.XML));
+ final DataStore store = DataStores.open(new StringReader(StoreTest.XML));
assertFalse(store.getMetadata().getContacts().isEmpty());
}
}
Modified: sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java?rev=1732239&r1=1732238&r2=1732239&view=diff
==============================================================================
--- sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java [UTF-8] (original)
+++ sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/storage/StorageConnectorTest.java [UTF-8] Wed Feb 24 22:57:20 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")
Modified: sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java?rev=1732239&r1=1732238&r2=1732239&view=diff
==============================================================================
--- sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java [UTF-8] (original)
+++ sis/branches/JDK6/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java [UTF-8] Wed Feb 24 22:57:20 2016
@@ -26,7 +26,7 @@ import org.junit.BeforeClass;
*
* @author Martin Desruisseaux (Geomatys)
* @since 0.3
- * @version 0.5
+ * @version 0.7
* @module
*/
@Suite.SuiteClasses({
@@ -38,8 +38,10 @@ import org.junit.BeforeClass;
org.apache.sis.storage.ProbeResultTest.class,
org.apache.sis.storage.StorageConnectorTest.class,
org.apache.sis.internal.storage.xml.MimeTypeDetectorTest.class,
- org.apache.sis.internal.storage.xml.XMLStoreProviderTest.class,
- org.apache.sis.internal.storage.xml.XMLStoreTest.class,
+ org.apache.sis.internal.storage.xml.StoreProviderTest.class,
+ org.apache.sis.internal.storage.xml.StoreTest.class,
+ org.apache.sis.internal.storage.wkt.StoreProviderTest.class,
+ org.apache.sis.internal.storage.wkt.StoreTest.class,
org.apache.sis.storage.DataStoresTest.class,
org.apache.sis.index.GeoHashCoderTest.class
})