001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2003-2006, Geotools Project Managment Committee (PMC)
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation; either
009: * version 2.1 of the License, or (at your option) any later version.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: * Created on July 21, 2003, 4:00 PM
017: */
018: package org.geotools.feature;
019:
020: import java.lang.reflect.Array;
021: import java.net.URI;
022: import java.util.ArrayList;
023: import java.util.HashMap;
024: import java.util.HashSet;
025: import java.util.List;
026: import java.util.Map;
027: import java.util.Set;
028:
029: import junit.framework.Test;
030: import junit.framework.TestSuite;
031:
032: import org.geotools.data.DataTestCase;
033:
034: import com.vividsolutions.jts.geom.Coordinate;
035: import com.vividsolutions.jts.geom.Geometry;
036: import com.vividsolutions.jts.geom.GeometryFactory;
037: import com.vividsolutions.jts.geom.Point;
038: import com.vividsolutions.jts.geom.PrecisionModel;
039:
040: /**
041: *
042: * @author en
043: * @author jgarnett
044: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/feature/FeatureTypeTest.java $
045: */
046: public class FeatureTypeTest extends DataTestCase {
047:
048: public FeatureTypeTest(String testName) {
049: super (testName);
050: }
051:
052: public static void main(String[] args) {
053: junit.textui.TestRunner.run(suite());
054: }
055:
056: public static Test suite() {
057: TestSuite suite = new TestSuite(FeatureTypeTest.class);
058: return suite;
059: }
060:
061: public void testAbstractType() throws Exception {
062:
063: FeatureTypeFactory at = FeatureTypeFactory
064: .newInstance("AbstractThing");
065: at.setAbstract(true);
066: at.setNamespace(new URI("http://www.nowhereinparticular.net"));
067:
068: FeatureType type1 = at.getFeatureType();
069: at.addType(AttributeTypeFactory.newAttributeType("X",
070: String.class));
071: Set bases = new HashSet();
072: bases.add(type1);
073: at.setSuperTypes(bases);
074: FeatureType type2 = at.getFeatureType();
075: assertTrue(type1.isAbstract());
076: assertTrue(type2.isAbstract());
077:
078: assertTrue(type1.isDescendedFrom(new URI(
079: "http://www.opengis.net/gml"), "Feature"));
080: assertTrue(type2.isDescendedFrom(new URI(
081: "http://www.opengis.net/gml"), "Feature"));
082: assertTrue(type2.isDescendedFrom(type1));
083: assertTrue(!type1.isDescendedFrom(type2));
084:
085: try {
086: type1.create(new Object[0]);
087: fail("abstract type allowed create");
088: } catch (IllegalAttributeException iae) {
089:
090: } catch (UnsupportedOperationException uoe) {
091:
092: }
093: try {
094: type2.create(new Object[0]);
095: fail("abstract type allowed create");
096: } catch (IllegalAttributeException iae) {
097:
098: } catch (UnsupportedOperationException uoe) {
099:
100: }
101:
102: // with non-abstract super
103: try {
104: FeatureType[] super s = new FeatureType[1];
105: super s[0] = FeatureTypeFactory.newFeatureType(null,
106: "SillyThing", null, false);
107: FeatureTypeFactory.newFeatureType(null, "BadFeature", null,
108: true, super s);
109: fail("allowed bad super");
110: } catch (SchemaException se) {
111:
112: }
113: }
114:
115: public void testEquals() throws Exception {
116: FeatureTypeBuilder at = FeatureTypeFactory.newInstance("Thing");
117: at.setNamespace(new URI("http://www.nowhereinparticular.net"));
118: at.addType(AttributeTypeFactory.newAttributeType("X",
119: String.class));
120: final FeatureType ft = at.getFeatureType();
121: at = FeatureTypeFactory.newInstance("Thing");
122: at.setNamespace(new URI("http://www.nowhereinparticular.net"));
123: at.addType(AttributeTypeFactory.newAttributeType("X",
124: String.class));
125: FeatureType ft2 = at.getFeatureType();
126: assertEquals(ft, ft2);
127: at.setName("Thingee");
128: assertTrue(!ft.equals(at.getFeatureType()));
129: at = FeatureTypeFactory.createTemplate(ft);
130: at.setNamespace(new URI("http://www.somewhereelse.net"));
131: assertTrue(!ft.equals(at.getFeatureType()));
132: assertTrue(!ft.equals(null));
133: }
134:
135: public void testCopyFeature() throws Exception {
136: Feature feature = lakeFeatures[0];
137: assertDuplicate("feature", feature, feature.getFeatureType()
138: .duplicate(feature));
139: }
140:
141: public void testDeepCopy() throws Exception {
142: // primative
143: String str = "FooBar";
144: Integer i = new Integer(3);
145: Float f = new Float(3.14);
146: Double d = new Double(3.14159);
147: AttributeType testType = AttributeTypeFactory.newAttributeType(
148: "test", Object.class);
149: assertSame("String", str, testType.duplicate(str));
150: assertSame("Integer", i, testType.duplicate(i));
151: assertSame("Float", f, testType.duplicate(f));
152: assertSame("Double", d, testType.duplicate(d));
153:
154: // collections
155: Object objs[] = new Object[] { str, i, f, d, };
156: int ints[] = new int[] { 1, 2, 3, 4, };
157: List list = new ArrayList();
158: list.add(str);
159: list.add(i);
160: list.add(f);
161: list.add(d);
162: Map map = new HashMap();
163: map.put("a", str);
164: map.put("b", i);
165: map.put("c", f);
166: map.put("d", d);
167: assertDuplicate("objs", objs, testType.duplicate(objs));
168: assertDuplicate("ints", ints, testType.duplicate(ints));
169: assertDuplicate("list", list, testType.duplicate(list));
170: assertDuplicate("map", map, testType.duplicate(map));
171:
172: // complex type
173: Feature feature = lakeFeatures[0];
174:
175: Coordinate coords = new Coordinate(1, 3);
176: Coordinate coords2 = new Coordinate(1, 3);
177: GeometryFactory gf = new GeometryFactory();
178: Geometry point = gf.createPoint(coords);
179: Geometry point2 = gf.createPoint(coords2);
180:
181: // JTS does not implement Object equals contract
182: assertTrue("jts identity", point != point2);
183: assertTrue("jts equals1", point.equals(point2));
184: assertTrue("jts equals", !point.equals((Object) point2));
185:
186: assertDuplicate("jts duplicate", point, point2);
187: assertDuplicate("feature", feature, testType.duplicate(feature));
188: assertDuplicate("point", point, testType.duplicate(point));
189: }
190:
191: static Set immutable;
192: static {
193: immutable = new HashSet();
194: immutable.add(String.class);
195: immutable.add(Integer.class);
196: immutable.add(Double.class);
197: immutable.add(Float.class);
198: }
199:
200: protected void assertDuplicate(String message, Object expected,
201: Object value) {
202: // Ensure value is equal to expected
203: if (expected.getClass().isArray()) {
204: int length1 = Array.getLength(expected);
205: int length2 = Array.getLength(value);
206: assertEquals(message, length1, length2);
207: for (int i = 0; i < length1; i++) {
208: assertDuplicate(message + "[" + i + "]", Array.get(
209: expected, i), Array.get(value, i));
210: }
211: //assertNotSame( message, expected, value );
212: } else if (expected instanceof Geometry) {
213: // JTS Geometry does not meet the Obejct equals contract!
214: // So we need to do our assertEquals statement
215: //
216: assertTrue(message, value instanceof Geometry);
217: assertTrue(message, expected instanceof Geometry);
218: Geometry expectedGeom = (Geometry) expected;
219: Geometry actualGeom = (Geometry) value;
220: assertTrue(message, expectedGeom.equals(actualGeom));
221: } else if (expected instanceof Feature) {
222: assertDuplicate(message, ((Feature) expected)
223: .getAttributes(null), ((Feature) value)
224: .getAttributes(null));
225: } else {
226: assertEquals(message, expected, value);
227: }
228: // Ensure Non Immutables are actually copied
229: if (!immutable.contains(expected.getClass())) {
230: //assertNotSame( message, expected, value );
231: }
232: }
233: }
|