1 package org
.argeo
.slc
.core
.test
.tree
.htmlreport
;
4 import java
.io
.FileOutputStream
;
5 import java
.io
.InputStream
;
6 import java
.text
.SimpleDateFormat
;
9 import org
.dbunit
.dataset
.IDataSet
;
10 import org
.dbunit
.dataset
.xml
.FlatXmlDataSet
;
11 import org
.hsqldb
.lib
.FileUtil
;
13 import org
.apache
.commons
.io
.FileUtils
;
14 import org
.apache
.commons
.io
.IOUtils
;
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
;
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>.
32 public class FullHtmlTreeReport
implements TestReport
, StructureAware
{
33 SimpleDateFormat sdf
= new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
35 private TestResultDao testResultDao
;
36 private TreeSRegistryDao treeSRegistryDao
;
37 private File reportDir
;
39 private StructureRegistry localRegistry
;
41 public void generateTestReport(TestResult testResult
) {
43 if (testResultDao
== null) {
44 if (testResult
== null)
45 throw new SlcException(
46 "Cannot generate report without DAO or result instance.");
48 TreeTestResult result
= (TreeTestResult
) testResult
;
49 ResultPage page
= new ResultPage(this, result
);
50 page
.generate(getRegistry(result
));
52 if (reportDir
.exists()) {
54 for (File file
: reportDir
.listFiles()) {
60 resourceToFile("index.html");
61 resourceToFile("style.css");
63 ResultsList index
= new ResultsList(this);
64 List
<TestResult
> list
= testResultDao
.listTestResults();
65 for (TestResult testRes
: list
) {
66 TreeTestResult result
= (TreeTestResult
) testRes
;
68 index
.addTestResult(result
);
69 ResultPage page
= new ResultPage(this, result
);
70 page
.generate(getRegistry(result
));
77 * Generates a result file location based on the report dir and the id of
80 protected File
getResultFile(TreeTestResult result
) {
81 return new File(reportDir
.getPath() + File
.separator
+ "slc-result-"
82 + result
.getTestResultId() + ".html");
85 /** Sets the DAO to use to extract all data. */
86 public void setTestResultDao(TestResultDao testResultDao
) {
87 this.testResultDao
= testResultDao
;
90 /** Sets the tree structure registry DAO. */
91 public void setTreeSRegistryDao(TreeSRegistryDao treeSRegistryDao
) {
92 this.treeSRegistryDao
= treeSRegistryDao
;
95 /** Sets the directory where to generate all the data. */
96 public void setReportDir(File reportDir
) {
97 this.reportDir
= reportDir
;
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
);
106 if (registry
== null) {
107 registry
= localRegistry
;
109 if (registry
== null) {
110 throw new SlcException("No structure registry available");
115 public void notifyCurrentPath(StructureRegistry registry
, StructurePath path
) {
116 this.localRegistry
= registry
;
119 File
getReportDir() {
123 private void resourceToFile(String resourceName
) {
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
);