|
From: <svn...@os...> - 2012-01-25 05:49:42
|
Author: ang05a
Date: 2012-01-24 21:49:34 -0800 (Tue, 24 Jan 2012)
New Revision: 38523
Added:
trunk/modules/extension/app-schema/app-schema/src/test/java/org/geotools/util/ComplexAttributeConverterFactoryTest.java
Modified:
trunk/modules/extension/app-schema/app-schema/src/main/java/org/geotools/util/ComplexAttributeConverterFactory.java
Log:
GEOT-3908: ComplexAttributeConverterFactory creates misbehaving converters
Modified: trunk/modules/extension/app-schema/app-schema/src/main/java/org/geotools/util/ComplexAttributeConverterFactory.java
===================================================================
--- trunk/modules/extension/app-schema/app-schema/src/main/java/org/geotools/util/ComplexAttributeConverterFactory.java 2012-01-23 17:43:19 UTC (rev 38522)
+++ trunk/modules/extension/app-schema/app-schema/src/main/java/org/geotools/util/ComplexAttributeConverterFactory.java 2012-01-25 05:49:34 UTC (rev 38523)
@@ -20,9 +20,12 @@
import java.util.Collection;
import org.geotools.factory.Hints;
+import org.geotools.feature.AttributeImpl;
+import org.geotools.filter.identity.FeatureIdImpl;
import org.opengis.feature.Attribute;
import org.opengis.feature.ComplexAttribute;
import org.opengis.feature.Property;
+import org.opengis.filter.identity.FeatureId;
/**
* This converter retrieves the values out of attributes.
@@ -39,32 +42,31 @@
if (ComplexAttribute.class.isAssignableFrom(source)) {
return new Converter() {
public Object convert(Object source, Class target) throws Exception {
- while (source instanceof ComplexAttribute) {
- if (!((ComplexAttribute) source).getType().getDescriptors().isEmpty()) {
- // this is not the leaf type..
+ if (source instanceof ComplexAttribute) {
+ Collection<? extends Property> valueMap = ((ComplexAttribute) source)
+ .getValue();
+ if (valueMap.isEmpty() || valueMap.size() > 1) {
return null;
} else {
- Collection<? extends Property> valueMap = ((ComplexAttribute) source)
- .getValue();
- if (valueMap.isEmpty()) {
- return null;
- } else {
- // there should only be one value
- source = valueMap.iterator().next();
+ // there should only be one value
+ source = valueMap.iterator().next();
+ if (AttributeImpl.class.equals(source.getClass())) {
+ return Converters.convert(((Attribute) source).getValue(), target);
}
}
}
- if (source instanceof Attribute) {
- return ((Attribute) source).getValue();
- }
return null;
}
};
}
- if (Attribute.class.isAssignableFrom(source)) {
+ // String to FeatureId comparison
+ if (FeatureId.class.isAssignableFrom(target) && String.class.isAssignableFrom(source)) {
return new Converter() {
- public Object convert(Object source, Class target) throws Exception {
- return ((Attribute) source).getValue();
+ public Object convert(Object source, Class target) {
+ if (source != null) {
+ return new FeatureIdImpl((String) source);
+ }
+ return null;
}
};
}
Added: trunk/modules/extension/app-schema/app-schema/src/test/java/org/geotools/util/ComplexAttributeConverterFactoryTest.java
===================================================================
--- trunk/modules/extension/app-schema/app-schema/src/test/java/org/geotools/util/ComplexAttributeConverterFactoryTest.java (rev 0)
+++ trunk/modules/extension/app-schema/app-schema/src/test/java/org/geotools/util/ComplexAttributeConverterFactoryTest.java 2012-01-25 05:49:34 UTC (rev 38523)
@@ -0,0 +1,104 @@
+/*
+ * GeoTools - The Open Source Java GIS Toolkit
+ * http://geotools.org
+ *
+ * (C) 2009-2011, Open Source Geospatial Foundation (OSGeo)
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation;
+ * version 2.1 of the License.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ */
+
+package org.geotools.util;
+
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.Set;
+
+import junit.framework.TestCase;
+
+import org.geotools.data.complex.ComplexFeatureConstants;
+import org.geotools.feature.AttributeImpl;
+import org.geotools.feature.ComplexAttributeImpl;
+import org.geotools.feature.type.AttributeDescriptorImpl;
+import org.geotools.gml3.GMLSchema;
+import org.geotools.xs.XSSchema;
+import org.opengis.feature.Attribute;
+import org.opengis.feature.ComplexAttribute;
+import org.opengis.feature.Property;
+import org.opengis.feature.type.AttributeDescriptor;
+import org.opengis.filter.identity.FeatureId;
+/**
+ * Tests for {@link ComplexAttributeConverterFactory}.
+ *
+ * @author Rini Angreani (CSIRO Earth Science and Resource Engineering)
+ *
+ *
+ *
+ *
+ * @source $URL: http://svn.osgeo.org/geotools/trunk/modules/extension/app-schema/app-schema/src/test/java/org/geotools/util/ComplexAttributeConverterFactoryTest.java $
+ */
+public class ComplexAttributeConverterFactoryTest extends TestCase {
+
+ /**
+ * Test extracting complex attribute leaf value should be successful.
+ */
+ public void testLeafComplexAttribute() {
+ Collection<Property> attributes = new ArrayList<Property>();
+ AttributeDescriptor descriptor = new AttributeDescriptorImpl(XSSchema.STRING_TYPE,
+ ComplexFeatureConstants.SIMPLE_CONTENT, 1, 1, true, (Object) null);
+ attributes.add(new AttributeImpl("rini", descriptor, null));
+ ComplexAttribute gmlName = new ComplexAttributeImpl(attributes, GMLSchema.CODETYPE_TYPE, null);
+ String nameString = Converters.convert(gmlName, String.class);
+ assertEquals("rini", nameString);
+ }
+
+ /**
+ * Test extracting complex attribute non-leaf value should fail.
+ */
+ public void testParentComplexAttribute() {
+ Collection<Property> attributes = new ArrayList<Property>();
+ AttributeDescriptor descriptor = new AttributeDescriptorImpl(XSSchema.STRING_TYPE,
+ ComplexFeatureConstants.SIMPLE_CONTENT, 1, 1, true, (Object) null);
+ attributes.add(new AttributeImpl("rini", descriptor, null));
+ ComplexAttribute gmlName = new ComplexAttributeImpl(attributes, GMLSchema.CODETYPE_TYPE, null);
+
+ Collection<Property> parentAttributes = new ArrayList<Property>();
+ parentAttributes.add(gmlName);
+ ComplexAttribute parentAtt = new ComplexAttributeImpl(parentAttributes, GMLSchema.ABSTRACTFEATURETYPE_TYPE, null);
+ String nameString = Converters.convert(parentAtt, String.class);
+
+ assertEquals(parentAtt.toString(), nameString);
+ assertNotSame("rini", nameString);
+ }
+
+ /**
+ * Test that normal Attribute shouldn't be affected by the converter.
+ */
+ public void testAttribute() {
+ AttributeDescriptor descriptor = new AttributeDescriptorImpl(XSSchema.STRING_TYPE,
+ ComplexFeatureConstants.SIMPLE_CONTENT, 1, 1, true, (Object) null);
+ Attribute att = new AttributeImpl("rini", descriptor, null);
+ Set<ConverterFactory> factories = Converters.getConverterFactories(att.getClass(), String.class);
+ for (ConverterFactory factory : factories) {
+ assertFalse(factory instanceof ComplexAttributeConverterFactory);
+ }
+ }
+
+ /**
+ * Test converting String to FeatureId successful. This is required by feature chaining.
+ * @throws Exception
+ */
+ public void testFeatureId() throws Exception {
+ FeatureId id = Converters.convert("blah", FeatureId.class);
+ assertNotNull(id);
+ assertEquals(id.getID(), "blah");
+ }
+
+}
|