view src/org/thermostat/qa2/reporter/Generator.java @ 184:40990362e151

Improved reporter - for compatibility report thermostat versions are now diplayed in heading and title of pages. - fixed bug in replacePatterns method in CommonUtilities class, when there is more then one pattern on single line.
author Zdenek Zambersky <zzambers@redhat.com>
date Fri, 15 May 2015 19:09:02 +0200
parents 67f1ec14b9cd
children 877b403e8344
line wrap: on
line source

/*
 ThermostatQA - test framework for Thermostat Monitoring Tool

 Copyright 2015 Red Hat, Inc.

 This file is part of ThermostatQA

 ThermostatQA is distributed under the GNU General Public License,
 version 2 or any later version (with a special exception described
 below, commonly known as the "Classpath Exception").

 A copy of GNU General Public License (GPL) is included in this
 distribution, in the file COPYING.

 Linking ThermostatQA code with other modules is making a combined work
 based on ThermostatQA.  Thus, the terms and conditions of the GPL
 cover the whole combination.

 As a special exception, the copyright holders of ThermostatQA give you
 permission to link this code with independent modules to produce an
 executable, regardless of the license terms of these independent
 modules, and to copy and distribute the resulting executable under
 terms of your choice, provided that you also meet, for each linked
 independent module, the terms and conditions of the license of that
 module.  An independent module is a module which is not derived from
 or based on ThermostatQA code.  If you modify ThermostatQA, you may
 extend this exception to your version of the software, but you are
 not obligated to do so.  If you do not wish to do so, delete this
 exception statement from your version.
 */
package org.thermostat.qa2.reporter;

import org.thermostat.qa2.reporter.result.TestMethodResult;
import org.thermostat.qa2.reporter.result.TestClassResult;
import org.thermostat.qa2.reporter.result.TestRunResult;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.thermostat.qa2.framework.TestResult;
import org.thermostat.qa2.framework.utils.CommonUtilities;
import org.thermostat.qa2.framework.utils.FileUtilities;

/**
 *
 * @author Zdeněk Žamberský
 */
public class Generator {
    
    public static final String testsNamePattern = "${TESTS_NAME}";

    public static final String resultsUrlPattern = "${RESULTS_URL}";
    public static final String datePattern = "${DATE}";
    public static final String testCountPattern = "${TEST_COUNT}";

    public static final String passedPattern = "${PASSED}";
    public static final String failedPattern = "${FAILED}";
    public static final String errorPattern = "${ERROR}";
    public static final String ignoredPattern = "${IGNORED}";

    public static final String osNamePattern = "${OS_NAME}";
    public static final String osVersionPattern = "${OS_VER}";
    public static final String osArchPattern = "${OS_ARCH}";

    public static final String javaVersionPattern = "${JAVA_VERSION}";
    public static final String vmNamePattern = "${VM_NAME}";
    public static final String vmVersionPattern = "${VM_VERSION}";

    public static final String sumaryPattern = "${SUMMARY}";
    public static final String resultsPattern = "${RESULTS}";
    public static final String tableDataPattern = "${TABLE_DATA}";

    public static final String graphDataNonePattern = "${GRAPH_DATA_NONE}";
    public static final String graphDataPassedPattern = "${GRAPH_DATA_PASSED}";
    public static final String graphDataFailedPattern = "${GRAPH_DATA_FAILED}";
    public static final String graphDataErrorPattern = "${GRAPH_DATA_ERROR}";
    public static final String graphDataIgnoredPattern = "${GRAPH_DATA_IGNORED}";

    public static final String indexPageTemplate = "templates/index.html";
    public static final String logPageTemplate = "templates/log.html";
    public static final String historyPageTemplate = "templates/hist.html";
    public static final String graphPageTemplate = "templates/graph.html";

    ////////////////////
    //// INDEX PAGE ////
    ////////////////////
    public static void generateIndexPage(List<String> output, TestRunResult run, String testsName) throws IOException {
        Map<String, List<String>> replacements = new HashMap();
        CommonUtilities.addReplacementToMap(replacements, Generator.testsNamePattern, testsName);
        CommonUtilities.addReplacementToMap(replacements, Generator.resultsUrlPattern, "log_" + run.date + ".xhtml");
        CommonUtilities.addReplacementToMap(replacements, Generator.datePattern, run.date);
        CommonUtilities.addReplacementToMap(replacements, Generator.passedPattern, Integer.toString(run.passedCount));
        CommonUtilities.addReplacementToMap(replacements, Generator.failedPattern, Integer.toString(run.failedCount));
        CommonUtilities.addReplacementToMap(replacements, Generator.errorPattern, Integer.toString(run.errorCount));
        CommonUtilities.addReplacementToMap(replacements, Generator.ignoredPattern, Integer.toString(run.ignoredCount));
        CommonUtilities.addReplacementToMap(replacements, Generator.osNamePattern, System.getProperty("os.name"));
        CommonUtilities.addReplacementToMap(replacements, Generator.osVersionPattern, System.getProperty("os.version"));
        CommonUtilities.addReplacementToMap(replacements, Generator.osArchPattern, System.getProperty("os.arch"));
        CommonUtilities.addReplacementToMap(replacements, Generator.javaVersionPattern, System.getProperty("java.version"));
        CommonUtilities.addReplacementToMap(replacements, Generator.vmNamePattern, System.getProperty("java.vm.name"));
        CommonUtilities.addReplacementToMap(replacements, Generator.vmVersionPattern, System.getProperty("java.vm.version"));
        List<String> template = FileUtilities.getLineListFromFile(indexPageTemplate);
        CommonUtilities.replacePatterns(template, output, replacements);
    }

    //////////////////
    //// LOG PAGE ////
    //////////////////
    public static void generateLogPage(List<String> output, TestRunResult run, String testsName) throws IOException {
        List<String> dateText = new ArrayList();
        dateText.add(run.date);
        List<String> summaryText = new ArrayList();
        generateLogSummary(summaryText, run);
        List<String> resultsText = new ArrayList();
        generateLogTable(resultsText, run);
        Map<String, List<String>> replacements = new HashMap();
        CommonUtilities.addReplacementToMap(replacements, Generator.testsNamePattern, testsName);
        replacements.put(datePattern, dateText);
        replacements.put(sumaryPattern, summaryText);
        replacements.put(resultsPattern, resultsText);
        List<String> template = FileUtilities.getLineListFromFile(logPageTemplate);
        CommonUtilities.replacePatterns(template, output, replacements);
    }

    public static void generateLogSummary(List<String> output, TestRunResult run) {
        for (TestClassResult test : run.results) {
            output.add("<tr><td><a href='#" + test.name + "'>" + test.name + "</a></td></tr>");
        }
    }

    public static void generateLogTable(List<String> output, TestRunResult run) {
        for (TestClassResult test : run.results) {
            String testName = test.name;
            output.add("<tr id='" + testName + "' ><td class='table-header' colspan='2'>" + testName + "</td></tr>");
            if (test.beforeMethods.size() > 0 && (test.failedCount + test.errorCount > 0)) {
                for (TestMethodResult method : test.beforeMethods) {
                    generateLogTableRow(output, test, method, true);
                }
                output.add("<tr><td colspan='2'>&nbsp;</td></tr>");
            }
            for (TestMethodResult method : test.methods) {
                generateLogTableRow(output, test, method, false);
            }
            output.add("<tr><td class='error-text' colspan='2'>SUMMARY: " + testName + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;total: " + test.methods.size() + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;passed: " + test.passedCount + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;failed: " + test.failedCount + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;error: " + test.errorCount + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;not applicable: " + test.ignoredCount + "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;duration: " + test.duration + "</td></tr>");
            output.add("<tr><td colspan='2'>&nbsp;</td></tr>");
        }
    }

    public static void generateLogTableRow(List<String> output, TestClassResult klass, TestMethodResult method, boolean beforeMethod) {
        String testName = klass.name;
        TestResult result = method.result;
        String resultString = result.toString();
        String resultLine = "<tr id='" + testName + "." + method.name + "' >";
        resultLine += result == TestResult.PASSED ? "<td>" : "<td class='" + resultString.toLowerCase() + "-header'" + ">";
        if (beforeMethod) {
            resultLine += "<span class='test-name-postfix'>@Before </span>";
        }
        resultLine += "<span class='test-name-prefix'>" + testName + ".</span><span class='test-name-postfix'>" + method.name + "</span>";
        if (method.reason != null) {
            resultLine += "<span class='test-name-postfix'>:&nbsp;&nbsp;</span><small class='reason'><![CDATA[" + method.reason + "]]></small>";
        }
        String shownResultString = beforeMethod ? result == TestResult.PASSED ? "NO ERROR" : "ERROR" : resultString;
        resultLine += "</td><td class='" + resultString.toLowerCase() + "-text'>" + shownResultString + "</td></tr>";
        output.add(resultLine);
        List<String> stackTrace = method.stackTrace;
        if (result != TestResult.PASSED || beforeMethod) {
            output.add("<tr><td>");
            output.add("<input id=\"log-toggle-" + testName + "-" + method.name + "\" type=\"checkbox\" class=\"log-toggle\" />");
            output.add("<label for=\"log-toggle-" + testName + "-" + method.name + "\"></label><br />");
            output.add("<div>");
            if (stackTrace != null && stackTrace.size() > 0) {
                output.add("<h5>stack trace:</h5>");
                output.add("<pre class='stack-trace'><![CDATA[" + (stackTrace.size() > 0 ? stackTrace.get(0) : ""));
                for (int i = 1; i < stackTrace.size(); ++i) {
                    output.add(stackTrace.get(i));
                }
                output.add("]]></pre>");
            }
            output.add("<h5>log:</h5>");
            List<String> log = method.getLog();
            output.add("<pre class='stack-trace'><![CDATA[" + ((log != null && log.size() > 0) ? log.get(0) : ""));
            for (int i = 1; i < log.size(); ++i) {
                output.add(log.get(i));
            }
            output.add("]]></pre>");
            output.add("</div>");
            output.add("</td></tr>");
        }
    }

    //////////////////////
    //// HISTORY PAGE ////
    //////////////////////
    public static void generateHistoryPage(List<String> output, List<TestRunResult> runs, String testsName, String resultsType, int count, boolean onlyFail) throws IOException {
        List<String> resultTypeText = new ArrayList();
        resultTypeText.add(resultsType);
        List<String> historyTableText = new ArrayList();
        generateHistoryTable(historyTableText, runs, count, onlyFail);
        Map<String, List<String>> replacements = new HashMap();
        CommonUtilities.addReplacementToMap(replacements, Generator.testsNamePattern, testsName);
        replacements.put(resultsPattern, resultTypeText);
        replacements.put(tableDataPattern, historyTableText);
        List<String> template = FileUtilities.getLineListFromFile(historyPageTemplate);
        CommonUtilities.replacePatterns(template, output, replacements);
    }

    public static void generateHistoryTable(List<String> output, List<TestRunResult> runs, int count, boolean onlyFail) {
        int size = runs.size();
        int start = Math.max(0, size - count);
        TestRunResult lastRun = runs.get(size - 1);
        output.add("<tr><td class='group-id'>Test name/Date:</td>");
        for (int i = start; i < size; ++i) {
            String name = runs.get(i).date;
            int index = name.indexOf("-");
            String year = name.substring(0, index);
            String date = name.substring(index + 1, name.length());
            output.add("<td>" + year + "<br />" + date + "</td>");
        }
        output.add("</tr>");
        List<TestClassResult> lastResults = lastRun.results;
        List<TestClassResult> testHistory = new ArrayList();
        for (TestClassResult result : lastResults) {
            String name = result.name;
            for (int i = start; i < size; ++i) {
                Map<String, TestClassResult> testMap = runs.get(i).resultsMap;
                testHistory.add(testMap.get(name));
            }
            generateHistoryTableRow(output, runs, testHistory, count, onlyFail);
            testHistory.clear();
        }
    }

    public static void generateHistoryTableRow(List<String> output, List<TestRunResult> runs, List<TestClassResult> testHistory, int count, boolean onlyFail) {
        int size = testHistory.size();
        TestRunResult lastRun = runs.get(size - 1);
        TestClassResult current = testHistory.get(size - 1);
        List<TestMethodResult> allMethods = new ArrayList();
        for (TestMethodResult method : current.methods) {
            String name = method.getName();
            boolean fail = false;
            for (TestClassResult previous : testHistory) {
                if (previous != null) {
                    TestMethodResult previousMethod = previous.getMethod(name);
                    if (previousMethod != null && previousMethod.getResult() != TestResult.PASSED) {
                        fail = true;
                    }
                    allMethods.add(previousMethod);
                } else {
                    allMethods.add(null);
                }
            }
            if (!onlyFail || fail) {
                String lastTestFile = "log_" + lastRun.date + ".xhtml";
                String methodref = "#" + current.name + "." + method.name;
                output.add("<tr><td>" + current.name + ".<a href='" + lastTestFile + methodref + "'>" + method.getName() + "</a></td>");
                int i = 0;
                for (TestMethodResult printedMethod : allMethods) {
                    if (printedMethod != null) {
                        TestResult result = printedMethod.getResult();
                        String resultString = result.toString().toLowerCase();
                        String printedResultString = result == TestResult.PASSED ? "ok" : result == TestResult.FAILED ? "fail" : resultString;
                        output.add("<td class='" + resultString + "-header'><a href='" + "log_" + runs.get(i).date + ".xhtml" + methodref + "'>" + printedResultString + "</a></td>");
                    } else {
                        output.add("<td>n/a</td>");
                    }
                    ++i;
                }
                output.add("</tr>");
            }
            allMethods.clear();
        }
    }

    ////////////////////
    //// GRAPH PAGE ////
    ////////////////////
    public static void generateGraphPage(List<String> output, List<TestRunResult> runs, String testsName, int count) throws IOException {
        List<String> testCountText = new ArrayList();
        testCountText.add(Integer.toString(count));
        List<String> passedDataText = new ArrayList();
        generateGraphData(passedDataText, runs, TestResult.PASSED, count);
        List<String> failedDataText = new ArrayList();
        generateGraphData(failedDataText, runs, TestResult.FAILED, count);
        List<String> errorDataText = new ArrayList();
        generateGraphData(errorDataText, runs, TestResult.ERROR, count);
        List<String> ignorredDataText = new ArrayList();
        generateGraphData(ignorredDataText, runs, TestResult.IGNORED, count);
        List<String> noneDataText = new ArrayList();
        generateGraphData(noneDataText, runs, null, count);
        List<String> tableText = new ArrayList();
        generateGraphTable(tableText, runs, count);
        Map<String, List<String>> replacements = new HashMap();
        CommonUtilities.addReplacementToMap(replacements, testsNamePattern, testsName);
        replacements.put(testCountPattern, testCountText);
        replacements.put(graphDataPassedPattern, passedDataText);
        replacements.put(graphDataFailedPattern, failedDataText);
        replacements.put(graphDataErrorPattern, errorDataText);
        replacements.put(graphDataIgnoredPattern, ignorredDataText);
        replacements.put(graphDataNonePattern, noneDataText);
        replacements.put(tableDataPattern, tableText);
        List<String> template = FileUtilities.getLineListFromFile(graphPageTemplate);
        CommonUtilities.replacePatterns(template, output, replacements);
    }

    public static void generateGraphTable(List<String> output, List<TestRunResult> results, int count) {
        int size = results.size();
        int start = Math.max(0, size - count);
        for (int i = start; i < results.size(); ++i) {
            TestRunResult result = results.get(i);
            output.add("<tr><td>" + i + "</td><td><a href='log_" + result.date + ".xhtml'>&nbsp;" + result.date + "&nbsp;</a></td>");
            output.add("<td style='color:#006000;text-align:right;'>" + result.passedCount + "</td>");
            output.add("<td style='color:#800000;text-align:right;'>" + result.failedCount + "</td>");
            output.add("<td style='color:#000080;text-align:right;'>" + result.errorCount + "</td>");
            output.add("<td style='color:#606060;text-align:right;'>" + result.ignoredCount + "</td>");
            output.add("</tr>");
        }
    }

    public static void generateGraphData(List<String> output, List<TestRunResult> results, TestResult result, int count) {
        int size = results.size();
        int start = Math.max(0, size - count);
        for (int i = start; i < results.size(); ++i) {
            TestRunResult run = results.get(i);
            int targetResultCount = result == TestResult.PASSED ? run.passedCount : result == TestResult.FAILED ? run.failedCount : result == TestResult.ERROR ? run.errorCount : result == TestResult.IGNORED ? run.ignoredCount : 0;
            output.add("[" + (i + 1) + ", " + targetResultCount + "],");
        }
    }

}