001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004 TOPP - www.openplans.org
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.validation;
018:
019: import java.util.HashMap;
020:
021: import org.geotools.data.DataTestCase;
022: import org.geotools.data.DataUtilities;
023: import org.geotools.data.FeatureReader;
024: import org.geotools.data.memory.MemoryDataStore;
025: import org.geotools.feature.Feature;
026: import org.geotools.feature.FeatureCollection;
027: import org.geotools.feature.IllegalAttributeException;
028: import org.geotools.validation.spatial.LineNoSelfIntersectValidation;
029: import org.geotools.validation.spatial.LineNoSelfOverlappingValidation;
030: import org.geotools.validation.spatial.LinesNotIntersectValidation;
031:
032: /**
033: * ValidationPlugInTester purpose.
034: * <p>
035: * Description of ValidationPlugInTester ...
036: * <p>
037: * Capabilities:
038: * <ul>
039: * </li></li>
040: * </ul>
041: * Example Use:
042: * <pre><code>
043: * ValidationPlugInTester x = new ValidationPlugInTester(...);
044: * </code></pre>
045: *
046: * @author bowens, Refractions Research, Inc.
047: * @author $Author: sploreg $ (last modification)
048: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/test/java/org/geotools/validation/ValidationPlugInTester.java $
049: * @version $Id: ValidationPlugInTester.java 22754 2006-11-16 03:03:20Z jgarnett $
050: */
051: public class ValidationPlugInTester extends DataTestCase {
052:
053: MemoryDataStore store;
054: ValidationProcessor processor;
055:
056: /**
057: * ValidationPlugInTester constructor.
058: * <p>
059: * Description
060: * </p>
061: * @param arg0
062: */
063: public ValidationPlugInTester(String arg0) {
064: super (arg0);
065:
066: }
067:
068: /**
069: * Construct data store for use.
070: *
071: * @see junit.framework.TestCase#setUp()
072: *
073: * @throws Exception
074: */
075: protected void setUp() throws Exception {
076: super .setUp();
077: store = new MemoryDataStore();
078: store.addFeatures(roadFeatures);
079: store.addFeatures(riverFeatures);
080: processor = new ValidationProcessor();
081: }
082:
083: /**
084: * Override tearDown.
085: *
086: * @see junit.framework.TestCase#tearDown()
087: *
088: * @throws Exception
089: */
090: protected void tearDown() throws Exception {
091: store = null;
092: super .tearDown();
093:
094: }
095:
096: public void testLineNoSelfIntersectFV_CorrectData() {
097: // the visitor
098: RoadNetworkValidationResults roadValidationResults = new RoadNetworkValidationResults();
099:
100: LineNoSelfIntersectValidation valid = new LineNoSelfIntersectValidation();
101: valid.setName("RoadSelfIntersect");
102: valid
103: .setDescription("Tests to see if a road intersects itself, which is bad!");
104: valid.setTypeRef("road:road");
105:
106: try {
107: processor.addValidation(valid);
108: } catch (Exception e) {
109: assertTrue(false);
110: }
111:
112: try {
113: processor.runFeatureTests("road", DataUtilities
114: .collection(this .roadFeatures),
115: roadValidationResults);
116: } catch (Exception e1) {
117: assertTrue(false);
118: }
119:
120: System.out.println("NoLineSelfIntersect - correct");
121: String[] messages = roadValidationResults.getFailedMessages();
122: for (int i = 0; i < messages.length; i++) {
123: System.out.println(messages[i]);
124: }
125: assertTrue(roadValidationResults.getFailedMessages().length == 0);
126:
127: }
128:
129: public void testLineNoSelfIntersectFV_IncorrectData() {
130: // the visitor
131: RoadNetworkValidationResults roadValidationResults = new RoadNetworkValidationResults();
132:
133: /*LineNoSelfIntersectValidation selfIntersectValidatorRoads
134: = new LineNoSelfIntersectValidation("RoadSelfIntersect",
135: "Tests to see if a road intersects itself, which is bad!",
136: new String[] {"road"});*/
137:
138: try {
139: //processor.addValidation(selfIntersectValidatorRoads);
140: } catch (Exception e) {
141: assertTrue(false);
142: }
143:
144: // produce a broken road (newRoad)
145: try {
146: this .newRoad = this .roadType.create(new Object[] {
147: new Integer(2),
148: line(new int[] { 3, 6, 3, 8, 5, 8, 5, 7, 2, 7 }),
149: "r4" }, "road.rd4");
150: } catch (IllegalAttributeException e) {
151: }
152:
153: try {
154: FeatureReader reader = DataUtilities
155: .reader(new Feature[] { this .newRoad });
156: String typeName = reader.getFeatureType().getTypeName();
157: FeatureCollection collection = DataUtilities
158: .collection(new Feature[] { this .newRoad });
159: processor.runFeatureTests("road", collection,
160: roadValidationResults);
161: } catch (Exception e1) {
162: assertTrue(false);
163: }
164:
165: System.out.println("NoLineSelfIntersect - incorrect");
166: String[] messages = roadValidationResults.getFailedMessages();
167: for (int i = 0; i < messages.length; i++) {
168: System.out.println(messages[i]);
169: }
170: assertTrue(roadValidationResults.getFailedMessages().length > 0);
171:
172: }
173:
174: public void testLineNoSelfOverlapFV_CorrectData() {
175: // the visitor
176: RoadNetworkValidationResults roadValidationResults = new RoadNetworkValidationResults();
177:
178: LineNoSelfOverlappingValidation selfOverlappingValidatorRoads = new LineNoSelfOverlappingValidation();
179: selfOverlappingValidatorRoads.setName("RoadSelfIntersect");
180: selfOverlappingValidatorRoads
181: .setDescription("Tests to see if a road intersects itself, which is bad!");
182: selfOverlappingValidatorRoads.setTypeRef("road");
183:
184: try {
185: processor.addValidation(selfOverlappingValidatorRoads);
186: } catch (Exception e) {
187: assertTrue(false);
188: }
189:
190: try {
191: processor.runFeatureTests("id", DataUtilities
192: .collection(this .roadFeatures),
193: roadValidationResults);
194: } catch (Exception e1) {
195: assertTrue(false);
196: }
197:
198: System.out.println("NoLineSelfOverlap - correct");
199: String[] messages = roadValidationResults.getFailedMessages();
200: for (int i = 0; i < messages.length; i++)
201: System.out.println(messages[i]);
202: assertTrue(roadValidationResults.getFailedMessages().length == 0);
203:
204: }
205:
206: public void testLineNoSelfOverlapFV_IncorrectData() {
207: // the visitor
208: RoadNetworkValidationResults roadValidationResults = new RoadNetworkValidationResults();
209:
210: LineNoSelfOverlappingValidation selfOverlappingValidatorRoads = new LineNoSelfOverlappingValidation();
211: selfOverlappingValidatorRoads.setName("RoadSelfIntersect");
212: selfOverlappingValidatorRoads
213: .setDescription("Tests to see if a road intersects itself, which is bad!");
214: selfOverlappingValidatorRoads.setTypeRef("road");
215:
216: try {
217: processor.addValidation(selfOverlappingValidatorRoads);
218: } catch (Exception e) {
219: assertTrue(false);
220: }
221:
222: // produce a broken road (newRoad)
223: try {
224: this .newRoad = this .roadType.create(new Object[] {
225: new Integer(2),
226: line(new int[] { 7, 7, 8, 7, 9, 7, 9, 6, 8, 6, 8,
227: 7, 7, 7 }), "r4" }, "road.rd4");
228: } catch (IllegalAttributeException e) {
229: }
230:
231: try {
232: processor.runFeatureTests("datastoreId", DataUtilities
233: .collection(new Feature[] { this .newRoad }),
234: roadValidationResults);
235: } catch (Exception e1) {
236: assertTrue(false);
237: }
238:
239: System.out.println("NoLineSelfOverlap - incorrect");
240: String[] messages = roadValidationResults.getFailedMessages();
241: for (int i = 0; i < messages.length; i++) {
242: System.out.println(messages[i]);
243: }
244: assertTrue(roadValidationResults.getFailedMessages().length > 0);
245:
246: }
247:
248: public void testLineIsSingleSegmentFV_CorrectData() {
249: // the visitor
250: RoadNetworkValidationResults roadValidationResults = new RoadNetworkValidationResults();
251:
252: /*LineMustBeASinglePartValidation singleSegmentLineValidatorRoads
253: = new LineMustBeASinglePartValidation("RoadSelfIntersect",
254: "Tests to see if a road intersects itself, which is bad!",
255: new String[] {"road"});
256:
257: try {
258: processor.addValidation(singleSegmentLineValidatorRoads);
259: } catch (Exception e) {
260: assertTrue(false);
261: }*/
262:
263: try {
264: processor.runFeatureTests("dataStoreId", DataUtilities
265: .collection(new Feature[] { this .newRoad }),
266: roadValidationResults);
267: } catch (Exception e1) {
268: assertTrue(false);
269: }
270:
271: System.out.println("LineIsSingleSegment - correct");
272: String[] messages = roadValidationResults.getFailedMessages();
273: for (int i = 0; i < messages.length; i++)
274: System.out.println(messages[i]);
275: assertTrue(roadValidationResults.getFailedMessages().length == 0);
276:
277: }
278:
279: public void testLineIsSingleSegmentFV_IncorrectData() {
280: // the visitor
281: RoadNetworkValidationResults roadValidationResults = new RoadNetworkValidationResults();
282: /*
283: LineMustBeASinglePartValidation singleSegmentLineValidatorRoads
284: = new LineMustBeASinglePartValidation("RoadSelfIntersect",
285: "Tests to see if a road intersects itself, which is bad!",
286: new String[] {"road"});
287:
288: try {
289: processor.addValidation(singleSegmentLineValidatorRoads);
290: } catch (Exception e) {
291: assertTrue(false);
292: }*/
293:
294: try {
295: processor.runFeatureTests("dataStoreId", DataUtilities
296: .collection(this .roadFeatures),
297: roadValidationResults);
298: } catch (Exception e1) {
299: assertTrue(false);
300: }
301:
302: System.out.println("LineIsSingleSegment - incorrect");
303: String[] messages = roadValidationResults.getFailedMessages();
304: for (int i = 0; i < messages.length; i++) {
305: System.out.println(messages[i]);
306: }
307: assertTrue(roadValidationResults.getFailedMessages().length > 0);
308:
309: }
310:
311: public void testLinesIntersectIV_CorrectData() {
312: // the visitor
313: RoadNetworkValidationResults roadValidationResults = new RoadNetworkValidationResults();
314:
315: LinesNotIntersectValidation noIntersectingLinesValidatorAll = new LinesNotIntersectValidation();
316: noIntersectingLinesValidatorAll.setName("linesIntersect");
317: noIntersectingLinesValidatorAll
318: .setDescription("Tests to see if any line geometries cross!");
319: noIntersectingLinesValidatorAll.setLineTypeRef("road");
320: noIntersectingLinesValidatorAll.setLineTypeRef("river");
321:
322: try {
323: processor.addValidation(noIntersectingLinesValidatorAll);
324: } catch (Exception e) {
325: assertTrue(false);
326: }
327:
328: // Feature[] roads = new Feature[2];
329: // roads[0] = roadFeatures[0];
330: // roads[1] = newRoad;
331: // roadFeatures = null;
332: // roadFeatures = new Feature[2];
333: // roadFeatures = roads;
334: //
335: // Feature[] rivers = new Feature[1];
336: // rivers[0] = riverFeatures[1];
337: // rivers[0] = newRiver;
338: // riverFeatures = null;
339: // riverFeatures = new Feature[2];
340: // riverFeatures = roads;
341: //
342: // store = new MemoryDataStore();
343: // store.addFeatures( roadFeatures );
344: // store.addFeatures( riverFeatures );
345:
346: HashMap layers = new HashMap();
347: try {
348: layers.put("dataStoreId:road", DataUtilities
349: .source(new Feature[] { newRoad }));
350: layers.put("dataStoreId:river", DataUtilities
351: .source(riverFeatures));
352: } catch (Exception e) {
353: assertTrue(false);
354: }
355:
356: try {
357: processor.runIntegrityTests(null, layers, null,
358: roadValidationResults);
359: } catch (Exception e1) {
360: assertTrue(false);
361: }
362:
363: System.out.println("NoLinesIntersect - correct");
364: String[] messages = roadValidationResults.getFailedMessages();
365: for (int i = 0; i < messages.length; i++)
366: System.out.println(messages[i]);
367: assertTrue(roadValidationResults.getFailedMessages().length == 0);
368:
369: }
370:
371: public void testLinesIntersectIV_IncorrectData() {
372: // the visitor
373: RoadNetworkValidationResults roadValidationResults = new RoadNetworkValidationResults();
374:
375: LinesNotIntersectValidation noIntersectingLinesValidatorAll = new LinesNotIntersectValidation();
376: noIntersectingLinesValidatorAll.setName("linesIntersect");
377: noIntersectingLinesValidatorAll
378: .setDescription("Tests to see if any line geometries cross!");
379: noIntersectingLinesValidatorAll.setLineTypeRef("road");
380: noIntersectingLinesValidatorAll.setLineTypeRef("river");
381:
382: try {
383: processor.addValidation(noIntersectingLinesValidatorAll);
384: } catch (Exception e) {
385: assertTrue(false);
386: }
387:
388: HashMap layers = new HashMap();
389: try {
390: layers.put("dataStoreId:road", store
391: .getFeatureSource("road"));
392: layers.put("dataStoreId:river", store
393: .getFeatureSource("river"));
394: } catch (Exception e) {
395: assertTrue(false);
396: }
397:
398: try {
399: processor.runIntegrityTests(null, layers, null,
400: roadValidationResults);
401: } catch (Exception e1) {
402: assertTrue(false);
403: }
404:
405: System.out.println("NoLinesIntersect - incorrect");
406: String[] messages = roadValidationResults.getFailedMessages();
407: for (int i = 0; i < messages.length; i++)
408: System.out.println(messages[i]);
409: assertTrue(roadValidationResults.getFailedMessages().length > 0);
410:
411: }
412:
413: }
|