Author: desruisseaux
Date: Sun Mar  9 16:33:34 2014
New Revision: 1575723

URL: http://svn.apache.org/r1575723
Log:
Added tests.

Modified:
    
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
    
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterValueList.java
    
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/DefaultParameterDescriptorGroupTest.java
    
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/DefaultParameterValueGroupTest.java

Modified: 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java?rev=1575723&r1=1575722&r2=1575723&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterBuilder.java
 [UTF-8] Sun Mar  9 16:33:34 2014
@@ -74,10 +74,10 @@ import static org.apache.sis.util.Argume
  * by the following code in order to declare its aliases:
  *
  * {@preformat java
- *   builder.addName("Longitude of natural origin")        // Primary name in 
builder default namespace.
+ *   builder.addIdentifier("8802")                         // Primary key in 
builder default namespace (EPSG in this example).
+ *          .addName("Longitude of natural origin")        // Primary name in 
builder default namespace (EPSG in this example).
  *          .addName(Citations.OGC, "central_meridian")    // First alias in 
"OGC" namespace.
  *          .addName(Citations.GEOTIFF, "NatOriginLong")   // Second alias in 
"GeoTIFF" namespace.
- *          .addIdentifier("8802")                         // Primary key in 
EPSG database.
  *          .createBounded(-80, +84, 0, NonSI.DEGREE_ANGLE);
  * }
  *

Modified: 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterValueList.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterValueList.java?rev=1575723&r1=1575722&r2=1575723&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterValueList.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/main/java/org/apache/sis/parameter/ParameterValueList.java
 [UTF-8] Sun Mar  9 16:33:34 2014
@@ -182,7 +182,7 @@ final class ParameterValueList extends A
                 count++;
             }
         }
-        if (count > desc.getMaximumOccurs()) {
+        if (count >= desc.getMaximumOccurs()) {
             throw new InvalidParameterCardinalityException(Errors.format(
                     Errors.Keys.TooManyOccurrences_2, count, name), 
name.getCode());
         }

Modified: 
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/DefaultParameterDescriptorGroupTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/DefaultParameterDescriptorGroupTest.java?rev=1575723&r1=1575722&r2=1575723&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/DefaultParameterDescriptorGroupTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/DefaultParameterDescriptorGroupTest.java
 [UTF-8] Sun Mar  9 16:33:34 2014
@@ -41,15 +41,16 @@ import static org.opengis.referencing.Id
 @DependsOn(DefaultParameterDescriptorTest.class)
 public final strictfp class DefaultParameterDescriptorGroupTest extends 
TestCase {
     /**
-     * Returns a group of 4 parameters of type {@link Integer} with default 
value 10.
+     * A group of 4 parameters of type {@link Integer} with default value 10.
      * The two first parameters are mandatory, while the two last parameters 
are optional.
      * The very last parameter has a maximum number of occurrence of 2, which 
is illegal
      * according ISO 19111 but nevertheless supported by Apache SIS.
      */
-    static final DefaultParameterDescriptorGroup createGroup_2M_2O() {
+    static final DefaultParameterDescriptorGroup M1_M1_O1_O2;
+    static {
         final Integer DEFAULT_VALUE = 10;
         final Class<Integer> type = Integer.class;
-        return new DefaultParameterDescriptorGroup(name("The group"), 0, 1,
+        M1_M1_O1_O2 = new DefaultParameterDescriptorGroup(name("The group"), 
0, 1,
             new DefaultParameterDescriptor<>(name("Mandatory 1"), type, null, 
null, DEFAULT_VALUE, true),
             new DefaultParameterDescriptor<>(name("Mandatory 2"), type, null, 
null, DEFAULT_VALUE, true),
             new DefaultParameterDescriptor<>(name( "Optional 3"), type, null, 
null, DEFAULT_VALUE, false),
@@ -69,7 +70,7 @@ public final strictfp class DefaultParam
      */
     @Test
     public void validateTestObjects() {
-        for (final GeneralParameterDescriptor descriptor : 
createGroup_2M_2O().descriptors()) {
+        for (final GeneralParameterDescriptor descriptor : 
M1_M1_O1_O2.descriptors()) {
             AssertionError error = null;
             try {
                 validate(descriptor);
@@ -89,7 +90,7 @@ public final strictfp class DefaultParam
      */
     @Test
     public void testDescriptor() {
-        final DefaultParameterDescriptorGroup group = createGroup_2M_2O();
+        final DefaultParameterDescriptorGroup group = M1_M1_O1_O2;
         final List<GeneralParameterDescriptor> descriptors = 
group.descriptors();
         assertEquals("name", "The group", group.getName().getCode());
         assertEquals("size", 4, descriptors.size());
@@ -106,7 +107,7 @@ public final strictfp class DefaultParam
      */
     @Test
     public void testContains() {
-        final List<GeneralParameterDescriptor> descriptors = 
createGroup_2M_2O().descriptors();
+        final List<GeneralParameterDescriptor> descriptors = 
M1_M1_O1_O2.descriptors();
         for (final GeneralParameterDescriptor p : descriptors) {
             assertTrue(descriptors.contains(p));
         }

Modified: 
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/DefaultParameterValueGroupTest.java
URL: 
http://svn.apache.org/viewvc/sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/DefaultParameterValueGroupTest.java?rev=1575723&r1=1575722&r2=1575723&view=diff
==============================================================================
--- 
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/DefaultParameterValueGroupTest.java
 [UTF-8] (original)
+++ 
sis/branches/JDK7/core/sis-referencing/src/test/java/org/apache/sis/parameter/DefaultParameterValueGroupTest.java
 [UTF-8] Sun Mar  9 16:33:34 2014
@@ -22,7 +22,11 @@ import org.opengis.parameter.GeneralPara
 import org.opengis.parameter.GeneralParameterValue;
 import org.opengis.parameter.ParameterDescriptor;
 import org.opengis.parameter.ParameterDescriptorGroup;
+import org.opengis.parameter.ParameterValue;
 import org.opengis.parameter.ParameterValueGroup;
+import org.opengis.parameter.InvalidParameterNameException;
+import org.opengis.parameter.InvalidParameterCardinalityException;
+import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.DependsOn;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
@@ -46,12 +50,26 @@ import static org.apache.sis.parameter.D
 })
 public final strictfp class DefaultParameterValueGroupTest extends TestCase {
     /**
-     * Creates values for all parameters defines by the given descriptor,
-     * and assigns to them an integer value in sequence with the given step.
+     * The descriptors of parameters to be tested by this class.
+     * The default descriptors are:
+     * <ul>
+     *   <li>One mandatory parameter (cardinality [1…1]).</li>
+     *   <li>One mandatory parameter (cardinality [1…1]).</li>
+     *   <li>One optional  parameter (cardinality [0…1]).</li>
+     *   <li>One optional  parameter (cardinality [0…2]) — invalid according 
ISO 19111, but supported by SIS.</li>
+     * </ul>
+     *
+     * Some test methods may replace the default descriptor by an other one.
      */
-    private static DefaultParameterValue<?>[] createValues(
-            final List<GeneralParameterDescriptor> descriptors, final int step)
-    {
+    private ParameterDescriptorGroup descriptor = 
DefaultParameterDescriptorGroupTest.M1_M1_O1_O2;
+
+    /**
+     * Creates values for all parameters defined by the {@linkplain 
#descriptor} (regardless their cardinality),
+     * and assigns to them an integer value in sequence with the given step. 
For example if {@code step} is 10,
+     * then this method will create parameters with values 10, 20, 30 and 40.
+     */
+    private DefaultParameterValue<?>[] createValues(final int step) {
+        final List<GeneralParameterDescriptor> descriptors = 
descriptor.descriptors();
         final DefaultParameterValue<?>[] parameters = new 
DefaultParameterValue<?>[descriptors.size()];
         for (int i=0; i<parameters.length;) {
             parameters[i] = new 
DefaultParameterValue<>((ParameterDescriptor<?>) descriptors.get(i));
@@ -61,12 +79,22 @@ public final strictfp class DefaultParam
     }
 
     /**
+     * Same as {@link #createValues(int)}, but put the parameters in a {@link 
DefaultParameterValueGroup}.
+     *
+     * @see #testValuesAddAll()
+     */
+    private DefaultParameterValueGroup createGroup(final int step) {
+        final DefaultParameterValueGroup group = new 
DefaultParameterValueGroup(descriptor);
+        group.values().addAll(Arrays.asList(createValues(step)));
+        return group;
+    }
+
+    /**
      * Validates the test parameter values created by {@link 
#createValues(List, int)}.
      */
     @Test
     public void validateTestObjects() {
-        final DefaultParameterDescriptorGroup group = 
DefaultParameterDescriptorGroupTest.createGroup_2M_2O();
-        for (final DefaultParameterValue<?> param : 
createValues(group.descriptors(), 10)) {
+        for (final DefaultParameterValue<?> param : createValues(10)) {
             AssertionError error = null;
             try {
                 validate(param);
@@ -82,20 +110,165 @@ public final strictfp class DefaultParam
     }
 
     /**
+     * Tests {@link DefaultParameterValueGroup#parameter(String)}.
+     */
+    @Test
+    @DependsOnMethod("testValuesAddAll")
+    public void testParameter() {
+        final DefaultParameterValueGroup  group  = createGroup(10);
+        final List<GeneralParameterValue> values = group.values();
+        assertSame  ("parameter(“Mandatory 1”)", values.get(0), 
group.parameter("Mandatory 1"));
+        assertSame  ("parameter(“Mandatory 2”)", values.get(1), 
group.parameter("Mandatory 2"));
+        assertSame  ("parameter(“Optional 3”)",  values.get(2), 
group.parameter("Optional 3"));
+        assertSame  ("parameter(“Optional 4”)",  values.get(3), 
group.parameter("Optional 4"));
+        assertEquals("parameter(“Mandatory 1”)", 10, 
group.parameter("Mandatory 1").intValue());
+        assertEquals("parameter(“Mandatory 2”)", 20, 
group.parameter("Mandatory 2").intValue());
+        assertEquals("parameter(“Optional 3”)",  30, group.parameter("Optional 
3") .intValue());
+        assertEquals("parameter(“Optional 4”)",  40, group.parameter("Optional 
4") .intValue());
+    }
+
+    /**
+     * Tests {@code DefaultParameterValueGroup.values().get(…)} on a group 
expected to be pre-filled
+     * with mandatory parameters.
+     */
+    @Test
+    public void testValuesGet() {
+        final DefaultParameterValueGroup  group  = new 
DefaultParameterValueGroup(descriptor);
+        final List<GeneralParameterValue> values = group.values();
+        assertEquals("Initial size", 2, values.size());
+        assertEquals(descriptor.descriptors().get(0).createValue(), 
values.get(0));
+        assertEquals(descriptor.descriptors().get(1).createValue(), 
values.get(1));
+        try {
+            values.get(2);
+            fail("Index 2 shall be out of bounds.");
+        } catch (IndexOutOfBoundsException e) {
+            assertNotNull(e.getMessage());
+        }
+    }
+
+    /**
+     * Tests {@code DefaultParameterValueGroup.values().contains(…)}.
+     */
+    @Test
+    @DependsOnMethod("testValuesAddAll")
+    public void testValuesContains() {
+        final GeneralParameterValue[] positives = createValues(+10);
+        final GeneralParameterValue[] negatives = createValues(-10);
+        final List<GeneralParameterValue> values = createGroup(+10).values();
+        for (int i=0; i<positives.length; i++) {
+            assertTrue (values.contains(positives[i]));
+            assertFalse(values.contains(negatives[i]));
+        }
+    }
+
+    /**
      * Tests {@code DefaultParameterValueGroup.values().addAll(…)}.
      */
     @Test
-    public void testAddAll() {
-        final DefaultParameterDescriptorGroup descriptor = 
DefaultParameterDescriptorGroupTest.createGroup_2M_2O();
-        final DefaultParameterValueGroup group = new 
DefaultParameterValueGroup(descriptor);
+    @DependsOnMethod("testValuesAdd")
+    public void testValuesAddAll() {
+        final DefaultParameterValueGroup  group  = new 
DefaultParameterValueGroup(descriptor);
         final List<GeneralParameterValue> values = group.values();
         assertEquals("Initial size", 2, values.size());
 
-        final DefaultParameterValue<?>[] external = 
createValues(descriptor.descriptors(), 10);
-        assertTrue(values.addAll(Arrays.asList(external)));
-        assertEquals("Final size", external.length, values.size());
-        for (int i=0; i<external.length; i++) {
-            assertSame(external[i], values.get(i));
+        final DefaultParameterValue<?>[] parameters = createValues(10);
+        assertTrue(values.addAll(Arrays.asList(parameters)));
+        assertEquals("Final size", parameters.length, values.size());
+        for (int i=0; i<parameters.length; i++) {
+            assertSame(parameters[i], values.get(i));
+        }
+    }
+
+    /**
+     * Tests that attempts to add an invalid parameter cause an {@link 
InvalidParameterNameException} to be thrown.
+     */
+    @Test
+    @DependsOnMethod("testValuesAdd")
+    public void testValuesAddWrongParameter() {
+        final DefaultParameterValueGroup    group = createGroup(10);
+        final List<GeneralParameterValue>  values = group.values();
+        final ParameterValue<Integer> nonExistent = new 
DefaultParameterDescriptor<>(
+                name("Optional 5"), Integer.class, null, null, null, 
false).createValue();
+        try {
+            values.add(nonExistent);
+            fail("“Optional 5” is not a parameter for this group.");
+        } catch (InvalidParameterNameException e) {
+            assertEquals("Optional 5", e.getParameterName());
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("Optional 5"));
+            assertTrue(message, message.contains("The group"));
+        }
+    }
+
+    /**
+     * Tests {@code DefaultParameterValueGroup.values().add(…)}. In 
particular, tests that attempt to add
+     * a parameter already present causes an {@link 
InvalidParameterCardinalityException} to be thrown.
+     */
+    @Test
+    public void testValuesAdd() {
+        final GeneralParameterValue[]      toAdd = createValues(-10);
+        final DefaultParameterValueGroup   group = new 
DefaultParameterValueGroup(descriptor);
+        final List<GeneralParameterValue> values = group.values();
+        assertEquals("size", 2, values.size()); // Because the descriptor 
declares 2 parameters as mandatory.
+        assertTrue  ("add(“Mandatory 1”)", values.add(toAdd[0])); 
assertEquals("size", 2, values.size());
+        assertTrue  ("add(“Mandatory 2”)", values.add(toAdd[1])); 
assertEquals("size", 2, values.size());
+        assertTrue  ("add(“Optional 3”)",  values.add(toAdd[2])); 
assertEquals("size", 3, values.size());
+        assertTrue  ("add(“Optional 4”)",  values.add(toAdd[3])); 
assertEquals("size", 4, values.size());
+        /*
+         * Test [1…1] cardinality.
+         */
+        try {
+            values.add(toAdd[1]);
+            fail("“Mandatory 2” is already present in this group.");
+        } catch (InvalidParameterCardinalityException e) {
+            assertEquals("Mandatory 2", e.getParameterName());
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("Mandatory 2"));
+        }
+        assertEquals("size", 4, values.size()); // Size shall be unchanged.
+        /*
+         * Test [0…1] cardinality.
+         */
+        try {
+            values.add(toAdd[2]);
+            fail("“Optional 3” is already present in this group.");
+        } catch (InvalidParameterCardinalityException e) {
+            assertEquals("Optional 3", e.getParameterName());
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("Optional 3"));
+        }
+        assertEquals("size", 4, values.size()); // Size shall be unchanged.
+        /*
+         * Test [0…2] cardinality.
+         */
+        assertTrue("add(“Optional 4”)",values.add(toAdd[3]));
+        assertEquals("size", 5, values.size());
+        /*
+         * Verifies parameter values.
+         */
+        assertEquals("parameter(“Mandatory 1”)", -10, 
group.parameter("Mandatory 1").intValue());
+        assertEquals("parameter(“Mandatory 2”)", -20, 
group.parameter("Mandatory 2").intValue());
+        assertEquals("parameter(“Optional 3”)",  -30, group.parameter( 
"Optional 3").intValue());
+    }
+
+    /**
+     * Tests {@code DefaultParameterValueGroup.values().remove(…)}. In 
particular, tests that attempt to
+     * remove a mandatory parameter causes an {@link 
InvalidParameterCardinalityException} to be thrown.
+     */
+    @Test
+    @DependsOnMethod("testValuesAddAll")
+    public void testValuesRemove() {
+        final GeneralParameterValue[]  negatives = createValues(-10);
+        final DefaultParameterValueGroup   group = createGroup(10);
+        final List<GeneralParameterValue> values = group.values();
+        assertFalse(values.remove(negatives[0])); // Non-existant parameter.
+        try {
+            values.remove(values.get(0));
+            fail("“Mandatory 1” is a mandatory parameter; it should not be 
removeable.");
+        } catch (InvalidParameterCardinalityException e) {
+            assertEquals("Mandatory 1", e.getParameterName());
+            final String message = e.getMessage();
+            assertTrue(message, message.contains("Mandatory 1"));
         }
     }
 
@@ -105,15 +278,30 @@ public final strictfp class DefaultParam
      */
     @Test
     public void testAddGroup() {
-        final ParameterDescriptorGroup groupDesc = new 
DefaultParameterDescriptorGroup(name("theGroup"), 1, 1,
+        descriptor = new DefaultParameterDescriptorGroup(name("theGroup"), 1, 
1,
                 new DefaultParameterDescriptorGroup(name("theSubGroup"), 0, 10)
         );
-        validate(groupDesc);
+        validate(descriptor);
 
-        final ParameterValueGroup groupValues = groupDesc.createValue();
+        final ParameterValueGroup groupValues = descriptor.createValue();
         assertEquals("Size before add.", 0, groupValues.values().size());
         final ParameterValueGroup subGroupValues = 
groupValues.addGroup("theSubGroup");
         assertEquals("Size after add.", 1, groupValues.values().size());
         assertSame(subGroupValues, groupValues.values().get(0));
     }
+
+    /**
+     * Tests {@link #equals(Object)} and {@link #hashCode()} methods.
+     */
+    @Test
+    @DependsOnMethod("testValuesAddAll")
+    public void testEqualsAndHashCode() {
+        final DefaultParameterValueGroup g1 = createGroup( 10);
+        final DefaultParameterValueGroup g2 = createGroup(-10);
+        final DefaultParameterValueGroup g3 = createGroup( 10);
+        assertTrue ("equals", g1.equals(g1));
+        assertFalse("equals", g1.equals(g2));
+        assertTrue ("equals", g1.equals(g3));
+        assertEquals("hashCode", g1.hashCode(), g3.hashCode());
+    }
 }


Reply via email to