package org.argeo.slc.core.test.tree; import java.io.File; import java.io.FileWriter; import java.io.IOException; import java.util.List; import org.argeo.slc.core.structure.StructureAware; import org.argeo.slc.core.structure.StructureElement; import org.argeo.slc.core.structure.StructurePath; import org.argeo.slc.core.structure.StructureRegistry; import org.argeo.slc.core.structure.tree.TreeSPath; import org.argeo.slc.core.test.SimpleResultPart; import org.argeo.slc.core.test.SimpleTestResult; import org.argeo.slc.core.test.TestStatus; import org.argeo.slc.core.test.TestReport; import org.argeo.slc.core.test.TestResult; import org.argeo.slc.core.test.TestResultPart; import org.argeo.slc.dao.test.TestResultDao; /** * Basic implementation of TestReport generating static HTML pages. If a * TestResultDao is passed, all the datas are dumped, otherwise * only the passed TestResult. */ public class FullHtmlTreeReport implements TestReport, StructureAware { private TestResultDao testResultDao; private File reportDir; private StructureRegistry registry; public void generateTestReport(TestResult testResult) { if (testResultDao == null) { TreeTestResult result = (TreeTestResult) testResult; generateResultPage(getResultFile(result), result); } else { if (reportDir.exists()) { // clean for (File file : reportDir.listFiles()) { file.delete(); } } reportDir.mkdirs(); StringBuffer index = new StringBuffer(""); index .append("
Results
\n\n"); List list = testResultDao.listTestResults(); for (TestResult testRes : list) { TreeTestResult res = (TreeTestResult) testRes; File file = getResultFile(res); index.append("\n"); generateResultPage(file, res); } index.append("
"); index.append(res.getTestResultId()).append("
\n"); try { FileWriter writer = new FileWriter(reportDir.getPath() + File.separator + "index.html"); writer.write(index.toString()); writer.close(); } catch (IOException e) { e.printStackTrace(); } } } /** * Generates a result page for one test result * * @param file * file to which generate the HTML * @param result * the result to dump */ protected void generateResultPage(File file, TreeTestResult result) { StringBuffer buf = new StringBuffer(""); buf.append("\n"); buf.append("
Result #").append(result.getTestResultId()) .append("
\n"); buf.append("\n"); buf.append("

Result #").append(result.getTestResultId()).append( "

\n"); buf.append("\n"); for (TreeSPath path : result.getResultParts().keySet()) { buf.append("\n"); buf.append("\n"); } buf.append("
"); buf.append(path); StructureElement element = registry.getElement(path); if (registry != null) { if (element != null) { buf.append("
"); buf.append(element.getDescription()); buf.append(""); } } buf.append("
"); PartSubList subList = (PartSubList) result.getResultParts().get( path); buf.append("\n"); for (TestResultPart part : subList.getParts()) { SimpleResultPart sPart = (SimpleResultPart) part; String color = "yellow"; if (sPart.getStatus().equals(SimpleResultPart.PASSED)) { color = "green"; } else { color = "red"; } buf.append("\n"); } buf.append("
"); buf.append(sPart.getMessage()); buf.append("
\n"); buf.append("
\n"); buf.append(""); buf.append(""); try { FileWriter writer = new FileWriter(file); writer.write(buf.toString()); writer.close(); } catch (IOException e) { e.printStackTrace(); } } /** * Generates a result file location based on the report dir and the id of * the test result. */ protected File getResultFile(TreeTestResult result) { return new File(reportDir.getPath() + File.separator + result.getTestResultId() + ".html"); } /** Sets the dao to use to extract all data. */ public void setTestResultDao(TestResultDao testResultDao) { this.testResultDao = testResultDao; } /** Sets the directory where to generate all the data. */ public void setReportDir(File reportDir) { this.reportDir = reportDir; } public void notifyCurrentPath(StructureRegistry registry, StructurePath path) { this.registry = registry; } }