]> 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
Inhtroduce basedon context
[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("<table border=0>\n");
142 int displayedIndex = 1;// for display only
143 for (TestResultPart part : subList.getParts()) {
144 SimpleResultPart sPart = (SimpleResultPart) part;
145 String clss = "";
146 if (sPart.getStatus().equals(TestStatus.PASSED)) {
147 clss = "passed";
148 } else {
149 clss = "failed";
150 }
151 buf.append("<tr>");
152 buf.append("<td><b>").append(displayedIndex)
153 .append("</b></td>");
154 buf.append("<td class=\"").append(clss).append("\">");
155
156 buf.append(sPart.getMessage());
157 if (sPart.getStatus().equals(TestStatus.ERROR)) {
158 buf
159 .append("<p><b>An unexpected error prevented the test to run properly.</b>");
160 Throwable exception = sPart.getException();
161 if (exception != null) {
162 StringWriter writer = new StringWriter();
163 exception.printStackTrace(new PrintWriter(writer));
164 buf.append("<br/><pre>");
165 buf.append(writer.toString());
166 buf.append("</pre>");
167 IOUtils.closeQuietly(writer);
168 }
169 buf.append("</p>");
170 }
171 buf.append("</td>");
172 buf.append("</tr>\n");
173
174 displayedIndex++;
175 }
176 buf.append("</table>\n");
177 buf.append("<a class=\"nav\" href=\"#top\">top</a>\n");
178 buf.append("<hr/>\n");
179 }
180 }
181
182 private void fillToc(SortedMap<TreeSPath, Integer> toc, TreeSPath path,
183 boolean isFailed) {
184 if (isFailed) {
185 toc.put(path, TestStatus.FAILED);
186 } else {
187 if (!toc.containsKey(path)) {
188 toc.put(path, TestStatus.PASSED);
189 }
190 }
191
192 if (path.getParent() != null) {
193 fillToc(toc, path.getParent(), isFailed);
194 }
195 }
196
197 private String anchor(TreeSPath path) {
198 return path.getAsUniqueString().replace(path.getSeparator(), '_');
199 }
200
201 private void describedPath(TreeSPath path, StructureRegistry registry,
202 StringBuffer buf) {
203 // StringBuffer buf = new StringBuffer("");
204 if (path.getParent() != null) {
205 describedPath(path.getParent(), registry, buf);
206 }
207 String description = path.getName();
208 if (registry != null) {
209 StructureElement element = registry.getElement(path);
210 if (element != null) {
211 description = element.getLabel();
212 }
213 }
214 buf.append('/').append(description);
215 }
216 }