]> 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
Restructure deployment
[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.util.Date;
5 import java.util.SortedMap;
6 import java.util.TreeMap;
7
8 import org.apache.commons.io.FileUtils;
9 import org.apache.commons.logging.Log;
10 import org.apache.commons.logging.LogFactory;
11 import org.argeo.slc.core.structure.StructureElement;
12 import org.argeo.slc.core.structure.StructureRegistry;
13 import org.argeo.slc.core.structure.tree.TreeSPath;
14 import org.argeo.slc.core.test.SimpleResultPart;
15 import org.argeo.slc.core.test.TestResultPart;
16 import org.argeo.slc.core.test.TestStatus;
17 import org.argeo.slc.core.test.tree.PartSubList;
18 import org.argeo.slc.core.test.tree.TreeTestResult;
19
20 class ResultPage {
21 private final static Log log = LogFactory.getLog(ResultPage.class);
22
23 private final FullHtmlTreeReport report;
24 private final TreeTestResult result;
25
26 ResultPage(FullHtmlTreeReport report, TreeTestResult result) {
27 this.report = report;
28 this.result = result;
29 }
30
31 /**
32 * Generates a result page for one test result
33 *
34 * @param file
35 * file to which generate the HTML
36 * @param result
37 * the result to dump
38 */
39 protected void generate(StructureRegistry<TreeSPath> registry) {
40 StringBuffer buf = new StringBuffer("");
41 buf.append("<html>\n");
42 buf.append("<header>");
43 buf.append("<title>Result #").append(result.getUuid());
44 buf.append("</title>\n");
45 report.addStyles(buf);
46 buf.append("</header>\n");
47
48 buf.append("<body>\n");
49
50 // Header
51 buf.append("<a name=\"top\"/>\n");
52 buf.append("<h1>Result #").append(result.getUuid()).append("</h1>\n");
53 Date closeDate = result.getCloseDate();
54 if (closeDate == null) {
55 buf.append("[Not closed]");
56 } else {
57 buf.append(report.sdf.format(closeDate));
58 }
59
60 // TOC
61 generateToc(buf, registry);
62
63 generatePartsList(buf, registry);
64
65 buf.append("</body>");
66 buf.append("</html>");
67
68 try {
69 FileUtils.writeStringToFile(report.getResultFile(result), buf
70 .toString());
71 } catch (IOException e) {
72 log.error("Could not save result page.", e);
73 }
74 }
75
76 private void generateToc(StringBuffer buf,
77 StructureRegistry<TreeSPath> registry) {
78 buf.append("<h2>Overview</h2>\n");
79 SortedMap<TreeSPath, Integer> toc = new TreeMap<TreeSPath, Integer>();
80 for (TreeSPath path : result.getResultParts().keySet()) {
81 PartSubList subList = (PartSubList) result.getResultParts().get(
82 path);
83 boolean isFailed = false;
84 for (TestResultPart part : subList.getParts()) {
85 if (!part.getStatus().equals(TestStatus.PASSED)) {
86 isFailed = true;
87 break;
88 }
89 }
90 fillToc(toc, path, isFailed);
91 }
92
93 buf.append("<table border=\"0\">\n");
94 for (TreeSPath path : toc.keySet()) {
95 boolean inResult = result.getResultParts().containsKey(path);
96 boolean isFailed = !toc.get(path).equals(TestStatus.PASSED);
97
98 buf.append("<tr><td class=\"").append(
99 isFailed ? "failed" : "passed").append("\">");
100 int depth = path.getDepth();
101 for (int i = 0; i < depth; i++) {
102 buf.append("&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;");
103 }
104
105 if (inResult) {
106 buf.append("<a href=\"#").append(anchor(path)).append(
107 "\" class=\"").append(isFailed ? "failed" : "passed")
108 .append("\"><b>");
109 }
110 if (registry != null) {
111 StructureElement element = registry.getElement(path);
112 if (element != null) {
113 buf.append(element.getLabel());
114 } else {
115 buf.append(path.getName());
116 }
117 }
118 if (inResult) {
119 buf.append("</b></a>");
120 }
121 buf.append("</td></tr>\n");
122 }
123 buf.append("</table>\n");
124 buf.append("<hr/>\n");
125 }
126
127 private void generatePartsList(StringBuffer buf,
128 StructureRegistry<TreeSPath> registry) {
129 for (TreeSPath path : result.getResultParts().keySet()) {
130 buf.append("<p>\n");
131 buf.append("<a name=\"").append(anchor(path)).append("\"></a>");
132 buf.append("<h2>");
133 describedPath(path, registry, buf);
134 buf.append("</h2>");
135
136 PartSubList subList = (PartSubList) result.getResultParts().get(
137 path);
138 buf.append("<table border=0>\n");
139 int displayedIndex = 1;// for display only
140 for (TestResultPart part : subList.getParts()) {
141 SimpleResultPart sPart = (SimpleResultPart) part;
142 buf.append("Related Test Run Id:").append(
143 sPart.getTestRunUuid()).append("<br/>\n");
144 String clss = "";
145 if (sPart.getStatus().equals(TestStatus.PASSED)) {
146 clss = "passed";
147 } else {
148 clss = "failed";
149 }
150 buf.append("<tr>");
151 buf.append("<td><b>").append(displayedIndex)
152 .append("</b></td>");
153 buf.append("<td class=\"").append(clss).append("\">");
154
155 buf.append(sPart.getMessage());
156 if (sPart.getStatus().equals(TestStatus.ERROR)) {
157 buf
158 .append("<p><b>An unexpected error prevented the test to run properly.</b>");
159 buf.append(sPart.getExceptionMessage());
160 buf.append("</p>");
161 }
162 buf.append("</td>");
163 buf.append("</tr>\n");
164
165 displayedIndex++;
166 }
167 buf.append("</table>\n");
168 buf.append("<a class=\"nav\" href=\"#top\">top</a>\n");
169 buf.append("<hr/>\n");
170 }
171 }
172
173 private void fillToc(SortedMap<TreeSPath, Integer> toc, TreeSPath path,
174 boolean isFailed) {
175 if (isFailed) {
176 toc.put(path, TestStatus.FAILED);
177 } else {
178 if (!toc.containsKey(path)) {
179 toc.put(path, TestStatus.PASSED);
180 }
181 }
182
183 if (path.getParent() != null) {
184 fillToc(toc, path.getParent(), isFailed);
185 }
186 }
187
188 private String anchor(TreeSPath path) {
189 return path.getAsUniqueString().replace(path.getSeparator(), '_');
190 }
191
192 private void describedPath(TreeSPath path,
193 StructureRegistry<TreeSPath> registry, StringBuffer buf) {
194 // StringBuffer buf = new StringBuffer("");
195 if (path.getParent() != null) {
196 describedPath(path.getParent(), registry, buf);
197 }
198 String description = path.getName();
199 if (registry != null) {
200 StructureElement element = registry.getElement(path);
201 if (element != null) {
202 description = element.getLabel();
203 }
204 }
205 buf.append('/').append(description);
206 }
207 }