]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/htmlreport/ResultPage.java
Add Slc execution notification to results
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / tree / htmlreport / ResultPage.java
1 package org.argeo.slc.core.test.tree.htmlreport;
2
3 import java.io.IOException;
4 import java.io.PrintWriter;
5 import java.io.StringWriter;
6 import java.util.Date;
7 import java.util.SortedMap;
8 import java.util.TreeMap;
9
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;
14
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;
23
24 class ResultPage {
25 private final static Log log = LogFactory.getLog(ResultPage.class);
26
27 private final FullHtmlTreeReport report;
28 private final TreeTestResult result;
29
30 ResultPage(FullHtmlTreeReport report, TreeTestResult result) {
31 this.report = report;
32 this.result = result;
33 }
34
35 /**
36 * Generates a result page for one test result
37 *
38 * @param file
39 * file to which generate the HTML
40 * @param result
41 * the result to dump
42 */
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");
51
52 buf.append("<body>\n");
53
54 // Header
55 buf.append("<a name=\"top\"/>\n");
56 buf.append("<h1>Result #").append(result.getTestResultId()).append(
57 "</h1>\n");
58 Date closeDate = result.getCloseDate();
59 if (closeDate == null) {
60 buf.append("[Not closed]");
61 } else {
62 buf.append(report.sdf.format(closeDate));
63 }
64
65 // TOC
66 generateToc(buf, registry);
67
68 generatePartsList(buf, registry);
69
70 buf.append("</body>");
71 buf.append("</html>");
72
73 try {
74 FileUtils.writeStringToFile(report.getResultFile(result), buf
75 .toString());
76 } catch (IOException e) {
77 log.error("Could not save result page.", e);
78 }
79 }
80
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(
86 path);
87 boolean isFailed = false;
88 for (TestResultPart part : subList.getParts()) {
89 if (!part.getStatus().equals(TestStatus.PASSED)) {
90 isFailed = true;
91 break;
92 }
93 }
94 fillToc(toc, path, isFailed);
95 }
96
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);
101
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("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
107 }
108
109 if (inResult) {
110 buf.append("<a href=\"#").append(anchor(path)).append(
111 "\" class=\"").append(isFailed ? "failed" : "passed")
112 .append("\"><b>");
113 }
114 if (registry != null) {
115 StructureElement element = registry.getElement(path);
116 if (element != null) {
117 buf.append(element.getLabel());
118 } else {
119 buf.append(path.getName());
120 }
121 }
122 if (inResult) {
123 buf.append("</b></a>");
124 }
125 buf.append("</td></tr>\n");
126 }
127 buf.append("</table>\n");
128 buf.append("<hr/>\n");
129 }
130
131 private void generatePartsList(StringBuffer buf, StructureRegistry registry) {
132 for (TreeSPath path : result.getResultParts().keySet()) {
133 buf.append("<p>\n");
134 buf.append("<a name=\"").append(anchor(path)).append("\"></a>");
135 buf.append("<h2>");
136 describedPath(path, registry, buf);
137 buf.append("</h2>");
138
139 PartSubList subList = (PartSubList) result.getResultParts().get(
140 path);
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;
149 String clss = "";
150 if (sPart.getStatus().equals(TestStatus.PASSED)) {
151 clss = "passed";
152 } else {
153 clss = "failed";
154 }
155 buf.append("<tr>");
156 buf.append("<td><b>").append(displayedIndex)
157 .append("</b></td>");
158 buf.append("<td class=\"").append(clss).append("\">");
159
160 buf.append(sPart.getMessage());
161 if (sPart.getStatus().equals(TestStatus.ERROR)) {
162 buf
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);
172 }
173 buf.append("</p>");
174 }
175 buf.append("</td>");
176 buf.append("</tr>\n");
177
178 displayedIndex++;
179 }
180 buf.append("</table>\n");
181 buf.append("<a class=\"nav\" href=\"#top\">top</a>\n");
182 buf.append("<hr/>\n");
183 }
184 }
185
186 private void fillToc(SortedMap<TreeSPath, Integer> toc, TreeSPath path,
187 boolean isFailed) {
188 if (isFailed) {
189 toc.put(path, TestStatus.FAILED);
190 } else {
191 if (!toc.containsKey(path)) {
192 toc.put(path, TestStatus.PASSED);
193 }
194 }
195
196 if (path.getParent() != null) {
197 fillToc(toc, path.getParent(), isFailed);
198 }
199 }
200
201 private String anchor(TreeSPath path) {
202 return path.getAsUniqueString().replace(path.getSeparator(), '_');
203 }
204
205 private void describedPath(TreeSPath path, StructureRegistry registry,
206 StringBuffer buf) {
207 // StringBuffer buf = new StringBuffer("");
208 if (path.getParent() != null) {
209 describedPath(path.getParent(), registry, buf);
210 }
211 String description = path.getName();
212 if (registry != null) {
213 StructureElement element = registry.getElement(path);
214 if (element != null) {
215 description = element.getLabel();
216 }
217 }
218 buf.append('/').append(description);
219 }
220 }