sis-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From desruisse...@apache.org
Subject svn commit: r1594959 - in /sis/branches/JDK8/core/sis-feature/src: main/java/org/apache/sis/feature/ test/java/org/apache/sis/feature/ test/java/org/apache/sis/test/suite/
Date Thu, 15 May 2014 15:52:34 GMT
Author: desruisseaux
Date: Thu May 15 15:52:34 2014
New Revision: 1594959

URL: http://svn.apache.org/r1594959
Log:
Added tests for 'toString()' implementation.

Added:
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultOperationTest.java
  (with props)
Modified:
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociation.java
    sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociationRole.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/DefaultOperation.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAssociationRoleTest.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAssociationTest.java
    sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTest.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/test/suite/FeatureTestSuite.java

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociation.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociation.java?rev=1594959&r1=1594958&r2=1594959&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociation.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociation.java
[UTF-8] Thu May 15 15:52:34 2014
@@ -211,7 +211,13 @@ public class DefaultAssociation extends 
     @Debug
     @Override
     public String toString() {
-        return role.toString("Association", role.getValueType().getName().toString())
-                .append(" = ").append(value).toString();
+        final StringBuilder buffer = role.toString("FeatureAssociation", role.getValueType().getName().toString());
+        if (value != null) {
+            final String pt = role.getTitleProperty();
+            if (pt != null) {
+                buffer.append(" = ").append(value.getPropertyValue(pt));
+            }
+        }
+        return buffer.toString();
     }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociationRole.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociationRole.java?rev=1594959&r1=1594958&r2=1594959&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociationRole.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultAssociationRole.java
[UTF-8] Thu May 15 15:52:34 2014
@@ -62,6 +62,15 @@ public class DefaultAssociationRole exte
     private final DefaultFeatureType valueType;
 
     /**
+     * The name of the property to use as a title for the associated feature, or an empty
string if none.
+     * This field is initially null, then computed when first needed.
+     * This field is used only by {@link DefaultAssociation#toString()} implementation.
+     *
+     * @see #getTitleProperty()
+     */
+    private volatile transient String titleProperty;
+
+    /**
      * Constructs an association role from the given properties. The properties map is given
unchanged
      * to the {@linkplain AbstractIdentifiedType#AbstractIdentifiedType(Map) super-class
constructor}.
      * The following table is a reminder of main (not all) recognized map entries:
@@ -122,6 +131,28 @@ public class DefaultAssociationRole exte
     }
 
     /**
+     * Returns the name of the property to use as a title for the associated feature, or
{@code null} if none.
+     * This method search for the first attribute having a value class assignable to {@link
CharSequence}.
+     */
+    final String getTitleProperty() {
+        String p = titleProperty; // No synchronization - not a big deal if computed twice.
+        if (p == null) {
+            p = "";
+            for (final AbstractIdentifiedType type : valueType.properties(true)) {
+                if (type instanceof DefaultAttributeType<?>) {
+                    final DefaultAttributeType<?> pt = (DefaultAttributeType<?>)
type;
+                    if (pt.getMaximumOccurs() != 0 && CharSequence.class.isAssignableFrom(pt.getValueClass()))
{
+                        p = pt.getName().toString();
+                        break;
+                    }
+                }
+            }
+            titleProperty = p;
+        }
+        return p.isEmpty() ? null : p;
+    }
+
+    /**
      * Returns the minimum number of occurrences of the association within its containing
entity.
      * The returned value is greater than or equal to zero.
      *
@@ -181,6 +212,6 @@ public class DefaultAssociationRole exte
     @Debug
     @Override
     public String toString() {
-        return toString("AssociationRole", valueType.getName().toString()).toString();
+        return toString("FeatureAssociationRole", valueType.getName().toString()).toString();
     }
 }

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=1594959&r1=1594958&r2=1594959&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] Thu May 15 15:52:34 2014
@@ -228,7 +228,10 @@ public class DefaultAttribute<T> extends
     @Debug
     @Override
     public String toString() {
-        return type.toString("Attribute", Classes.getShortName(type.getValueClass()))
-                .append(" = ").append(value).toString();
+        final StringBuilder buffer = type.toString("Attribute", Classes.getShortName(type.getValueClass()));
+        if (value != null) {
+            buffer.append(" = ").append(value);
+        }
+        return buffer.toString();
     }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultOperation.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultOperation.java?rev=1594959&r1=1594958&r2=1594959&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultOperation.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/main/java/org/apache/sis/feature/DefaultOperation.java
[UTF-8] Thu May 15 15:52:34 2014
@@ -17,8 +17,15 @@
 package org.apache.sis.feature;
 
 import java.util.Map;
+import org.opengis.util.GenericName;
+import org.opengis.parameter.GeneralParameterDescriptor;
 import org.opengis.parameter.ParameterDescriptorGroup;
+import org.apache.sis.referencing.IdentifiedObjects;
 import org.apache.sis.util.ArgumentChecks;
+import org.apache.sis.util.Debug;
+
+// Related to JDK7
+import java.util.Objects;
 
 
 /**
@@ -69,7 +76,7 @@ public class DefaultOperation extends Pr
             final ParameterDescriptorGroup parameters, final DefaultAttributeType<?>
result)
     {
         super(identification);
-        ArgumentChecks.ensureNonNull("inputs", parameters);
+        ArgumentChecks.ensureNonNull("parameters", parameters);
         this.parameters = parameters;
         this.result     = result;
     }
@@ -91,4 +98,64 @@ public class DefaultOperation extends Pr
     public DefaultAttributeType<?> getResult() {
         return result;
     }
+
+    /**
+     * Returns a hash code value for this operation.
+     *
+     * @return {@inheritDoc}
+     */
+    @Override
+    public int hashCode() {
+        return super.hashCode() + parameters.hashCode() + Objects.hashCode(result);
+    }
+
+    /**
+     * Compares this operation with the given object for equality.
+     *
+     * @return {@inheritDoc}
+     */
+    @Override
+    public boolean equals(final Object obj) {
+        if (obj == this) {
+            return true;
+        }
+        if (super.equals(obj)) {
+            final DefaultOperation that = (DefaultOperation) obj;
+            return parameters.equals(that.parameters) &&
+                   Objects.equals(result, that.result);
+        }
+        return false;
+    }
+
+    /**
+     * Returns a string representation of this operation.
+     * The returned string is for debugging purpose and may change in any future SIS version.
+     *
+     * @return A string representation of this operation for debugging purpose.
+     */
+    @Debug
+    @Override
+    public String toString() {
+        final StringBuilder buffer = new StringBuilder(40).append("Operation").append('[');
+        final GenericName name = super.getName();
+        if (name != null) {
+            buffer.append('“');
+        }
+        buffer.append(name);
+        if (name != null) {
+            buffer.append('”');
+        }
+        String separator = " (";
+        for (final GeneralParameterDescriptor param : parameters.descriptors()) {
+            buffer.append(separator).append(IdentifiedObjects.toString(param.getName()));
+            separator = ", ";
+        }
+        if (separator == ", ") { // Identity comparaison is okay here.
+            buffer.append(')');
+        }
+        if (result != null) {
+            buffer.append(" : ").append(result.getName());
+        }
+        return buffer.append(']').toString();
+    }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAssociationRoleTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAssociationRoleTest.java?rev=1594959&r1=1594958&r2=1594959&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAssociationRoleTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAssociationRoleTest.java
[UTF-8] Thu May 15 15:52:34 2014
@@ -49,4 +49,22 @@ public final strictfp class DefaultAssoc
     public void testSerialization() {
         assertSerializedEquals(twinTown());
     }
+
+    /**
+     * Tests {@link DefaultAssociationRole#getTitleProperty()}.
+     */
+    @Test
+    public void testGetTitleProperty() {
+        final DefaultAssociationRole twinTown = twinTown();
+        assertEquals("city", twinTown.getTitleProperty());
+    }
+
+    /**
+     * Tests {@link DefaultAssociationRole#toString()}.
+     */
+    @Test
+    public void testToString() {
+        final DefaultAssociationRole twinTown = twinTown();
+        assertEquals("FeatureAssociationRole[“twin town” : City]", twinTown.toString());
+    }
 }

Modified: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAssociationTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAssociationTest.java?rev=1594959&r1=1594958&r2=1594959&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAssociationTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAssociationTest.java
[UTF-8] Thu May 15 15:52:34 2014
@@ -78,4 +78,13 @@ public final strictfp class DefaultAssoc
     public void testSerialization() {
         assertSerializedEquals(twinTown());
     }
+
+    /**
+     * Tests {@link DefaultAssociation#toString()}.
+     */
+    @Test
+    public void testToString() {
+        final DefaultAssociation twinTown = twinTown();
+        assertEquals("FeatureAssociation[“twin town” : City] = Le Mans", twinTown.toString());
+    }
 }

Modified: 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=1594959&r1=1594958&r2=1594959&view=diff
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTest.java
[UTF-8] (original)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultAttributeTest.java
[UTF-8] Thu May 15 15:52:34 2014
@@ -151,4 +151,16 @@ public final strictfp class DefaultAttri
         final DefaultAttribute<String> attribute = city();
         assertSerializedEquals(attribute);
     }
+
+    /**
+     * Tests {@link DefaultAttribute#toString()}.
+     */
+    @Test
+    @DependsOnMethod("testValue")
+    public void testToString() {
+        final DefaultAttribute<String> city = city();
+        assertEquals("Attribute[“city” : String] = Utopia", city.toString());
+        city.setValue("Dystopia");
+        assertEquals("Attribute[“city” : String] = Dystopia", city.toString());
+    }
 }

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=1594959&r1=1594958&r2=1594959&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] Thu May 15 15:52:34 2014
@@ -123,4 +123,13 @@ public final strictfp class DefaultAttri
         final DefaultAttributeType<String> attribute = city(new HashMap<>(4));
         assertSerializedEquals(attribute);
     }
+
+    /**
+     * Tests {@link DefaultAttributeType#toString()}.
+     */
+    @Test
+    public void testToString() {
+        final DefaultAttributeType<String> city = city(new HashMap<>());
+        assertEquals("AttributeType[“city” : String]", city.toString());
+    }
 }

Added: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultOperationTest.java
URL: http://svn.apache.org/viewvc/sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultOperationTest.java?rev=1594959&view=auto
==============================================================================
--- sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultOperationTest.java
(added)
+++ sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultOperationTest.java
[UTF-8] Thu May 15 15:52:34 2014
@@ -0,0 +1,68 @@
+/*
+ * 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.opengis.parameter.ParameterDescriptor;
+import org.apache.sis.parameter.ParameterBuilder;
+import org.apache.sis.test.DependsOn;
+import org.apache.sis.test.TestCase;
+import org.junit.Test;
+
+import static java.util.Collections.singletonMap;
+import static org.apache.sis.test.Assert.*;
+
+
+/**
+ * Tests {@link DefaultOperationTest}.
+ *
+ * @author  Martin Desruisseaux (Geomatys)
+ * @since   0.5
+ * @version 0.5
+ * @module
+ */
+@DependsOn(DefaultAttributeTest.class)
+public final strictfp class DefaultOperationTest extends TestCase {
+    /**
+     * Returns an operation that found new cities.
+     */
+    static DefaultOperation foundCity() {
+        final ParameterBuilder builder = new ParameterBuilder();
+        final ParameterDescriptor<?>[] parameters = {
+            builder.addName("founder").create(String.class, null)
+        };
+        return new DefaultOperation(singletonMap(DefaultOperation.NAME_KEY, "found city"),
+                builder.addName("found city").createGroup(parameters),
+                DefaultAttributeTypeTest.city(new HashMap<>(4)));
+    }
+
+    /**
+     * Tests serialization of {@link DefaultOperation}.
+     */
+    @Test
+    public void testSerialization() {
+        assertSerializedEquals(foundCity());
+    }
+
+    /**
+     * Tests {@link DefaultOperation#toString()}.
+     */
+    @Test
+    public void testToString() {
+        assertEquals("Operation[“found city” (founder) : city]", foundCity().toString());
+    }
+}

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

Propchange: sis/branches/JDK8/core/sis-feature/src/test/java/org/apache/sis/feature/DefaultOperationTest.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=1594959&r1=1594958&r2=1594959&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] Thu May 15 15:52:34 2014
@@ -36,7 +36,8 @@ import org.junit.BeforeClass;
     org.apache.sis.feature.PropertySingletonTest.class,
     org.apache.sis.feature.DefaultFeatureTest.class,
     org.apache.sis.feature.DefaultAssociationRoleTest.class,
-    org.apache.sis.feature.DefaultAssociationTest.class
+    org.apache.sis.feature.DefaultAssociationTest.class,
+    org.apache.sis.feature.DefaultOperationTest.class
 })
 public final strictfp class FeatureTestSuite extends TestSuite {
     /**



Mime
View raw message