sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1477804 - in /sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis: metadata/MetadataTreeChildrenTest.java test/suite/MetadataTestSuite.java
Date Tue, 30 Apr 2013 20:04:49 GMT
Author: desruisseaux
Date: Tue Apr 30 20:04:48 2013
New Revision: 1477804

URL: http://svn.apache.org/r1477804
Log:
Added tests for the remove operation.

Modified:
    sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeChildrenTest.java
    sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/test/suite/MetadataTestSuite.java

Modified: sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeChildrenTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeChildrenTest.java?rev=1477804&r1=1477803&r2=1477804&view=diff
==============================================================================
--- sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeChildrenTest.java
[UTF-8] (original)
+++ sis/branches/JDK7/sis-metadata/src/test/java/org/apache/sis/metadata/MetadataTreeChildrenTest.java
[UTF-8] Tue Apr 30 20:04:48 2013
@@ -18,6 +18,8 @@ package org.apache.sis.metadata;
 
 import java.util.Random;
 import java.util.Iterator;
+import java.util.List;
+import java.util.ArrayList;
 import org.opengis.metadata.citation.PresentationForm;
 import org.apache.sis.metadata.iso.citation.DefaultCitation;
 import org.apache.sis.util.iso.SimpleInternationalString;
@@ -151,6 +153,47 @@ public final strictfp class MetadataTree
         assertAllNextEqual(expected, children.iterator());
     }
 
+    /**
+     * Tests the {@link Iterator#remove()} operation on a list of properties without collections.
+     */
+    @Test
+    @DependsOnMethod("testReadOnlyWithoutCollections")
+    public void testRemoveWithoutCollections() {
+        final DefaultCitation      citation = metadataWithoutCollections();
+        final MetadataTreeChildren children = create(citation, ValueExistencePolicy.NON_EMPTY);
+        testRemove(createRandomNumberGenerator("testRemoveWithoutCollections"), children);
+    }
+
+    /**
+     * Tests the {@link Iterator#remove()} operation on a list of properties with
+     * collections containing only one element.
+     */
+    @Test
+    @DependsOnMethod({
+        "testRemoveWithoutCollections",
+        "testReadOnlyWithSingletonInCollections"
+    })
+    public void testRemoveWithSingletonInCollections() {
+        final DefaultCitation      citation = metadataWithSingletonInCollections();
+        final MetadataTreeChildren children = create(citation, ValueExistencePolicy.NON_EMPTY);
+        testRemove(createRandomNumberGenerator("testRemoveWithSingletonInCollections"), children);
+    }
+
+    /**
+     * Tests the {@link Iterator#remove()} operation on a list of properties with
+     * collections containing more than one element.
+     */
+    @Test
+    @DependsOnMethod({
+        "testRemoveWithSingletonInCollections",
+        "testReadOnlyWithMultiOccurrences"
+    })
+    public void testRemoveWithMultiOccurrences() {
+        final DefaultCitation      citation = metadataWithSingletonInCollections();
+        final MetadataTreeChildren children = create(citation, ValueExistencePolicy.NON_EMPTY);
+        testRemove(createRandomNumberGenerator("testRemoveWithMultiOccurrences"), children);
+    }
+
 
     // ------------------------ Support methods for the above tests ------------------------
 
@@ -176,4 +219,45 @@ public final strictfp class MetadataTree
         }
         assertFalse("Iterator.hasNext()", it.hasNext());
     }
+
+    /**
+     * Asserts that all next elements traversed by the {@code actual} iterator are equal
+     * to the next elements traversed by {@code expected}.
+     *
+     * @param expected The iterator over expected values.
+     * @param actual   The iterator over actual values.
+     */
+    private static void assertAllNextEqual(final Iterator<?> expected, final Iterator<?>
actual) {
+        while (expected.hasNext()) {
+            assertTrue("Iterator.hasNext()", actual.hasNext());
+            assertEquals("Iterator.next()", expected.next(), actual.next());
+        }
+        assertFalse("Iterator.hasNext()", actual.hasNext());
+    }
+
+    /**
+     * Tests the {@link Iterator#remove()} operation on the given collection of children.
+     * Elements are removed randomly until the collection is empty. After each removal,
+     * the remaining elements are compared with the content of a standard Java collection.
+     *
+     * @param random   A random number generator.
+     * @param children The collection from which to remove elements.
+     */
+    private static void testRemove(final Random random, final MetadataTreeChildren children)
{
+        final List<TreeTable.Node> reference = new ArrayList<>(children);
+        assertFalse("The collection shall not be initially empty.", reference.isEmpty());
+        do {
+            final Iterator<TreeTable.Node> rit = reference.iterator(); // The reference
iterator.
+            final Iterator<TreeTable.Node> cit = children .iterator(); // The children
iterator to be tested.
+            while (rit.hasNext()) {
+                assertTrue(cit.hasNext());
+                assertSame(rit.next(), cit.next());
+                if (random.nextInt(3) == 0) { // Remove only 1/3 of entries on each pass.
+                    rit.remove();
+                    cit.remove();
+                    assertAllNextEqual(reference.iterator(), children.iterator());
+                }
+            }
+        } while (!reference.isEmpty());
+    }
 }

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=1477804&r1=1477803&r2=1477804&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] Tue Apr 30 20:04:48 2013
@@ -42,6 +42,7 @@ 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.MetadataTreeChildrenTest.class,
     org.apache.sis.metadata.MetadataStandardTest.class,
     org.apache.sis.metadata.PrunerTest.class,
     org.apache.sis.metadata.iso.AllMetadataTest.class



Mime
View raw message