sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1726047 - in /sis/branches/JDK8/core: sis-referencing/src/main/java/org/apache/sis/referencing/factory/ sis-referencing/src/test/java/org/apache/sis/referencing/factory/ sis-utility/src/main/java/org/apache/sis/util/resources/
Date Thu, 21 Jan 2016 18:17:04 GMT
Author: desruisseaux
Date: Thu Jan 21 18:17:04 2016
New Revision: 1726047

URL: http://svn.apache.org/viewvc?rev=1726047&view=rev
Log:
More tests for MultiAuthoritiesFactory.

Modified:
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/AuthorityFactoryProxy.java
    sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactory.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/AuthorityFactoryProxyTest.java
    sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactoryTest.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/AuthorityFactoryProxy.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/AuthorityFactoryProxy.java?rev=1726047&r1=1726046&r2=1726047&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/AuthorityFactoryProxy.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/AuthorityFactoryProxy.java
[UTF-8] Thu Jan 21 18:17:04 2016
@@ -29,6 +29,7 @@ import org.opengis.referencing.Authority
 import org.opengis.parameter.ParameterDescriptor;
 import org.opengis.metadata.extent.Extent;
 import org.opengis.util.FactoryException;
+import org.opengis.util.InternationalString;
 import org.apache.sis.util.resources.Errors;
 
 
@@ -172,7 +173,20 @@ abstract class AuthorityFactoryProxy<T>
     abstract T createFromAPI(AuthorityFactory factory, String code) throws FactoryException;
 
     /**
-     * The proxy for the {@link GeodeticAuthorityFactory#createObject} method.
+     * The proxy for the {@link GeodeticAuthorityFactory#getDescriptionText(String)} method.
+     */
+    static final AuthorityFactoryProxy<InternationalString> DESCRIPTION =
+        new AuthorityFactoryProxy<InternationalString>(InternationalString.class, AuthorityFactoryIdentifier.ANY)
{
+            @Override InternationalString create(GeodeticAuthorityFactory factory, String
code) throws FactoryException {
+                return factory.getDescriptionText(code);
+            }
+            @Override InternationalString createFromAPI(AuthorityFactory factory, String
code) throws FactoryException {
+                return factory.getDescriptionText(code);
+            }
+    };
+
+    /**
+     * The proxy for the {@link GeodeticAuthorityFactory#createObject(String)} method.
      */
     static final AuthorityFactoryProxy<IdentifiedObject> OBJECT =
         new AuthorityFactoryProxy<IdentifiedObject>(IdentifiedObject.class, AuthorityFactoryIdentifier.ANY)
{
@@ -544,7 +558,8 @@ abstract class AuthorityFactoryProxy<T>
         PARAMETER,
         UNIT,
         EXTENT,
-        OBJECT
+        OBJECT,
+        DESCRIPTION
     };
 
     /**
@@ -596,10 +611,20 @@ abstract class AuthorityFactoryProxy<T>
      * The proxy to use for a given type declared in a URN.
      * For example in the {@code "urn:ogc:def:crs:EPSG::4326"} URN, the proxy to use is {@link
#CRS}.
      *
-     * @param  type The URN type.
-     * @return The proxy for the given type, or {@code null} if none.
+     * @param  typeName The URN type.
+     * @return The proxy for the given type, or {@code null} if the given type is illegal.
      */
-    static AuthorityFactoryProxy<?> getInstance(final String type) {
-        return BY_URN_TYPE.get(type.toLowerCase(Locale.US));
+    @SuppressWarnings("unchecked")
+    final AuthorityFactoryProxy<? extends T> cast(final String typeName) {
+        final AuthorityFactoryProxy<?> c = BY_URN_TYPE.get(typeName.toLowerCase(Locale.US));
+        if (c != null) {
+            if (c.type.isAssignableFrom(type)) {
+                return this;
+            }
+            if (type.isAssignableFrom(c.type)) {
+                return (AuthorityFactoryProxy<? extends T>) c;
+            }
+        }
+        return null;
     }
 }

Modified: sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactory.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactory.java?rev=1726047&r1=1726046&r2=1726047&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactory.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/main/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactory.java
[UTF-8] Thu Jan 21 18:17:04 2016
@@ -33,6 +33,7 @@ import org.opengis.metadata.citation.Cit
 import org.opengis.metadata.extent.Extent;
 import org.opengis.parameter.ParameterDescriptor;
 import org.opengis.util.FactoryException;
+import org.opengis.util.InternationalString;
 import javax.measure.unit.Unit;
 import org.apache.sis.internal.util.Citations;
 import org.apache.sis.internal.util.DefinitionURI;
@@ -57,9 +58,13 @@ import org.apache.sis.util.iso.DefaultNa
  * then the work is delegated to that factory. Otherwise a {@link NoSuchAuthorityFactoryException}
is thrown.</p>
  *
  * <div class="section">URI syntax</div>
- * This factory can also parse URNs of the
- * {@code "urn:ogc:def:}<var>type</var>{@code :}<var>authority</var>{@code
:}<var>version</var>{@code :}<var>code</var>{@code "}
- * form and URLs of the {@code "http://www.opengis.net/gml/srs/}<var>authority</var>{@code
.xml#}<var>code</var>{@code "} form.
+ * This factory can also parse URNs of the following forms:
+ *
+ * <ul>
+ *   <li>{@code "urn:ogc:def:}<var>type</var>{@code :}<var>authority</var>{@code
:}<var>version</var>{@code :}<var>code</var>{@code "}</li>
+ *   <li>{@code "http://www.opengis.net/gml/srs/}<var>authority</var>{@code
.xml#}<var>code</var>{@code "}</li>
+ * </ul>
+ *
  * In such cases, the <var>type</var> specified in the URN may be used for invoking
a more specific method.
  * However {@code MultiAuthoritiesFactory} uses the type information in the URN only for
  * delegating to a more specific method, never for delegating to a less specific method.
@@ -85,7 +90,7 @@ import org.apache.sis.util.iso.DefaultNa
  * one for version 8.2 and another one for version 7.9 of the EPSG dataset.
  * A specific version can be requested in the URN given to {@code createFoo(String)} methods,
  * for example <code>"urn:ogc:def:crs:EPSG:<b>8.2</b>:4326"</code>.
- * If no version is given (for example {@code "urn:ogc:def:crs:EPSG::4326"}),
+ * If no version is given of if the given version is zero,
  * then the first EPSG factory in iteration order is used regardless of its version number.
  * </div>
  *
@@ -420,15 +425,21 @@ public class MultiAuthoritiesFactory ext
      * @return The object from one of the authority factory specified at construction time.
      * @throws FactoryException If an error occurred while creating the object.
      */
-    private <T> T create(final AuthorityFactoryProxy<T> proxy, String code) throws
FactoryException {
+    private <T> T create(AuthorityFactoryProxy<? extends T> proxy, String code)
throws FactoryException {
         ArgumentChecks.ensureNonNull("code", code);
         final String authority;
         String version;
         final DefinitionURI uri = DefinitionURI.parse(code);
         if (uri != null) {
+            final Class<? extends T> type = proxy.type;
             authority = uri.authority;
             version   = uri.version;
             code      = uri.code;
+            proxy     = proxy.cast(uri.type);
+            if (proxy == null) {
+                throw new NoSuchAuthorityCodeException(Errors.format(Errors.Keys.CanNotCreateObjectOfType_2,
+                        type, uri.type), uri.authority, uri.code, uri.toString());
+            }
         } else {
             /*
              * Separate the authority from the rest of the code. The authority is mandatory;
if missing,
@@ -480,6 +491,25 @@ public class MultiAuthoritiesFactory ext
     }
 
     /**
+     * Returns a description of the object corresponding to a code.
+     * The given code can use any of the following patterns, where <var>version</var>
is optional:
+     * <ul>
+     *   <li><var>authority</var>{@code :}<var>code</var></li>
+     *   <li><var>authority</var>{@code :}<var>version</var>{@code
:}<var>code</var></li>
+     *   <li>{@code urn:ogc:def:}<var>type</var>{@code :}<var>authority</var>{@code
:}<var>version</var>{@code :}<var>code</var></li>
+     *   <li>{@code http://www.opengis.net/gml/srs/}<var>authority</var>{@code
.xml#}<var>code</var></li>
+     * </ul>
+     *
+     * @return A description of the object, or {@code null} if the object
+     *         corresponding to the specified {@code code} has no description.
+     * @throws FactoryException if an error occurred while fetching the description.
+     */
+    @Override
+    public InternationalString getDescriptionText(final String code) throws FactoryException
{
+        return create(AuthorityFactoryProxy.DESCRIPTION, code);
+    }
+
+    /**
      * Creates an arbitrary object from a code.
      * The given code can use any of the following patterns, where <var>version</var>
is optional:
      * <ul>
@@ -489,8 +519,8 @@ public class MultiAuthoritiesFactory ext
      *   <li>{@code http://www.opengis.net/gml/srs/}<var>authority</var>{@code
.xml#}<var>code</var></li>
      * </ul>
      *
-     * The two first formats are ambiguous when used with this {@code createObject(String)}
because different kinds
-     * of objects can have the same code.
+     * The two first formats are ambiguous when used with this {@code createObject(String)}
method
+     * because different kinds of objects can have the same code.
      *
      * @return The object for the given code.
      * @throws FactoryException if the object creation failed.

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/AuthorityFactoryProxyTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/AuthorityFactoryProxyTest.java?rev=1726047&r1=1726046&r2=1726047&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/AuthorityFactoryProxyTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/AuthorityFactoryProxyTest.java
[UTF-8] Thu Jan 21 18:17:04 2016
@@ -67,7 +67,7 @@ public final strictfp class AuthorityFac
      * Tests {@link AuthorityFactoryProxy#getInstance(Class)}.
      */
     @Test
-    public void testGetInstanceFromType() {
+    public void testGetInstance() {
         assertEquals(ProjectedCRS.class,              AuthorityFactoryProxy.getInstance(ProjectedCRS.class)
       .type);
         assertEquals(ProjectedCRS.class,              AuthorityFactoryProxy.getInstance(DefaultProjectedCRS.class)
.type);
         assertEquals(GeographicCRS.class,             AuthorityFactoryProxy.getInstance(GeographicCRS.class)
      .type);
@@ -83,16 +83,20 @@ public final strictfp class AuthorityFac
     }
 
     /**
-     * Tests {@link AuthorityFactoryProxy#getInstance(String)}.
+     * Tests {@link AuthorityFactoryProxy#cast(String)}.
      */
     @Test
-    public void testGetInstanceFromName() {
-        assertEquals(CoordinateReferenceSystem.class, AuthorityFactoryProxy.getInstance("CRS")
     .type);
-        assertEquals(CoordinateSystem.class,          AuthorityFactoryProxy.getInstance("CS")
      .type);
-        assertEquals(CoordinateSystemAxis.class,      AuthorityFactoryProxy.getInstance("aXis")
    .type);
-        assertEquals(PrimeMeridian.class,             AuthorityFactoryProxy.getInstance("Meridian")
.type);
-        assertEquals(Ellipsoid.class,                 AuthorityFactoryProxy.getInstance("ellipsoid").type);
-        assertEquals(Datum.class,                     AuthorityFactoryProxy.getInstance("datum")
   .type);
+    public void testCast() {
+        final AuthorityFactoryProxy<IdentifiedObject> base = AuthorityFactoryProxy.OBJECT;
+        assertEquals(CoordinateReferenceSystem.class, base.cast("CRS")      .type);
+        assertEquals(CoordinateSystem.class,          base.cast("CS")       .type);
+        assertEquals(CoordinateSystemAxis.class,      base.cast("aXis")     .type);
+        assertEquals(PrimeMeridian.class,             base.cast("Meridian") .type);
+        assertEquals(Ellipsoid.class,                 base.cast("ellipsoid").type);
+        assertEquals(Datum.class,                     base.cast("datum")    .type);
+
+        assertEquals(GeodeticDatum.class, AuthorityFactoryProxy.GEODETIC_DATUM.cast("datum").type);
+        assertNull(AuthorityFactoryProxy.COORDINATE_SYSTEM.cast("datum"));
     }
 
     /**

Modified: sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactoryTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactoryTest.java?rev=1726047&r1=1726046&r2=1726047&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactoryTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-referencing/src/test/java/org/apache/sis/referencing/factory/MultiAuthoritiesFactoryTest.java
[UTF-8] Thu Jan 21 18:17:04 2016
@@ -20,6 +20,7 @@ import java.util.Arrays;
 import java.util.Collections;
 import java.util.Set;
 import org.opengis.referencing.IdentifiedObject;
+import org.opengis.referencing.NoSuchAuthorityCodeException;
 import org.opengis.referencing.crs.CRSAuthorityFactory;
 import org.opengis.referencing.crs.CoordinateReferenceSystem;
 import org.opengis.referencing.crs.GeocentricCRS;
@@ -203,6 +204,7 @@ public final strictfp class MultiAuthori
      * @throws FactoryException if an authority or a code is not recognized.
      */
     @Test
+    @DependsOnMethod("testGetAuthorityFactory")
     public void testCreateFromSimpleCodes() throws FactoryException {
         final Set<AuthorityFactoryMock> mock = Collections.singleton(new AuthorityFactoryMock("MOCK",
"2.3"));
         final MultiAuthoritiesFactory factory = new MultiAuthoritiesFactory(mock, null, mock,
null);
@@ -213,5 +215,41 @@ public final strictfp class MultiAuthori
         assertSame("Empty version", HardCodedDatum.GREENWICH, factory.createPrimeMeridian("
MoCk :: 8901"));
         assertSame("With spaces",   HardCodedCRS  .DEPTH,     factory.createVerticalCRS 
(" MoCk : : 9905"));
         assertSame("Version 0",     HardCodedDatum.SPHERE,    factory.createGeodeticDatum("MOCK:
0:6047"));
+        assertEquals("Greenwich",   factory.getDescriptionText("MOCK:8901").toString());
+        try {
+            factory.createGeodeticDatum("MOCK2:4326");
+            fail("Should not have found an object from a non-existent factory.");
+        } catch (NoSuchAuthorityFactoryException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("MOCK2"));
+        }
+    }
+
+    /**
+     * Tests {@code MultiAuthoritiesFactory.createFoo(String)} from codes in the
+     * {@code "urn:ogc:def:type:authority:version:code"} form.
+     *
+     * @throws FactoryException if an authority or a code is not recognized.
+     */
+    @Test
+    @DependsOnMethod("testCreateFromSimpleCodes")
+    public void testCreateFromURNs() throws FactoryException {
+        final Set<AuthorityFactoryMock> mock = Collections.singleton(new AuthorityFactoryMock("MOCK",
"2.3"));
+        final MultiAuthoritiesFactory factory = new MultiAuthoritiesFactory(mock, null, mock,
null);
+
+        assertSame("Straight",      HardCodedCRS  .WGS84_φλ,  factory.createGeographicCRS("urn:ogc:def:crs:MOCK::4326"));
+//      assertSame("With spaces",   HardCodedCRS  .WGS84,     factory.createGeographicCRS("
urn : ogc  : def:crs :  mock :  84 "));
+        assertSame("With version",  HardCodedDatum.WGS84,     factory.createDatum       
("urn:ogc:def:datum:mock:2.3:6326"));
+        assertSame("Empty version", HardCodedDatum.GREENWICH, factory.createObject      
("urn:ogc:def:meridian: MoCk :: 8901"));
+        assertSame("With spaces",   HardCodedCRS  .DEPTH,     factory.createVerticalCRS 
(" urn : ogc : def : crs : MoCk : : 9905"));
+//      assertSame("Version 0",     HardCodedDatum.SPHERE,    factory.createGeodeticDatum("urn:ogc:def:datum:MOCK:
0 :6047"));
+        try {
+            factory.createGeographicCRS("urn:ogc:def:datum:MOCK::4326");
+            fail("Should create an object of the wrong type.");
+        } catch (NoSuchAuthorityCodeException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("datum"));
+            assertTrue(message, message.contains("GeographicCRS"));
+        }
     }
 }

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java?rev=1726047&r1=1726046&r2=1726047&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.java
[UTF-8] Thu Jan 21 18:17:04 2016
@@ -121,6 +121,11 @@ public final class Errors extends Indexe
         public static final short CanNotConvertValue_2 = 4;
 
         /**
+         * Can not create an object of type “{1}” as an instance of ‘{0}’.
+         */
+        public static final short CanNotCreateObjectOfType_2 = 215;
+
+        /**
          * Can not instantiate an object of type ‘{0}’.
          */
         public static final short CanNotInstantiate_1 = 5;

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties?rev=1726047&r1=1726046&r2=1726047&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors.properties
[ISO-8859-1] Thu Jan 21 18:17:04 2016
@@ -35,6 +35,7 @@ CanNotConvertFromType_2           = Can
 CanNotConvertValue_2              = Can not convert value \u201c{0}\u201d to type \u2018{1}\u2019.
 CanNotCompute_1                   = Can not compute \u201c{0}\u201d.
 CanNotComputeDerivative           = Can not compute the derivative.
+CanNotCreateObjectOfType_2        = Can not create an object of type \u201c{1}\u201d as an
instance of \u2018{0}\u2019.
 CanNotInstantiate_1               = Can not instantiate an object of type \u2018{0}\u2019.
 CanNotMapAxisToDirection_2        = Can not map an axis from \u201c{0}\u201d to direction
\u201c{1}\u201d.
 CanNotOpen_1                      = Can not open \u201c{0}\u201d.

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties?rev=1726047&r1=1726046&r2=1726047&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/util/resources/Errors_fr.properties
[ISO-8859-1] Thu Jan 21 18:17:04 2016
@@ -32,6 +32,7 @@ CanNotConvertFromType_2           = Ne p
 CanNotConvertValue_2              = La valeur \u00ab\u202f{0}\u202f\u00bb ne peut pas \u00eatre
convertie vers le type \u2018{1}\u2019.
 CanNotCompute_1                   = Ne peut pas calculer \u00ab\u202f{0}\u202f\u00bb.
 CanNotComputeDerivative           = La d\u00e9riv\u00e9 ne peut pas \u00eatre calcul\u00e9e.
+CanNotCreateObjectOfType_2        = Ne peut pas cr\u00e9er un objet de type \u00ab\u202f{1}\u202f\u00bb
comme une instance de \u2018{0}\u2019.
 CanNotInstantiate_1               = Ne peut pas cr\u00e9er un objet de type \u2018{0}\u2019.
 CanNotMapAxisToDirection_2        = Aucun axe de \u00ab\u202f{0}\u202f\u00bb n\u2019a pu
\u00eatre associ\u00e9 \u00e0 la direction \u00ab\u202f{1}\u202f\u00bb.
 CanNotOpen_1                      = Ne peut pas ouvrir \u00ab\u202f{0}\u202f\u00bb.



Mime
View raw message