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: package org.geotools.feature;
017:
018: import java.util.logging.Logger;
019:
020: import junit.framework.Test;
021: import junit.framework.TestCase;
022: import junit.framework.TestSuite;
023:
024: import org.opengis.util.Cloneable;
025:
026: import com.vividsolutions.jts.geom.Coordinate;
027: import com.vividsolutions.jts.geom.Geometry;
028: import com.vividsolutions.jts.geom.GeometryCollection;
029: import com.vividsolutions.jts.geom.GeometryFactory;
030: import com.vividsolutions.jts.geom.Point;
031: import com.vividsolutions.jts.geom.PrecisionModel;
032:
033: public class FeatureFlatTest extends TestCase {
034:
035: /**
036: * The logger for the default core module.
037: */
038: private static final Logger LOGGER = org.geotools.util.logging.Logging
039: .getLogger("org.geotools.defaultcore");
040:
041: /** Feature on which to preform tests */
042: private Feature testFeature = null;
043:
044: TestSuite suite = null;
045:
046: public FeatureFlatTest(String testName) {
047: super (testName);
048: }
049:
050: public static void main(String[] args) {
051: org.geotools.util.logging.Logging.GEOTOOLS
052: .forceMonolineConsoleOutput();
053: junit.textui.TestRunner.run(suite());
054: }
055:
056: public static Test suite() {
057: TestSuite suite = new TestSuite(FeatureFlatTest.class);
058: return suite;
059: }
060:
061: public void setUp() {
062: testFeature = SampleFeatureFixtures.createFeature();
063: }
064:
065: public void testRetrieve() {
066: GeometryFactory gf = new GeometryFactory();
067: assertTrue("geometry retrieval and match", ((Point) testFeature
068: .getAttribute("testGeometry")).equals(gf
069: .createPoint(new Coordinate(1, 2))));
070: assertTrue("boolean retrieval and match",
071: ((Boolean) testFeature.getAttribute("testBoolean"))
072: .equals(new Boolean(true)));
073: assertTrue("character retrieval and match",
074: ((Character) testFeature.getAttribute("testCharacter"))
075: .equals(new Character('t')));
076: assertTrue("byte retrieval and match", ((Byte) testFeature
077: .getAttribute("testByte")).equals(new Byte("10")));
078: assertTrue("short retrieval and match", ((Short) testFeature
079: .getAttribute("testShort")).equals(new Short("101")));
080: assertTrue("integer retrieval and match",
081: ((Integer) testFeature.getAttribute("testInteger"))
082: .equals(new Integer(1002)));
083: assertTrue("long retrieval and match", ((Long) testFeature
084: .getAttribute("testLong")).equals(new Long(10003)));
085: assertTrue("float retrieval and match", ((Float) testFeature
086: .getAttribute("testFloat")).equals(new Float(10000.4)));
087: assertTrue("double retrieval and match", ((Double) testFeature
088: .getAttribute("testDouble"))
089: .equals(new Double(100000.5)));
090: assertTrue("string retrieval and match", ((String) testFeature
091: .getAttribute("testString")).equals("test string data"));
092:
093: }
094:
095: public void testBogusCreation() throws Exception {
096: FeatureTypeFactory factory = FeatureTypeFactory
097: .newInstance("test1");
098: factory.addType(newAtt("billy", String.class, false));
099: factory.addType(newAtt("jimmy", String.class, false));
100: FeatureType test = factory.getFeatureType();
101: try {
102: test.create(null);
103: fail("no error");
104: } catch (IllegalAttributeException iae) {
105: }
106:
107: try {
108: test.create(new Object[32]);
109: fail("no error");
110: } catch (IllegalAttributeException iae) {
111: }
112:
113: }
114:
115: public void testBounds() throws Exception {
116: GeometryFactory gf = new GeometryFactory();
117: Geometry[] g = new Geometry[4];
118: g[0] = gf.createPoint(new Coordinate(0, 0));
119: g[1] = gf.createPoint(new Coordinate(0, 10));
120: g[2] = gf.createPoint(new Coordinate(10, 0));
121: g[3] = gf.createPoint(new Coordinate(10, 10));
122:
123: GeometryCollection gc = gf.createGeometryCollection(g);
124: FeatureTypeFactory factory = FeatureTypeFactory
125: .newInstance("bounds");
126: factory.addType(newAtt("p1", Point.class));
127: factory.addType(newAtt("p2", Point.class));
128: factory.addType(newAtt("p3", Point.class));
129: factory.addType(newAtt("p4", Point.class));
130: FeatureType t = factory.createFeatureType();
131: Feature f = t.create(g);
132: assertEquals(gc.getEnvelopeInternal(), f.getBounds());
133:
134: g[1].getCoordinate().y = 20;
135: g[2].getCoordinate().x = 20;
136: f.setAttribute(1, g[1]);
137: f.setAttribute(2, g[2]);
138: gc = gf.createGeometryCollection(g);
139: assertEquals(gc.getEnvelopeInternal(), f.getBounds());
140: }
141:
142: public void testClone() {
143: DefaultFeature f = (DefaultFeature) SampleFeatureFixtures
144: .createFeature();
145: Feature c = (Feature) f.clone();
146: for (int i = 0, ii = c.getNumberOfAttributes(); i < ii; i++) {
147: assertEquals(c.getAttribute(i), f.getAttribute(i));
148: }
149: }
150:
151: public void testClone2() throws Exception {
152: FeatureType type = SampleFeatureFixtures.createTestType();
153: Object[] attributes = SampleFeatureFixtures.createAttributes();
154: DefaultFeature feature = (DefaultFeature) type.create(
155: attributes, "fid");
156: Feature clone = (Feature) ((Cloneable) feature).clone();
157: assertTrue("Clone was not equal", feature.equals(clone));
158: }
159:
160: public void testToStringWontThrow()
161: throws IllegalAttributeException {
162: SimpleFeature f = (SimpleFeature) SampleFeatureFixtures
163: .createFeature();
164: f.setAttributes(new Object[f.getNumberOfAttributes()]);
165: String s = f.toString();
166: }
167:
168: static AttributeType newAtt(String name, Class c) {
169: return AttributeTypeFactory.newAttributeType(name, c, true);
170: }
171:
172: static AttributeType newAtt(String name, Class c, boolean nillable) {
173: return AttributeTypeFactory.newAttributeType(name, c, nillable);
174: }
175:
176: public void testModify() throws IllegalAttributeException {
177: String newData = "new test string data";
178: testFeature.setAttribute("testString", newData);
179: assertEquals("match modified (string) attribute", testFeature
180: .getAttribute("testString"), newData);
181:
182: GeometryFactory gf = new GeometryFactory();
183: Point newGeom = gf.createPoint(new Coordinate(3, 4));
184: testFeature.setAttribute("testGeometry", newGeom);
185: assertEquals("match modified (geometry) attribute", testFeature
186: .getAttribute("testGeometry"), newGeom);
187:
188: testFeature.setDefaultGeometry(newGeom);
189: assertEquals("match modified (geometry) attribute", testFeature
190: .getAttribute("testGeometry"), newGeom);
191:
192: }
193:
194: // public void testFindAttribute() {
195: // DefaultFeature f = (DefaultFeature) SampleFeatureFixtures.createFeature();
196: // FeatureType t = f.getFeatureType();
197: // for (int i = 0, ii = t.getAttributeCount(); i < ii; i++) {
198: // AttributeType a = t.getAttributeType(i);
199: // assertEquals(i, f.findAttributeByName(a.getName()));
200: // }
201: // assertEquals(-1, f.findAttributeByName("bilbo baggins"));
202: // assertEquals(null, f.getAttribute("jimmy hoffa"));
203: // }
204:
205: public void testAttributeAccess() throws Exception {
206: // this ones kinda silly
207: SimpleFeature f = (SimpleFeature) SampleFeatureFixtures
208: .createFeature();
209: Object[] atts = null;
210: atts = f.getAttributes(atts);
211: for (int i = 0, ii = atts.length; i < ii; i++) {
212: assertEquals(atts[i], f.getAttribute(i));
213: }
214: Object[] attsAgain = f.getAttributes(null);
215: assertTrue(atts != attsAgain);
216: f.setAttributes(atts);
217: attsAgain = f.getAttributes(attsAgain);
218: assertTrue(atts != attsAgain);
219: for (int i = 0, ii = atts.length; i < ii; i++) {
220: assertEquals(atts[i], f.getAttribute(i));
221: assertEquals(attsAgain[i], f.getAttribute(i));
222: }
223: try {
224: f.setAttribute(1244, "x");
225: fail("not out of bounds");
226: } catch (ArrayIndexOutOfBoundsException aioobe) {
227:
228: }
229: try {
230: f.setAttribute("1244", "x");
231: fail("allowed bogus attribute setting");
232: } catch (IllegalAttributeException iae) {
233:
234: }
235: try {
236: f.setAttribute("testGeometry", "x");
237: fail("allowed bogus attribute setting");
238: } catch (IllegalAttributeException iae) {
239:
240: } catch (RuntimeException rt) {
241: }
242: }
243:
244: // IanS - this is no longer good, cause we deal with parsing
245: // public void testEnforceType() {
246: //
247: // Date d = new Date();
248: //
249: // Feature f = SampleFeatureFixtures.createFeature();
250: // for (int i = 0, ii = f.getNumberOfAttributes(); i < ii; i++) {
251: // try {
252: // f.setAttribute(i, d);
253: // } catch (IllegalAttributeException iae) {
254: // continue;
255: // }
256: // fail("No error thrown during illegal set");
257: // }
258: //
259: // }
260:
261: public void testEquals() throws Exception {
262: Feature f1 = SampleFeatureFixtures.createFeature();
263: Feature f2 = SampleFeatureFixtures.createFeature();
264: assertTrue(f1.equals(f1));
265: assertTrue(f2.equals(f2));
266: assertTrue(!f1.equals(f2));
267: assertTrue(!f1.equals(null));
268: FeatureType another = FeatureTypeFactory.newFeatureType(
269: new AttributeType[] { newAtt("name", String.class) },
270: "different");
271: assertTrue(!f1.equals(another.create(new Object[1])));
272: }
273:
274: /*
275: * This is actually a test for FeatureTypeFlat, but there is no test for that
276: * written right now, so I'm just putting it here, as I just changed the
277: * getDefaultGeometry method, and it should have a unit test. It tests
278: * to make sure getDefaultGeometry returns null if there is no geometry,
279: * as we now allow
280: */
281: public void testDefaultGeometry() throws Exception {
282: FeatureType testType = testFeature.getFeatureType();
283: AttributeType geometry = testType
284: .getAttributeType("testGeometry");
285: assertTrue(geometry == testType.getDefaultGeometry());
286: assertTrue(testFeature.getDefaultGeometry()
287: .getEnvelopeInternal().equals(testFeature.getBounds()));
288:
289: FeatureType another = FeatureTypeFactory.newFeatureType(
290: new AttributeType[] { newAtt("name", String.class) },
291: "different");
292: DefaultFeature f1 = (DefaultFeature) another
293: .create(new Object[1]);
294: assertEquals(null, f1.getDefaultGeometry());
295: try {
296: f1.setDefaultGeometry(null);
297: fail("allowed bogus default geometry set ");
298: } catch (IllegalAttributeException iae) {
299:
300: }
301: }
302:
303: }
|