39.3. Reading Tests from a Text FileListing 39.1 shows the class SumRunner which processes the comma-separated test files, mapping the test into Fit. The main() method of the class expects two arguments: the input and report file names. The constructor of SumRunner passes three things to the superclass: the title of the HTML file that's to be produced; the input File, so that the file details can be added to the fixture; and the name of the report file. The constructor then calls makeTable() to read the input file and create the internal form of the Fit tables required. Then the tests are run and reported with a call to the runAndReportAndExit() method of the superclass, CustomRunner. This method runs Fit on the internal tables, writes the report, prints the count information to System.err, and exits with a suitable error code. Listing 39.1. SumRunner.javapublic class SumRunner extends CustomRunner { public static void main(String args[]) throws IOException { if (args.length != 2) { System.err.println( "Usage: java SumRunner inputFile.txt reportFile.html"); System.exit(-1); } new SumRunner(new File(args[0]),args[1]); } public SumRunner(File inFile, String reportFileName) throws IOException { super("Sum",inFile,reportFileName); BufferedReader in = new BufferedReader( new FileReader(inFile)); makeTable(in); runAndReportAndExit(); } private void makeTable(BufferedReader in) throws IOException { addTable("Sum"); addRow("a | b | sum()"); while (true) { String line = in.readLine(); if (line == null) break; addRow(line); } addTableWithLeaderText("fit.Summary","<BR>"); } } The method makeTable() in Listing 39.1 calls the inherited addTable() to create a new table with the first row value as argument. The method addRow() creates a new row in the table. Finally, the second table is added, a fit. Summary, with the text "<BR>" included before the table so that the tables are separated in the report. Other functionality to ease writing custom runners is provided in class CustomRunner; see the code for details. Questions & Answers
|