1 package org
.argeo
.slc
.core
.test
.tree
.htmlreport
;
3 import java
.io
.IOException
;
4 import java
.io
.PrintWriter
;
5 import java
.io
.StringWriter
;
7 import java
.util
.SortedMap
;
8 import java
.util
.TreeMap
;
10 import org
.apache
.commons
.io
.FileUtils
;
11 import org
.apache
.commons
.io
.IOUtils
;
12 import org
.apache
.commons
.logging
.Log
;
13 import org
.apache
.commons
.logging
.LogFactory
;
15 import org
.argeo
.slc
.core
.structure
.StructureElement
;
16 import org
.argeo
.slc
.core
.structure
.StructureRegistry
;
17 import org
.argeo
.slc
.core
.structure
.tree
.TreeSPath
;
18 import org
.argeo
.slc
.core
.test
.SimpleResultPart
;
19 import org
.argeo
.slc
.core
.test
.TestResultPart
;
20 import org
.argeo
.slc
.core
.test
.TestStatus
;
21 import org
.argeo
.slc
.core
.test
.tree
.PartSubList
;
22 import org
.argeo
.slc
.core
.test
.tree
.TreeTestResult
;
25 private final static Log log
= LogFactory
.getLog(ResultPage
.class);
27 private final FullHtmlTreeReport report
;
28 private final TreeTestResult result
;
30 ResultPage(FullHtmlTreeReport report
, TreeTestResult result
) {
36 * Generates a result page for one test result
39 * file to which generate the HTML
43 protected void generate(StructureRegistry registry
) {
44 StringBuffer buf
= new StringBuffer("");
45 buf
.append("<html>\n");
46 buf
.append("<header>");
47 buf
.append("<title>Result #").append(result
.getTestResultId());
48 buf
.append("</title>\n");
49 report
.addStyles(buf
);
50 buf
.append("</header>\n");
52 buf
.append("<body>\n");
55 buf
.append("<a name=\"top\"/>\n");
56 buf
.append("<h1>Result #").append(result
.getTestResultId()).append(
58 Date closeDate
= result
.getCloseDate();
59 if (closeDate
== null) {
60 buf
.append("[Not closed]");
62 buf
.append(report
.sdf
.format(closeDate
));
66 generateToc(buf
, registry
);
68 generatePartsList(buf
, registry
);
70 buf
.append("</body>");
71 buf
.append("</html>");
74 FileUtils
.writeStringToFile(report
.getResultFile(result
), buf
76 } catch (IOException e
) {
77 log
.error("Could not save result page.", e
);
81 private void generateToc(StringBuffer buf
, StructureRegistry registry
) {
82 buf
.append("<h2>Overview</h2>\n");
83 SortedMap
<TreeSPath
, Integer
> toc
= new TreeMap
<TreeSPath
, Integer
>();
84 for (TreeSPath path
: result
.getResultParts().keySet()) {
85 PartSubList subList
= (PartSubList
) result
.getResultParts().get(
87 boolean isFailed
= false;
88 for (TestResultPart part
: subList
.getParts()) {
89 if (!part
.getStatus().equals(TestStatus
.PASSED
)) {
94 fillToc(toc
, path
, isFailed
);
97 buf
.append("<table border=\"0\">\n");
98 for (TreeSPath path
: toc
.keySet()) {
99 boolean inResult
= result
.getResultParts().containsKey(path
);
100 boolean isFailed
= !toc
.get(path
).equals(TestStatus
.PASSED
);
102 buf
.append("<tr><td class=\"").append(
103 isFailed ?
"failed" : "passed").append("\">");
104 int depth
= path
.getDepth();
105 for (int i
= 0; i
< depth
; i
++) {
106 buf
.append(" ");
110 buf
.append("<a href=\"#").append(anchor(path
)).append(
111 "\" class=\"").append(isFailed ?
"failed" : "passed")
114 if (registry
!= null) {
115 StructureElement element
= registry
.getElement(path
);
116 if (element
!= null) {
117 buf
.append(element
.getLabel());
119 buf
.append(path
.getName());
123 buf
.append("</b></a>");
125 buf
.append("</td></tr>\n");
127 buf
.append("</table>\n");
128 buf
.append("<hr/>\n");
131 private void generatePartsList(StringBuffer buf
, StructureRegistry registry
) {
132 for (TreeSPath path
: result
.getResultParts().keySet()) {
134 buf
.append("<a name=\"").append(anchor(path
)).append("\"></a>");
136 describedPath(path
, registry
, buf
);
139 PartSubList subList
= (PartSubList
) result
.getResultParts().get(
141 buf
.append("Related SLC execution:").append(
142 subList
.getSlcExecutionUuid()).append("<br/>\n");
143 buf
.append("Related SLC execution step:").append(
144 subList
.getSlcExecutionUuid()).append("<br/>\n");
145 buf
.append("<table border=0>\n");
146 int displayedIndex
= 1;// for display only
147 for (TestResultPart part
: subList
.getParts()) {
148 SimpleResultPart sPart
= (SimpleResultPart
) part
;
150 if (sPart
.getStatus().equals(TestStatus
.PASSED
)) {
156 buf
.append("<td><b>").append(displayedIndex
)
157 .append("</b></td>");
158 buf
.append("<td class=\"").append(clss
).append("\">");
160 buf
.append(sPart
.getMessage());
161 if (sPart
.getStatus().equals(TestStatus
.ERROR
)) {
163 .append("<p><b>An unexpected error prevented the test to run properly.</b>");
164 Throwable exception
= sPart
.getException();
165 if (exception
!= null) {
166 StringWriter writer
= new StringWriter();
167 exception
.printStackTrace(new PrintWriter(writer
));
168 buf
.append("<br/><pre>");
169 buf
.append(writer
.toString());
170 buf
.append("</pre>");
171 IOUtils
.closeQuietly(writer
);
176 buf
.append("</tr>\n");
180 buf
.append("</table>\n");
181 buf
.append("<a class=\"nav\" href=\"#top\">top</a>\n");
182 buf
.append("<hr/>\n");
186 private void fillToc(SortedMap
<TreeSPath
, Integer
> toc
, TreeSPath path
,
189 toc
.put(path
, TestStatus
.FAILED
);
191 if (!toc
.containsKey(path
)) {
192 toc
.put(path
, TestStatus
.PASSED
);
196 if (path
.getParent() != null) {
197 fillToc(toc
, path
.getParent(), isFailed
);
201 private String
anchor(TreeSPath path
) {
202 return path
.getAsUniqueString().replace(path
.getSeparator(), '_');
205 private void describedPath(TreeSPath path
, StructureRegistry registry
,
207 // StringBuffer buf = new StringBuffer("");
208 if (path
.getParent() != null) {
209 describedPath(path
.getParent(), registry
, buf
);
211 String description
= path
.getName();
212 if (registry
!= null) {
213 StructureElement element
= registry
.getElement(path
);
214 if (element
!= null) {
215 description
= element
.getLabel();
218 buf
.append('/').append(description
);