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.html; |
12 | |
13 | import java.beans.XMLEncoder; |
14 | import java.io.ByteArrayInputStream; |
15 | import java.io.ByteArrayOutputStream; |
16 | import java.io.File; |
17 | import java.io.FileNotFoundException; |
18 | import java.io.FileOutputStream; |
19 | import java.io.IOException; |
20 | import java.io.InputStream; |
21 | import java.io.OutputStream; |
22 | import javax.xml.transform.Transformer; |
23 | import javax.xml.transform.TransformerConfigurationException; |
24 | import javax.xml.transform.TransformerException; |
25 | import javax.xml.transform.TransformerFactory; |
26 | import javax.xml.transform.stream.StreamResult; |
27 | import javax.xml.transform.stream.StreamSource; |
28 | import org.jtiger.framework.FixtureResultsHandlerException; |
29 | import org.jtiger.framework.ReadOnlyArray; |
30 | |
31 | final class OverallHtmlFileWriterFactory |
32 | { |
33 | private static final String XSL_RESOURCE = "/org/jtiger/report/xsl/overall.xsl"; |
34 | private static final String FILENAME = "overall.html"; |
35 | private static final String PARAM_BUILD_TIME = "buildTime"; |
36 | private static final String PARAM_SUCCESS = "success"; |
37 | private static final String PARAM_FAILURE = "failure"; |
38 | private static final String PARAM_FAILURE_SETUP = "failure.setup"; |
39 | private static final String PARAM_FAILURE_TEARDOWN = "failure.teardown"; |
40 | private static final String PARAM_IGNORED_ANNOTATED = "ignored.annotated"; |
41 | private static final String PARAM_IGNORED_CANNOT_INVOKE = "ignored.cannot.invoke"; |
42 | |
43 | private OverallHtmlFileWriterFactory() |
44 | { |
45 | |
46 | } |
47 | |
48 | static OverallHtmlFileWriter newOverallHtmlFileWriter() |
49 | { |
50 | return new OverallHtmlFileWriterImpl(); |
51 | } |
52 | |
53 | private static final class OverallHtmlFileWriterImpl implements OverallHtmlFileWriter |
54 | { |
55 | OverallHtmlFileWriterImpl() |
56 | { |
57 | |
58 | } |
59 | |
60 | public void writeOverallHtml(final ReadOnlyArray<OverallResult> results, |
61 | final File destination, |
62 | final FilenamePolicy policy, |
63 | final OverallTotal total, |
64 | final String buildTime) |
65 | throws FixtureResultsHandlerException |
66 | { |
67 | final ByteArrayOutputStream out = new ByteArrayOutputStream(); |
68 | final XMLEncoder encoder = new XMLEncoder(out); |
69 | |
70 | for(OverallResult result : results) |
71 | { |
72 | final Object bean = new OverallResultBeanImpl(result.getName(), |
73 | policy.nextFilename(), result.getDescription(), result.getElapsedTime(), result.getResult()); |
74 | encoder.writeObject(bean); |
75 | } |
76 | |
77 | encoder.close(); |
78 | |
79 | InputStream in = null; |
80 | |
81 | try |
82 | { |
83 | in = getClass().getResourceAsStream(XSL_RESOURCE); |
84 | final Transformer t = TransformerFactory.newInstance().newTransformer(new StreamSource(in)); |
85 | t.setParameter(PARAM_BUILD_TIME, buildTime); |
86 | t.setParameter(PARAM_SUCCESS, total.getSuccess()); |
87 | t.setParameter(PARAM_FAILURE, total.getFailure()); |
88 | t.setParameter(PARAM_FAILURE_SETUP, total.getFailureSetUp()); |
89 | t.setParameter(PARAM_FAILURE_TEARDOWN, total.getFailureTearDown()); |
90 | t.setParameter(PARAM_IGNORED_ANNOTATED, total.getIgnoredAnnotated()); |
91 | t.setParameter(PARAM_IGNORED_CANNOT_INVOKE, total.getIgnoredCannotInvoke()); |
92 | OutputStream o = null; |
93 | |
94 | try |
95 | { |
96 | final File f = new File(destination, FILENAME); |
97 | o = new FileOutputStream(f); |
98 | final StreamResult r = new StreamResult(o); |
99 | t.transform(new StreamSource(new ByteArrayInputStream(out.toByteArray())), r); |
100 | } |
101 | catch(FileNotFoundException e) |
102 | { |
103 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
104 | } |
105 | finally |
106 | { |
107 | if(o != null) |
108 | { |
109 | try |
110 | { |
111 | o.close(); |
112 | } |
113 | catch(IOException e) |
114 | { |
115 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
116 | } |
117 | } |
118 | } |
119 | } |
120 | catch(TransformerConfigurationException e) |
121 | { |
122 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
123 | } |
124 | catch(TransformerException e) |
125 | { |
126 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
127 | } |
128 | finally |
129 | { |
130 | if(in != null) |
131 | { |
132 | try |
133 | { |
134 | in.close(); |
135 | } |
136 | catch(IOException e) |
137 | { |
138 | throw new FixtureResultsHandlerException(e.getMessage(), e); |
139 | } |
140 | } |
141 | } |
142 | } |
143 | } |
144 | } |