1 | /* |
2 | * JTiger Unit Testing Framework for J2SE 1.5 |
3 | * Copyright (C) 2005 Tony Morris |
4 | * |
5 | * This software is licenced under the |
6 | * Common Public Licence version 1.0 |
7 | * http://www.opensource.org/licenses/cpl1.0.php |
8 | * |
9 | * You received a copy of this licence with this software. |
10 | */ |
11 | package org.jtiger.report.text; |
12 | |
13 | import java.io.FileWriter; |
14 | import java.io.IOException; |
15 | import java.io.PrintWriter; |
16 | import java.io.File; |
17 | import org.jtiger.framework.FixtureResult; |
18 | import org.jtiger.framework.FixtureResults; |
19 | import org.jtiger.framework.FixtureResultsHandler; |
20 | import org.jtiger.framework.FixtureResultsHandlerException; |
21 | import org.jtiger.framework.ReadOnlyArray; |
22 | import org.jtiger.framework.TestResult; |
23 | |
24 | /** |
25 | * A result handler that produces a plain text report of test run results. |
26 | * The report is written to a file which is passed in the parameters to |
27 | * {@link #handleResult(org.jtiger.framework.FixtureResults, org.jtiger.framework.ReadOnlyArray) the handleResult method}. |
28 | * |
29 | * @author %javadoc.author.tag% |
30 | * @version %version%<br/> |
31 | * <i>Build Number %build.number%</i><br/> |
32 | * <i>Build Time %build.time% CET (GMT + 1)</i> |
33 | */ |
34 | public final class TextFixtureResultsHandler implements FixtureResultsHandler |
35 | { |
36 | /** |
37 | * Create a default <tt>TextFixtureResultsHandler</tt>. |
38 | */ |
39 | public TextFixtureResultsHandler() |
40 | { |
41 | |
42 | } |
43 | |
44 | /** |
45 | * Write a plain text report of the given test run results to a file which is passed in the parameters. |
46 | * If the <tt>params</tt> are empty, the current directory is used to write the report to in a file called |
47 | * 'result.txt'. |
48 | * |
49 | * @param results The test run results to produce the plain text report of. |
50 | * @param params The parameters that contain at least one parameter which is the file to write the plain text report |
51 | * to otherwise the current directory is used. |
52 | * @throws FixtureResultsHandlerException If an error occurs while writing the plain text report. |
53 | */ |
54 | public void handleResult(final FixtureResults results, final ReadOnlyArray<String> params) throws FixtureResultsHandlerException |
55 | { |
56 | final File destination; |
57 | |
58 | if(params == null || params.length() == 0) |
59 | { |
60 | destination = new File("result.txt"); |
61 | } |
62 | else |
63 | { |
64 | destination = new File(params.get(0)); |
65 | } |
66 | |
67 | PrintWriter out = null; |
68 | |
69 | try |
70 | { |
71 | out = new PrintWriter(new FileWriter(destination)); |
72 | |
73 | for(FixtureResult result : results) |
74 | { |
75 | for(TestResult tr : result) |
76 | { |
77 | out.println(tr); |
78 | } |
79 | |
80 | out.println(); |
81 | } |
82 | } |
83 | catch(IOException e) |
84 | { |
85 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
86 | } |
87 | finally |
88 | { |
89 | if(out != null) |
90 | { |
91 | out.close(); |
92 | } |
93 | } |
94 | } |
95 | |
96 | } |