sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1470042 - in /sis/branches/JDK7/sis-metadata/src: main/java/org/apache/sis/metadata/ test/java/org/apache/sis/metadata/ test/java/org/apache/sis/test/suite/
Date Fri, 19 Apr 2013 21:00:34 GMT
Author: desruisseaux
Date: Fri Apr 19 21:00:33 2013
New Revision: 1470042

URL: http://svn.apache.org/r1470042
Log:
Added tests for AbstractMetadata.isEmpty() and prune() methods.

Added:
    sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PrunerTest.java 
 (with props)
Modified:
    sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java
    sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/Pruner.java
    sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java
    sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java
    sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java

Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java?rev=1470042&r1=1470041&r2=1470042&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/PropertyAccessor.java
[UTF-8] Fri Apr 19 21:00:33 2013
@@ -25,7 +25,6 @@ import java.util.HashMap;
 import java.util.IdentityHashMap;
 import java.util.Iterator;
 import java.util.Collection;
-import java.lang.reflect.Array;
 import java.lang.reflect.Method;
 import java.lang.reflect.InvocationTargetException;
 import net.jcip.annotations.ThreadSafe;
@@ -49,6 +48,7 @@ import org.apache.sis.util.resources.Err
 import org.apache.sis.xml.IdentifiedObject;
 
 import static org.apache.sis.metadata.PropertyComparator.*;
+import static org.apache.sis.metadata.ValueExistencePolicy.isNullOrEmpty;
 import static org.apache.sis.internal.util.Utilities.floatEpsilonEqual;
 import static org.apache.sis.internal.util.CollectionsExt.snapshot;
 import static org.apache.sis.internal.util.CollectionsExt.modifiableCopy;
@@ -1038,19 +1038,4 @@ final class PropertyAccessor {
         }
         return count;
     }
-
-    /**
-     * Returns {@code true} if the specified object is null or an empty collection, array
or string.
-     *
-     * <p>This method intentionally does not inspect array or collection elements,
since this method
-     * is invoked from methods doing shallow copy or comparison. If we were inspecting elements,
-     * we would need to add a check against infinite recursivity.</p>
-     */
-    static boolean isNullOrEmpty(final Object value) {
-        return value == null
-                || ((value instanceof CharSequence)  && ((CharSequence) value).length()
== 0)
-                || ((value instanceof Collection<?>) && ((Collection<?>)
value).isEmpty())
-                || ((value instanceof Map<?,?>)      && ((Map<?,?>) value).isEmpty())
-                || (value.getClass().isArray()       && Array.getLength(value) ==
0);
-    }
 }

Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/Pruner.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/Pruner.java?rev=1470042&r1=1470041&r2=1470042&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/Pruner.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/Pruner.java [UTF-8]
Fri Apr 19 21:00:33 2013
@@ -23,6 +23,8 @@ import java.util.IdentityHashMap;
 import org.opengis.util.CodeList;
 import org.apache.sis.internal.util.CollectionsExt;
 
+import static org.apache.sis.metadata.ValueExistencePolicy.*;
+
 
 /**
  * Implementation of {@link AbstractMetadata#isEmpty()} and {@link ModifiableMetadata#prune()}
@@ -59,8 +61,7 @@ final class Pruner extends ThreadLocal<M
      * include empty (but non-null) values in order to allow us to set them to {@code null}.
      */
     private static Map<String, Object> asMap(final MetadataStandard standard, final
Object metadata, final boolean prune) {
-        return standard.asValueMap(metadata, KeyNamePolicy.JAVABEANS_PROPERTY, prune ?
-                ValueExistencePolicy.NON_NULL : ValueExistencePolicy.NON_EMPTY);
+        return standard.asValueMap(metadata, KeyNamePolicy.JAVABEANS_PROPERTY, prune ? NON_NULL
: NON_EMPTY);
     }
 
     /**
@@ -136,7 +137,7 @@ final class Pruner extends ThreadLocal<M
                 final Collection<?> values = CollectionsExt.toCollection(value);
                 for (final Iterator<?> it = values.iterator(); it.hasNext();) {
                     final Object element = it.next();
-                    if (!PropertyAccessor.isNullOrEmpty(element)) {
+                    if (!isNullOrEmpty(element)) {
                         /*
                          * If the value is not an empty "simple" property (null value, or
empty
                          * string, or an empty collection or array), check if it is an other

Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java?rev=1470042&r1=1470041&r2=1470042&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ValueExistencePolicy.java
[UTF-8] Fri Apr 19 21:00:33 2013
@@ -16,6 +16,10 @@
  */
 package org.apache.sis.metadata;
 
+import java.util.Map;
+import java.util.Collection;
+import java.lang.reflect.Array;
+
 
 /**
  * Whatever {@link MetadataStandard#asValueMap MetadataStandard.asValueMap(…)} shall contain
@@ -49,5 +53,40 @@ public enum ValueExistencePolicy {
      * non-{@linkplain java.util.Collection#isEmpty() empty}.
      * This is the default behavior of {@link AbstractMetadata#asMap()}.
      */
-    NON_EMPTY
+    NON_EMPTY;
+
+
+    /**
+     * Returns {@code true} if the specified object is null or an empty collection, array
or string.
+     *
+     * <p>This method intentionally does not inspect array or collection elements,
since this method
+     * is invoked from methods doing shallow copy or comparison. If we were inspecting elements,
+     * we would need to add a check against infinite recursivity.</p>
+     */
+    static boolean isNullOrEmpty(final Object value) {
+        return value == null
+                || ((value instanceof CharSequence)  && isEmpty((CharSequence) value))
+                || ((value instanceof Collection<?>) && ((Collection<?>)
value).isEmpty())
+                || ((value instanceof Map<?,?>)      && ((Map<?,?>) value).isEmpty())
+                || (value.getClass().isArray()       && Array.getLength(value) ==
0);
+    }
+
+    /**
+     * Returns {@code true} if the given character sequence shall be considered empty.
+     * The current implementation returns {@code true} if the sequence contains only
+     * whitespaces in the sense of Java (i.e. ignoring line feeds, but not ignoring
+     * non-breaking spaces). The exact criterion is not a committed part of the API
+     * and may change in future SIS versions according experiences.
+     */
+    private static boolean isEmpty(final CharSequence value) {
+        final int length = value.length();
+        for (int i=0; i<length;) {
+            final int c = Character.codePointAt(value, i);
+            if (!Character.isWhitespace(c)) {
+                return false;
+            }
+            i += Character.charCount(c);
+        }
+        return true;
+    }
 }

Modified: sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java?rev=1470042&r1=1470041&r2=1470042&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java [UTF-8]
(original)
+++ sis/branches/JDK7/sis-metadata/src/main/java/org/apache/sis/metadata/ValueMap.java [UTF-8]
Fri Apr 19 21:00:33 2013
@@ -68,7 +68,7 @@ final class ValueMap extends PropertyMap
      * Returns {@code true} if the given value should be ignored.
      */
     final boolean ignore(final Object value) {
-        return valuePolicy == ValueExistencePolicy.NON_EMPTY && PropertyAccessor.isNullOrEmpty(value);
+        return valuePolicy == ValueExistencePolicy.NON_EMPTY && ValueExistencePolicy.isNullOrEmpty(value);
     }
 
     /**
@@ -311,7 +311,7 @@ final class ValueMap extends PropertyMap
                         break;
                     }
                     case NON_EMPTY: {
-                        skip = PropertyAccessor.isNullOrEmpty(value);
+                        skip = ValueExistencePolicy.isNullOrEmpty(value);
                         break;
                     }
                     default: {

Added: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PrunerTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PrunerTest.java?rev=1470042&view=auto
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PrunerTest.java (added)
+++ sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PrunerTest.java [UTF-8]
Fri Apr 19 21:00:33 2013
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.sis.metadata;
+
+import org.apache.sis.metadata.iso.DefaultMetadata;
+import org.apache.sis.metadata.iso.citation.DefaultCitation;
+import org.apache.sis.metadata.iso.extent.DefaultExtent;
+import org.apache.sis.metadata.iso.extent.DefaultGeographicBoundingBox;
+import org.apache.sis.metadata.iso.identification.DefaultDataIdentification;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static org.apache.sis.metadata.ValueExistencePolicy.isNullOrEmpty;
+
+
+/**
+ * Tests the {@link AbstractMetadata#isEmpty()} and {@link ModifiableMetadata#prune()} methods.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.3 (derived from geotk-3.20)
+ * @version 0.3
+ * @module
+ */
+@DependsOn(ValueMapTest.class)
+public final strictfp class PrunerTest extends TestCase {
+    /**
+     * The root metadata object being tested.
+     */
+    private final DefaultMetadata metadata;
+
+    /**
+     * A child of the metadata object being tested.
+     */
+    private final DefaultDataIdentification identification;
+
+    /**
+     * A child of the metadata object being tested.
+     */
+    private final DefaultExtent extent;
+
+    /**
+     * A child of the metadata object being tested.
+     */
+    private final DefaultGeographicBoundingBox bbox;
+
+    /**
+     * Creates the metadata objects to be used for the test.
+     */
+    public PrunerTest() {
+        metadata       = new DefaultMetadata();
+        identification = new DefaultDataIdentification();
+        extent         = new DefaultExtent();
+        bbox           = new DefaultGeographicBoundingBox();
+        extent.getGeographicElements().add(bbox);
+        identification.getExtents().add(extent);
+        metadata.getIdentificationInfo().add(identification);
+    }
+
+    /**
+     * Tests the {@link AbstractMetadata#isEmpty()} method.
+     */
+    @Test
+    public void testIsEmpty() {
+        /*
+         * Initially empty tree, or tree with only empty element.
+         */
+        assertTrue("GeographicBoundingBox", bbox.isEmpty());
+        assertTrue("Extent",                extent.isEmpty());
+        assertTrue("DataIdentification",    identification.isEmpty());
+        assertTrue("Metadata",              metadata.isEmpty());
+        /*
+         * Set a non-empty identification info.
+         */
+        identification.setCitation(new DefaultCitation("A citation title"));
+        assertTrue ("GeographicBoundingBox", bbox.isEmpty());
+        assertTrue ("Extent",                extent.isEmpty());
+        assertFalse("DataIdentification",    identification.isEmpty());
+        assertFalse("Metadata",              metadata.isEmpty());
+        /*
+         * Set a non-empty metadata info.
+         */
+        metadata.setFileIdentifier("A file identifiers");
+        assertTrue ("GeographicBoundingBox", bbox.isEmpty());
+        assertTrue ("Extent",                extent.isEmpty());
+        assertFalse("DataIdentification",    identification.isEmpty());
+        assertFalse("Metadata",              metadata.isEmpty());
+        /*
+         * Set an empty string in an element.
+         */
+        identification.setCitation(new DefaultCitation("  "));
+        assertTrue ("GeographicBoundingBox", bbox.isEmpty());
+        assertTrue ("Extent",                extent.isEmpty());
+        assertTrue ("DataIdentification",    identification.isEmpty());
+        assertFalse("Metadata",              metadata.isEmpty());
+        /*
+         * Set an empty string in an element.
+         */
+        metadata.setFileIdentifier("   ");
+        assertTrue("Metadata", metadata.isEmpty());
+    }
+
+    /**
+     * Tests the {@link ModifiableMetadata#prune()} method.
+     */
+    @Test
+    public void testPrune() {
+        metadata.setFileIdentifier("A file identifiers");
+        identification.setCitation(new DefaultCitation("A citation title"));
+        assertFalse(isNullOrEmpty(metadata.getFileIdentifier()));
+        assertFalse(isNullOrEmpty(identification.getCitation()));
+        assertEquals(1, metadata.getIdentificationInfo().size());
+        assertEquals(1, identification.getExtents().size());
+        assertEquals(1, extent.getGeographicElements().size());
+        assertFalse(metadata.isEmpty());
+
+        metadata.prune();
+        assertFalse(isNullOrEmpty(metadata.getFileIdentifier()));
+        assertFalse(isNullOrEmpty(identification.getCitation()));
+        assertEquals(1, metadata.getIdentificationInfo().size());
+        assertEquals(0, identification.getExtents().size());
+        assertEquals(0, extent.getGeographicElements().size());
+        assertFalse(metadata.isEmpty());
+
+        metadata.setFileIdentifier(" ");
+        identification.setCitation(new DefaultCitation(" "));
+        assertNotNull(metadata.getFileIdentifier());
+        metadata.prune();
+
+        assertNull(metadata.getFileIdentifier());
+        assertNull(identification.getCitation());
+        assertTrue(metadata.getIdentificationInfo().isEmpty());
+        assertTrue(identification.getExtents().isEmpty());
+        assertTrue(extent.getGeographicElements().isEmpty());
+        assertTrue(metadata.isEmpty());
+    }
+}

Propchange: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PrunerTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/PrunerTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java?rev=1470042&r1=1470041&r2=1470042&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java
[UTF-8] Fri Apr 19 21:00:33 2013
@@ -37,7 +37,8 @@ import org.junit.BeforeClass;
     org.apache.sis.metadata.TypeMapTest.class,
     org.apache.sis.metadata.InformationMapTest.class,
     org.apache.sis.metadata.ValueMapTest.class,
-    org.apache.sis.metadata.MetadataStandardTest.class
+    org.apache.sis.metadata.MetadataStandardTest.class,
+    org.apache.sis.metadata.PrunerTest.class
 })
 public final strictfp class MetadataTestSuite extends TestSuite {
     /**



Mime
View raw message