sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1799335 - 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 Tue, 20 Jun 2017 10:38:29 GMT
Author: desruisseaux
Date: Tue Jun 20 10:38:28 2017
New Revision: 1799335

URL: http://svn.apache.org/viewvc?rev=1799335&view=rev
Log:
Allow two GenericName instances to be considered equal even if one of them contains String
and the other InternationalString parts.

Added:
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultLocalNameTest.java
      - copied, changed from r1799334, sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/AbstractNameTest.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultScopedNameTest.java
      - copied, changed from r1799173, sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/AbstractNameTest.java
Removed:
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/AbstractNameTest.java
Modified:
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractName.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/test/java/org/apache/sis/test/suite/UtilityTestSuite.java
    sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultNameFactoryTest.java

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractName.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractName.java?rev=1799335&r1=1799334&r2=1799335&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractName.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/iso/AbstractName.java
[UTF-8] Tue Jun 20 10:38:28 2017
@@ -316,9 +316,11 @@ public abstract class AbstractName imple
             }
             asString = buffer.toString();
         }
-        // Note: there is no need to invoke InternationalString.toString(Locale.ROOT) for
-        // the unlocalized version, because our International inner class is implemented
in
-        // such a way that InternationalString.toString() returns AbstractName.toString().
+        /*
+         * Note: there is no need to invoke InternationalString.toString(Locale.ROOT) for
+         * the unlocalized version, because our International inner class is implemented
in
+         * such a way that InternationalString.toString() returns AbstractName.toString().
+         */
         return asString.toString();
     }
 

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=1799335&r1=1799334&r2=1799335&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] Tue Jun 20 10:38:28 2017
@@ -313,8 +313,10 @@ public class DefaultNameFactory extends
             }
         }
         if (names.size() == 1) {
-            // Preserves the InternationalString (current implementation of
-            // the parsing code above has lost the internationalization).
+            /*
+             * Preserves the InternationalString (current implementation of
+             * the parsing code above has lost the internationalization).
+             */
             return createLocalName(scope, name);
         }
         return createGenericName(scope, names.toArray(new String[names.size()]));

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=1799335&r1=1799334&r2=1799335&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] Tue Jun 20 10:38:28 2017
@@ -155,23 +155,34 @@ public class DefaultNameSpace implements
      *          the separator to insert between the {@linkplain AbstractName#getParsedNames()
      *          parsed names} of any name in that namespace.
      */
-    protected DefaultNameSpace(final DefaultNameSpace parent, CharSequence name,
+    protected DefaultNameSpace(final DefaultNameSpace parent, final CharSequence name,
                                final String headSeparator, final String separator)
     {
         this.parent = (parent != GlobalNameSpace.GLOBAL) ? parent : null;
         ensureNonNull("name",          name);
         ensureNonNull("headSeparator", headSeparator);
         ensureNonNull("separator",     separator);
-        if (!(name instanceof InternationalString)) {
-            name = name.toString();
-        }
-        this.name          = name;
+        this.name          = simplify(name);
         this.headSeparator = headSeparator;
         this.separator     = separator;
         init();
     }
 
     /**
+     * Converts the given name to its {@link String} representation if that name is not an
{@link InternationalString}
+     * instance from which this {@code DefaultNameSpace} implementation can extract useful
information. For example if
+     * the given name is a {@link SimpleInternationalString}, that international string does
not give more information
+     * than the {@code String} that it wraps. Using the {@code String} as the canonical value
increase the chances that
+     * {@link #equals(Object)} detect that two {@code GenericName} instances are equal.
+     */
+    private static CharSequence simplify(CharSequence name) {
+        if (!(name instanceof InternationalString) || name.getClass() == SimpleInternationalString.class)
{
+            name = name.toString();
+        }
+        return name;
+    }
+
+    /**
      * Initializes the transient fields.
      */
     private void init() {
@@ -386,6 +397,8 @@ public class DefaultNameSpace implements
         ensureNonNull("key", key);
         if (name == null) {
             name = key;
+        } else {
+            name = simplify(name);
         }
         final WeakValueHashMap<String,Object> childs = this.childs;     // Paranoiac
protection against accidental changes.
         DefaultNameSpace child;
@@ -433,7 +446,7 @@ public class DefaultNameSpace implements
             final Object existing = childs.get(key);
             if (existing instanceof DefaultLocalName) {
                 child = (DefaultLocalName) existing;
-                if (name.equals(child.name)) {
+                if (simplify(name).equals(child.name)) {
                     assert (child.scope != null ? child.scope : GlobalNameSpace.GLOBAL) ==
this;
                     return child;
                 }

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=1799335&r1=1799334&r2=1799335&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] Tue Jun 20 10:38:28 2017
@@ -93,7 +93,8 @@ import org.junit.BeforeClass;
     org.apache.sis.util.iso.SimpleInternationalStringTest.class,
     org.apache.sis.util.iso.DefaultInternationalStringTest.class,
     org.apache.sis.internal.util.LocalizedParseExceptionTest.class,
-    org.apache.sis.util.iso.AbstractNameTest.class,
+    org.apache.sis.util.iso.DefaultLocalNameTest.class,
+    org.apache.sis.util.iso.DefaultScopedNameTest.class,
     org.apache.sis.util.iso.DefaultNameFactoryTest.class,
     org.apache.sis.util.iso.NamesTest.class,
     org.apache.sis.util.iso.TypeNamesTest.class,

Copied: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultLocalNameTest.java
(from r1799334, sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/AbstractNameTest.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultLocalNameTest.java?p2=sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultLocalNameTest.java&p1=sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/AbstractNameTest.java&r1=1799334&r2=1799335&rev=1799335&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/AbstractNameTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultLocalNameTest.java
[UTF-8] Tue Jun 20 10:38:28 2017
@@ -16,8 +16,6 @@
  */
 package org.apache.sis.util.iso;
 
-import java.util.Arrays;
-import org.opengis.util.GenericName;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
@@ -28,16 +26,16 @@ import static org.apache.sis.util.iso.De
 
 
 /**
- * Tests the {@link DefaultLocalName} and {@link DefaultScopedName} implementations.
- * This test suite instantiate the objects directly, without using {@link DefaultNameFactory}.
+ * Tests the {@link DefaultLocalName} implementation.
+ * Those tests instantiate the objects directly, without using {@link DefaultNameFactory}.
  * For tests using the name factory, see {@link DefaultNameFactoryTest}.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.3
+ * @version 0.8
  * @since   0.3
  * @module
  */
-public final strictfp class AbstractNameTest extends TestCase {
+public final strictfp class DefaultLocalNameTest extends TestCase {
     /**
      * Tests the creation of a local name in the global namespace.
      * The fully qualified name is {@code "EPSG"}.
@@ -72,40 +70,4 @@ public final strictfp class AbstractName
         assertNotSame(name, assertSerializedEquals(name));
         validate(name); // GeoAPI tests.
     }
-
-    /**
-     * Tests the creation of a scoped name in a new namespace.
-     * The fully qualified name is {@code "urn:ogc:def:crs:epsg:4326"}.
-     */
-    @Test
-    public void testUrnNamespace() {
-        final String[] parsed = new String[] {
-            "urn","ogc","def","crs","epsg","4326"
-        };
-        GenericName name = new DefaultScopedName(null, Arrays.asList(parsed));
-        assertSame(name, name.toFullyQualifiedName());
-        assertEquals("urn:ogc:def:crs:epsg:4326", name.toString());
-        assertNotSame(name, assertSerializedEquals(name));
-        validate(name); // GeoAPI tests.
-        for (int i=parsed.length; --i>=0;) {
-            name = name.tip();
-            validate(name);
-            assertSame(parsed[i], name.toString());
-            name = name.scope().name();
-        }
-    }
-
-    /**
-     * Tests navigation in a name parsed from a string.
-     */
-    @Test
-    public void testNavigating() {
-        final DefaultNameFactory factory = new DefaultNameFactory();
-        final GenericName name = factory.parseGenericName(null, "codespace:subspace:name");
-        assertEquals("codespace:subspace:name", name.toString());
-        assertEquals("codespace:subspace",      name.tip().scope().name().toString());
-        assertEquals("codespace",               name.tip().scope().name().tip().scope().name().toString());
-        assertSame(name, name.toFullyQualifiedName());
-        assertSame(name, name.tip().toFullyQualifiedName());
-    }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultNameFactoryTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultNameFactoryTest.java?rev=1799335&r1=1799334&r2=1799335&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultNameFactoryTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultNameFactoryTest.java
[UTF-8] Tue Jun 20 10:38:28 2017
@@ -16,12 +16,16 @@
  */
 package org.apache.sis.util.iso;
 
-import org.junit.runner.RunWith;
-import org.junit.BeforeClass;
-import org.junit.AfterClass;
+import org.opengis.util.GenericName;
 import org.opengis.test.util.NameTest;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestRunner;
+import org.junit.runner.RunWith;
+import org.junit.BeforeClass;
+import org.junit.AfterClass;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
 
 
 /**
@@ -29,12 +33,12 @@ import org.apache.sis.test.TestRunner;
  * a {@link DefaultNameFactory} instance shared for all tests in this class.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.3
+ * @version 0.8
  * @since   0.3
  * @module
  */
 @RunWith(TestRunner.class)
-@DependsOn(AbstractNameTest.class)
+@DependsOn({DefaultLocalNameTest.class, DefaultScopedNameTest.class})
 public final strictfp class DefaultNameFactoryTest extends NameTest {
     /**
      * The factory to test.
@@ -63,4 +67,33 @@ public final strictfp class DefaultNameF
     public static void disposeFactory() {
         factorySIS = null;
     }
+
+    /**
+     * Tests navigation in a name parsed from a string.
+     */
+    @Test
+    public void testNavigation() {
+        final GenericName name = factory.parseGenericName(null, "codespace:subspace:name");
+        assertEquals("codespace:subspace:name", name.toString());
+        assertEquals("codespace:subspace",      name.tip().scope().name().toString());
+        assertEquals("codespace",               name.tip().scope().name().tip().scope().name().toString());
+        assertSame(name, name.toFullyQualifiedName());
+        assertSame(name, name.tip().toFullyQualifiedName());
+    }
+
+    /**
+     * Tests the creation of scoped names where different parts of the name are {@link SimpleInternationalString}
+     * instances. The implementation should be able to detect that the names and their hash
codes are equal.
+     *
+     * @see DefaultScopedNameTest#testSimpleInternationalString()
+     */
+    @Test
+    public void testSimpleInternationalString() {
+        GenericName n1 = factory.createGenericName(null, "ns1", "Route");
+        GenericName n2 = factory.createGenericName(null, new SimpleInternationalString("ns1"),
"Route");
+        GenericName n3 = factory.createGenericName(null, "ns1", new SimpleInternationalString("Route"));
+        assertSame(n1, n2);
+        assertSame(n1, n3);
+        assertSame(n2, n3);
+    }
 }

Copied: sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultScopedNameTest.java
(from r1799173, sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/AbstractNameTest.java)
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultScopedNameTest.java?p2=sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultScopedNameTest.java&p1=sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/AbstractNameTest.java&r1=1799173&r2=1799335&rev=1799335&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/AbstractNameTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/test/java/org/apache/sis/util/iso/DefaultScopedNameTest.java
[UTF-8] Tue Jun 20 10:38:28 2017
@@ -18,61 +18,26 @@ package org.apache.sis.util.iso;
 
 import java.util.Arrays;
 import org.opengis.util.GenericName;
+import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
 import static org.opengis.test.Validators.*;
 import static org.apache.sis.test.Assert.*;
-import static org.apache.sis.internal.util.Constants.EPSG;
-import static org.apache.sis.util.iso.DefaultNameSpace.DEFAULT_SEPARATOR_STRING;
 
 
 /**
- * Tests the {@link DefaultLocalName} and {@link DefaultScopedName} implementations.
- * This test suite instantiate the objects directly, without using {@link DefaultNameFactory}.
+ * Tests the {@link DefaultScopedName} implementations.
+ * Those tests instantiate the objects directly, without using {@link DefaultNameFactory}.
  * For tests using the name factory, see {@link DefaultNameFactoryTest}.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.3
+ * @version 0.8
  * @since   0.3
  * @module
  */
-public final strictfp class AbstractNameTest extends TestCase {
-    /**
-     * Tests the creation of a local name in the global namespace.
-     * The fully qualified name is {@code "EPSG"}.
-     */
-    @Test
-    public void testGlobalNamespace() {
-        final DefaultLocalName name = new DefaultLocalName(null, EPSG);
-        assertSame(EPSG, name.toString());
-        assertSame(EPSG, name.toInternationalString().toString());
-        assertSame(GlobalNameSpace.GLOBAL, name.scope());
-        assertNotSame(name, assertSerializedEquals(name));
-        validate(name); // GeoAPI tests.
-    }
-
-    /**
-     * Tests the creation of a local name in a new namespace.
-     * The fully qualified name is {@code "EPSG:4326"}.
-     * The tail and the tip are both local names.
-     */
-    @Test
-    public void testEpsgNamespace() {
-        final DefaultNameSpace ns = DefaultNameSpace.forName(new DefaultLocalName(null, EPSG),
-                DEFAULT_SEPARATOR_STRING, DEFAULT_SEPARATOR_STRING);
-        assertSame(EPSG, ns.name().toString());
-        validate(ns); // GeoAPI tests.
-
-        final String WGS84 = "4326";
-        final DefaultLocalName name = new DefaultLocalName(ns, WGS84);
-        assertSame(ns, name.scope());
-        assertSame(WGS84, name.toString());
-        assertEquals(EPSG + ':' + WGS84, name.toFullyQualifiedName().toString());
-        assertNotSame(name, assertSerializedEquals(name));
-        validate(name); // GeoAPI tests.
-    }
-
+@DependsOn(DefaultLocalNameTest.class)
+public final strictfp class DefaultScopedNameTest extends TestCase {
     /**
      * Tests the creation of a scoped name in a new namespace.
      * The fully qualified name is {@code "urn:ogc:def:crs:epsg:4326"}.
@@ -96,16 +61,27 @@ public final strictfp class AbstractName
     }
 
     /**
-     * Tests navigation in a name parsed from a string.
+     * Tests the creation of scoped names where different parts of the name are {@link SimpleInternationalString}
+     * instances. The implementation should be able to detect that the names and their hash
codes are equal.
+     *
+     * @see DefaultNameFactoryTest#testSimpleInternationalString()
      */
     @Test
-    public void testNavigating() {
-        final DefaultNameFactory factory = new DefaultNameFactory();
-        final GenericName name = factory.parseGenericName(null, "codespace:subspace:name");
-        assertEquals("codespace:subspace:name", name.toString());
-        assertEquals("codespace:subspace",      name.tip().scope().name().toString());
-        assertEquals("codespace",               name.tip().scope().name().tip().scope().name().toString());
-        assertSame(name, name.toFullyQualifiedName());
-        assertSame(name, name.tip().toFullyQualifiedName());
+    public void testSimpleInternationalString() {
+        GenericName n1 = new DefaultScopedName(null, Arrays.asList("ns1", "Route"));
+        GenericName n2 = new DefaultScopedName(null, Arrays.asList(new SimpleInternationalString("ns1"),
"Route"));
+        GenericName n3 = new DefaultScopedName(null, Arrays.asList("ns1", new SimpleInternationalString("Route")));
+        assertNameEqual(n1, n2);
+        assertNameEqual(n1, n3);
+        assertNameEqual(n2, n3);
+    }
+
+    /**
+     * Verifies that the following names are equal and have the same hash code.
+     */
+    private static void assertNameEqual(final GenericName n1, final GenericName n2) {
+        assertEquals("equals(Object)", n1, n2);
+        assertEquals("equals(Object)", n2, n1);
+        assertEquals("hashCode()", n1.hashCode(), n2.hashCode());
     }
 }



Mime
View raw message