001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2002-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: package org.geotools.feature;
017:
018: import com.vividsolutions.jts.geom.Coordinate;
019: import com.vividsolutions.jts.geom.Geometry;
020: import com.vividsolutions.jts.geom.GeometryCollection;
021: import com.vividsolutions.jts.geom.GeometryFactory;
022: import com.vividsolutions.jts.geom.Point;
023: import com.vividsolutions.jts.geom.PrecisionModel;
024: import junit.framework.Test;
025: import junit.framework.TestCase;
026: import junit.framework.TestSuite;
027: import org.opengis.util.Cloneable;
028: import java.util.Collections;
029: import java.util.List;
030: import java.util.logging.Logger;
031:
032: /**
033: * Provides testing for a special type of Feature a Simple Feature wrapped in
034: * Complex clothing. Specifically DefaultFeature.WrappedComplex. It should
035: * return Lists for all of its get Attributes, since that's what a complex
036: * feature does.
037: *
038: * @author Chris Holmes, TOPP
039: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/main/src/test/java/org/geotools/feature/FeatureWrappedComplexTest.java $
040: */
041: public class FeatureWrappedComplexTest extends TestCase {
042: /** The logger for the default core module. */
043: private static final Logger LOGGER = org.geotools.util.logging.Logging
044: .getLogger("org.geotools.feature");
045:
046: /** Feature on which to preform tests */
047: private Feature testFeature = null;
048: TestSuite suite = null;
049:
050: public FeatureWrappedComplexTest(String testName) {
051: super (testName);
052: }
053:
054: public static void main(String[] args) {
055: org.geotools.util.logging.Logging.GEOTOOLS
056: .forceMonolineConsoleOutput();
057: junit.textui.TestRunner.run(suite());
058: }
059:
060: public static Test suite() {
061: TestSuite suite = new TestSuite(FeatureWrappedComplexTest.class);
062:
063: return suite;
064: }
065:
066: public void setUp() {
067: SimpleFeature feature = (SimpleFeature) SampleFeatureFixtures
068: .createFeature();
069: //testFeature = feature.toComplex(); //this isn't in the api...yet.
070: try {
071: testFeature = new DefaultFeature.ComplexWrapper(feature);
072: } catch (IllegalAttributeException iae) {
073: throw new RuntimeException("programming error with wrapper");
074: }
075: }
076:
077: public void testAttributeAccess() throws Exception {
078: // this ones kinda silly
079: Feature f = testFeature;
080:
081: try {
082: f.setAttribute(1244, Collections.singletonList("x"));
083: fail("not out of bounds");
084: } catch (ArrayIndexOutOfBoundsException aioobe) {
085: }
086:
087: try {
088: f.setAttribute("1244", "x");
089: fail("allowed bogus attribute setting");
090: } catch (IllegalAttributeException iae) {
091: }
092:
093: try {
094: f.setAttribute("testGeometry", "x");
095: fail("allowed bogus attribute setting");
096: } catch (IllegalAttributeException iae) {
097: }
098: }
099:
100: public void testWrappedAccessIndex() {
101: int attrIndex = 5;
102: Feature simple = SampleFeatureFixtures.createFeature();
103: Feature wrapped = testFeature;
104: Object intAtt = wrapped.getAttribute(attrIndex);
105: LOGGER.info("simple is: " + simple + ", and wrapped is: "
106: + wrapped);
107: LOGGER.info("intAtt is: " + intAtt);
108: assertTrue(intAtt instanceof List);
109:
110: List intAttList = (List) intAtt;
111: assertTrue(intAttList.get(0).equals(
112: simple.getAttribute(attrIndex)));
113: }
114:
115: public void testWrappedAccessName() {
116: String attName = "testCharacter";
117: Feature simple = SampleFeatureFixtures.createFeature();
118: Feature wrapped = testFeature;
119: Object intAtt = wrapped.getAttribute(attName);
120: assertTrue(intAtt instanceof List);
121:
122: List intAttList = (List) intAtt;
123: assertTrue(intAttList.get(0).equals(
124: simple.getAttribute(attName)));
125: }
126:
127: public void testWrappedAccessAll() {
128: Feature simple = SampleFeatureFixtures.createFeature();
129: Feature wrapped = testFeature;
130: Object[] simpleAtts = simple.getAttributes(null);
131: Object[] complexAtts = wrapped.getAttributes(null);
132: List curAttList = null;
133:
134: //we're starting at 1 because of the annoying equals with geometries,
135: //and I don't want to figure out where the util method is...
136: for (int i = 1; i < complexAtts.length; i++) {
137: assertTrue(complexAtts[i] instanceof List);
138: curAttList = (List) complexAtts[i];
139: assertEquals(1, curAttList.size());
140: assertEquals(curAttList.get(0), (simpleAtts[i]));
141: }
142: }
143:
144: public void testModify() throws IllegalAttributeException {
145: String newData = "new string";
146:
147: try {
148: testFeature.setAttribute("testString", newData);
149: fail("All objects should be wrapped in Lists for Complex Features");
150: } catch (Exception iae) {
151: assertTrue(iae instanceof IllegalAttributeException);
152: }
153:
154: List singleNewData = Collections.singletonList(newData);
155: testFeature.setAttribute("testString", singleNewData);
156: assertEquals(testFeature.getAttribute("testString"),
157: singleNewData);
158:
159: try {
160: testFeature.setAttribute("testGeometry", singleNewData);
161: fail("a wrapped string should not be able to set a geometry");
162: } catch (Exception e) {
163: //e.printStackTrace();
164: assertTrue(e instanceof IllegalAttributeException);
165: }
166: }
167:
168: public void testEquals() throws Exception {
169: //todo
170: }
171: }
|