sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From ama...@apache.org
Subject [sis] branch geoapi-4.0 updated: fix(Storage): Prevent error on metadata creation when a feature typedeclares 0 records
Date Wed, 07 Jul 2021 08:39:02 GMT
This is an automated email from the ASF dual-hosted git repository.

amanin pushed a commit to branch geoapi-4.0
in repository https://gitbox.apache.org/repos/asf/sis.git


The following commit(s) were added to refs/heads/geoapi-4.0 by this push:
     new 699afb3  fix(Storage): Prevent error on metadata creation when a feature typedeclares
0 records
699afb3 is described below

commit 699afb3b7c8184b91e5ceeeb3daae353c8411825
Author: Alexis Manin <alexis.manin@geomatys.com>
AuthorDate: Wed Jul 7 10:33:50 2021 +0200

    fix(Storage): Prevent error on metadata creation when a feature typedeclares 0 records
---
 .../sis/internal/storage/MetadataBuilder.java      | 12 ++++-
 .../sis/internal/storage/MetadataBuilderTest.java  | 54 ++++++++++++++++++++++
 2 files changed, 64 insertions(+), 2 deletions(-)

diff --git a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java
b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java
index 2a33f3b..1e52c28 100644
--- a/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java
+++ b/storage/sis-storage/src/main/java/org/apache/sis/internal/storage/MetadataBuilder.java
@@ -1881,7 +1881,9 @@ parse:      for (int i = 0; i < length;) {
      * Note that the {@link FeatureCatalogBuilder} subclasses can also be used for that chaining.
      *
      * @param  type         the feature type to add, or {@code null} for no-operation.
-     * @param  occurrences  number of instances of the given feature type, or a negative
value if unknown.
+     * @param  occurrences  number of instances of the given feature type, or a negative
value if unknown. Note that if
+     *                      this value is 0, it will be considered "unknown", because ISO-19115
consider it invalid.
+     *                      However, this is a valid case in practice (it represents an empty
dataset at the moment).
      * @return the name of the added feature, or {@code null} if none.
      *
      * @see FeatureCatalogBuilder#define(FeatureType)
@@ -1891,7 +1893,13 @@ parse:      for (int i = 0; i < length;) {
             final GenericName name = type.getName();
             if (name != null) {
                 final DefaultFeatureTypeInfo info = new DefaultFeatureTypeInfo(name);
-                if (occurrences >= 0) {
+                /* Warning: Exclude 0 as valid instance count.
+                 * Reason: ISO-19115 consider 0 (empty dataset) as an invalid count. However,
in practice, it can happen
+                 * to open data sources that contain no record. They're still valid datasets,
because as long as their
+                 * structure is valid, there's no point in raising an error (at our level,
without any other context
+                 * information) here.
+                 */
+                if (occurrences > 0) {
                     info.setFeatureInstanceCount(shared((int) Math.min(occurrences, Integer.MAX_VALUE)));
                 }
                 addIfNotPresent(featureDescription().getFeatureTypeInfo(), info);
diff --git a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/MetadataBuilderTest.java
b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/MetadataBuilderTest.java
index 416e438..bc8a8e3 100644
--- a/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/MetadataBuilderTest.java
+++ b/storage/sis-storage/src/test/java/org/apache/sis/internal/storage/MetadataBuilderTest.java
@@ -16,11 +16,17 @@
  */
 package org.apache.sis.internal.storage;
 
+import org.apache.sis.feature.builder.FeatureTypeBuilder;
+import org.apache.sis.metadata.iso.DefaultMetadata;
+import org.opengis.feature.FeatureType;
 import org.opengis.metadata.constraint.LegalConstraints;
 import org.opengis.metadata.constraint.Restriction;
 import org.opengis.metadata.citation.Citation;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
+import org.opengis.metadata.content.FeatureCatalogueDescription;
+import org.opengis.metadata.content.FeatureTypeInfo;
+import org.opengis.util.GenericName;
 
 import static org.apache.sis.test.MetadataAssert.*;
 import static org.apache.sis.test.TestUtilities.date;
@@ -63,6 +69,54 @@ public final strictfp class MetadataBuilderTest extends TestCase {
         verifyCopyrightParsing("(C) COPYRIGHT 1992 John Smith.");
     }
 
+    @Test
+    public void negative_feature_count_are_ignored() {
+        verifyFeatureInstanceCount("Feature count should not be written if it is negative",
null, -1);
+    }
+
+    @Test
+    public void no_overflow_on_feature_count() {
+        verifyFeatureInstanceCount("Feature count should be limited to maximum 32bit integer
value", Integer.MAX_VALUE, 7_000_000_000L);
+    }
+
+    @Test
+    public void verify_feature_count_is_written() {
+        verifyFeatureInstanceCount("Feature count should be written as is", 42, 42);
+    }
+
+    @Test
+    public void feature_count_should_be_ignored_when_it_is_zero() {
+        verifyFeatureInstanceCount("Feature count should not be written if it is 0", null,
0);
+    }
+
+    /**
+     * Create a new simple metadata with a single simple feature type and the given
+     * {@link FeatureTypeInfo#getFeatureInstanceCount() feature instance count}. Then, assert
that the value in the
+     * built metadata is compliant with a given control value.
+     *
+     * @param expected The feature instance count value we want to see in the metadata (control
value)
+     * @param valueToInsert The value to send to the metadata builder.
+     *
+     * @see MetadataBuilder#addFeatureType(FeatureType, long)
+     */
+    private static void verifyFeatureInstanceCount(final String errorMessage, final Integer
expected, final long valueToInsert) {
+        final FeatureType dataType = new FeatureTypeBuilder()
+                .setName("Test type")
+                .build();
+        final MetadataBuilder builder = new MetadataBuilder();
+        final GenericName name = builder.addFeatureType(dataType, valueToInsert);
+        assertNotNull(name);
+
+        final DefaultMetadata metadata = builder.build(true);
+        final FeatureTypeInfo info = metadata.getContentInfo().stream()
+                .filter(it -> it instanceof FeatureCatalogueDescription)
+                .flatMap(it -> ((FeatureCatalogueDescription) it).getFeatureTypeInfo().stream())
+                .reduce((v1, v2) -> { throw new AssertionError("A single feature type
info is expected"); })
+                .orElseThrow(() -> new AssertionError("A single feature type info is expected"));
+
+        assertEquals(errorMessage, expected, info.getFeatureInstanceCount());
+    }
+
     /**
      * Verifies the metadata that contains the result of parsing a copyright statement.
      * Should contains the "John Smith" name and 1992 year.

Mime
View raw message