sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1478864 - in /sis/branches/JDK7/sis-metadata/src: main/java/org/apache/sis/metadata/ test/java/org/apache/sis/metadata/ test/java/org/apache/sis/test/suite/
Date Fri, 03 May 2013 16:23:26 GMT
Author: desruisseaux
Date: Fri May  3 16:23:25 2013
New Revision: 1478864

URL: http://svn.apache.org/r1478864
Log:
Added more tests.

Added:
    sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeTableTest.java
  (with props)
Modified:
    sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
    sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/StandardImplementation.java
    sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java
    sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java

Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java?rev=1478864&r1=1478863&r2=1478864&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/MetadataStandard.java
[UTF-8] Fri May  3 16:23:25 2013
@@ -22,6 +22,10 @@ import java.util.IdentityHashMap;
 import java.util.LinkedHashSet;
 import java.util.Collection;
 import java.util.Iterator;
+import java.io.IOException;
+import java.io.Serializable;
+import java.io.ObjectInputStream;
+import java.lang.reflect.Field;
 import net.jcip.annotations.ThreadSafe;
 import org.opengis.metadata.citation.Citation;
 import org.opengis.metadata.ExtendedElementInformation;
@@ -81,7 +85,12 @@ import static org.apache.sis.util.Argume
  * @module
  */
 @ThreadSafe
-public class MetadataStandard {
+public class MetadataStandard implements Serializable {
+    /**
+     * For cross-version compatibility.
+     */
+    private static final long serialVersionUID = 7549790450195184843L;
+
     /**
      * Metadata instances defined in this class. The current implementation does not yet
      * contains the user-defined instances. However this may be something we will need to
@@ -115,6 +124,8 @@ public class MetadataStandard {
     static {
         final String[] prefix = {"Default", "Abstract"};
         final String[] acronyms = {"CoordinateSystem", "CS", "CoordinateReferenceSystem",
"CRS"};
+
+        // If new StandardImplementation instances are added below, please update StandardImplementation.readResolve().
         ISO_19111 = new StandardImplementation("ISO 19111", "org.opengis.referencing.", "org.apache.sis.referencing.",
prefix, acronyms);
         ISO_19115 = new StandardImplementation("ISO 19115", "org.opengis.metadata.", "org.apache.sis.metadata.iso.",
prefix, null);
         ISO_19119 = new MetadataStandard      ("ISO 19119", "org.opengis.service.");
@@ -137,7 +148,7 @@ public class MetadataStandard {
      *
      * @see #getCitation()
      */
-    private final Citation citation;
+    final Citation citation;
 
     /**
      * The root packages for metadata interfaces. Must have a trailing {@code '.'}.
@@ -154,7 +165,7 @@ public class MetadataStandard {
      *   <li>{@link PropertyAccessor} otherwise.</li>
      * </ul>
      */
-    private final Map<Class<?>, Object> accessors;
+    private final transient Map<Class<?>, Object> accessors; // written by reflection
on deserialization.
 
     /**
      * Creates a new instance working on implementation of interfaces defined in the specified
package.
@@ -170,7 +181,7 @@ public class MetadataStandard {
         ensureNonNull("interfacePackage", interfacePackage);
         this.citation         = citation;
         this.interfacePackage = interfacePackage.getName() + '.';
-        this.accessors        = new IdentityHashMap<>();
+        this.accessors        = new IdentityHashMap<>(); // Also defined in readObject(…)
     }
 
     /**
@@ -707,4 +718,26 @@ public class MetadataStandard {
     public String toString() {
         return Classes.getShortClassName(this) + '[' + citation.getTitle() + ']';
     }
+
+    /**
+     * Assigns an {@link IdentityHashMap} instance to the given field.
+     * Used on deserialization only.
+     */
+    final void setMapForField(final Class<?> classe, final String name) {
+        try {
+            final Field field = classe.getDeclaredField(name);
+            field.setAccessible(true);
+            field.set(this, new IdentityHashMap<>());
+        } catch (ReflectiveOperationException e) {
+            throw new AssertionError(e); // Should never happen (tested by MetadataStandardTest).
+        }
+    }
+
+    /**
+     * Invoked during deserialization for restoring the transient fields.
+     */
+    private void readObject(final ObjectInputStream in) throws IOException, ClassNotFoundException
{
+        in.defaultReadObject();
+        setMapForField(MetadataStandard.class, "accessors");
+    }
 }

Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/StandardImplementation.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/StandardImplementation.java?rev=1478864&r1=1478863&r2=1478864&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/StandardImplementation.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/StandardImplementation.java
[UTF-8] Fri May  3 16:23:25 2013
@@ -18,6 +18,7 @@ package org.apache.sis.metadata;
 
 import java.util.Map;
 import java.util.IdentityHashMap;
+import java.io.ObjectStreamException;
 import org.apache.sis.util.CharSequences;
 import org.apache.sis.util.logging.Logging;
 
@@ -32,6 +33,11 @@ import org.apache.sis.util.logging.Loggi
  */
 final class StandardImplementation extends MetadataStandard {
     /**
+     * For cross-version compatibility.
+     */
+    private static final long serialVersionUID = 855786625369724248L;
+
+    /**
      * The root packages for metadata implementations, or {@code null} if none.
      * If non-null, then this string must ends with a trailing {@code "."}.
      */
@@ -63,7 +69,7 @@ final class StandardImplementation exten
      *        less objects since <code>IdentityHashMap</code> implementation
doesn't need the chain
      *        of objects created by <code>HashMap</code>.}
      */
-    private final Map<Class<?>,Class<?>> implementations;
+    private final transient Map<Class<?>,Class<?>> implementations; //
written by reflection on deserialization.
 
     /**
      * Creates a new instance working on implementation of interfaces defined in the
@@ -151,4 +157,19 @@ final class StandardImplementation exten
         }
         return null;
     }
+
+    /**
+     * Invoked on deserialization. Returns one of the pre-existing constants if possible.
+     */
+    Object readResolve() throws ObjectStreamException {
+        if (ISO_19111.citation.equals(citation)) return ISO_19111;
+        if (ISO_19115.citation.equals(citation)) return ISO_19115;
+        /*
+         * Following should not occurs, unless we are deserializing an instance created by
a
+         * newer version of the Apache SIS library. The newer version could contains constants
+         * not yet declared in this older SIS version, so we have to use this instance.
+         */
+        setMapForField(StandardImplementation.class, "implementations");
+        return this;
+    }
 }

Modified: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java?rev=1478864&r1=1478863&r2=1478864&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataStandardTest.java
[UTF-8] Fri May  3 16:23:25 2013
@@ -34,7 +34,7 @@ import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
-import static org.opengis.test.Assert.*;
+import static org.apache.sis.test.Assert.*;
 
 
 /**
@@ -195,4 +195,13 @@ public final strictfp class MetadataStan
         assertEquals("As elements in a list of dimensions.",       Integer.class,  types.get("dimension"));
         assertEquals("As elements in the list of offset vectors.", double[].class, types.get("offsetVectors"));
     }
+
+    /**
+     * Tests serialization of pre-defined constants.
+     */
+    @Test
+    public void testSerialization() {
+        assertSame(MetadataStandard.ISO_19111, assertSerializedEquals(MetadataStandard.ISO_19111));
+        assertSame(MetadataStandard.ISO_19115, assertSerializedEquals(MetadataStandard.ISO_19115));
+    }
 }

Added: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeTableTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeTableTest.java?rev=1478864&view=auto
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeTableTest.java
(added)
+++ sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeTableTest.java
[UTF-8] Fri May  3 16:23:25 2013
@@ -0,0 +1,103 @@
+/*
+ * 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.metadata;
+
+import java.io.ObjectInputStream;
+import java.io.ObjectOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+
+
+/**
+ * Tests the {@link MetadataTreeTable} class.
+ * Unless otherwise specified, all tests use the {@link MetadataStandard#ISO_19115} constant.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3
+ * @version 0.3
+ * @module
+ */
+@DependsOn(MetadataTreeNodeTest.class)
+public final strictfp class MetadataTreeTableTest extends TestCase {
+    /**
+     * Creates a table to be tested for the given value policy.
+     */
+    private static MetadataTreeTable create(final ValueExistencePolicy valuePolicy) {
+        return new MetadataTreeTable(MetadataStandard.ISO_19115, MetadataTreeNodeTest.metadataWithHierarchy(),
valuePolicy);
+    }
+
+    /**
+     * Asserts that the given metadata object has the expected string representation.
+     */
+    private static void assertExpectedString(final MetadataTreeTable metadata) {
+        assertMultilinesEquals("toString()",
+                "DefaultCitation\n" +
+                "  ├─Title……………………………………………………………………………………
Some title\n" +
+                "  ├─Alternate title (1 of 2)…………………………………
First alternate title\n" +
+                "  ├─Alternate title (2 of 2)…………………………………
Second alternate title\n" +
+                "  ├─Edition………………………………………………………………………………
Some edition\n" +
+                "  ├─Cited responsible party (1 of 2)\n" +
+                "  │   ├─Organisation name…………………………………………
Some organisation\n" +
+                "  │   └─Role……………………………………………………………………………
Distributor\n" +
+                "  ├─Cited responsible party (2 of 2)\n" +
+                "  │   ├─Individual name………………………………………………
Some person of contact\n" +
+                "  │   ├─Contact info\n" +
+                "  │   │   └─Address\n" +
+                "  │   │       └─Electronic mail address…… Some email\n"
+
+                "  │   └─Role……………………………………………………………………………
Point of contact\n" +
+                "  ├─Presentation form (1 of 2)…………………………… Map
digital\n" +
+                "  ├─Presentation form (2 of 2)…………………………… Map
hardcopy\n" +
+                "  └─Other citation details………………………………………
Some other details\n",
+                metadata.toString());
+    }
+
+    /**
+     * Tests {@link MetadataTreeTable#toString()}.
+     */
+    @Test
+    public void testToString() {
+        assertExpectedString(create(ValueExistencePolicy.NON_EMPTY));
+    }
+
+    /**
+     * Tests serialization.
+     *
+     * @throws Exception If an error occurred during the serialization process.
+     */
+    @Test
+    @DependsOnMethod("testToString")
+    public void testSerialization() throws Exception {
+        final Object original = create(ValueExistencePolicy.NON_EMPTY);
+        final Object deserialized;
+        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        try (ObjectOutputStream out = new ObjectOutputStream(buffer)) {
+            out.writeObject(original);
+        }
+        // Now reads the object we just serialized.
+        final byte[] data = buffer.toByteArray();
+        try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(data)))
{
+            deserialized = in.readObject();
+        }
+        assertExpectedString((MetadataTreeTable) deserialized);
+    }
+}

Propchange: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeTableTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeTableTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java?rev=1478864&r1=1478863&r2=1478864&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
[UTF-8] Fri May  3 16:23:25 2013
@@ -44,6 +44,7 @@ import org.junit.BeforeClass;
     org.apache.sis.metadata.ValueMapTest.class,
     org.apache.sis.metadata.MetadataTreeChildrenTest.class,
     org.apache.sis.metadata.MetadataTreeNodeTest.class,
+    org.apache.sis.metadata.MetadataTreeTableTest.class,
     org.apache.sis.metadata.MetadataStandardTest.class,
     org.apache.sis.metadata.PrunerTest.class,
     org.apache.sis.metadata.iso.AllMetadataTest.class



Mime
View raw message