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.xml; |
12 | |
13 | import java.beans.Encoder; |
14 | import java.beans.Expression; |
15 | import java.beans.PersistenceDelegate; |
16 | import java.beans.XMLEncoder; |
17 | import java.io.FileNotFoundException; |
18 | import java.io.FileOutputStream; |
19 | import java.io.File; |
20 | import org.jtiger.framework.FixtureResults; |
21 | import org.jtiger.framework.FixtureResultsHandler; |
22 | import org.jtiger.framework.FixtureResultsHandlerException; |
23 | import org.jtiger.framework.ReadOnlyArray; |
24 | |
25 | /** |
26 | * A result handler that produces a XML report of test run results. |
27 | * The report is written to a file which is passed in the parameters to |
28 | * {@link #handleResult(org.jtiger.framework.FixtureResults, org.jtiger.framework.ReadOnlyArray) the handleResult method}. |
29 | * |
30 | * @author %javadoc.author.tag% |
31 | * @version %version%<br/> |
32 | * <i>Build Number %build.number%</i><br/> |
33 | * <i>Build Time %build.time% CET (GMT + 1)</i> |
34 | */ |
35 | public final class XmlFixtureResultsHandler implements FixtureResultsHandler |
36 | { |
37 | /** |
38 | * Create a default <tt>XmlFixtureResultsHandler</tt>. |
39 | */ |
40 | public XmlFixtureResultsHandler() |
41 | { |
42 | |
43 | } |
44 | |
45 | /** |
46 | * Write a XML report of the given test run results to a file which is passed in the parameters. |
47 | * If the <tt>params</tt> are empty, the current directory is used to write the report to in a file called |
48 | * 'result.xml'. |
49 | * |
50 | * @param results The test run results to produce the XML report of. |
51 | * @param params The parameters that contain at least one parameter which is the file to write the XML report to |
52 | * otherwise the current directory is used. |
53 | * @throws FixtureResultsHandlerException If an error occurs while writing the XML report. |
54 | */ |
55 | public void handleResult(final FixtureResults results, final ReadOnlyArray<String> params) throws FixtureResultsHandlerException |
56 | { |
57 | final File destination; |
58 | |
59 | if(params == null || params.length() == 0) |
60 | { |
61 | destination = new File("result.xml"); |
62 | } |
63 | else |
64 | { |
65 | destination = new File(params.get(0)); |
66 | } |
67 | |
68 | XMLEncoder encoder = null; |
69 | |
70 | try |
71 | { |
72 | encoder = new XMLEncoder(new FileOutputStream(destination)); |
73 | encoder.setPersistenceDelegate(StackTraceElement.class, new PersistenceDelegate() |
74 | { |
75 | public Expression instantiate(final Object oldInstance, final Encoder out) |
76 | { |
77 | if(!(oldInstance instanceof StackTraceElement)) |
78 | { |
79 | throw new IllegalStateException(); |
80 | } |
81 | |
82 | final StackTraceElement e = (StackTraceElement)oldInstance; |
83 | |
84 | return new Expression(oldInstance, StackTraceElement.class, "new", |
85 | new Object[]{e.getClassName(), e.getMethodName(), e.getFileName(), e.getLineNumber()}); |
86 | } |
87 | }); |
88 | |
89 | encoder.writeObject(new FixtureResultsBeanImpl(results)); |
90 | } |
91 | catch(FileNotFoundException e) |
92 | { |
93 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
94 | } |
95 | finally |
96 | { |
97 | if(encoder != null) |
98 | { |
99 | encoder.close(); |
100 | } |
101 | } |
102 | } |
103 | } |