001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2004, Institut de Recherche pour le Développement
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.referencing;
018:
019: // J2SE dependencies
020: import java.io.FileReader;
021: import java.io.IOException;
022: import java.io.LineNumberReader;
023: import java.text.NumberFormat;
024:
025: import org.geotools.io.TableWriter;
026: import org.geotools.referencing.Console;
027: import org.geotools.referencing.ScriptTest;
028: import org.opengis.referencing.operation.TransformException;
029: import org.opengis.geometry.MismatchedDimensionException;
030:
031: /**
032: * A console for running test scripts. Most of the work is already done by the subclass.
033: * <code>TestScript</code> mostly add statistics about the test executed. This class is
034: * used by {@link ScriptTest}. It can also be run from the command line for executing all
035: * files specified in argument.
036: *
037: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/referencing/TestScript.java $
038: * @version $Id: TestScript.java 24925 2007-03-27 20:12:08Z jgarnett $
039: * @author Martin Desruisseaux
040: */
041: public final class TestScript extends Console {
042: /**
043: * Number of tests run and passed. Used for displaying statistics.
044: */
045: private int testRun, testPassed;
046:
047: /**
048: * Creates a new console instance using the specified input stream.
049: *
050: * @param in The input stream.
051: */
052: public TestScript(final LineNumberReader in) {
053: super (in);
054: setPrompt(null);
055: }
056:
057: /**
058: * Invoked automatically when the <code>target pt</code> instruction were executed.
059: */
060: protected void test() throws TransformException,
061: MismatchedDimensionException {
062: testRun++;
063: super .test();
064: testPassed++;
065: }
066:
067: /**
068: * Prints the number of tests executed, the number of errors and the success rate.
069: */
070: private void printStatistics() throws IOException {
071: NumberFormat f = NumberFormat.getNumberInstance();
072: final TableWriter table = new TableWriter(out, 1);
073: table.setMultiLinesCells(true);
074: table.writeHorizontalSeparator();
075: table.write("Tests:");
076: table.nextColumn();
077: table.setAlignment(TableWriter.ALIGN_RIGHT);
078: table.write(f.format(testRun));
079: table.nextLine();
080: table.setAlignment(TableWriter.ALIGN_LEFT);
081: table.write("Errors:");
082: table.nextColumn();
083: table.setAlignment(TableWriter.ALIGN_RIGHT);
084: table.write(f.format(testRun - testPassed));
085: table.nextLine();
086: if (testRun != 0) {
087: f = NumberFormat.getPercentInstance();
088: table.setAlignment(TableWriter.ALIGN_LEFT);
089: table.write("Success rate:");
090: table.nextColumn();
091: table.setAlignment(TableWriter.ALIGN_RIGHT);
092: table.write(f
093: .format((double) testPassed / (double) testRun));
094: table.nextLine();
095: }
096: table.writeHorizontalSeparator();
097: table.flush();
098: }
099:
100: /**
101: * Run all tests scripts specified on the command line.
102: */
103: public static void main(final String[] args) {
104: final String lineSeparator = System.getProperty(
105: "line.separator", "\r");
106: try {
107: for (int i = 0; i < args.length; i++) {
108: final String filename = args[i];
109: final LineNumberReader in = new LineNumberReader(
110: new FileReader(filename));
111: final TestScript test = new TestScript(in);
112: test.out.write("Running \"");
113: test.out.write(filename);
114: test.out.write('"');
115: test.out.write(lineSeparator);
116: test.out.flush();
117: test.run();
118: test.printStatistics();
119: test.out.write(lineSeparator);
120: test.out.flush();
121: in.close();
122: }
123: } catch (IOException exception) {
124: exception.printStackTrace();
125: }
126: }
127: }
|