1 package org
.argeo
.slc
.core
.test
.tree
.htmlreport
;
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
;
10 import java
.util
.SortedSet
;
11 import java
.util
.TreeSet
;
13 import org
.apache
.commons
.io
.IOUtils
;
14 import org
.apache
.commons
.logging
.Log
;
15 import org
.apache
.commons
.logging
.LogFactory
;
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
;
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>.
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");
37 private TestResultDao testResultDao
;
38 private TreeSRegistryDao treeSRegistryDao
;
39 private File reportDir
;
41 private StructureRegistry localRegistry
;
43 public void generateTestReport(TestResult testResult
) {
45 if (testResultDao
== null) {
46 if (testResult
== null)
47 throw new SlcException(
48 "Cannot generate report without DAO or result instance.");
50 TreeTestResult result
= (TreeTestResult
) testResult
;
51 ResultPage page
= new ResultPage(this, result
);
52 page
.generate(getRegistry(result
));
54 if (reportDir
.exists()) {
56 for (File file
: reportDir
.listFiles()) {
62 resourceToFile("index.html");
64 ResultsList index
= new ResultsList(this);
65 List
<TestResult
> list
= testResultDao
.listTestResults();
66 SortedSet
<TestResult
> sortedSet
= new TreeSet
<TestResult
>(
67 new Comparator
<TestResult
>() {
69 public int compare(TestResult o1
, TestResult o2
) {
70 if (o1
.getCloseDate() == null
71 || o2
.getCloseDate() == null)
73 // inverse date order (last first)
74 return o2
.getCloseDate().compareTo(
79 sortedSet
.addAll(list
);
80 for (TestResult testRes
: sortedSet
) {
81 TreeTestResult result
= (TreeTestResult
) testRes
;
83 index
.addTestResult(result
);
84 ResultPage page
= new ResultPage(this, result
);
85 page
.generate(getRegistry(result
));
89 log
.info("Generated HTML test result report to "+reportDir
);
93 * Generates a result file location based on the report dir and the id of
96 protected File
getResultFile(TreeTestResult result
) {
97 return new File(reportDir
.getPath() + File
.separator
+ "slc-result-"
98 + result
.getUuid() + ".html");
101 /** Sets the DAO to use to extract all data. */
102 public void setTestResultDao(TestResultDao testResultDao
) {
103 this.testResultDao
= testResultDao
;
106 /** Sets the tree structure registry DAO. */
107 public void setTreeSRegistryDao(TreeSRegistryDao treeSRegistryDao
) {
108 this.treeSRegistryDao
= treeSRegistryDao
;
111 /** Sets the directory where to generate all the data. */
112 public void setReportDir(File reportDir
) {
113 this.reportDir
= reportDir
;
116 private StructureRegistry
getRegistry(TreeTestResult result
) {
117 StructureRegistry registry
= null;
118 if (treeSRegistryDao
!= null) {
119 TreeSPath path
= result
.getResultParts().firstKey();
120 registry
= treeSRegistryDao
.getActiveTreeSRegistry();
122 if (registry
== null) {
123 registry
= localRegistry
;
125 if (registry
== null) {
126 throw new SlcException("No structure registry available");
131 public void notifyCurrentPath(StructureRegistry registry
, StructurePath path
) {
132 this.localRegistry
= registry
;
135 File
getReportDir() {
139 void addStyles(StringBuffer buf
) {
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
);
147 buf
.append("\n</style>\n");
148 } catch (IOException e
) {
149 throw new SlcException("Cannot load styles", e
);
153 private void resourceToFile(String resourceName
) {
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
);