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
<TreeSPath
> registry
) {
44 StringBuffer buf
= new StringBuffer("");
45 buf
.append("<html>\n");
46 buf
.append("<header>");
47 buf
.append("<title>Result #").append(result
.getUuid());
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
.getUuid()).append("</h1>\n");
57 Date closeDate
= result
.getCloseDate();
58 if (closeDate
== null) {
59 buf
.append("[Not closed]");
61 buf
.append(report
.sdf
.format(closeDate
));
65 generateToc(buf
, registry
);
67 generatePartsList(buf
, registry
);
69 buf
.append("</body>");
70 buf
.append("</html>");
73 FileUtils
.writeStringToFile(report
.getResultFile(result
), buf
75 } catch (IOException e
) {
76 log
.error("Could not save result page.", e
);
80 private void generateToc(StringBuffer buf
,
81 StructureRegistry
<TreeSPath
> 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
,
132 StructureRegistry
<TreeSPath
> registry
) {
133 for (TreeSPath path
: result
.getResultParts().keySet()) {
135 buf
.append("<a name=\"").append(anchor(path
)).append("\"></a>");
137 describedPath(path
, registry
, buf
);
140 PartSubList subList
= (PartSubList
) result
.getResultParts().get(
142 buf
.append("<table border=0>\n");
143 int displayedIndex
= 1;// for display only
144 for (TestResultPart part
: subList
.getParts()) {
145 SimpleResultPart sPart
= (SimpleResultPart
) part
;
146 buf
.append("Related Test Run Id:").append(
147 sPart
.getTestRunUuid()).append("<br/>\n");
149 if (sPart
.getStatus().equals(TestStatus
.PASSED
)) {
155 buf
.append("<td><b>").append(displayedIndex
)
156 .append("</b></td>");
157 buf
.append("<td class=\"").append(clss
).append("\">");
159 buf
.append(sPart
.getMessage());
160 if (sPart
.getStatus().equals(TestStatus
.ERROR
)) {
162 .append("<p><b>An unexpected error prevented the test to run properly.</b>");
163 buf
.append(sPart
.getExceptionMessage());
167 buf
.append("</tr>\n");
171 buf
.append("</table>\n");
172 buf
.append("<a class=\"nav\" href=\"#top\">top</a>\n");
173 buf
.append("<hr/>\n");
177 private void fillToc(SortedMap
<TreeSPath
, Integer
> toc
, TreeSPath path
,
180 toc
.put(path
, TestStatus
.FAILED
);
182 if (!toc
.containsKey(path
)) {
183 toc
.put(path
, TestStatus
.PASSED
);
187 if (path
.getParent() != null) {
188 fillToc(toc
, path
.getParent(), isFailed
);
192 private String
anchor(TreeSPath path
) {
193 return path
.getAsUniqueString().replace(path
.getSeparator(), '_');
196 private void describedPath(TreeSPath path
,
197 StructureRegistry
<TreeSPath
> registry
, StringBuffer buf
) {
198 // StringBuffer buf = new StringBuffer("");
199 if (path
.getParent() != null) {
200 describedPath(path
.getParent(), registry
, buf
);
202 String description
= path
.getName();
203 if (registry
!= null) {
204 StructureElement element
= registry
.getElement(path
);
205 if (element
!= null) {
206 description
= element
.getLabel();
209 buf
.append('/').append(description
);