]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.hibernate/src/main/java/org/argeo/slc/core/test/tree/htmlreport/ResultPage.java
Branch hibernate project from core
[gpl/argeo-slc.git] / org.argeo.slc.hibernate / 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<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");
51
52 buf.append("<body>\n");
53
54 // Header
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]");
60 } else {
61 buf.append(report.sdf.format(closeDate));
62 }
63
64 // TOC
65 generateToc(buf, registry);
66
67 generatePartsList(buf, registry);
68
69 buf.append("</body>");
70 buf.append("</html>");
71
72 try {
73 FileUtils.writeStringToFile(report.getResultFile(result), buf
74 .toString());
75 } catch (IOException e) {
76 log.error("Could not save result page.", e);
77 }
78 }
79
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(
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,
132 StructureRegistry<TreeSPath> registry) {
133 for (TreeSPath path : result.getResultParts().keySet()) {
134 buf.append("<p>\n");
135 buf.append("<a name=\"").append(anchor(path)).append("\"></a>");
136 buf.append("<h2>");
137 describedPath(path, registry, buf);
138 buf.append("</h2>");
139
140 PartSubList subList = (PartSubList) result.getResultParts().get(
141 path);
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");
148 String clss = "";
149 if (sPart.getStatus().equals(TestStatus.PASSED)) {
150 clss = "passed";
151 } else {
152 clss = "failed";
153 }
154 buf.append("<tr>");
155 buf.append("<td><b>").append(displayedIndex)
156 .append("</b></td>");
157 buf.append("<td class=\"").append(clss).append("\">");
158
159 buf.append(sPart.getMessage());
160 if (sPart.getStatus().equals(TestStatus.ERROR)) {
161 buf
162 .append("<p><b>An unexpected error prevented the test to run properly.</b>");
163 buf.append(sPart.getExceptionMessage());
164 buf.append("</p>");
165 }
166 buf.append("</td>");
167 buf.append("</tr>\n");
168
169 displayedIndex++;
170 }
171 buf.append("</table>\n");
172 buf.append("<a class=\"nav\" href=\"#top\">top</a>\n");
173 buf.append("<hr/>\n");
174 }
175 }
176
177 private void fillToc(SortedMap<TreeSPath, Integer> toc, TreeSPath path,
178 boolean isFailed) {
179 if (isFailed) {
180 toc.put(path, TestStatus.FAILED);
181 } else {
182 if (!toc.containsKey(path)) {
183 toc.put(path, TestStatus.PASSED);
184 }
185 }
186
187 if (path.getParent() != null) {
188 fillToc(toc, path.getParent(), isFailed);
189 }
190 }
191
192 private String anchor(TreeSPath path) {
193 return path.getAsUniqueString().replace(path.getSeparator(), '_');
194 }
195
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);
201 }
202 String description = path.getName();
203 if (registry != null) {
204 StructureElement element = registry.getElement(path);
205 if (element != null) {
206 description = element.getLabel();
207 }
208 }
209 buf.append('/').append(description);
210 }
211 }