]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/htmlreport/FullHtmlTreeReport.java
Inhtroduce basedon context
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / tree / htmlreport / FullHtmlTreeReport.java
1 package org.argeo.slc.core.test.tree.htmlreport;
2
3 import java.io.File;
4 import java.io.FileOutputStream;
5 import java.io.IOException;
6 import java.io.InputStream;
7 import java.text.SimpleDateFormat;
8 import java.util.Comparator;
9 import java.util.List;
10 import java.util.SortedSet;
11 import java.util.TreeSet;
12
13 import org.apache.commons.io.IOUtils;
14 import org.apache.commons.logging.Log;
15 import org.apache.commons.logging.LogFactory;
16
17 import org.argeo.slc.core.SlcException;
18 import org.argeo.slc.core.structure.StructureAware;
19 import org.argeo.slc.core.structure.StructurePath;
20 import org.argeo.slc.core.structure.StructureRegistry;
21 import org.argeo.slc.core.structure.tree.TreeSPath;
22 import org.argeo.slc.core.test.TestReport;
23 import org.argeo.slc.core.test.TestResult;
24 import org.argeo.slc.core.test.tree.TreeTestResult;
25 import org.argeo.slc.dao.structure.tree.TreeSRegistryDao;
26 import org.argeo.slc.dao.test.TestResultDao;
27
28 /**
29 * Basic implementation of TestReport generating static HTML pages. If a
30 * <code>TestResultDao</code> is passed, all the data is dumped, otherwise
31 * only the passed <code>TestResult</code>.
32 */
33 public class FullHtmlTreeReport implements TestReport, StructureAware {
34 private static final Log log = LogFactory.getLog(FullHtmlTreeReport.class);
35 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
36
37 private TestResultDao testResultDao;
38 private TreeSRegistryDao treeSRegistryDao;
39 private File reportDir;
40
41 private StructureRegistry localRegistry;
42
43 public void generateTestReport(TestResult testResult) {
44
45 if (testResultDao == null) {
46 if (testResult == null)
47 throw new SlcException(
48 "Cannot generate report without DAO or result instance.");
49
50 TreeTestResult result = (TreeTestResult) testResult;
51 ResultPage page = new ResultPage(this, result);
52 page.generate(getRegistry(result));
53 } else {
54 if (reportDir.exists()) {
55 // clean
56 for (File file : reportDir.listFiles()) {
57 file.delete();
58 }
59 }
60 reportDir.mkdirs();
61
62 resourceToFile("index.html");
63
64 ResultsList index = new ResultsList(this);
65 List<TestResult> list = testResultDao.listTestResults();
66 SortedSet<TestResult> sortedSet = new TreeSet<TestResult>(
67 new Comparator<TestResult>() {
68
69 public int compare(TestResult o1, TestResult o2) {
70 if (o1.getCloseDate() == null
71 || o2.getCloseDate() == null)
72 return 0;
73 // inverse date order (last first)
74 return o2.getCloseDate().compareTo(
75 o1.getCloseDate());
76 }
77
78 });
79 sortedSet.addAll(list);
80 for (TestResult testRes : sortedSet) {
81 TreeTestResult result = (TreeTestResult) testRes;
82
83 index.addTestResult(result);
84 ResultPage page = new ResultPage(this, result);
85 page.generate(getRegistry(result));
86 }
87 index.close();
88 }
89 log.info("Generated HTML test result report to "+reportDir);
90 }
91
92 /**
93 * Generates a result file location based on the report dir and the id of
94 * the test result.
95 */
96 protected File getResultFile(TreeTestResult result) {
97 return new File(reportDir.getPath() + File.separator + "slc-result-"
98 + result.getTestResultId() + ".html");
99 }
100
101 /** Sets the DAO to use to extract all data. */
102 public void setTestResultDao(TestResultDao testResultDao) {
103 this.testResultDao = testResultDao;
104 }
105
106 /** Sets the tree structure registry DAO. */
107 public void setTreeSRegistryDao(TreeSRegistryDao treeSRegistryDao) {
108 this.treeSRegistryDao = treeSRegistryDao;
109 }
110
111 /** Sets the directory where to generate all the data. */
112 public void setReportDir(File reportDir) {
113 this.reportDir = reportDir;
114 }
115
116 private StructureRegistry getRegistry(TreeTestResult result) {
117 StructureRegistry registry = null;
118 if (treeSRegistryDao != null) {
119 TreeSPath path = result.getResultParts().firstKey();
120 registry = treeSRegistryDao.getActiveTreeSRegistry();
121 }
122 if (registry == null) {
123 registry = localRegistry;
124 }
125 if (registry == null) {
126 throw new SlcException("No structure registry available");
127 }
128 return registry;
129 }
130
131 public void notifyCurrentPath(StructureRegistry registry, StructurePath path) {
132 this.localRegistry = registry;
133 }
134
135 File getReportDir() {
136 return reportDir;
137 }
138
139 void addStyles(StringBuffer buf) {
140 try {
141 buf.append("<style type=\"text/css\">\n");
142 InputStream in = FullHtmlTreeReport.class
143 .getResourceAsStream("style.css");
144 String styles = IOUtils.toString(in);
145 IOUtils.closeQuietly(in);
146 buf.append(styles);
147 buf.append("\n</style>\n");
148 } catch (IOException e) {
149 throw new SlcException("Cannot load styles", e);
150 }
151 }
152
153 private void resourceToFile(String resourceName) {
154 try {
155 File file = new File(getReportDir() + File.separator + resourceName);
156 InputStream in = FullHtmlTreeReport.class
157 .getResourceAsStream(resourceName);
158 FileOutputStream out = new FileOutputStream(file);
159 IOUtils.copy(in, out);
160 IOUtils.closeQuietly(in);
161 IOUtils.closeQuietly(out);
162 } catch (Exception e) {
163 throw new SlcException("Cannot load resource", e);
164 }
165
166 }
167
168 }