001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-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.validation;
017:
018: import java.util.HashMap;
019: import java.util.HashSet;
020: import java.util.Iterator;
021: import java.util.Map;
022: import java.util.Set;
023:
024: import junit.framework.TestCase;
025:
026: import org.geotools.data.DataUtilities;
027: import org.geotools.data.DefaultQuery;
028: import org.geotools.feature.FeatureIterator;
029: import org.geotools.data.FeatureSource;
030: import org.geotools.feature.AttributeType;
031: import org.geotools.feature.Feature;
032: import org.geotools.feature.FeatureCollection;
033: import org.geotools.feature.FeatureType;
034: import org.geotools.filter.Filter;
035:
036: import com.vividsolutions.jts.geom.Coordinate;
037: import com.vividsolutions.jts.geom.GeometryFactory;
038: import com.vividsolutions.jts.geom.LinearRing;
039: import com.vividsolutions.jts.geom.Polygon;
040:
041: /**
042: * ValidatorTest<br>
043: *
044: * @author bowens<br> Created Jun 28, 2004<br>
045: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/test/java/org/geotools/validation/ValidatorTest.java $
046: * @version <br><b>Puropse:</b><br><p><b>Description:</b><br><p><b>Usage:</b><br><p>
047: */
048: public class ValidatorTest extends TestCase {
049: TestFixture fixture;
050:
051: /*
052: * @see TestCase#setUp()
053: */
054: protected void setUp() throws Exception {
055: super .setUp();
056: fixture = new TestFixture();
057: }
058:
059: /*
060: * @see TestCase#tearDown()
061: */
062: protected void tearDown() throws Exception {
063: super .tearDown();
064: fixture = null;
065: }
066:
067: public void testRepositoryGeneration() throws Exception {
068: //DefaultRepository dataRepository = new DefaultRepository();
069: assertNotNull(fixture.repository.datastore("LAKES"));
070: assertNotNull(fixture.repository.datastore("STREAMS"));
071: assertNotNull(fixture.repository.datastore("SWAMPS"));
072: assertNotNull(fixture.repository.datastore("RIVERS"));
073:
074: Map types = fixture.repository.types();
075: assertTrue(types.containsKey("LAKES:lakes"));
076: assertTrue(types.containsKey("STREAMS:streams"));
077: assertTrue(types.containsKey("SWAMPS:swamps"));
078: assertTrue(types.containsKey("RIVERS:rivers"));
079: }
080:
081: public void testFeatureValidation() throws Exception {
082: FeatureSource lakes = fixture.repository.source("LAKES",
083: "lakes");
084: FeatureCollection features = lakes.getFeatures();
085: DefaultFeatureResults results = new DefaultFeatureResults();
086: fixture.processor.runFeatureTests("LAKES", features, results);
087:
088: assertEquals("lakes test", 0, results.error.size());
089: }
090:
091: public Feature createInvalidLake() throws Exception {
092: FeatureSource lakes = fixture.repository.source("LAKES",
093: "lakes");
094:
095: FeatureIterator features = lakes
096: .getFeatures(
097: new DefaultQuery("lakes", Filter.INCLUDE, 1,
098: null, null)).features();
099: Feature feature = features.next();
100: features.close();
101:
102: FeatureType LAKE = lakes.getSchema();
103: Object array[] = new Object[LAKE.getAttributeCount()];
104: for (int i = 0; i < LAKE.getAttributeCount(); i++) {
105: AttributeType attr = LAKE.getAttributeType(i);
106: // System.out.println( i+" "+attr.getType()+":"+attr.getName()+"="+feature.getAttribute( i ) );
107: if (LAKE.getDefaultGeometry() == attr) {
108: GeometryFactory factory = new GeometryFactory();
109: Coordinate coords[] = new Coordinate[] {
110: new Coordinate(1, 1), new Coordinate(2, 2),
111: new Coordinate(2, 1), new Coordinate(1, 2),
112: new Coordinate(1, 1), };
113: LinearRing ring = factory.createLinearRing(coords);
114: Polygon poly = factory.createPolygon(ring, null);
115: array[i] = factory
116: .createMultiPolygon(new Polygon[] { poly, });
117: } else {
118: array[i] = feature.getAttribute(i);
119: }
120: }
121: return LAKE.create(array, "splash");
122: }
123:
124: public void testFeatureValidation2() throws Exception {
125: FeatureSource lakes = fixture.repository.source("LAKES",
126: "lakes");
127: Feature newFeature = createInvalidLake();
128:
129: FeatureCollection add = DataUtilities
130: .collection(new Feature[] { newFeature, });
131:
132: DefaultFeatureResults results = new DefaultFeatureResults();
133: fixture.processor.runFeatureTests("LAKES", add, results);
134:
135: assertEquals("lakes test", 2, results.error.size());
136: }
137:
138: public void testIntegrityValidation() throws Exception {
139: DefaultFeatureResults results = new DefaultFeatureResults();
140: Set set = fixture.repository.types().keySet();
141: Map map = new HashMap();
142: for (Iterator i = set.iterator(); i.hasNext();) {
143: String typeRef = (String) i.next();
144: String split[] = typeRef.split(":");
145: map.put(typeRef, fixture.repository.source(split[0],
146: split[1]));
147: }
148: fixture.processor.runIntegrityTests(set, map, null, results);
149: assertEquals("integrity test", 0, results.error.size());
150: }
151:
152: public void testValidator() throws Exception {
153: Validator validator = new Validator(fixture.repository,
154: fixture.processor);
155:
156: FeatureSource lakes = fixture.repository.source("LAKES",
157: "lakes");
158: FeatureCollection features = lakes.getFeatures();
159: DefaultFeatureResults results = new DefaultFeatureResults();
160: validator.featureValidation("LAKES", features, results);
161:
162: assertEquals(0, results.error.size());
163: }
164:
165: public void testValidator2() throws Exception {
166: Validator validator = new Validator(fixture.repository,
167: fixture.processor);
168:
169: FeatureSource lakes = fixture.repository.source("LAKES",
170: "lakes");
171: Feature newFeature = createInvalidLake();
172:
173: FeatureCollection add = DataUtilities
174: .collection(new Feature[] { newFeature, });
175: DefaultFeatureResults results = new DefaultFeatureResults();
176: fixture.processor.runFeatureTests("LAKES", add, results);
177:
178: System.out.println(results.error);
179: assertEquals("lakes test", 2, results.error.size());
180:
181: //results = new DefaultFeatureResults();
182: validator.featureValidation("LAKES", add, results);
183: assertEquals("lakes test2", 5, results.error.size());
184: }
185:
186: public void testIntegrityValidator() throws Exception {
187: Validator validator = new Validator(fixture.repository,
188: fixture.processor);
189:
190: DefaultFeatureResults results = new DefaultFeatureResults();
191: Set set = fixture.repository.types().keySet();
192: Map map = new HashMap();
193: for (Iterator i = set.iterator(); i.hasNext();) {
194: String typeRef = (String) i.next();
195: String split[] = typeRef.split(":");
196: map.put(typeRef, fixture.repository.source(split[0],
197: split[1]));
198: }
199: validator.integrityValidation(map, null, results);
200: assertEquals("integrity test", 0, results.error.size());
201: }
202:
203: public void testIntegrityValidator2() throws Exception {
204: Validator validator = new Validator(fixture.repository,
205: fixture.processor);
206:
207: DefaultFeatureResults results = new DefaultFeatureResults();
208: Set set = new HashSet();
209: Map map = new HashMap();
210: set.add("RIVERS:rivers");
211: map.put("RIVERS:rivers", fixture.repository.source("RIVERS",
212: "rivers"));
213:
214: validator.integrityValidation(map, null, results);
215: assertEquals("integrity test", 0, results.error.size());
216: }
217: }
|