package org.argeo.slc.core.test.tree.htmlreport; import java.io.IOException; import java.io.PrintWriter; import java.io.StringWriter; import java.util.Date; import java.util.SortedMap; import java.util.TreeMap; import org.apache.commons.io.FileUtils; import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.slc.core.structure.StructureElement; 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.TestResultPart; import org.argeo.slc.core.test.TestStatus; import org.argeo.slc.core.test.tree.PartSubList; import org.argeo.slc.core.test.tree.TreeTestResult; class ResultPage { private final static Log log = LogFactory.getLog(ResultPage.class); private final FullHtmlTreeReport report; private final TreeTestResult result; ResultPage(FullHtmlTreeReport report, TreeTestResult result) { this.report = report; this.result = result; } /** * Generates a result page for one test result * * @param file * file to which generate the HTML * @param result * the result to dump */ protected void generate(StructureRegistry registry) { StringBuffer buf = new StringBuffer(""); buf.append("\n"); buf.append("
"); buf.append("Result #").append(result.getUuid()); buf.append("\n"); report.addStyles(buf); buf.append("
\n"); buf.append("\n"); // Header buf.append("\n"); buf.append("

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

\n"); Date closeDate = result.getCloseDate(); if (closeDate == null) { buf.append("[Not closed]"); } else { buf.append(report.sdf.format(closeDate)); } // TOC generateToc(buf, registry); generatePartsList(buf, registry); buf.append(""); buf.append(""); try { FileUtils.writeStringToFile(report.getResultFile(result), buf .toString()); } catch (IOException e) { log.error("Could not save result page.", e); } } private void generateToc(StringBuffer buf, StructureRegistry registry) { buf.append("

Overview

\n"); SortedMap toc = new TreeMap(); for (TreeSPath path : result.getResultParts().keySet()) { PartSubList subList = (PartSubList) result.getResultParts().get( path); boolean isFailed = false; for (TestResultPart part : subList.getParts()) { if (!part.getStatus().equals(TestStatus.PASSED)) { isFailed = true; break; } } fillToc(toc, path, isFailed); } buf.append("\n"); for (TreeSPath path : toc.keySet()) { boolean inResult = result.getResultParts().containsKey(path); boolean isFailed = !toc.get(path).equals(TestStatus.PASSED); buf.append("\n"); } buf.append("
"); int depth = path.getDepth(); for (int i = 0; i < depth; i++) { buf.append("      "); } if (inResult) { buf.append(""); } if (registry != null) { StructureElement element = registry.getElement(path); if (element != null) { buf.append(element.getLabel()); } else { buf.append(path.getName()); } } if (inResult) { buf.append(""); } buf.append("
\n"); buf.append("
\n"); } private void generatePartsList(StringBuffer buf, StructureRegistry registry) { for (TreeSPath path : result.getResultParts().keySet()) { buf.append("

\n"); buf.append(""); buf.append("

"); describedPath(path, registry, buf); buf.append("

"); PartSubList subList = (PartSubList) result.getResultParts().get( path); buf.append("\n"); int displayedIndex = 1;// for display only for (TestResultPart part : subList.getParts()) { SimpleResultPart sPart = (SimpleResultPart) part; buf.append("Related Test Run Id:").append( sPart.getTestRunUuid()).append("
\n"); String clss = ""; if (sPart.getStatus().equals(TestStatus.PASSED)) { clss = "passed"; } else { clss = "failed"; } buf.append(""); buf.append(""); buf.append(""); buf.append("\n"); displayedIndex++; } buf.append("
").append(displayedIndex) .append(""); buf.append(sPart.getMessage()); if (sPart.getStatus().equals(TestStatus.ERROR)) { buf .append("

An unexpected error prevented the test to run properly."); buf.append(sPart.getExceptionMessage()); buf.append("

"); } buf.append("
\n"); buf.append("top\n"); buf.append("
\n"); } } private void fillToc(SortedMap toc, TreeSPath path, boolean isFailed) { if (isFailed) { toc.put(path, TestStatus.FAILED); } else { if (!toc.containsKey(path)) { toc.put(path, TestStatus.PASSED); } } if (path.getParent() != null) { fillToc(toc, path.getParent(), isFailed); } } private String anchor(TreeSPath path) { return path.getAsUniqueString().replace(path.getSeparator(), '_'); } private void describedPath(TreeSPath path, StructureRegistry registry, StringBuffer buf) { // StringBuffer buf = new StringBuffer(""); if (path.getParent() != null) { describedPath(path.getParent(), registry, buf); } String description = path.getName(); if (registry != null) { StructureElement element = registry.getElement(path); if (element != null) { description = element.getLabel(); } } buf.append('/').append(description); } }