Author: desruisseaux
Date: Fri May 31 22:35:49 2013
New Revision: 1488424
URL: http://svn.apache.org/r1488424
Log:
Some test cases for DataStoreConnection.
Added:
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoreConnectionTest.java
(with props)
Modified:
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
Modified:
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java?rev=1488424&r1=1488423&r2=1488424&view=diff
==============================================================================
---
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
[UTF-8] (original)
+++
sis/branches/JDK7/storage/sis-storage/src/main/java/org/apache/sis/storage/DataStoreConnection.java
[UTF-8] Fri May 31 22:35:49 2013
@@ -111,9 +111,9 @@ public class DataStoreConnection impleme
* </ul>
*
* A non-existent entry means that the value has not yet been computed. A
{@link Void#TYPE} value means the value
- * has been computed and we have determined that {@link #viewAs(Class)}
shall returns {@code null} for that type.
+ * has been computed and we have determined that {@link #openAs(Class)}
shall returns {@code null} for that type.
*
- * @see #viewAs(Class)
+ * @see #openAs(Class)
*/
private transient Map<Class<?>, Object> views;
@@ -255,7 +255,7 @@ public class DataStoreConnection impleme
* <li>Otherwise this method returns {@code null}.</li>
* </ul></p></li>
*
- * <li><p>{@link DataInput}:
+ * <li><p>{@link Connection}:
* Performs the following choice based on the type of the
{@linkplain #getStorage() storage} object:
* <ul>
* <li>If the storage is already an instance of {@link
Connection}, then it is returned unchanged.</li>
@@ -267,17 +267,20 @@ public class DataStoreConnection impleme
* </ul></p></li>
* </ul>
*
- * Multiple invocations of this method on the same {@code
DataStoreConnection} instance will return the same view
- * instance. Consequently, callers shall not close the stream or database
connection returned by this method.
+ * Callers shall not close the stream or database connection returned by
this method. Multiple invocations
+ * of this method on the same {@code DataStoreConnection} instance will
try to return the same instance on
+ * a <cite>best effort</cite> basis.
*
* @param <T> The compile-time type of the {@code type} argument.
- * @param type The type of desired view, as one of {@code ByteBuffer},
{@code DataInput}, {@code Connection}
+ * @param type The desired type as one of {@code ByteBuffer}, {@code
DataInput}, {@code Connection}
* class or other type supported by {@code DataStoreConnection}
subclasses.
* @return The storage as a view of the given type, or {@code null} if no
view can be created for the given type.
* @throws IllegalArgumentException If the given {@code type} argument is
not a known type.
* @throws DataStoreException If an error occurred while opening a stream
or database connection.
+ *
+ * @see #closeAllExcept(Object)
*/
- public <T> T viewAs(final Class<T> type) throws IllegalArgumentException,
DataStoreException {
+ public <T> T openAs(final Class<T> type) throws IllegalArgumentException,
DataStoreException {
ArgumentChecks.ensureNonNull("type", type);
if (views != null) {
final Object view = views.get(type);
@@ -317,7 +320,7 @@ public class DataStoreConnection impleme
/**
* Creates a view for the input as a {@link DataInput} if possible. This
method performs the choice
- * documented in the {@link #viewAs(Class)} method for the {@code
DataInput} case, and create the
+ * documented in the {@link #openAs(Class)} method for the {@code
DataInput} case, and create the
* {@code ByteBuffer} in same time if possible.
*
* @throws IOException If an error occurred while opening a stream for the
input.
@@ -335,9 +338,12 @@ public class DataStoreConnection impleme
*/
final ReadableByteChannel channel = IOUtilities.open(storage,
getOption(OptionKey.URL_ENCODING));
if (channel != null) {
- ByteBuffer buffer = getOption(OptionKey.BYTE_BUFFER);
+ ByteBuffer buffer = getView(ByteBuffer.class);
if (buffer == null) {
- buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
+ buffer = getOption(OptionKey.BYTE_BUFFER);
+ if (buffer == null) {
+ buffer = ByteBuffer.allocate(DEFAULT_BUFFER_SIZE);
+ }
}
asDataInput = new ChannelImageInputStream(getStorageName(),
channel, buffer, false);
asByteBuffer = buffer.asReadOnlyBuffer();
@@ -354,10 +360,10 @@ public class DataStoreConnection impleme
/**
* Creates a storage view of the given type if possible, or returns {@code
null} otherwise.
- * This method is invoked by {@link #viewAs(Class)} when first needed, and
the result is cached.
+ * This method is invoked by {@link #openAs(Class)} when first needed, and
the result is cached.
*
* @param <T> The compile-time type of the {@code type} argument.
- * @param type The type of desired view.
+ * @param type The type of the view to create.
* @return The storage as a view of the given type, or {@code null} if no
view can be created for the given type.
* @throws IllegalArgumentException If the given {@code type} argument is
not a known type.
* @throws Exception If an error occurred while opening a stream or
database connection.
@@ -432,7 +438,7 @@ public class DataStoreConnection impleme
/**
* Closes all streams and connections created by this {@code
DataStoreConnection} except the given view.
- * This method closes all objects created by the {@link #viewAs(Class)}
method, leaving only {@code view} open.
+ * This method closes all objects created by the {@link #openAs(Class)}
method, leaving only {@code view} open.
* If {@code view} is {@code null}, then this method closes everything
including the {@linkplain #getStorage()
* storage} if it is closeable.
*
@@ -440,15 +446,17 @@ public class DataStoreConnection impleme
* by the data store is given in argument to this method - or when no
suitable {@code DataStore} has been
* found - in which case the {@code view} argument is null.</p>
*
+ * <p>This {@code DataStoreConnection} instance shall not be used anymore
after invocation of this method.</p>
+ *
* @param view The view to leave open, or {@code null} if none.
* @throws DataStoreException If an error occurred while closing the
stream or database connection.
+ *
+ * @see #openAs(Class)
*/
public void closeAllExcept(final Object view) throws DataStoreException {
- final Map<Class<?>, Object> map = views;
- views = Collections.emptyMap();
boolean close = (view == null);
try {
- for (final Object value : map.values()) {
+ for (final Object value : views.values()) {
if (value != view) {
if (value instanceof AutoCloseable) {
((AutoCloseable) value).close();
@@ -472,6 +480,8 @@ public class DataStoreConnection impleme
}
} catch (Exception e) {
throw new DataStoreException(e);
+ } finally {
+ views = Collections.emptyMap();
}
}
Added:
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoreConnectionTest.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoreConnectionTest.java?rev=1488424&view=auto
==============================================================================
---
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoreConnectionTest.java
(added)
+++
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoreConnectionTest.java
[UTF-8] Fri May 31 22:35:49 2013
@@ -0,0 +1,115 @@
+/*
+ * 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.storage;
+
+import java.io.DataInput;
+import java.io.IOException;
+import java.nio.channels.ReadableByteChannel;
+import org.apache.sis.internal.storage.ChannelImageInputStream;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.opengis.test.Assert.*;
+
+
+/**
+ * Tests {@link DataStoreConnection}.
+ *
+ * @author Martin Desruisseaux (Geomatys)
+ * @since 0.3
+ * @version 0.3
+ * @module
+ */
+public final strictfp class DataStoreConnectionTest extends TestCase {
+ /**
+ * The magic number of Java class files, used for verifying the content of
our test file.
+ */
+ private static final int MAGIC_NUMBER = 0xCAFEBABE;
+
+ /**
+ * Creates the instance to test. This method uses the {@code
DataStoreConnectionTest} compiled
+ * class file as the resource to test. The resource can be provided either
as a URL or as a stream.
+ */
+ private DataStoreConnection create(final boolean asStream) {
+ final Class<?> c = DataStoreConnectionTest.class;
+ final String name = c.getSimpleName() + ".class";
+ final Object storage = asStream ? c.getResourceAsStream(name) :
c.getResource(name);
+ assertNotNull(storage);
+ return new DataStoreConnection(storage);
+ }
+
+ /**
+ * Tests the {@link DataStoreConnection#getStorageName()} method.
+ */
+ @Test
+ public void testGetStorageName() {
+ final DataStoreConnection c = create(false);
+ assertEquals("DataStoreConnectionTest.class", c.getStorageName());
+ }
+
+ /**
+ * Tests the {@link DataStoreConnection#getFileExtension()} method.
+ */
+ @Test
+ public void testGetExtension() {
+ final DataStoreConnection c = create(false);
+ assertEquals("class", c.getFileExtension());
+ }
+
+ /**
+ * Tests the {@link DataStoreConnection#openAs(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.
+ */
+ @Test
+ public void testOpenFromURL() throws DataStoreException, IOException {
+ testOpenAsDataInput(false);
+ }
+
+ /**
+ * Tests the {@link DataStoreConnection#openAs(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.
+ */
+ @Test
+ public void testOpenFromStream() throws DataStoreException, IOException {
+ testOpenAsDataInput(true);
+ }
+
+ /**
+ * Implementation of {@link #testOpenAsStream()}.
+ */
+ private void testOpenAsDataInput(final boolean asStream) throws
DataStoreException, IOException {
+ final DataStoreConnection connection = create(asStream);
+ final DataInput input = connection.openAs(DataInput.class);
+ assertSame("Value shall be cached.", input,
connection.openAs(DataInput.class));
+ assertInstanceOf("Needs the SIS implementation",
ChannelImageInputStream.class, input);
+ final ReadableByteChannel channel = ((ChannelImageInputStream)
input).channel;
+ /*
+ * Reads a single integer for checking that the stream is at the right
position,
+ * then close the stream.
+ */
+ assertTrue("channel.isOpen()", channel.isOpen());
+ assertEquals(MAGIC_NUMBER, input.readInt());
+ connection.closeAllExcept(null);
+ assertFalse("channel.isOpen()", channel.isOpen());
+ }
+}
Propchange:
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoreConnectionTest.java
------------------------------------------------------------------------------
svn:eol-style = native
Propchange:
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/storage/DataStoreConnectionTest.java
------------------------------------------------------------------------------
svn:mime-type = text/plain;charset=UTF-8
Modified:
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
URL:
http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java?rev=1488424&r1=1488423&r2=1488424&view=diff
==============================================================================
---
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
[UTF-8] (original)
+++
sis/branches/JDK7/storage/sis-storage/src/test/java/org/apache/sis/test/suite/StorageTestSuite.java
[UTF-8] Fri May 31 22:35:49 2013
@@ -33,7 +33,8 @@ import org.junit.BeforeClass;
org.apache.sis.internal.storage.WarningConsumerTest.class,
org.apache.sis.internal.storage.IOUtilitiesTest.class,
org.apache.sis.internal.storage.ChannelDataInputTest.class,
- org.apache.sis.internal.storage.ChannelImageInputStreamTest.class
+ org.apache.sis.internal.storage.ChannelImageInputStreamTest.class,
+ org.apache.sis.storage.DataStoreConnectionTest.class
})
public final strictfp class StorageTestSuite extends TestSuite {
/**