sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1592563 - in /sis/branches/JDK8/core: sis-feature/src/main/java/org/apache/sis/feature/ sis-feature/src/test/java/org/apache/sis/feature/ sis-feature/src/test/java/org/apache/sis/test/suite/ sis-utility/src/main/java/org/apache/sis/interna...
Date Mon, 05 May 2014 16:02:20 GMT
Author: desruisseaux
Date: Mon May  5 16:02:19 2014
New Revision: 1592563

URL: http://svn.apache.org/r1592563
Log:
Added more Feature tests.

Added:
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTest.java
  (with props)
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/SingletonValueTest.java
  (with props)
Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractIdentifiedType.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAttribute.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultFeature.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/SingletonValue.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTypeTest.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultFeatureTest.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultFeatureTypeTest.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
    sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractIdentifiedType.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractIdentifiedType.java?rev=1592563&r1=1592562&r2=1592563&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractIdentifiedType.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/AbstractIdentifiedType.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -270,7 +270,7 @@ public class AbstractIdentifiedType impl
      */
     @Override
     public boolean equals(final Object obj) {
-        if (obj != null && getClass() != obj.getClass()) {
+        if (obj != null && getClass() == obj.getClass()) {
             final AbstractIdentifiedType that = (AbstractIdentifiedType) obj;
             return Objects.equals(name,        that.name) &&
                    Objects.equals(definition,  that.definition) &&

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAttribute.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAttribute.java?rev=1592563&r1=1592562&r2=1592563&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAttribute.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAttribute.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -37,7 +37,7 @@ import java.util.Objects;
  * @version 0.5
  * @module
  */
-final class DefaultAttribute<T> implements Serializable {
+public class DefaultAttribute<T> implements Serializable {
     /**
      * For cross-version compatibility.
      */
@@ -78,6 +78,8 @@ final class DefaultAttribute<T> implemen
      * Returns the attribute value.
      *
      * @return The attribute value (may be {@code null}).
+     *
+     * @see DefaultFeature#getAttributeValue(String)
      */
     public T getValue() {
         return value;
@@ -86,17 +88,36 @@ final class DefaultAttribute<T> implemen
     /**
      * Sets the attribute value.
      *
-     * <div class="warning">Current implementation does not yet performed any validation.
-     * However future Apache SIS version is likely to check argument validity here.</div>
+     * {@section Validation}
+     * The amount of validation performed by this method is implementation dependent.
+     * The current {@code DefaultAttribute} implementation performs only very cheap (if any)
validations.
+     * A more exhaustive verification can be performed by invoking the {@link #validate()}
method.
      *
      * @param  value The new value.
-     * @throws IllegalArgumentException If the given value is outside the attribute domain.
+     * @throws RuntimeException If this method performs validation and the given value does
not meet the conditions.
+     *         <em>This exception will be changed to {@code IllegalAttributeException}
in a future SIS version.</em>
+     *
+     * @see DefaultFeature#setAttributeValue(String, Object)
      */
-    public void setValue(final T value) throws IllegalArgumentException {
+    public void setValue(final T value) {
         this.value = value;
     }
 
     /**
+     * Ensures that the current attribute value complies with the constraints defined by
the attribute type.
+     * This method can be invoked explicitly on a single attribute, or may be invoked implicitly
by a call to
+     * {@link DefaultFeature#validate()}.
+     *
+     * @throws RuntimeException If the current attribute value violates a constraint.
+     *         <em>This exception will be changed to {@code IllegalAttributeException}
in a future SIS version.</em>
+     *
+     * @see DefaultFeature#validate()
+     */
+    public void validate() {
+        // TODO: future SIS implementation shall check constraints here.
+    }
+
+    /**
      * Returns a hash code value for this attribute type.
      *
      * @return A hash code value.
@@ -116,7 +137,7 @@ final class DefaultAttribute<T> implemen
         if (obj == this) {
             return true;
         }
-        if (obj.getClass() == getClass()) {
+        if (obj != null && obj.getClass() == getClass()) {
             final DefaultAttribute<?> that = (DefaultAttribute<?>) obj;
             return type.equals(that.type) &&
                    Objects.equals(value, that.value);

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultFeature.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultFeature.java?rev=1592563&r1=1592562&r2=1592563&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultFeature.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultFeature.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -16,6 +16,7 @@
  */
 package org.apache.sis.feature;
 
+import java.util.List;
 import java.util.Map;
 import java.util.HashMap;
 import java.util.ConcurrentModificationException;
@@ -76,10 +77,22 @@ public class DefaultFeature implements S
     }
 
     /**
-     * Returns the value of the attribute of the given name.
+     * Returns all attributes of the given name.
+     *
+     * @param  name The attribute name.
+     * @return All attributes of the given name, or an empty list if none.
+     */
+    public List<DefaultAttribute<?>> getAttributes(final String name) {
+        return null;
+    }
+
+    /**
+     * Returns the value(s) of the attribute of the given name.
      *
      * @param  name The attribute name.
      * @return The value for the given attribute, or {@code null} if none.
+     *
+     * @see DefaultAttribute#getValue()
      */
     public Object getAttributeValue(final String name) {
         final DefaultAttribute<?> attribute = properties.get(name);
@@ -96,8 +109,15 @@ public class DefaultFeature implements S
     /**
      * Sets the value of the attribute of the given name.
      *
+     * {@section Validation}
+     * The amount of validation performed by this method is implementation dependent.
+     * The current {@code DefaultFeature} implementation performs only very cheap (if any)
validations.
+     * A more exhaustive verification can be performed by invoking the {@link #validate()}
method.
+     *
      * @param name  The attribute name.
      * @param value The new value for the given attribute (may be {@code null}).
+     *
+     * @see DefaultAttribute#setValue(Object)
      */
     @SuppressWarnings("unchecked")
     public void setAttributeValue(final String name, final Object value) {
@@ -127,6 +147,19 @@ public class DefaultFeature implements S
     }
 
     /**
+     * Ensures that all current property values comply with the constraints defined by the
feature type.
+     * This method will implicitly invokes {@link DefaultAttribute#validate()} for all attribute
values.
+     *
+     * @throws RuntimeException If the current attribute value violates a constraint.
+     *         <em>This exception will be changed to {@code IllegalAttributeException}
in a future SIS version.</em>
+     *
+     * @see DefaultAttribute#validate()
+     */
+    public void validate() {
+        // TODO
+    }
+
+    /**
      * Returns a hash code value for this feature.
      *
      * @return A hash code value.

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/SingletonValue.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/SingletonValue.java?rev=1592563&r1=1592562&r2=1592563&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/SingletonValue.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/SingletonValue.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -34,6 +34,10 @@ import static org.apache.sis.util.Argume
  * <p>There is no need to keep long-lived references to instances of this class.
  * Instances can be recreated when needed.</p>
  *
+ * {@section Non serialization}
+ * This class is intentionally not serializable, since serializing this instance would imply
serializing the whole
+ * map of attributes if we want to keep the <cite>change in this list are reflected
in the feature</cite> contract.
+ *
  * @author  Martin Desruisseaux (Geomatys)
  * @since   0.5
  * @version 0.5
@@ -68,6 +72,22 @@ final class SingletonValue extends Abstr
     }
 
     /**
+     * Returns the index of the given element (which can only be 0), or -1 if not present.
+     */
+    @Override
+    public int indexOf(final Object element) {
+        return (element != null) && element.equals(properties.get(key)) ? 0 : -1;
+    }
+
+    /**
+     * Returns the index of the given element (which can only be 0), or -1 if not present.
+     */
+    @Override
+    public int lastIndexOf(final Object element) {
+        return indexOf(element);
+    }
+
+    /**
      * Returns the attribute associated to the key, if present.
      */
     @Override
@@ -88,6 +108,7 @@ final class SingletonValue extends Abstr
     public DefaultAttribute<?> set(final int index, final DefaultAttribute<?>
element) {
         ensureNonNull("element", element);
         if (index == 0) {
+            modCount++;
             final Object previous = properties.put(key, element);
             if (previous != null) {
                 return (DefaultAttribute<?>) previous;
@@ -107,6 +128,7 @@ final class SingletonValue extends Abstr
         ensureNonNull("element", element);
         if (index == 0) {
             if (properties.putIfAbsent(key, element) == null) {
+                modCount++;
                 return;
             }
             throw new IllegalStateException(Errors.format(Errors.Keys.ElementAlreadyPresent_1,
key));
@@ -125,12 +147,17 @@ final class SingletonValue extends Abstr
 
     /**
      * Removes the attribute associated to the key.
+     *
+     * This method does not checks if the removal is allowed by the
+     * {@linkplain DefaultAttributeType#getCardinality() cardinality}.
+     * Such check can be performed by {@link DefaultFeature#validate()}.
      */
     @Override
-    public  DefaultAttribute<?> remove(final int index) {
+    public DefaultAttribute<?> remove(final int index) {
         if (index == 0) {
             final Object previous = properties.remove(key);
             if (previous != null) {
+                modCount++;
                 return (DefaultAttribute<?>) previous;
             }
         }
@@ -138,10 +165,29 @@ final class SingletonValue extends Abstr
     }
 
     /**
+     * Removes the singleton value, if presents.
+     * This method is for {@link Iter#remove()} implementation only.
+     *
+     * @param  c The expected {@link #modCount} value, for check against concurrent modification.
+     * @return {@code true} if the value has been removed.
+     */
+    final boolean clear(final int c) {
+        if (c != modCount) {
+            throw new ConcurrentModificationException();
+        }
+        return properties.remove(key) != null;
+    }
+
+    /**
      * Removes the attribute associated to the key.
+     *
+     * This method does not checks if the removal is allowed by the
+     * {@linkplain DefaultAttributeType#getCardinality() cardinality}.
+     * Such check can be performed by {@link DefaultFeature#validate()}.
      */
     @Override
     public void clear() {
+        modCount++;
         properties.remove(key);
     }
 
@@ -150,23 +196,29 @@ final class SingletonValue extends Abstr
      */
     @Override
     public Iterator<DefaultAttribute<?>> iterator() {
-        return new Iter((DefaultAttribute<?>) properties.get(key));
+        return new Iter((DefaultAttribute<?>) properties.get(key), modCount);
     }
 
     /**
      * Implementation of the iterator returned by {@link SingletonValue#iterator()}.
      */
-    private static final class Iter implements Iterator<DefaultAttribute<?>>
{
+    private final class Iter implements Iterator<DefaultAttribute<?>> {
         /**
          * The attribute to return, or {@code null} if we reached the iteration end.
          */
         private DefaultAttribute<?> element;
 
         /**
+         * Initial {@link SingletonValue#modCount} value, for checks against concurrent modifications.
+         */
+        private final int c;
+
+        /**
          * Creates a new iterator which will return the given attribute.
          */
-        Iter(final DefaultAttribute<?> element) {
+        Iter(final DefaultAttribute<?> element, final int c) {
             this.element = element;
+            this.c = c;
         }
 
         /**
@@ -189,5 +241,15 @@ final class SingletonValue extends Abstr
             element = null;
             return v;
         }
+
+        /**
+         * Removes the value returned by the last call to {@link #next()}.
+         */
+        @Override
+        public void remove() {
+            if (element != null || !clear(c)) {
+                throw new IllegalStateException();
+            }
+        }
     }
 }

Added: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTest.java?rev=1592563&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTest.java
(added)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTest.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -0,0 +1,91 @@
+/*
+ * 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.feature;
+
+import java.util.HashMap;
+import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.apache.sis.test.Assert.*;
+
+
+/**
+ * Tests {@link DefaultAttribute}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.5
+ * @version 0.5
+ * @module
+ */
+@DependsOn(DefaultAttributeTypeTest.class)
+public final strictfp class DefaultAttributeTest extends TestCase {
+    /**
+     * Creates an attribute for the city name.
+     * This attribute has a default value.
+     */
+    static DefaultAttribute<String> city() {
+        return new DefaultAttribute<>(DefaultAttributeTypeTest.city(new HashMap<>(4)));
+    }
+
+    /**
+     * Creates an attribute for a singleton value.
+     * This attribute has no default value.
+     */
+    static DefaultAttribute<Integer> population() {
+        return new DefaultAttribute<>(DefaultAttributeTypeTest.population(new HashMap<>(4)));
+    }
+
+    /**
+     * Tests getting and setting an attribute value.
+     */
+    @Test
+    public void testValue() {
+        final DefaultAttribute<Integer> attribute = population();
+        assertNull("value", attribute.getValue());
+        attribute.setValue(1000);
+        assertEquals("value", Integer.valueOf(1000), attribute.getValue());
+        attribute.setValue(null);
+        assertNull("value", attribute.getValue());
+    }
+
+    /**
+     * Tests attribute comparison.
+     */
+    @Test
+    public void testEquals() {
+        final DefaultAttribute<Integer> a1 = population();
+        final DefaultAttribute<Integer> a2 = population();
+        assertFalse ("equals",   a1.equals(null));
+        assertTrue  ("equals",   a1.equals(a2));
+        assertEquals("hashCode", a1.hashCode(), a2.hashCode());
+        a2.setValue(1000);
+        assertFalse("equals",   a1.equals(a2));
+        assertFalse("hashCode", a1.hashCode() == a2.hashCode());
+    }
+
+    /**
+     * Tests serialization.
+     */
+    @Test
+    @DependsOnMethod("testEquals")
+    public void testSerialization() {
+        final DefaultAttribute<String> attribute = city();
+        assertSerializedEquals(attribute);
+    }
+}

Propchange: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTypeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTypeTest.java?rev=1592563&r1=1592562&r2=1592563&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTypeTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTypeTest.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -23,10 +23,11 @@ import org.opengis.util.LocalName;
 import org.opengis.util.GenericName;
 import org.opengis.util.InternationalString;
 import org.apache.sis.measure.NumberRange;
+import org.apache.sis.test.DependsOnMethod;
 import org.apache.sis.test.TestCase;
 import org.junit.Test;
 
-import static org.opengis.test.Assert.*;
+import static org.apache.sis.test.Assert.*;
 
 
 /**
@@ -59,6 +60,19 @@ public final strictfp class DefaultAttri
     }
 
     /**
+     * Creates an attribute type for city population.
+     *
+     * @param properties An empty temporary map (provided only for recycling existing instances).
+     */
+    static DefaultAttributeType<Integer> population(final Map<String,Object>
properties) {
+        assertNull(properties.put(DefaultAttributeType.NAME_KEY, "population"));
+        final DefaultAttributeType<Integer> population = new DefaultAttributeType<>(
+                properties, Integer.class, null, NumberRange.create(1, true, 1, true));
+        properties.clear();
+        return population;
+    }
+
+    /**
      * Tests the creation of a simple {@link DefaultAttributeType} instance for a mandatory
singleton.
      */
     @Test
@@ -89,4 +103,27 @@ public final strictfp class DefaultAttri
         assertEquals("cardinality.minValue", Integer.valueOf(1), cardinality.getMinValue());
         assertEquals("cardinality.maxValue", Integer.valueOf(1), cardinality.getMaxValue());
     }
+
+    /**
+     * Tests attribute comparison.
+     */
+    @Test
+    public void testEquals() {
+        final Map<String,Object> properties = new HashMap<>(4);
+        final DefaultAttributeType<Integer> a1 = population(properties);
+        final DefaultAttributeType<Integer> a2 = population(properties);
+        assertFalse ("equals",   a1.equals(null));
+        assertTrue  ("equals",   a1.equals(a2));
+        assertEquals("hashCode", a1.hashCode(), a2.hashCode());
+    }
+
+    /**
+     * Tests serialization.
+     */
+    @Test
+    @DependsOnMethod("testEquals")
+    public void testSerialization() {
+        final DefaultAttributeType<String> attribute = city(new HashMap<>(4));
+        assertSerializedEquals(attribute);
+    }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultFeatureTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultFeatureTest.java?rev=1592563&r1=1592562&r2=1592563&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultFeatureTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultFeatureTest.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -31,21 +31,25 @@ import static org.junit.Assert.*;
  * @version 0.5
  * @module
  */
-@DependsOn(DefaultFeatureTypeTest.class)
+@DependsOn({
+    DefaultFeatureTypeTest.class,
+    DefaultAttributeTest.class,
+    SingletonValueTest.class
+})
 public final strictfp class DefaultFeatureTest extends TestCase {
     /**
      * Tests the construction of a simple feature without super-types.
      */
     @Test
     public void testSimple() {
-        final DefaultFeature simple = new DefaultFeature(DefaultFeatureTypeTest.simple());
+        final DefaultFeature cityPopulation = new DefaultFeature(DefaultFeatureTypeTest.cityPopulation());
 
-        assertEquals("Utopia", simple.getAttributeValue("city"));
-        simple.setAttributeValue("city", "Atlantide");
-        assertEquals("Atlantide", simple.getAttributeValue("city"));
+        assertEquals("Utopia", cityPopulation.getAttributeValue("city"));
+        cityPopulation.setAttributeValue("city", "Atlantide");
+        assertEquals("Atlantide", cityPopulation.getAttributeValue("city"));
 
-        assertNull(simple.getAttributeValue("population"));
-        simple.setAttributeValue("population", 1000);
-        assertEquals(1000, simple.getAttributeValue("population"));
+        assertNull(cityPopulation.getAttributeValue("population"));
+        cityPopulation.setAttributeValue("population", 1000);
+        assertEquals(1000, cityPopulation.getAttributeValue("population"));
     }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultFeatureTypeTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultFeatureTypeTest.java?rev=1592563&r1=1592562&r2=1592563&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultFeatureTypeTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultFeatureTypeTest.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -42,14 +42,10 @@ public final strictfp class DefaultFeatu
     /**
      * Creates a simple feature type without super-types.
      */
-    static DefaultFeatureType simple() {
+    static DefaultFeatureType cityPopulation() {
         final Map<String,Object> properties = new HashMap<>();
         final DefaultAttributeType<String> city = DefaultAttributeTypeTest.city(properties);
-
-        properties.clear();
-        assertNull(properties.put(DefaultAttributeType.NAME_KEY, "population"));
-        final DefaultAttributeType<Integer> population = new DefaultAttributeType<>(
-                properties, Integer.class, null, NumberRange.create(1, true, 1, true));
+        final DefaultAttributeType<Integer> population = DefaultAttributeTypeTest.population(properties);
 
         properties.clear();
         assertNull(properties.put(DefaultAttributeType.NAME_KEY, "City population"));
@@ -61,7 +57,7 @@ public final strictfp class DefaultFeatu
      */
     @Test
     public void testSimple() {
-        final DefaultFeatureType simple = simple();
+        final DefaultFeatureType simple = cityPopulation();
         assertEquals("name", "City population", simple.getName().toString());
         /*
          * Verify content.

Added: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/SingletonValueTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/SingletonValueTest.java?rev=1592563&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/SingletonValueTest.java
(added)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/SingletonValueTest.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -0,0 +1,151 @@
+/*
+ * 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.feature;
+
+import java.util.Set;
+import java.util.Map;
+import java.util.HashMap;
+import org.apache.sis.test.DependsOnMethod;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static org.junit.Assert.*;
+import static java.util.Collections.singleton;
+import static java.util.Collections.singletonMap;
+import static org.apache.sis.test.TestUtilities.getSingleton;
+
+
+/**
+ * Tests {@link SingletonValue}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.5
+ * @version 0.5
+ * @module
+ */
+@DependsOn(DefaultAttributeTest.class)
+public final strictfp class SingletonValueTest extends TestCase {
+    /**
+     * The key used for storing value in this test class.
+     */
+    private static final String KEY = "test key";
+
+    /**
+     * The instance to test.
+     */
+    private final SingletonValue singleton;
+
+    /**
+     * The map of properties given to the {@link #singleton} instance to test.
+     */
+    private final Map<String, Object> properties;
+
+    /**
+     * Arbitrary values added to {@link #properties} for making sure
+     */
+    private final Map<String,?> otherValues;
+
+    /**
+     * Creates a new test case.
+     */
+    public SingletonValueTest() {
+        otherValues = singletonMap("other key", "other value");
+        properties  = new HashMap<>(otherValues);
+        singleton   = new SingletonValue(properties, KEY);
+    }
+
+    /**
+     * Tests an empty list.
+     */
+    @Test
+    public void testEmpty() {
+        assertEquals("size", 0, singleton.size());
+        assertTrue("isEmpty", singleton.isEmpty());
+        assertFalse("iterator.hasNext", singleton.iterator().hasNext());
+        assertFalse("listIterator.hasNext", singleton.listIterator().hasNext());
+        try {
+            singleton.get(0);
+            fail("Element 0 is not expected to exist.");
+        } catch (IndexOutOfBoundsException e) {
+            assertNotNull(e.getMessage());
+        }
+        try {
+            singleton.remove(0);
+            fail("Element 0 is not expected to exist.");
+        } catch (IndexOutOfBoundsException e) {
+            assertNotNull(e.getMessage());
+        }
+        assertEquals("Other values shall be unmodified.", otherValues, properties);
+    }
+
+    /**
+     * Tests the addition, setting and removal of a single element.
+     */
+    @Test
+    @DependsOnMethod("testEmpty")
+    public void testSingleton() {
+        final DefaultAttribute<Integer> a1 = DefaultAttributeTest.population();
+        final DefaultAttribute<Integer> a2 = DefaultAttributeTest.population();
+        a1.setValue(1000);
+        a2.setValue(2000);
+        assertEquals("indexOf",  -1, singleton.indexOf(a1));
+        assertTrue  ("add",          singleton.add(a1));
+        assertEquals("size",      1, singleton.size());
+        assertFalse ("isEmpty",      singleton.isEmpty());
+        assertEquals("indexOf",   0, singleton.indexOf(a1));
+        assertSame  ("get",      a1, singleton.get(0));
+        assertSame  ("iterator", a1, getSingleton(singleton));
+        assertSame  ("set",      a1, singleton.set(0, a2));
+        assertSame  ("get",      a2, singleton.get(0));
+        assertSame  ("iterator", a2, getSingleton(singleton));
+        assertSame  ("remove",   a2, singleton.remove(0));
+        assertEquals("size",      0, singleton.size());
+        assertEquals("Other values shall be unmodified.", otherValues, properties);
+    }
+
+    /**
+     * Ensures that we can not add more than 1 element.
+     */
+    @Test
+    @DependsOnMethod("testSingleton")
+    public void testMaximumOccurrence() {
+        final DefaultAttribute<Integer> a1 = DefaultAttributeTest.population();
+        final DefaultAttribute<Integer> a2 = DefaultAttributeTest.population();
+        assertTrue("add", singleton.add(a1));
+        try {
+            assertTrue("add", singleton.add(a2));
+            fail("Shall not be allowed to add more than 1 element.");
+        } catch (IllegalStateException e) {
+            final String message = e.getMessage();
+            assertTrue(message, message.contains(KEY));
+        }
+    }
+
+    /**
+     * Tests {@code addAll(…)} and {@code removeAll(…)} methods.
+     */
+    @Test
+    @DependsOnMethod("testSingleton")
+    public void testRemoveAll() {
+        final Set<DefaultAttribute<Integer>> attributes = singleton(DefaultAttributeTest.population());
+        assertTrue (singleton.addAll(attributes));
+        assertFalse(singleton.isEmpty());
+        assertTrue (singleton.removeAll(attributes));
+        assertTrue (singleton.isEmpty());
+    }
+}

Propchange: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/SingletonValueTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/SingletonValueTest.java
------------------------------------------------------------------------------
    svn:mime-type = text/plain;charset=UTF-8

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java?rev=1592563&r1=1592562&r2=1592563&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/test/suite/FeatureTestSuite.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -32,6 +32,8 @@ import org.junit.BeforeClass;
 @Suite.SuiteClasses({
     org.apache.sis.feature.DefaultAttributeTypeTest.class,
     org.apache.sis.feature.DefaultFeatureTypeTest.class,
+    org.apache.sis.feature.DefaultAttributeTest.class,
+    org.apache.sis.feature.SingletonValueTest.class,
     org.apache.sis.feature.DefaultFeatureTest.class
 })
 public final strictfp class FeatureTestSuite extends TestSuite {

Modified: sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java?rev=1592563&r1=1592562&r2=1592563&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-utility/src/main/java/org/apache/sis/internal/util/CheckedArrayList.java
[UTF-8] Mon May  5 16:02:19 2014
@@ -43,6 +43,9 @@ import static org.apache.sis.util.Argume
  *   <li>Does not accept null elements.</li>
  * </ul>
  *
+ * The checks are performed only on a <cite>best effort</cite> basis. In current
implementation,
+ * holes are known to exist in use cases like {@code sublist(…).set(…)} or when using
the list iterator.
+ *
  * @param <E> The type of elements in the list.
  *
  * @author  Martin Desruisseaux (Geomatys)



Mime
View raw message