]> git.argeo.org Git - gpl/argeo-slc.git/blobdiff - org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/htmlreport/FullHtmlTreeReport.java
Rename into Core
[gpl/argeo-slc.git] / org.argeo.slc.core / src / main / java / org / argeo / slc / core / test / tree / htmlreport / FullHtmlTreeReport.java
diff --git a/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/htmlreport/FullHtmlTreeReport.java b/org.argeo.slc.core/src/main/java/org/argeo/slc/core/test/tree/htmlreport/FullHtmlTreeReport.java
new file mode 100644 (file)
index 0000000..4afd8dc
--- /dev/null
@@ -0,0 +1,168 @@
+package org.argeo.slc.core.test.tree.htmlreport;\r
+\r
+import java.io.File;\r
+import java.io.FileOutputStream;\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.text.SimpleDateFormat;\r
+import java.util.Comparator;\r
+import java.util.List;\r
+import java.util.SortedSet;\r
+import java.util.TreeSet;\r
+\r
+import org.apache.commons.io.IOUtils;\r
+import org.apache.commons.logging.Log;\r
+import org.apache.commons.logging.LogFactory;\r
+\r
+import org.argeo.slc.core.SlcException;\r
+import org.argeo.slc.core.structure.StructureAware;\r
+import org.argeo.slc.core.structure.StructurePath;\r
+import org.argeo.slc.core.structure.StructureRegistry;\r
+import org.argeo.slc.core.structure.tree.TreeSPath;\r
+import org.argeo.slc.core.test.TestReport;\r
+import org.argeo.slc.core.test.TestResult;\r
+import org.argeo.slc.core.test.tree.TreeTestResult;\r
+import org.argeo.slc.dao.structure.tree.TreeSRegistryDao;\r
+import org.argeo.slc.dao.test.TestResultDao;\r
+\r
+/**\r
+ * Basic implementation of TestReport generating static HTML pages. If a\r
+ * <code>TestResultDao</code> is passed, all the data is dumped, otherwise\r
+ * only the passed <code>TestResult</code>.\r
+ */\r
+public class FullHtmlTreeReport implements TestReport, StructureAware {\r
+       private static final Log log = LogFactory.getLog(FullHtmlTreeReport.class);\r
+       SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");\r
+\r
+       private TestResultDao testResultDao;\r
+       private TreeSRegistryDao treeSRegistryDao;\r
+       private File reportDir;\r
+\r
+       private StructureRegistry localRegistry;\r
+\r
+       public void generateTestReport(TestResult testResult) {\r
+\r
+               if (testResultDao == null) {\r
+                       if (testResult == null)\r
+                               throw new SlcException(\r
+                                               "Cannot generate report without DAO or result instance.");\r
+\r
+                       TreeTestResult result = (TreeTestResult) testResult;\r
+                       ResultPage page = new ResultPage(this, result);\r
+                       page.generate(getRegistry(result));\r
+               } else {\r
+                       if (reportDir.exists()) {\r
+                               // clean\r
+                               for (File file : reportDir.listFiles()) {\r
+                                       file.delete();\r
+                               }\r
+                       }\r
+                       reportDir.mkdirs();\r
+\r
+                       resourceToFile("index.html");\r
+\r
+                       ResultsList index = new ResultsList(this);\r
+                       List<TestResult> list = testResultDao.listTestResults();\r
+                       SortedSet<TestResult> sortedSet = new TreeSet<TestResult>(\r
+                                       new Comparator<TestResult>() {\r
+\r
+                                               public int compare(TestResult o1, TestResult o2) {\r
+                                                       if (o1.getCloseDate() == null\r
+                                                                       || o2.getCloseDate() == null)\r
+                                                               return 0;\r
+                                                       // inverse date order (last first)\r
+                                                       return o2.getCloseDate().compareTo(\r
+                                                                       o1.getCloseDate());\r
+                                               }\r
+\r
+                                       });\r
+                       sortedSet.addAll(list);\r
+                       for (TestResult testRes : sortedSet) {\r
+                               TreeTestResult result = (TreeTestResult) testRes;\r
+\r
+                               index.addTestResult(result);\r
+                               ResultPage page = new ResultPage(this, result);\r
+                               page.generate(getRegistry(result));\r
+                       }\r
+                       index.close();\r
+               }\r
+               log.info("Generated HTML test result report to "+reportDir);\r
+       }\r
+\r
+       /**\r
+        * Generates a result file location based on the report dir and the id of\r
+        * the test result.\r
+        */\r
+       protected File getResultFile(TreeTestResult result) {\r
+               return new File(reportDir.getPath() + File.separator + "slc-result-"\r
+                               + result.getTestResultId() + ".html");\r
+       }\r
+\r
+       /** Sets the DAO to use to extract all data. */\r
+       public void setTestResultDao(TestResultDao testResultDao) {\r
+               this.testResultDao = testResultDao;\r
+       }\r
+\r
+       /** Sets the tree structure registry DAO. */\r
+       public void setTreeSRegistryDao(TreeSRegistryDao treeSRegistryDao) {\r
+               this.treeSRegistryDao = treeSRegistryDao;\r
+       }\r
+\r
+       /** Sets the directory where to generate all the data. */\r
+       public void setReportDir(File reportDir) {\r
+               this.reportDir = reportDir;\r
+       }\r
+\r
+       private StructureRegistry getRegistry(TreeTestResult result) {\r
+               StructureRegistry registry = null;\r
+               if (treeSRegistryDao != null) {\r
+                       TreeSPath path = result.getResultParts().firstKey();\r
+                       registry = treeSRegistryDao.getTreeSRegistry(path);\r
+               }\r
+               if (registry == null) {\r
+                       registry = localRegistry;\r
+               }\r
+               if (registry == null) {\r
+                       throw new SlcException("No structure registry available");\r
+               }\r
+               return registry;\r
+       }\r
+\r
+       public void notifyCurrentPath(StructureRegistry registry, StructurePath path) {\r
+               this.localRegistry = registry;\r
+       }\r
+\r
+       File getReportDir() {\r
+               return reportDir;\r
+       }\r
+\r
+       void addStyles(StringBuffer buf) {\r
+               try {\r
+                       buf.append("<style type=\"text/css\">\n");\r
+                       InputStream in = FullHtmlTreeReport.class\r
+                                       .getResourceAsStream("style.css");\r
+                       String styles = IOUtils.toString(in);\r
+                       IOUtils.closeQuietly(in);\r
+                       buf.append(styles);\r
+                       buf.append("\n</style>\n");\r
+               } catch (IOException e) {\r
+                       throw new SlcException("Cannot load styles", e);\r
+               }\r
+       }\r
+\r
+       private void resourceToFile(String resourceName) {\r
+               try {\r
+                       File file = new File(getReportDir() + File.separator + resourceName);\r
+                       InputStream in = FullHtmlTreeReport.class\r
+                                       .getResourceAsStream(resourceName);\r
+                       FileOutputStream out = new FileOutputStream(file);\r
+                       IOUtils.copy(in, out);\r
+                       IOUtils.closeQuietly(in);\r
+                       IOUtils.closeQuietly(out);\r
+               } catch (Exception e) {\r
+                       throw new SlcException("Cannot load resource", e);\r
+               }\r
+\r
+       }\r
+\r
+}\r