sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1609095 - in /sis/branches/JDK8/core/sis-utility/src: main/java/org/apache/sis/util/iso/ test/java/org/apache/sis/test/suite/ test/java/org/apache/sis/util/iso/
Date Wed, 09 Jul 2014 11:01:08 GMT
Author: desruisseaux
Date: Wed Jul  9 11:01:07 2014
New Revision: 1609095

URL: http://svn.apache.org/r1609095
Log:
Completed DefaultRecord implementation (except for XML (un)marshalling) and added tests.

Added:
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordTest.java
  (with props)
Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultLocalName.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultMemberName.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultNameFactory.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultNameSpace.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecord.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecordSchema.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecordType.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultScopedName.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultTypeName.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/Names.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/RecordDefinition.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/package-info.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordSchemaTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultLocalName.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultLocalName.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultLocalName.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultLocalName.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -60,6 +60,9 @@ import java.util.Objects;
  * @since   0.3 (derived from geotk-2.1)
  * @version 0.3
  * @module
+ *
+ * @see DefaultNameSpace
+ * @see DefaultScopedName
  */
 @XmlType(name = "LocalName") // Actually 'gml:CodeType', but the later is used elsewhere.
 @XmlRootElement(name = "LocalName")

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultMemberName.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultMemberName.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultMemberName.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultMemberName.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -46,6 +46,10 @@ import java.util.Objects;
  * @since   0.3 (derived from geotk-3.17)
  * @version 0.5
  * @module
+ *
+ * @see DefaultTypeName
+ * @see DefaultNameFactory
+ * @see DefaultRecordType
  */
 @XmlRootElement(name = "MemberName")
 public class DefaultMemberName extends DefaultLocalName implements MemberName {

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultNameFactory.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultNameFactory.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultNameFactory.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultNameFactory.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -72,6 +72,13 @@ import static org.apache.sis.util.iso.De
  * @since   0.3 (derived from geotk-2.1)
  * @version 0.5
  * @module
+ *
+ * @see Names
+ * @see DefaultNameSpace
+ * @see DefaultScopedName
+ * @see DefaultLocalName
+ * @see DefaultTypeName
+ * @see DefaultMemberName
  */
 public class DefaultNameFactory extends AbstractFactory implements NameFactory {
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultNameSpace.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultNameSpace.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultNameSpace.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultNameSpace.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -55,6 +55,12 @@ import java.util.Objects;
  * @since   0.3 (derived from geotk-3.00)
  * @version 0.3
  * @module
+ *
+ * @see DefaultScopedName
+ * @see DefaultLocalName
+ * @see DefaultTypeName
+ * @see DefaultMemberName
+ * @see DefaultNameFactory
  */
 public class DefaultNameSpace implements NameSpace, Serializable {
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecord.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecord.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecord.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecord.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -22,10 +22,12 @@ import java.util.AbstractMap;
 import java.util.AbstractSet;
 import java.util.Iterator;
 import java.util.NoSuchElementException;
+import java.util.Arrays;
 import java.io.Serializable;
 import org.opengis.util.MemberName;
 import org.opengis.util.Record;
 import org.opengis.util.RecordType;
+import org.apache.sis.util.Debug;
 import org.apache.sis.util.ArgumentChecks;
 import org.apache.sis.util.resources.Errors;
 
@@ -47,6 +49,9 @@ import org.apache.sis.util.resources.Err
  * @since   0.5
  * @version 0.5
  * @module
+ *
+ * @see DefaultRecordType
+ * @see DefaultRecordSchema
  */
 public class DefaultRecord implements Record, Serializable {
     /**
@@ -166,7 +171,7 @@ public class DefaultRecord implements Re
         @Override
         public Map.Entry<MemberName, Object> next() {
             if (hasNext()) {
-                return new Entry(index);
+                return new Entry(index++);
             }
             throw new NoSuchElementException();
         }
@@ -244,4 +249,71 @@ public class DefaultRecord implements Re
         }
         values[index] = value;
     }
+
+    /**
+     * Sets all attribute values in this record, in attribute order.
+     *
+     * @param  newValues The attribute values.
+     * @throws IllegalArgumentException if the given number of values does not match the
expected number.
+     * @throws ClassCastException if a value is not an instance of the expected type for
this record.
+     */
+    public void setValues(final Object... newValues) {
+        ArgumentChecks.ensureNonNull("values", newValues);
+        if (newValues.length != values.length) {
+            throw new IllegalArgumentException(Errors.format(
+                    Errors.Keys.UnexpectedArrayLength_2, values.length, newValues.length));
+        }
+        for (int i=0; i<newValues.length; i++) {
+            final Object value = newValues[i];
+            if (value != null) {
+                final Class<?> valueClass = definition.getValueClass(i);
+                if (valueClass != null && !valueClass.isInstance(value)) {
+                    throw new ClassCastException(Errors.format(Errors.Keys.IllegalPropertyClass_2,
+                            definition.getName(i), value.getClass()));
+                }
+            }
+            values[i] = value;
+        }
+    }
+
+    /**
+     * Compares this record with the given object for equality.
+     *
+     * @param  object The object to compare with this record for equality.
+     * @return {@code true} if both objects are equal.
+     */
+    @Override
+    public boolean equals(final Object object) {
+        if (object == this) {
+            return true; // Slight optimization for a common case.
+        }
+        if (object != null && object.getClass() == getClass()) {
+            final DefaultRecord that = (DefaultRecord) object;
+            return definition.getRecordType().equals(that.definition.getRecordType()) &&
+                   Arrays.equals(values, that.values);
+        }
+        return false;
+    }
+
+    /**
+     * Returns a hash code value for this record.
+     *
+     * @return A hash code value for this record.
+     */
+    @Override
+    public int hashCode() {
+        return Arrays.hashCode(values) ^ definition.getRecordType().hashCode();
+    }
+
+    /**
+     * Returns a string representation of this record.
+     * The string representation is for debugging purpose and may change in any future SIS
version.
+     *
+     * @return A string representation of this record.
+     */
+    @Debug
+    @Override
+    public String toString() {
+        return definition.toString("Record", values);
+    }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecordSchema.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecordSchema.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecordSchema.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecordSchema.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -74,6 +74,9 @@ import org.apache.sis.internal.converter
  * @since   0.5
  * @version 0.5
  * @module
+ *
+ * @see DefaultRecordType
+ * @see DefaultRecord
  */
 public class DefaultRecordSchema implements RecordSchema {
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecordType.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecordType.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecordType.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultRecordType.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -91,6 +91,10 @@ import java.util.Objects;
  * @since   0.3
  * @version 0.5
  * @module
+ *
+ * @see DefaultRecord
+ * @see DefaultRecordSchema
+ * @see DefaultMemberName
  */
 @XmlType(name = "RecordType")
 public class DefaultRecordType extends RecordDefinition implements RecordType, Serializable
{

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultScopedName.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultScopedName.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultScopedName.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultScopedName.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -52,6 +52,9 @@ import org.apache.sis.internal.util.Unmo
  * @since   0.3 (derived from geotk-2.1)
  * @version 0.3
  * @module
+ *
+ * @see DefaultNameSpace
+ * @see DefaultLocalName
  */
 @XmlType(name = "ScopedName") // Actually 'gml:CodeType', but the later is used elsewhere.
 @XmlRootElement(name = "ScopedName")

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultTypeName.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultTypeName.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultTypeName.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/DefaultTypeName.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -39,6 +39,9 @@ import org.opengis.util.NameSpace;
  * @since   0.3 (derived from geotk-3.00)
  * @version 0.3
  * @module
+ *
+ * @see DefaultMemberName
+ * @see DefaultNameFactory
  */
 @XmlRootElement(name = "TypeName")
 public class DefaultTypeName extends DefaultLocalName implements TypeName {

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/Names.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/Names.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/Names.java [UTF-8]
(original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/Names.java [UTF-8]
Wed Jul  9 11:01:07 2014
@@ -71,6 +71,13 @@ import static org.apache.sis.util.Argume
  * @since   0.5
  * @version 0.5
  * @module
+ *
+ * @see DefaultNameFactory
+ * @see DefaultNameSpace
+ * @see DefaultScopedName
+ * @see DefaultLocalName
+ * @see DefaultTypeName
+ * @see DefaultMemberName
  */
 public final class Names extends Static {
     /**

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/RecordDefinition.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/RecordDefinition.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/RecordDefinition.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/RecordDefinition.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -198,22 +198,39 @@ abstract class RecordDefinition { // Int
     /**
      * Returns a string representation of this object.
      * The string representation is for debugging purpose and may change in any future SIS
version.
+     *
+     * @return A string representation of this record type.
      */
     @Debug
     @Override
     public String toString() {
+        return toString("RecordType", null);
+    }
+
+    /**
+     * Returns a string representation of a {@code Record} or {@code RecordType}.
+     *
+     * @param  head   Either {@code "Record"} or {@code "RecordType"}.
+     * @param  values The values, or {@code null} for writing the types instead.
+     * @return The string representation.
+     */
+    final String toString(final String head, final Object[] values) {
         final StringBuilder buffer = new StringBuilder(250);
         final String lineSeparator = System.lineSeparator();
         final String[] names = new String[members.length];
         int width = 0;
-        buffer.append("RecordType[“").append(getRecordType().getTypeName()).append("”]
{").append(lineSeparator);
+        buffer.append(head).append("[“").append(getRecordType().getTypeName()).append("”]
{").append(lineSeparator);
         for (int i=0; i<names.length; i++) {
             width = Math.max(width, (names[i] = members[i].toString()).length());
         }
         for (int i=0; i<names.length; i++) {
             final String name = names[i];
-            buffer.append("    ").append(name).append(CharSequences.spaces(width - name.length()))
-                  .append(" : ").append(members[i].getAttributeType()).append(lineSeparator);
+            buffer.append("    ").append(name);
+            final Object value = (values != null) ? values[i] : members[i].getAttributeType();
+            if (value != null) {
+                buffer.append(CharSequences.spaces(width - name.length())).append(" : ").append(value);
+            }
+            buffer.append(lineSeparator);
         }
         return buffer.append('}').append(lineSeparator).toString();
     }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/package-info.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/package-info.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/package-info.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/package-info.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -38,6 +38,13 @@
  *       <li>{@link org.apache.sis.util.iso.DefaultScopedName} for a composite of a
<cite>head</cite> name and a <cite>tail</cite> name.</li>
  *     </ul>
  *   </li>
+ *   <li>Implementations of {@link org.opengis.util.Record} and related classes:
+ *     <ul>
+ *       <li>{@link org.apache.sis.util.iso.DefaultRecord}       for a list of logically
related elements as (<var>name</var>, <var>value</var>) pairs.</li>
+ *       <li>{@link org.apache.sis.util.iso.DefaultRecordType}   for definition of
the type of a {@code Record}.</li>
+ *       <li>{@link org.apache.sis.util.iso.DefaultRecordSchema} for a collection of
{@code RecordType}s in a given namespace.</li>
+ *     </ul>
+ *   </li>
  *   <li>Static utility methods:
  *     <ul>
  *       <li>{@link org.apache.sis.util.iso.Types} for working with UML identifiers
and description of GeoAPI types.</li>

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -89,6 +89,7 @@ import org.junit.BeforeClass;
     org.apache.sis.internal.simple.SimpleReferenceIdentifierTest.class,
     org.apache.sis.util.iso.DefaultRecordTypeTest.class,
     org.apache.sis.util.iso.DefaultRecordSchemaTest.class,
+    org.apache.sis.util.iso.DefaultRecordTest.class,
 
     // Measurements and formatting.
     org.apache.sis.measure.SexagesimalConverterTest.class,

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordSchemaTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordSchemaTest.java?rev=1609095&r1=1609094&r2=1609095&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordSchemaTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordSchemaTest.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -21,9 +21,9 @@ import java.util.LinkedHashMap;
 import org.opengis.util.Type;
 import org.opengis.util.RecordType;
 import org.opengis.util.MemberName;
+import org.apache.sis.internal.simple.SimpleAttributeType;
 
 // Test imports
-import org.apache.sis.internal.simple.SimpleAttributeType;
 import org.junit.Test;
 import org.apache.sis.test.TestCase;
 import org.apache.sis.test.DependsOn;
@@ -62,10 +62,10 @@ public final strictfp class DefaultRecor
         assertSame("container", schema, recordType.getContainer());
         assertEquals("typeName", Names.createTypeName("MySchema", ":", "MyRecordType"), recordType.getTypeName());
         int count = 0;
-        for (final Map.Entry<MemberName, Type> entry : recordType.getMemberTypes().entrySet())
{
-            final String     expectedName;
-            final String     expectedType;
-            final Class<?>   expectedClass;
+        for (final Map.Entry<MemberName,Type> entry : recordType.getMemberTypes().entrySet())
{
+            final String   expectedName;
+            final String   expectedType;
+            final Class<?> expectedClass;
             switch (count) {
                 case 0: {
                     expectedName  = "city";

Added: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordTest.java?rev=1609095&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordTest.java
(added)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordTest.java
[UTF-8] Wed Jul  9 11:01:07 2014
@@ -0,0 +1,167 @@
+/*
+ * 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.util.iso;
+
+
+import java.util.LinkedHashMap;
+import java.util.Map;
+import org.opengis.util.MemberName;
+import org.opengis.util.RecordType;
+
+// Test imports
+import org.junit.Test;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.apache.sis.test.TestCase;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.DependsOnMethod;
+
+import static org.apache.sis.test.Assert.*;
+
+
+/**
+ * Tests the {@link DefaultRecord} implementation.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.5
+ * @version 0.5
+ * @module
+ */
+@DependsOn(DefaultRecordSchemaTest.class)
+public final strictfp class DefaultRecordTest extends TestCase {
+    /**
+     * The record type to be shared by all tests.
+     */
+    private static RecordType recordType;
+
+    /**
+     * Creates the {@link DefaultRecordType} to be used by all tests in this class.
+     */
+    @BeforeClass
+    public static void createRecordType() {
+        final DefaultRecordSchema schema = new SerializableRecordSchema("MySchema");
+        final Map<CharSequence,Class<?>> members = new LinkedHashMap<>(8);
+        assertNull(members.put("city",       String.class));
+        assertNull(members.put("latitude",   Double.class));
+        assertNull(members.put("longitude",  Double.class));
+        assertNull(members.put("population", Integer.class));
+        recordType = schema.createRecordType("MyRecordType", members);
+        SerializableRecordSchema.INSTANCE = schema;
+    }
+
+    /**
+     * Clears the {@link DefaultRecordType} used by the tests.
+     */
+    @AfterClass
+    public static void clearRecordType() {
+        SerializableRecordSchema.INSTANCE = null;
+        recordType = null;
+    }
+
+    /**
+     * Tests {@link DefaultRecord#setValues(Object[])}.
+     */
+    @Test
+    public void testSetValues() {
+        final DefaultRecord record = new DefaultRecord(recordType);
+        try {
+            record.setValues("Machu Picchu", -13.1639, -72.5468);
+            fail("Shall not accept array of illegal length.");
+        } catch (IllegalArgumentException e) {
+            assertNotNull(e.getMessage());
+        }
+        try {
+            record.setValues("Machu Picchu", -13.1639, -72.5468, "Unknown");
+            fail("Shall not accept 'population' value of class String.");
+        } catch (ClassCastException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("population"));
+            assertTrue(message, message.contains("String"));
+        }
+        final Object[] values = {"Machu Picchu", -13.1639, -72.5468, null};
+        record.setValues(values);
+        assertArrayEquals(values, record.getAttributes().values().toArray());
+    }
+
+    /**
+     * Tests iteration over the {@link DefaultRecord#getAttributes()} elements.
+     */
+    @Test
+    public void testAttributes() {
+        final DefaultRecord record = new DefaultRecord(recordType);
+        assertSame("recordType", recordType, record.getRecordType());
+        int index = 0;
+        for (final Map.Entry<MemberName,Object> entry : record.getAttributes().entrySet())
{
+            final String name;
+            final Object value;
+            switch (index) {
+                case 0: name = "city";       value = "Machu Picchu"; break;
+                case 1: name = "latitude";   value = -13.1639;       break;
+                case 2: name = "longitude";  value = -72.5468;       break;
+                case 3: name = "population"; value = null;           break;
+                default: throw new AssertionError(index);
+            }
+            assertEquals(name, entry.getKey().toString());
+            assertNull  (name, entry.getValue());
+            assertNull  (name, entry.setValue(value));
+            assertEquals(name, value, entry.getValue());
+            index++;
+        }
+        assertEquals(4, index);
+    }
+
+    /**
+     * Tests {@link DefaultRecord#toString()}.
+     */
+    @Test
+    @DependsOnMethod("testSetValues")
+    public void testToString() {
+        final DefaultRecord record = new DefaultRecord(recordType);
+        record.setValues("Machu Picchu", -13.1639, -72.5468, null);
+        assertMultilinesEquals(
+                "Record[“MyRecordType”] {\n" +
+                "    city       : Machu Picchu\n" +
+                "    latitude   : -13.1639\n" +
+                "    longitude  : -72.5468\n" +
+                "    population\n" +
+                "}\n",
+                record.toString());
+        /*
+         * Opportunist test of RecordType.toString(),
+         * which share the same implementation code.
+         */
+        assertMultilinesEquals(
+                "RecordType[“MyRecordType”] {\n" +
+                "    city       : CharacterString\n" +
+                "    latitude   : Real\n" +
+                "    longitude  : Real\n" +
+                "    population : Integer\n" +
+                "}\n",
+                record.getRecordType().toString());
+    }
+
+    /**
+     * Tests serialization of a {@link DefaultRecord}.
+     */
+    @Test
+    @DependsOnMethod("testSetValues")
+    public void testSerialization() {
+        final DefaultRecord record = new DefaultRecord(recordType);
+        record.setValues("Machu Picchu", -13.1639, -72.5468, null);
+        assertNotSame(record, assertSerializedEquals(record));
+    }
+}

Propchange: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultRecordTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8



Mime
View raw message