sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1482843 - in /sis/branches/JDK7/storage/sis-netcdf/src: main/java/org/apache/sis/internal/netcdf/ main/java/org/apache/sis/internal/netcdf/ucar/ test/java/org/apache/sis/internal/netcdf/
Date Wed, 15 May 2013 13:57:35 GMT
Author: desruisseaux
Date: Wed May 15 13:57:35 2013
New Revision: 1482843

URL: http://svn.apache.org/r1482843
Log:
Added a few more tests.

Added:
    sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/TestCase.java
  (with props)
Modified:
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
    sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
    sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/DecoderTest.java

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java?rev=1482843&r1=1482842&r2=1482843&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/Decoder.java
[UTF-8] Wed May 15 13:57:35 2013
@@ -18,6 +18,7 @@ package org.apache.sis.internal.netcdf;
 
 import java.util.Date;
 import java.util.List;
+import java.io.Closeable;
 import java.io.IOException;
 import javax.measure.unit.Unit;
 import org.apache.sis.measure.Units;
@@ -31,7 +32,7 @@ import org.apache.sis.measure.Units;
  * @version 0.3
  * @module
  */
-public abstract class Decoder extends WarningProducer {
+public abstract class Decoder extends WarningProducer implements Closeable {
     /**
      * Creates a new decoder.
      *

Modified: sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java?rev=1482843&r1=1482842&r2=1482843&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/main/java/org/apache/sis/internal/netcdf/ucar/DecoderWrapper.java
[UTF-8] Wed May 15 13:57:35 2013
@@ -304,4 +304,14 @@ public final class DecoderWrapper extend
         }
         return Collections.emptyList();
     }
+
+    /**
+     * Closes the NetCDF file.
+     *
+     * @throws IOException If an error occurred while closing the file.
+     */
+    @Override
+    public void close() throws IOException {
+        file.close();
+    }
 }

Modified: sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/DecoderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/DecoderTest.java?rev=1482843&r1=1482842&r2=1482843&view=diff
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/DecoderTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/DecoderTest.java
[UTF-8] Wed May 15 13:57:35 2013
@@ -16,58 +16,72 @@
  */
 package org.apache.sis.internal.netcdf;
 
+import java.util.Date;
 import java.io.IOException;
-import ucar.nc2.NetcdfFile;
-import org.opengis.wrapper.netcdf.IOTestCase;
-import org.apache.sis.storage.netcdf.AttributeNames;
-import org.apache.sis.internal.netcdf.ucar.DecoderWrapper;
 import org.junit.Test;
 
 import static org.junit.Assert.*;
+import static org.apache.sis.test.TestUtilities.date;
+import static org.apache.sis.storage.netcdf.AttributeNames.*;
 
 
 /**
- * Tests the {@link Decoder} implementations.
+ * Tests the {@link Decoder} implementation. The default implementation tests
+ * {@link org.apache.sis.internal.netcdf.ucar.DecoderWrapper} since the UCAR
+ * library is our reference implementation. However subclasses can override the
+ * {@link #createDecoder(String)} method in order to test a different implementation.
  *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.3
  * @version 0.3
  * @module
  */
-public final strictfp class DecoderTest extends IOTestCase {
+public strictfp class DecoderTest extends TestCase {
     /**
-     * The decoder to test.
-     */
-    private Decoder decoder;
-
-    /**
-     * Tests the {@link DecoderWrapper} implementation.
+     * Tests {@link Decoder#stringValue(String)} with global attributes.
      *
      * @throws IOException If an error occurred while reading the NetCDF file.
      */
     @Test
-    public void testUCAR() throws IOException {
-        final NetcdfFile file = open(IOTestCase.NCEP);
-        try {
-            decoder = new DecoderWrapper(null, file);
-            runAllTests();
-        } finally {
-            file.close();
-        }
+    public void testStringValue() throws IOException {
+        selectDataset(NCEP);
+        assertAttributeEquals("Sea Surface Temperature Analysis Model",      TITLE);
+        assertAttributeEquals("NCEP SST Global 5.0 x 2.5 degree model data", SUMMARY);
+        assertAttributeEquals("NOAA/NWS/NCEP",                               CREATOR.NAME);
+        assertAttributeEquals(/* Empty string in file   */ (String) null,    CREATOR.EMAIL);
+        assertAttributeEquals(/* Non-existent attribute */ (String) null,    CONTRIBUTOR.NAME);
+
+        selectDataset(CIP);
+        assertAttributeEquals(/* Only control character */ (String) null,    TITLE);
+        assertAttributeEquals("UCAR",                                        CREATOR.INSTITUTION);
+        assertAttributeEquals("U.S. National Weather Service - NCEP (WMC)",  HISTORY);
     }
 
     /**
-     * Runs all the tests defined below this method.
+     * Tests {@link Decoder#numericValue(String)} with global attributes.
+     *
+     * @throws IOException If an error occurred while reading the NetCDF file.
      */
-    private void runAllTests() throws IOException {
-        decoder.setSearchPath(new String[1]);
-        testStringValue();
+    @Test
+    public void testNumericValue() throws IOException {
+        selectDataset(NCEP);
+        assertAttributeEquals(Double.valueOf( -90), LATITUDE .MINIMUM);
+        assertAttributeEquals(Double.valueOf( +90), LATITUDE .MAXIMUM);
+        assertAttributeEquals(Double.valueOf(-180), LONGITUDE.MINIMUM);
+        assertAttributeEquals(Double.valueOf(+180), LONGITUDE.MAXIMUM);
+        assertAttributeEquals((Double) null,        LATITUDE .RESOLUTION);
+        assertAttributeEquals((Double) null,        LONGITUDE.RESOLUTION);
     }
 
     /**
-     * Tests {@link Decoder#stringValue(String)}.
+     * Tests {@link Decoder#dateValue(String)} with global attributes.
+     *
+     * @throws IOException If an error occurred while reading the NetCDF file.
      */
-    private void testStringValue() throws IOException {
-        assertEquals("Sea Surface Temperature Analysis Model", decoder.stringValue(AttributeNames.TITLE));
+    @Test
+    public void testDateValue() throws IOException {
+        selectDataset(NCEP);
+        assertAttributeEquals(date("2005-09-22 00:00:00"), DATE_CREATED);
+        assertAttributeEquals((Date) null,                 DATE_MODIFIED);
     }
 }

Added: sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/TestCase.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/TestCase.java?rev=1482843&view=auto
==============================================================================
--- sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/TestCase.java
(added)
+++ sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/TestCase.java
[UTF-8] Wed May 15 13:57:35 2013
@@ -0,0 +1,194 @@
+/*
+ * 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.internal.netcdf;
+
+import java.util.Date;
+import java.util.Map;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.io.IOException;
+import java.lang.reflect.UndeclaredThrowableException;
+import org.apache.sis.internal.netcdf.ucar.DecoderWrapper;
+import org.opengis.wrapper.netcdf.IOTestCase;
+import org.junit.AfterClass;
+
+import static org.junit.Assert.*;
+
+
+/**
+ * Base class of NetCDF tests. Subclasses shall override the {@link #createDecoder(String)}.
+ *
+ * <p>This class is <strong>not</strong> thread safe - do not run subclasses
in parallel.</p>
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3
+ * @version 0.3
+ * @module
+ */
+public abstract strictfp class TestCase extends IOTestCase {
+    /**
+     * The {@code searchPath} argument value to be given to the {@link Decoder#setSearchPath(String[])}
+     * method when the decoder shall search only in global attributes.
+     */
+    private static final String[] GLOBAL = new String[1];
+
+    /**
+     * The decoders cached by {@link #selectDataset(String)}.
+     */
+    private static final Map<String,Decoder> DECODERS = new HashMap<>();
+
+    /**
+     * The decoder to test, which is set by {@link #selectDataset(String)}.
+     * This field must be set before any {@code assert} method is invoked.
+     */
+    private Decoder decoder;
+
+    /**
+     * Creates a new test case.
+     */
+    protected TestCase() {
+    }
+
+    /**
+     * Invoked when a new {@link Decoder} instance needs to be created for the given name.
+     * The {@code name} parameter can be one of the following values:
+     *
+     * <ul>
+     *   <li>{@link #THREDDS} for a NcML file.</li>
+     *   <li>{@link #NCEP}    for a NetCDF binary file.</li>
+     *   <li>{@link #CIP}     for a NetCDF binary file.</li>
+     *   <li>{@link #LANDSAT} for a NetCDF binary file.</li>
+     * </ul>
+     *
+     * The default implementation first delegates to {@link #open(String)}, then wraps the
result
+     * in a {@link DecoderWrapper}. We proceeded that way because the UCAR library is used
as the
+     * reference implementation. However subclasses can override if they want to test a different
+     * library.
+     *
+     * @param  name The file name as one of the above-cited constants.
+     * @return The decoder for the given name.
+     * @throws IOException If an error occurred while opening the file.
+     */
+    protected Decoder createDecoder(String name) throws IOException {
+        return new DecoderWrapper(null, open(name));
+    }
+
+    /**
+     * Selects the dataset to use for the tests. If a decoder for the given name has already
been
+     * opened, then this method returns that decoder. Otherwise a new decoder is created
by a call
+     * to {@link #createDecoder(String)}, then cached.
+     *
+     * <p>The {@linkplain Decoder#setSearchPath(String[]) search path} of the returned
decoder
+     * is initialized to the global attributes only.</p>
+     *
+     * @param  name The file name as one of the constants enumerated in the {@link #createDecoder(String)}
method.
+     * @return The decoder for the given name.
+     * @throws IOException If an error occurred while opening the file.
+     */
+    protected final Decoder selectDataset(final String name) throws IOException {
+        synchronized (DECODERS) { // Paranoiac safety, but should not be used in multi-threads
environment.
+            decoder = DECODERS.get(name);
+            if (decoder == null) {
+                decoder = createDecoder(name);
+                assertNotNull(decoder);
+                assertNull(DECODERS.put(name, decoder));
+            }
+            decoder.setSearchPath(GLOBAL);
+            return decoder; // Reminder: Decoder instances are not thread-safe.
+        }
+    }
+
+    /**
+     * Invoked after all tests in a class have been executed.
+     * This method closes all NetCDF files.
+     *
+     * @throws IOException If an error occurred while closing a file.
+     */
+    @AfterClass
+    public static void closeAllDecoders() throws IOException {
+        Throwable failure = null;
+        synchronized (DECODERS) { // Paranoiac safety.
+            final Iterator<Decoder> it = DECODERS.values().iterator();
+            while (it.hasNext()) {
+                final Decoder decoder = it.next();
+                try {
+                    decoder.close();
+                } catch (Throwable e) {
+                    if (failure == null) {
+                        failure = e;
+                    } else {
+                        failure.addSuppressed(e);
+                    }
+                }
+                it.remove();
+            }
+            assertTrue(DECODERS.isEmpty());
+        }
+        /*
+         * If we failed to close a file, propagates the error
+         * only after we have closed all other files.
+         */
+        if (failure != null) {
+            if (failure instanceof IOException) {
+                throw (IOException) failure;
+            }
+            if (failure instanceof RuntimeException) {
+                throw (RuntimeException) failure;
+            }
+            if (failure instanceof Error) {
+                throw (Error) failure;
+            }
+            throw new UndeclaredThrowableException(failure);
+        }
+    }
+
+    /**
+     * Asserts that the textual value of the named attribute is equals to the expected value.
+     * The {@link #selectDataset(String)} method must be invoked at least once before this
method.
+     *
+     * @param  expected      The expected attribute value.
+     * @param  attributeName The name of the attribute to test.
+     * @throws IOException   If an error occurred while reading the NetCDF file.
+     */
+    protected final void assertAttributeEquals(final String expected, final String attributeName)
throws IOException {
+        assertEquals(attributeName, expected, decoder.stringValue(attributeName));
+    }
+
+    /**
+     * Asserts that the numeric value of the named attribute is equals to the expected value.
+     * The {@link #selectDataset(String)} method must be invoked at least once before this
method.
+     *
+     * @param  expected      The expected attribute value.
+     * @param  attributeName The name of the attribute to test.
+     * @throws IOException   If an error occurred while reading the NetCDF file.
+     */
+    protected final void assertAttributeEquals(final Number expected, final String attributeName)
throws IOException {
+        assertEquals(attributeName, expected, decoder.numericValue(attributeName));
+    }
+
+    /**
+     * Asserts that the temporal value of the named attribute is equals to the expected value.
+     * The {@link #selectDataset(String)} method must be invoked at least once before this
method.
+     *
+     * @param  expected      The expected attribute value.
+     * @param  attributeName The name of the attribute to test.
+     * @throws IOException   If an error occurred while reading the NetCDF file.
+     */
+    protected final void assertAttributeEquals(final Date expected, final String attributeName)
throws IOException {
+        assertEquals(attributeName, expected, decoder.dateValue(attributeName));
+    }
+}

Propchange: sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/TestCase.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/storage/sis-netcdf/src/test/java/org/apache/sis/internal/netcdf/TestCase.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8



Mime
View raw message