/*
 *    GeoTools - OpenSource mapping toolkit
 *    http://geotools.org
 *    (C) 2004-2006, Geotools Project Managment Committee (PMC)
 *    (C) 2004, Institut de Recherche pour le Développement
 *    
 *    This library is free software; you can redistribute it and/or
 *    modify it under the terms of the GNU Lesser General Public
 *    License as published by the Free Software Foundation; either
 *    version 2.1 of the License, or (at your option) any later version.
 *
 *    This library is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *    Lesser General Public License for more details.
 */
package org.geotools.referencing;

// J2SE dependencies
import java.io.FileReader;
import java.io.IOException;
import java.io.LineNumberReader;
import java.text.NumberFormat;

import org.geotools.io.TableWriter;
import org.geotools.referencing.Console;
import org.geotools.referencing.ScriptTest;
import org.opengis.referencing.operation.TransformException;
import org.opengis.geometry.MismatchedDimensionException;


/**
 * A console for running test scripts. Most of the work is already done by the subclass.
 * <code>TestScript</code> mostly add statistics about the test executed. This class is
 * used by {@link ScriptTest}. It can also be run from the command line for executing all
 * files specified in argument.
 *
 * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/library/referencing/src/test/java/org/geotools/referencing/TestScript.java $
 * @version $Id: TestScript.java 24925 2007-03-27 20:12:08Z jgarnett $
 * @author Martin Desruisseaux
 */
public final class TestScript extends Console {
    /**
     * Number of tests run and passed. Used for displaying statistics.
     */
    private int testRun, testPassed;

    /**
     * Creates a new console instance using the specified input stream.
     *
     * @param in The input stream.
     */
    public TestScript(final LineNumberReader in) {
        super(in);
        setPrompt(null);
    }

    /**
     * Invoked automatically when the <code>target pt</code> instruction were executed.
     */
    protected void test() throws TransformException, MismatchedDimensionException {
        testRun++;
        super.test();
        testPassed++;
    }

    /**
     * Prints the number of tests executed, the number of errors and the success rate.
     */
    private void printStatistics() throws IOException {
        NumberFormat f = NumberFormat.getNumberInstance();
        final TableWriter table = new TableWriter(out, 1);
        table.setMultiLinesCells(true);
        table.writeHorizontalSeparator();
        table.write("Tests:");
        table.nextColumn();
        table.setAlignment(TableWriter.ALIGN_RIGHT);
        table.write(f.format(testRun));
        table.nextLine();
        table.setAlignment(TableWriter.ALIGN_LEFT);
        table.write("Errors:");
        table.nextColumn();
        table.setAlignment(TableWriter.ALIGN_RIGHT);
        table.write(f.format(testRun - testPassed));
        table.nextLine();
        if (testRun != 0) {
            f = NumberFormat.getPercentInstance();
            table.setAlignment(TableWriter.ALIGN_LEFT);
            table.write("Success rate:");
            table.nextColumn();
            table.setAlignment(TableWriter.ALIGN_RIGHT);
            table.write(f.format((double)testPassed / (double)testRun));
            table.nextLine();
        }
        table.writeHorizontalSeparator();
        table.flush();
    }

    /**
     * Run all tests scripts specified on the command line.
     */
    public static void main(final String[] args) {
        final String lineSeparator = System.getProperty("line.separator", "\r");
        try {
            for (int i=0; i<args.length; i++) {
                final String filename = args[i];
                final LineNumberReader in = new LineNumberReader(new FileReader(filename));
                final TestScript test = new TestScript(in);
                test.out.write("Running \"");
                test.out.write(filename);
                test.out.write('"');
                test.out.write(lineSeparator);
                test.out.flush();
                test.run();
                test.printStatistics();
                test.out.write(lineSeparator);
                test.out.flush();
                in.close();
            }
        } catch (IOException exception) {
            exception.printStackTrace();
        }
    }
}
