sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject [sis] 02/02: Avoid the use of `FeatureTypeBuilder` for keeping tests more isolated (i.e. for not being impacted by potential bugs in FeatureTypeBuilder). Use `TestUtilities.getSingleton(…)` method for checking the result. Keep `verifyCopyrightParsing(…)` close to the place where it is used.
Date Wed, 07 Jul 2021 11:06:16 GMT
This is an automated email from the ASF dual-hosted git repository.

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

commit 99cafb6339df2837cc7bf0c5a505e8ad2f141321
Author: Martin Desruisseaux <martin.desruisseaux@geomatys.com>
AuthorDate: Wed Jul 7 12:03:54 2021 +0200

    Avoid the use of `FeatureTypeBuilder` for keeping tests more isolated
    (i.e. for not being impacted by potential bugs in FeatureTypeBuilder).
    Use `TestUtilities.getSingleton(…)` method for checking the result.
    Keep `verifyCopyrightParsing(…)` close to the place where it is used.
---
 .../sis/internal/storage/MetadataBuilderTest.java  | 101 ++++++++++++---------
 1 file changed, 58 insertions(+), 43 deletions(-)

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 bc8a8e3..616f431 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,28 +16,33 @@
  */
 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 java.util.Collections;
+import org.opengis.util.GenericName;
+import org.opengis.metadata.citation.Citation;
+import org.opengis.metadata.content.ContentInformation;
+import org.opengis.metadata.content.FeatureCatalogueDescription;
+import org.opengis.metadata.content.FeatureTypeInfo;
 import org.opengis.metadata.constraint.LegalConstraints;
 import org.opengis.metadata.constraint.Restriction;
-import org.opengis.metadata.citation.Citation;
+import org.apache.sis.metadata.iso.DefaultMetadata;
+import org.apache.sis.feature.DefaultFeatureType;
 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;
 import static org.apache.sis.test.TestUtilities.getSingleton;
 
+// Branch-dependent imports
+import org.opengis.feature.FeatureType;
+
 
 /**
  * Tests {@link MetadataBuilder}.
  *
  * @author  Martin Desruisseaux (Geomatys)
- * @version 0.8
+ * @author  Alexis Manin (Geomatys)
+ * @version 1.1
  * @since   0.8
  * @module
  */
@@ -69,70 +74,80 @@ public final strictfp class MetadataBuilderTest extends TestCase {
         verifyCopyrightParsing("(C) COPYRIGHT 1992 John Smith.");
     }
 
+    /**
+     * Verifies the metadata that contains the result of parsing a copyright statement.
+     * Should contains the "John Smith" name and 1992 year.
+     *
+     * @param notice  the copyright statement to parse.
+     */
+    private static void verifyCopyrightParsing(final String notice) {
+        final MetadataBuilder builder = new MetadataBuilder();
+        builder.parseLegalNotice(notice);
+        final LegalConstraints constraints = (LegalConstraints) getSingleton(getSingleton(
+                builder.build(false).getIdentificationInfo()).getResourceConstraints());
+
+        assertEquals("useConstraints", Restriction.COPYRIGHT, getSingleton(constraints.getUseConstraints()));
+        final Citation ref = getSingleton(constraints.getReferences());
+        assertTitleEquals("reference.title", notice, ref);
+        assertPartyNameEquals("reference.citedResponsibleParty", "John Smith", ref);
+        assertEquals("date", date("1992-01-01 00:00:00"), getSingleton(ref.getDates()).getDate());
+    }
+
+    /**
+     * Tests {@link MetadataBuilder#addFeatureType(FeatureType, long)}.
+     *
+     * @todo Combine the 4 tests in a single one for leveraging the same {@link DefaultFeatureType}
instance?
+     *       It would be consistent with {@link #testParseLegalNotice()}, and the error message
in those tests
+     *       are already quite clear.
+     */
     @Test
     public void negative_feature_count_are_ignored() {
         verifyFeatureInstanceCount("Feature count should not be written if it is negative",
null, -1);
     }
 
+    /**
+     * Tests {@link MetadataBuilder#addFeatureType(FeatureType, long)}.
+     */
     @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);
     }
 
+    /**
+     * Tests {@link MetadataBuilder#addFeatureType(FeatureType, long)}.
+     */
     @Test
     public void verify_feature_count_is_written() {
         verifyFeatureInstanceCount("Feature count should be written as is", 42, 42);
     }
 
+    /**
+     * Tests {@link MetadataBuilder#addFeatureType(FeatureType, long)}.
+     */
     @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.
+     * Creates a new simple metadata with a single simple feature type and the given
+     * {@linkplain FeatureTypeInfo#getFeatureInstanceCount() feature instance count}.
+     * Then, asserts 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)
+     * @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.
      */
     private static void verifyFeatureInstanceCount(final String errorMessage, final Integer
expected, final long valueToInsert) {
-        final FeatureType dataType = new FeatureTypeBuilder()
-                .setName("Test type")
-                .build();
+        final DefaultFeatureType dataType = new DefaultFeatureType(
+                Collections.singletonMap(DefaultFeatureType.NAME_KEY, "Test type"), false,
null);
         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"));
-
+        final ContentInformation content = getSingleton(metadata.getContentInfo());
+        assertInstanceOf("Metadata.contentInfo", FeatureCatalogueDescription.class, content);
+        final FeatureTypeInfo info = getSingleton(((FeatureCatalogueDescription) content).getFeatureTypeInfo());
         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.
-     *
-     * @param notice  the copyright statement to parse.
-     */
-    private static void verifyCopyrightParsing(final String notice) {
-        final MetadataBuilder builder = new MetadataBuilder();
-        builder.parseLegalNotice(notice);
-        final LegalConstraints constraints = (LegalConstraints) getSingleton(getSingleton(
-                builder.build(false).getIdentificationInfo()).getResourceConstraints());
-
-        assertEquals("useConstraints", Restriction.COPYRIGHT, getSingleton(constraints.getUseConstraints()));
-        final Citation ref = getSingleton(constraints.getReferences());
-        assertTitleEquals("reference.title", notice, ref);
-        assertPartyNameEquals("reference.citedResponsibleParty", "John Smith", ref);
-        assertEquals("date", date("1992-01-01 00:00:00"), getSingleton(ref.getDates()).getDate());
-    }
 }

Mime
View raw message