]> git.argeo.org Git - gpl/argeo-slc.git/blob - org.argeo.slc/src/main/java/org/argeo/slc/core/test/tree/htmlreport/FullHtmlTreeReport.java
Improve reporting
[gpl/argeo-slc.git] / org.argeo.slc / 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.InputStream;
6 import java.text.SimpleDateFormat;
7 import java.util.List;
8
9 import org.dbunit.dataset.IDataSet;
10 import org.dbunit.dataset.xml.FlatXmlDataSet;
11 import org.hsqldb.lib.FileUtil;
12
13 import org.apache.commons.io.FileUtils;
14 import org.apache.commons.io.IOUtils;
15
16 import org.argeo.slc.core.SlcException;
17 import org.argeo.slc.core.structure.StructureAware;
18 import org.argeo.slc.core.structure.StructurePath;
19 import org.argeo.slc.core.structure.StructureRegistry;
20 import org.argeo.slc.core.structure.tree.TreeSPath;
21 import org.argeo.slc.core.test.TestReport;
22 import org.argeo.slc.core.test.TestResult;
23 import org.argeo.slc.core.test.tree.TreeTestResult;
24 import org.argeo.slc.dao.structure.tree.TreeSRegistryDao;
25 import org.argeo.slc.dao.test.TestResultDao;
26
27 /**
28 * Basic implementation of TestReport generating static HTML pages. If a
29 * <code>TestResultDao</code> is passed, all the data is dumped, otherwise
30 * only the passed <code>TestResult</code>.
31 */
32 public class FullHtmlTreeReport implements TestReport, StructureAware {
33 SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
34
35 private TestResultDao testResultDao;
36 private TreeSRegistryDao treeSRegistryDao;
37 private File reportDir;
38
39 private StructureRegistry localRegistry;
40
41 public void generateTestReport(TestResult testResult) {
42
43 if (testResultDao == null) {
44 if (testResult == null)
45 throw new SlcException(
46 "Cannot generate report without DAO or result instance.");
47
48 TreeTestResult result = (TreeTestResult) testResult;
49 ResultPage page = new ResultPage(this, result);
50 page.generate(getRegistry(result));
51 } else {
52 if (reportDir.exists()) {
53 // clean
54 for (File file : reportDir.listFiles()) {
55 file.delete();
56 }
57 }
58 reportDir.mkdirs();
59
60 resourceToFile("index.html");
61 resourceToFile("style.css");
62
63 ResultsList index = new ResultsList(this);
64 List<TestResult> list = testResultDao.listTestResults();
65 for (TestResult testRes : list) {
66 TreeTestResult result = (TreeTestResult) testRes;
67
68 index.addTestResult(result);
69 ResultPage page = new ResultPage(this, result);
70 page.generate(getRegistry(result));
71 }
72 index.close();
73 }
74 }
75
76 /**
77 * Generates a result file location based on the report dir and the id of
78 * the test result.
79 */
80 protected File getResultFile(TreeTestResult result) {
81 return new File(reportDir.getPath() + File.separator + "slc-result-"
82 + result.getTestResultId() + ".html");
83 }
84
85 /** Sets the DAO to use to extract all data. */
86 public void setTestResultDao(TestResultDao testResultDao) {
87 this.testResultDao = testResultDao;
88 }
89
90 /** Sets the tree structure registry DAO. */
91 public void setTreeSRegistryDao(TreeSRegistryDao treeSRegistryDao) {
92 this.treeSRegistryDao = treeSRegistryDao;
93 }
94
95 /** Sets the directory where to generate all the data. */
96 public void setReportDir(File reportDir) {
97 this.reportDir = reportDir;
98 }
99
100 private StructureRegistry getRegistry(TreeTestResult result) {
101 StructureRegistry registry = null;
102 if (treeSRegistryDao != null) {
103 TreeSPath path = result.getResultParts().firstKey();
104 registry = treeSRegistryDao.getTreeSRegistry(path);
105 }
106 if (registry == null) {
107 registry = localRegistry;
108 }
109 if (registry == null) {
110 throw new SlcException("No structure registry available");
111 }
112 return registry;
113 }
114
115 public void notifyCurrentPath(StructureRegistry registry, StructurePath path) {
116 this.localRegistry = registry;
117 }
118
119 File getReportDir() {
120 return reportDir;
121 }
122
123 private void resourceToFile(String resourceName) {
124 try {
125 File file = new File(getReportDir() + File.separator + resourceName);
126 InputStream in = FullHtmlTreeReport.class
127 .getResourceAsStream(resourceName);
128 FileOutputStream out = new FileOutputStream(file);
129 IOUtils.copy(in, out);
130 IOUtils.closeQuietly(in);
131 IOUtils.closeQuietly(out);
132 } catch (Exception e) {
133 throw new SlcException("Cannot load resource", e);
134 }
135
136 }
137
138 }