sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1754409 - in /sis/branches/JDK8/core/sis-feature/src: main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java
Date Thu, 28 Jul 2016 13:17:02 GMT
Author: desruisseaux
Date: Thu Jul 28 13:17:02 2016
New Revision: 1754409

URL: http://svn.apache.org/viewvc?rev=1754409&view=rev
Log:
Fix a NullPointerException when building a FeatureType where the first attribute is a property
named "@geometry".

Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java?rev=1754409&r1=1754408&r2=1754409&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/builder/FeatureTypeBuilder.java
[UTF-8] Thu Jul 28 13:17:02 2016
@@ -614,9 +614,10 @@ public class FeatureTypeBuilder extends
                 identifierTypes = new PropertyType[identifierCount];
             }
             if (defaultGeometry != null) {
-                envelopeIndex = numSynthetic;
-                geometryIndex = numSynthetic + 1;
-                numSynthetic += 2;
+                envelopeIndex = numSynthetic++;
+                if (!AttributeConvention.GEOMETRY_PROPERTY.equals(defaultGeometry.getName()))
{
+                    geometryIndex = numSynthetic++;
+                }
             }
             final PropertyType[] propertyTypes = new PropertyType[numSynthetic + numSpecified];
             int propertyCursor = numSynthetic;
@@ -638,18 +639,12 @@ public class FeatureTypeBuilder extends
                  * It may happen that the property created by the user is already named "@geometry",
                  * in which case we will avoid to duplicate the property.
                  */
-                if (builder == defaultGeometry) {
+                if (builder == defaultGeometry && geometryIndex >= 0) {
                     if (propertyTypes[geometryIndex] != null) {
                         // Assuming that there is no bug in our implementation, this error
could happen if the user
                         // has modified this FeatureTypeBuilder in another thread during
this build() execution.
                         throw new CorruptedObjectException();
                     }
-                    if (AttributeConvention.GEOMETRY_PROPERTY.equals(instance.getName()))
{
-                        System.arraycopy(propertyTypes, geometryIndex, propertyTypes, geometryIndex-1,
(numSynthetic - geometryIndex) + i);
-                        geometryIndex = -1;
-                        numSynthetic--;
-                        continue;           // Skip the increment of propertyCursor.
-                    }
                     propertyTypes[geometryIndex] = FeatureOperations.link(name(AttributeConvention.GEOMETRY_PROPERTY),
instance);
                 }
                 propertyCursor++;
@@ -675,7 +670,11 @@ public class FeatureTypeBuilder extends
                     // has modified this FeatureTypeBuilder in another thread during this
build() execution.
                     throw new CorruptedObjectException();
                 }
-                if (identifierCursor == 1 && AttributeConvention.IDENTIFIER_PROPERTY.equals(identifierTypes[0].getName()))
{
+                if (AttributeConvention.IDENTIFIER_PROPERTY.equals(identifierTypes[0].getName()))
{
+                    if (identifierCursor > 1) {
+                        throw new IllegalStateException(Errors.format(Errors.Keys.PropertyAlreadyExists_2,
+                                getDisplayName(), AttributeConvention.IDENTIFIER_PROPERTY));
+                    }
                     System.arraycopy(propertyTypes, 1, propertyTypes, 0, --propertyCursor);
                 } else {
                     propertyTypes[0] = FeatureOperations.compound(name(AttributeConvention.IDENTIFIER_PROPERTY),

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java?rev=1754409&r1=1754408&r2=1754409&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/builder/FeatureTypeBuilderTest.java
[UTF-8] Thu Jul 28 13:17:02 2016
@@ -46,10 +46,10 @@ import org.opengis.feature.PropertyType;
  */
 public final strictfp class FeatureTypeBuilderTest extends TestCase {
     /**
-     * Tests a {@code FeatureTypeBuilder.Property} with the minimum number of parameters.
+     * Tests a {@code AttributeTypeBuilder} with the minimum number of parameters.
      */
     @Test
-    public void testEmptyProperty() {
+    public void testAttributeTypeBuilderInitialization() {
         final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
         assertEquals("default name", "string", builder.getName().toString());
 
@@ -70,8 +70,8 @@ public final strictfp class FeatureTypeB
      * Tests a {@code FeatureTypeBuilder} with the minimum number of parameters (no property
and no super type).
      */
     @Test
-    @DependsOnMethod("testEmptyProperty")
-    public void testEmptyFeature() {
+    @DependsOnMethod("testAttributeTypeBuilderInitialization")
+    public void testFeatureTypeBuilderInitialization() {
         final FeatureTypeBuilder builder = new FeatureTypeBuilder();
         try {
             builder.build();
@@ -80,13 +80,6 @@ public final strictfp class FeatureTypeB
             final String message = ex.getMessage();
             assertTrue(message, message.contains("name"));
         }
-        testEmptyFeature(builder);
-    }
-
-    /**
-     * Implementation of {@link #testEmptyFeature()} using the given pre-existing builder.
-     */
-    private static void testEmptyFeature(final FeatureTypeBuilder builder) {
         builder.setName("scope", "test");
         final FeatureType type = builder.build();
 
@@ -97,11 +90,11 @@ public final strictfp class FeatureTypeB
     }
 
     /**
-     * Test creation of a single property.
+     * Test creation of a single attribute.
      */
     @Test
-    @DependsOnMethod("testEmptyProperty")
-    public void testPropertyBuild() {
+    @DependsOnMethod("testAttributeTypeBuilderInitialization")
+    public void testAttributeTypeBuilder() {
         final AttributeTypeBuilder<String> builder = new FeatureTypeBuilder().addAttribute(String.class);
         builder.setName        ("myScope", "myName");
         builder.setDefinition  ("test definition");
@@ -129,15 +122,9 @@ public final strictfp class FeatureTypeB
      * Tests {@link FeatureTypeBuilder#addAttribute(Class)}.
      */
     @Test
-    @DependsOnMethod("testEmptyFeature")
-    public void testAddProperties() {
-        testAddProperties(new FeatureTypeBuilder());
-    }
-
-    /**
-     * Implementation of {@link #testAddProperties()} using the given pre-existing builder.
-     */
-    private static void testAddProperties(final FeatureTypeBuilder builder) {
+    @DependsOnMethod("testAttributeTypeBuilder")
+    public void testAddAttribute() {
+        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
         builder.setName("myScope", "myName");
         builder.setDefinition ("test definition");
         builder.setDesignation("test designation");
@@ -194,11 +181,12 @@ public final strictfp class FeatureTypeB
     }
 
     /**
-     * Tests {@link FeatureTypeBuilder#addIdentifier(Class)}.
+     * Tests {@link FeatureTypeBuilder#addAttribute(Class)} where one property is an identifier
+     * and another property is the geometry.
      */
     @Test
-    @DependsOnMethod("testAddProperties")
-    public void testAddIdentifier() {
+    @DependsOnMethod("testAddAttribute")
+    public void testAddIdentifierAndGeometry() {
         final FeatureTypeBuilder builder = new FeatureTypeBuilder();
         builder.setName("scope", "test");
         builder.setIdentifierDelimiters("-", "pref.", null);
@@ -228,6 +216,54 @@ public final strictfp class FeatureTypeB
     }
 
     /**
+     * Tests {@link FeatureTypeBuilder#addAttribute(Class)} where one attribute is an identifier
that already has
+     * the {@code "@identifier"} name. This is called "anonymous" because identifiers with
an explicit name in the
+     * data file should use that name instead in the feature type.
+     */
+    @Test
+    @DependsOnMethod("testAddIdentifierAndGeometry")
+    public void testAddAnonymousIdentifier() {
+        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
+        builder.setName("City");
+        builder.addAttribute(String.class).setName(AttributeConvention.IDENTIFIER_PROPERTY).addRole(AttributeRole.IDENTIFIER_COMPONENT);
+        builder.addAttribute(Integer.class).setName("population");
+        final FeatureType type = builder.build();
+        final Iterator<? extends PropertyType> it = type.getProperties(true).iterator();
+        final PropertyType a0 = it.next();
+        final PropertyType a1 = it.next();
+        assertFalse("properties count", it.hasNext());
+        assertEquals("name", AttributeConvention.IDENTIFIER_PROPERTY, a0.getName());
+        assertEquals("type", String.class,  ((AttributeType<?>) a0).getValueClass());
+        assertEquals("name", "population", a1.getName().toString());
+        assertEquals("type", Integer.class, ((AttributeType<?>) a1).getValueClass());
+    }
+
+    /**
+     * Tests {@link FeatureTypeBuilder#addAttribute(Class)} where one attribute is a geometry
that already has
+     * the {@code "@geometry"} name. This is called "anonymous" because geometries with an
explicit name in the
+     * data file should use that name instead in the feature type.
+     */
+    @Test
+    @DependsOnMethod("testAddIdentifierAndGeometry")
+    public void testAddAnonymousGeometry() {
+        final FeatureTypeBuilder builder = new FeatureTypeBuilder();
+        builder.setName("City");
+        builder.addAttribute(Point.class).setName(AttributeConvention.GEOMETRY_PROPERTY).addRole(AttributeRole.DEFAULT_GEOMETRY);
+        builder.addAttribute(Integer.class).setName("population");
+        final FeatureType type = builder.build();
+        final Iterator<? extends PropertyType> it = type.getProperties(true).iterator();
+        final PropertyType a0 = it.next();
+        final PropertyType a1 = it.next();
+        final PropertyType a2 = it.next();
+        assertFalse("properties count", it.hasNext());
+        assertEquals("name", AttributeConvention.ENVELOPE_PROPERTY, a0.getName());
+        assertEquals("name", AttributeConvention.GEOMETRY_PROPERTY, a1.getName());
+        assertEquals("type", Point.class,   ((AttributeType<?>) a1).getValueClass());
+        assertEquals("name", "population", a2.getName().toString());
+        assertEquals("type", Integer.class, ((AttributeType<?>) a2).getValueClass());
+    }
+
+    /**
      * Tests creation of a builder from an existing feature type.
      * This method also acts as a test of {@code FeatureTypeBuilder} getter methods.
      */



Mime
View raw message