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: * Created on Feb 12, 2004
017: */
018: package org.geotools.validation;
019:
020: import java.io.File;
021: import java.io.FileInputStream;
022: import java.io.FileNotFoundException;
023: import java.io.FileReader;
024: import java.io.IOException;
025: import java.util.HashMap;
026: import java.util.HashSet;
027: import java.util.Iterator;
028: import java.util.Map;
029: import java.util.Properties;
030: import java.util.Set;
031:
032: import org.geotools.data.DataStore;
033: import org.geotools.data.DataStoreFinder;
034: import org.geotools.data.DefaultRepository;
035: import org.geotools.data.FeatureSource;
036: import org.geotools.feature.Feature;
037: import org.geotools.validation.dto.ArgumentDTO;
038: import org.geotools.validation.dto.PlugInDTO;
039: import org.geotools.validation.dto.TestDTO;
040: import org.geotools.validation.dto.TestSuiteDTO;
041: import org.geotools.validation.xml.ValidationException;
042: import org.geotools.validation.xml.XMLReader;
043:
044: import com.vividsolutions.jts.geom.Envelope;
045:
046: /**
047: * Validator purpose.
048: *
049: * <p>
050: * Description of Validator ...
051: * </p>
052: *
053: * @author dzwiers, Refractions Research, Inc.
054: * @author $Author: dmzwiers $ (last modification)
055: * @author bowens
056: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/extension/validation/src/test/java/org/geotools/validation/BatchValidator.java $
057: * @version $Id: BatchValidator.java 22754 2006-11-16 03:03:20Z jgarnett $
058: */
059: public class BatchValidator {
060: private static Properties dataStoreProp;
061: private static Properties transProp;
062:
063: private static Validator validator;
064: private static DefaultRepository dataRepository;
065:
066: public static void main(String[] args) {
067: loadProperties(args);
068:
069: Map ds = loadDataStores();
070: ValidationProcessor v = loadTests();
071: dataRepository = new DefaultRepository();
072:
073: runTransactions(ds, v);
074: }
075:
076: private static void runTransactions(Map dsm, ValidationProcessor v) {
077: if ((dsm == null) || (dsm.size() == 0)) {
078: System.out.println("No Datastores were defined.");
079:
080: return;
081: }
082:
083: if (v == null) {
084: System.err
085: .println("An error occured: Cannot run without a ValidationProcessor.");
086:
087: return;
088: }
089:
090: Iterator it = dsm.keySet().iterator();
091:
092: /** set up the data repository */
093: while (it.hasNext()) {
094: String typeRef = it.next().toString();
095: DataStore ds = (DataStore) dsm.get(typeRef);
096: try {
097: dataRepository.register(typeRef, ds);
098: } catch (IOException e) {
099: e.printStackTrace();
100: }
101: }
102: validator = new Validator(dataRepository, v);
103: /** validator is now ready to go */
104:
105: it = dataRepository.getDataStores().values().iterator();
106: /** do the feature type validation dance */
107: while (it.hasNext()) {
108: DataStore store = (DataStore) it.next();
109: String typeNames[] = null;
110: try {
111: typeNames = store.getTypeNames();
112: } catch (IOException e) {
113: e.printStackTrace();
114: }
115: //HACK: get ALL feature types and smash through their features
116: // this is really really slow and will be fixed
117: for (int p = 0; p < typeNames.length; p++) {
118: try {
119: validator.featureValidation(typeNames[p], store
120: .getFeatureSource(typeNames[p])
121: .getFeatures(), null);
122: } catch (IOException e1) {
123: e1.printStackTrace();
124: } catch (Exception e1) {
125: e1.printStackTrace();
126: }
127: }
128:
129: }
130:
131: Envelope envelope = makeEnvelope();
132:
133: /** do the integrity validation dance */
134: try {
135: validator.integrityValidation(dsm, envelope, null);
136: } catch (IOException e1) {
137: e1.printStackTrace();
138: } catch (Exception e1) {
139: e1.printStackTrace();
140: }
141:
142: if (true) //HACK premature evacuation
143: return;
144:
145: // --------------------------------------------------
146: // start of the old code
147:
148: //v.integrityValidation()
149: //v.featureValidation( , fs.getSchema(),fs.getFeatures().features(), vr);
150:
151: it = dsm.keySet().iterator();
152: while (it.hasNext()) {
153: Map sources = new HashMap();
154: String key = it.next().toString();
155: DataStore ds = (DataStore) dsm.get(key);
156: String t = transProp.getProperty(key + ".Sources");
157: String[] ss = t.split(",");
158:
159: for (int j = 0; j < ss.length; j++) {
160: ss[j] = ss[j].trim();
161:
162: try {
163: FeatureSource fs = ds.getFeatureSource(ss[j]);
164: sources.put(ss, fs);
165:
166: //BatchValidationResults vr = new BatchValidationResults();
167:
168: // v.runFeatureTests( "" /** fix me */, fs.getSchema(),
169: // fs.getFeatures().features(), vr);
170: //System.out.println("Feature Test Results for " + key + ":"
171: // + ss[j]);
172: //System.out.println(vr.toString());
173: } catch (Exception e) {
174: e.printStackTrace();
175: }
176: }
177:
178: Envelope env = null;
179:
180: try {
181: double minx = Double.parseDouble(transProp
182: .getProperty("Bounds.minX"));
183: double miny = Double.parseDouble(transProp
184: .getProperty("Bounds.maxX"));
185: double maxx = Double.parseDouble(transProp
186: .getProperty("Bounds.maxX"));
187: double maxy = Double.parseDouble(transProp
188: .getProperty("Bounds.maxY"));
189: env = new Envelope(minx, miny, maxx, maxy);
190: } catch (Exception e) {
191: System.err
192: .println("Envelope not specified in Transaction.properties.");
193: env = new Envelope();
194: }
195:
196: if (env == null) {
197: env = new Envelope(Integer.MIN_VALUE,
198: Integer.MIN_VALUE, Integer.MAX_VALUE,
199: Integer.MAX_VALUE);
200: }
201:
202: try {
203: BatchValidationResults vr = new BatchValidationResults();
204:
205: //v.runIntegrityTests( null /* fix me */, sources, env, vr);
206: System.out.println("Feature Integrety Test Results");
207: System.out.println(vr.toString());
208: } catch (Exception e) {
209: e.printStackTrace();
210: }
211: }
212: }
213:
214: private static Envelope makeEnvelope() {
215: Envelope env;
216: try {
217: double minx = Double.parseDouble(transProp
218: .getProperty("Bounds.minX"));
219: double miny = Double.parseDouble(transProp
220: .getProperty("Bounds.maxX"));
221: double maxx = Double.parseDouble(transProp
222: .getProperty("Bounds.maxX"));
223: double maxy = Double.parseDouble(transProp
224: .getProperty("Bounds.maxY"));
225: env = new Envelope(minx, miny, maxx, maxy);
226: } catch (Exception e) {
227: System.err
228: .println("Envelope not specified in Transaction.properties.");
229: env = new Envelope();
230: }
231:
232: if (env == null) {
233: env = new Envelope(Integer.MIN_VALUE, Integer.MIN_VALUE,
234: Integer.MAX_VALUE, Integer.MAX_VALUE);
235: }
236:
237: return env;
238: }
239:
240: private static void loadProperties(String[] args) {
241: File dsProp = null;
242: File trProp = null;
243:
244: dsProp = new File(ClassLoader.getSystemResource(
245: "org/geotools/demos/DataStore.properties").getFile());
246: trProp = new File(ClassLoader.getSystemResource(
247: "org/geotools/demos/Transaction.properties").getFile());
248:
249: if (args.length > 0) {
250: int i = 0;
251:
252: while ((i + 1) < args.length) {
253: String param = args[i++];
254: String value = args[i++];
255:
256: if ("-data".equals(param)) {
257: File tmp = null;
258: tmp = new File(ClassLoader.getSystemResource(value)
259: .getFile());
260:
261: if (tmp != null) {
262: dsProp = tmp;
263: } else {
264: System.err
265: .println("Error: The data property file could not be found.");
266: System.err.println("Data file:" + value);
267: System.exit(1);
268: }
269: } else {
270: if ("-trans".equals(param)) {
271: File tmp = null;
272: tmp = new File(ClassLoader.getSystemResource(
273: value).getFile());
274:
275: if (tmp != null) {
276: trProp = tmp;
277: } else {
278: System.err
279: .println("Error: The transaction property file could not be found.");
280: System.err.println("Data file:" + value);
281: System.exit(1);
282: }
283: } else {
284: if ("-help".equals(param)) {
285: System.out.println("Batch Validator");
286: System.out.println("Version 0.1");
287: System.out.println("");
288: System.out
289: .println("Usage: java Validator [Options]");
290: System.out
291: .println("Options: -help -data -trans");
292: System.out.println("");
293: System.out.println("-data <filename>");
294: System.out
295: .println("Replaces the default datastore property file");
296: System.out.println("");
297: System.out.println("-trans <filename>");
298: System.out
299: .println("Replaces the default transaction property file");
300: System.out.println("");
301: System.out.println("-help");
302: System.out.println("This screen.");
303: System.exit(0);
304: } else {
305: System.err
306: .println("Usage Error: use -help for usage");
307: System.err.println("Invalid option:"
308: + param);
309: System.exit(1);
310: }
311: }
312: }
313: }
314: }
315:
316: // the files should all contain the specified data, time to check for existance ...
317: if ((dsProp == null)
318: || (!dsProp.exists() || (!dsProp.isFile() || !dsProp
319: .canRead()))) {
320: System.err
321: .println("Error: The data property file had errors.");
322:
323: if (dsProp == null) {
324: System.err.println("Data file was null");
325: } else {
326: if (!dsProp.exists()) {
327: System.err.println("Data file was does not exist");
328: System.err.println(dsProp.toString());
329: } else {
330: if (!dsProp.isFile()) {
331: System.err
332: .println("Data path specified is not a file.");
333: System.err.println(dsProp.toString());
334: } else {
335: if (!dsProp.canRead()) {
336: System.err
337: .println("Data file cannot be read.");
338: System.err.println(dsProp.toString());
339: } else {
340: System.err
341: .println("Data file had an unknown error.");
342: System.err.println(dsProp.toString());
343: }
344: }
345: }
346: }
347:
348: System.exit(1);
349: }
350:
351: if ((trProp == null)
352: || (!trProp.exists() || (!trProp.isFile() || !trProp
353: .canRead()))) {
354: System.err
355: .println("Error: The test property file had errors.");
356:
357: if (trProp == null) {
358: System.err.println("Transaction file was null");
359: } else {
360: if (!trProp.exists()) {
361: System.err
362: .println("Transaction file was does not exist");
363: System.err.println(trProp.toString());
364: } else {
365: if (!trProp.isFile()) {
366: System.err
367: .println("Transaction path specified is not a file.");
368: System.err.println(trProp.toString());
369: } else {
370: if (!trProp.canRead()) {
371: System.err
372: .println("Transaction file cannot be read.");
373: System.err.println(trProp.toString());
374: } else {
375: System.err
376: .println("Transaction file had an unknown error.");
377: System.err.println(trProp.toString());
378: }
379: }
380: }
381: }
382:
383: System.exit(1);
384: }
385:
386: // the files are valid, time to load them
387: dataStoreProp = new Properties();
388: transProp = new Properties();
389:
390: try {
391: dataStoreProp.load(new FileInputStream(dsProp));
392: } catch (FileNotFoundException e) {
393: System.err.println("DataStore file was does not exist");
394: System.err.println(dsProp.toString());
395: e.printStackTrace();
396: System.exit(1);
397: } catch (IOException e) {
398: System.err.println("DataStore file had errors reading");
399: System.err.println(dsProp.toString());
400: e.printStackTrace();
401: System.exit(1);
402: }
403:
404: try {
405: transProp.load(new FileInputStream(trProp));
406: } catch (FileNotFoundException e) {
407: System.err.println("Transaction file was does not exist");
408: System.err.println(trProp.toString());
409: e.printStackTrace();
410: System.exit(1);
411: } catch (IOException e) {
412: System.err.println("Transaction file had errors reading");
413: System.err.println(trProp.toString());
414: e.printStackTrace();
415: System.exit(1);
416: }
417: }
418:
419: private synchronized static Map loadDataStores() {
420: String dsIds = dataStoreProp.getProperty("DataStoreIds");
421: String[] ids = dsIds.split(",");
422: Map result = new HashMap();
423: //int rcount = 0;
424:
425: // remove whitespace
426: for (int i = 0; i < ids.length; i++) {
427: ids[i] = ids[i].trim();
428:
429: Map m = new HashMap();
430: String tmp = dataStoreProp.getProperty(ids[i] + ".Params");
431: String[] params = tmp.split(",");
432:
433: for (int j = 0; j < params.length; j++) {
434: String[] vals = params[j].split("=");
435:
436: if (vals.length == 2) {
437: m.put(vals[0].trim(), vals[1].trim());
438: } else {
439: System.err.println("DataStore " + ids[i]
440: + " incured an error loading a parameter.");
441: System.err
442: .println("This DataStore may not be loaded correctly.");
443: System.err
444: .println("Parameters should be specified in a comma delimited list key=value");
445: System.err.println("You specified:" + tmp);
446: }
447: }
448:
449: DataStore dataStore = null;
450:
451: try {
452: dataStore = DataStoreFinder.getDataStore(m);
453: } catch (Throwable ex) {
454: ex.printStackTrace();
455: System.err.println("DataStore " + ids[i]
456: + " incured an error and will not be used.");
457: }
458:
459: if (dataStore != null) {
460: result.put(ids[i], dataStore);
461: } else {
462: System.err.println("DataStore " + ids[i]
463: + " incured an error and will not be used.");
464: }
465: }// end for ids
466:
467: return result;
468: }
469:
470: private static ValidationProcessor loadTests() {
471: File plugInDir = null;
472: plugInDir = new File(transProp.getProperty("PlugInDir"));
473:
474: if (plugInDir == null) {
475: System.err.println("PlugIn Dir does not exist");
476: System.exit(1);
477: }
478:
479: if (!plugInDir.exists()) {
480: System.err.println("PlugIn Dir does not exist");
481: System.err.println(plugInDir.toString());
482: System.exit(1);
483: }
484:
485: if (!plugInDir.isDirectory()) {
486: System.err.println("PlugIn Dir is not a directory");
487: System.err.println(plugInDir.toString());
488: System.exit(1);
489: }
490:
491: if (!plugInDir.canRead()) {
492: System.err.println("PlugIn Dir cannot be read");
493: System.err.println(plugInDir.toString());
494: System.exit(1);
495: }
496:
497: Map plugIns = null;
498:
499: try {
500: plugIns = XMLReader.loadPlugIns(plugInDir);
501: } catch (ValidationException e) {
502: System.err.println("PlugIn load had errors.");
503: System.err.println(plugInDir.toString());
504: e.printStackTrace();
505: System.exit(1);
506: }
507:
508: if (plugIns == null) {
509: System.err.println("PlugIn load had errors.");
510: System.err.println("No plugins were loaded");
511: System.err.println(plugInDir.toString());
512: System.exit(1);
513: }
514:
515: if (plugIns.size() == 0) {
516: System.err.println("PlugIn load had errors.");
517: System.err.println("No plugins were found");
518: System.err.println(plugInDir.toString());
519: System.exit(1);
520: }
521:
522: int numSuites = 0;
523: try {
524: numSuites = Integer.parseInt(transProp
525: .getProperty("NumberTestSuites"));
526: } catch (Exception e) {
527: }
528:
529: Map ts = new HashMap(numSuites);
530: for (int i = 0; i < numSuites; i++) {
531: String path = transProp.getProperty("TestSuiteFile."
532: + (i + 1));
533: loadTestSuite(ts, plugIns, path);
534: }
535:
536: // We need to make our own validator for batch
537: // processing
538: // (for starters it should use a custom FeatureResults
539: // that logs fail/warning information)
540: BatchValidatorProcessor gv = new BatchValidatorProcessor(ts,
541: plugIns);
542:
543: return gv;
544: }
545:
546: private static void loadTestSuite(Map ts, Map plugIns, String tsPath) {
547: File testSuite = null;
548: try {
549: testSuite = new File(tsPath);
550: } catch (Exception e) {
551: System.err.println(tsPath);
552: e.printStackTrace();
553: return;
554: }
555:
556: if (testSuite == null) {
557: System.err.println("TestSuite file does not exist");
558: System.exit(1);
559: }
560:
561: if (!testSuite.exists()) {
562: System.err.println("TestSuite file does not exist");
563: System.err.println(testSuite.toString());
564: System.exit(1);
565: }
566:
567: if (!testSuite.isFile()) {
568: System.err.println("TestSuite file is not a file");
569: System.err.println(testSuite.toString());
570: System.exit(1);
571: }
572:
573: if (!testSuite.canRead()) {
574: System.err.println("TestSuite file cannot be read");
575: System.err.println(testSuite.toString());
576: System.exit(1);
577: }
578:
579: TestSuiteDTO dto = null;
580:
581: try {
582: dto = XMLReader.readTestSuite(testSuite.getName(),
583: new FileReader(testSuite), plugIns);
584: } catch (FileNotFoundException e) {
585: System.err.println("TestSuite file was not found.");
586: System.err.println(testSuite.toString());
587: e.printStackTrace();
588: System.exit(1);
589: } catch (ValidationException e) {
590: System.err.println("TestSuite load had errors.");
591: System.err.println(testSuite.toString());
592: e.printStackTrace();
593: System.exit(1);
594: }
595:
596: ts.put(dto.getName(), dto);
597: }
598:
599: static class BatchValidationResults implements ValidationResults {
600: Map errors = new HashMap();
601: Validation v;
602:
603: public void error(Feature feature, String message) {
604: errors.put(feature, message);
605: }
606:
607: public void warning(Feature feature, String message) {
608: errors.put(feature, message);
609: }
610:
611: public void setValidation(Validation validation) {
612: v = validation;
613: }
614:
615: public Map getErrors() {
616: return errors;
617: }
618:
619: public String toString() {
620: String r = "";
621: Iterator i = errors.keySet().iterator();
622: if (i.hasNext()) {
623: while (i.hasNext()) {
624: Feature f = (Feature) i.next();
625: String msg = (String) errors.get(f);
626: r += (f.getID() + " " + msg + "\n");
627: }
628: } else {
629: r = "PASSED";
630: }
631:
632: return r + "\n";
633: }
634: }
635: }
636:
637: /*
638: * Created on Feb 9, 2004
639: *
640: * To change the template for this generated file go to
641: * Window - Preferences - Java - Code Generation - Code and Comments
642: */
643: class BatchValidatorProcessor extends ValidationProcessor {
644: /**
645: * BatchValidator constructor.
646: *
647: * <p>
648: * super();
649: * </p>
650: */
651: public BatchValidatorProcessor() {
652: super ();
653: }
654:
655: /**
656: * ValidationProcessor constructor.
657: *
658: * <p>
659: * Builds a ValidationProcessor with the DTO provided.
660: * </p>
661: *
662: * @param testSuites Map a map of names -> TestSuiteDTO objects
663: * @param plugIns Map a map of names -> PlugInDTO objects
664: *
665: * @see load(Map,Map)
666: */
667: public BatchValidatorProcessor(Map testSuites, Map plugIns) {
668: super ();
669: load(testSuites, plugIns);
670: }
671:
672: /**
673: * load purpose.
674: *
675: * <p>
676: * loads this instance data into this instance.
677: * </p>
678: *
679: * @param testSuites
680: * @param plugIns
681: */
682: public void load(Map testSuites, Map plugIns) {
683: // step 1 make a list required plug-ins
684: Set plugInNames = new HashSet();
685: Iterator i = testSuites.keySet().iterator();
686:
687: // go through each test suite
688: while (i.hasNext()) {
689: TestSuiteDTO dto = (TestSuiteDTO) testSuites.get(i.next());
690: Iterator j = dto.getTests().keySet().iterator();
691: // go through each test plugIn
692: while (j.hasNext()) {
693: TestDTO tdto = (TestDTO) dto.getTests().get(j.next());
694: plugInNames.add(tdto.getPlugIn().getName());
695: }
696: }
697:
698: i = plugIns.values().iterator();
699: Map errors = new HashMap();
700:
701: // go through each plugIn and add it to errors
702: while (i.hasNext())
703: errors.put(i.next(), Boolean.FALSE);
704:
705: // step 2 configure plug-ins with defaults
706: Map defaultPlugIns = new HashMap(plugInNames.size());
707: i = plugInNames.iterator();
708:
709: // go through each plugIn
710: while (i.hasNext()) {
711: String plugInName = (String) i.next();
712: PlugInDTO dto = (PlugInDTO) plugIns.get(plugInName);
713: Class plugInClass = null;
714:
715: try {
716: plugInClass = Class.forName(dto.getClassName());
717: } catch (ClassNotFoundException e) {
718: //Error, using default.
719: errors.put(dto, e);
720: e.printStackTrace();
721: }
722:
723: if (plugInClass == null) {
724: plugInClass = Validation.class;
725: }
726:
727: Map plugInArgs = dto.getArgs();
728:
729: if (plugInArgs == null) {
730: plugInArgs = new HashMap();
731: }
732:
733: try {
734: PlugIn plugIn = new org.geotools.validation.PlugIn(
735: plugInName, plugInClass, dto.getDescription(),
736: plugInArgs);
737: defaultPlugIns.put(plugInName, plugIn);
738: } catch (ValidationException e) {
739: e.printStackTrace();
740: errors.put(dto, e);
741:
742: //error should log here
743: continue;
744: }
745:
746: errors.put(dto, Boolean.TRUE); // store the plugIn
747: }
748:
749: // step 3 configure plug-ins with tests + add to processor
750: i = testSuites.keySet().iterator();
751:
752: // for each TEST SUITE
753: while (i.hasNext()) {
754: TestSuiteDTO tdto = (TestSuiteDTO) testSuites.get(i.next());
755: Iterator j = tdto.getTests().keySet().iterator();
756:
757: // for each TEST in the test suite
758: while (j.hasNext()) {
759: TestDTO dto = (TestDTO) tdto.getTests().get(j.next());
760:
761: // deal with test
762: Map testArgs = dto.getArgs();
763:
764: if (testArgs == null) {
765: testArgs = new HashMap();
766: } else {
767: Map m = new HashMap();
768: Iterator k = testArgs.keySet().iterator();
769:
770: while (k.hasNext()) {
771: ArgumentDTO adto = (ArgumentDTO) testArgs.get(k
772: .next());
773: m.put(adto.getName(), adto.getValue());
774: }
775:
776: testArgs = m;
777: }
778:
779: try {
780: PlugIn plugIn = (org.geotools.validation.PlugIn) defaultPlugIns
781: .get(dto.getPlugIn().getName());
782: Validation validation = plugIn.createValidation(dto
783: .getName(), dto.getDescription(), testArgs);
784:
785: if (validation instanceof FeatureValidation) {
786: addValidation((FeatureValidation) validation);
787: }
788:
789: if (validation instanceof IntegrityValidation) {
790: addValidation((IntegrityValidation) validation);
791: }
792: } catch (ValidationException e) {
793: e.printStackTrace();
794: errors.put(dto, e);
795:
796: //error should log here
797: continue;
798: }
799:
800: errors.put(dto, Boolean.TRUE);
801: }
802:
803: errors.put(tdto, Boolean.TRUE);
804: } // end while each test suite
805: }// end load method
806:
807: }
|