view src/org/thermostat/qa2/reporter/LogParser.java @ 201:2d312f47b00a

log parser fix
author Zdenek Zambersky <zzambers@redhat.com>
date Wed, 26 Aug 2015 14:57:04 +0200
parents 877b403e8344
children
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.File;
import java.io.IOException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import static org.thermostat.qa2.framework.TestResult.*;
import org.thermostat.qa2.framework.TestResult;
import org.thermostat.qa2.framework.utils.FileUtilities;

/**
 *
 * @author Zdeněk Žamberský
 */
public class LogParser {

    public static TestRunResult parseLogDir(File dir) throws IOException {
        List<TestClassResult> testClasses = new ArrayList();
        for (File f : dir.listFiles()) {
            testClasses.add(parseLog(f));
        }
        Collections.sort(testClasses, new Comparator<TestClassResult>() {
            Collator collator = Collator.getInstance();

            @Override
            public int compare(TestClassResult o1, TestClassResult o2) {
                return collator.compare(o1.name, o2.name);
            }

        });
        return new TestRunResult(dir.getName(), testClasses);
    }

    public static TestClassResult parseLog(File file) throws IOException {
        List<String> lineList = FileUtilities.getLineListFromFile(file.getAbsolutePath());
        List<TestMethodResult> methodList = new ArrayList();
        List<TestMethodResult> beforeMethodList = new ArrayList();
        long duration = 0;
        List<String> log = new ArrayList();
        for (int i = 0; i < lineList.size(); ++i) {
            String line = lineList.get(i);
            TestResult result = getResult(line);
            if (result != null) {
                String methodLine = line.substring(result.toString().length() + 1);
                String methodName = getMethodName(methodLine);
                boolean beforeMethod = methodName.equals("@Before");
                if (beforeMethod) {
                    methodLine = methodLine.substring(methodLine.indexOf(methodName) + methodName.length() + 1);
                    methodName = getMethodName(methodLine);
                }

                TestMethodResult testMethod = new TestMethodResult(methodName, result);
                if (result == FAILED || result == ERROR || beforeMethod) {
                    String reason = getReason(methodLine);
                    List<String> stackTrace = new ArrayList();
                    for (;;) {
                        line = lineList.get(++i);
                        if (line.startsWith("INFO:") || line.startsWith("SUMMARY:") || getResult(line) != null) {
                            --i;
                            break;
                        }
                        stackTrace.add(line);
                    }
                    for (String s : stackTrace) {
                        String pattern = "Caused by: java.lang.AssertionError:";
                        if (s.startsWith(pattern)) {
                            testMethod.setReason(s.substring(pattern.length()));
                        }
                    }
                    testMethod.setStackTrace(stackTrace);
                    testMethod.setLog(log);
                    log = new ArrayList();
                } else {
                    log.clear();
                }
                if (beforeMethod) {
                    beforeMethodList.add(testMethod);
                } else {
                    methodList.add(testMethod);
                }
            } else if (line.startsWith("INFO:")) {
                log.add(line);
            } else if (line.startsWith("SUMMARY")) {
                int index = line.indexOf("duration:");
                if (index >= 0) {
                    String durationString = line.substring(line.indexOf(":", index) + 1).trim();
                    duration = Long.parseLong(durationString);
                }
            }
        }
        String testName = file.getName();
        testName = testName.substring(0, testName.length() - 4);
        TestClassResult test = new TestClassResult(testName, methodList, beforeMethodList, duration);
        return test;
    }

    public static TestResult getResult(String s) {
        return s.startsWith(PASSED.toString()) ? PASSED
                : s.startsWith(FAILED.toString()) ? FAILED
                        : s.startsWith(IGNORED.toString()) ? IGNORED
                                : s.startsWith(ERROR.toString()) ? ERROR
                                        : null;
    }

    public static String getMethodName(String line) {
        int index1 = line.indexOf(':');
        String tmp = index1 >= 0 ? line.substring(0, index1) : line;
        int index2 = tmp.lastIndexOf('.');
        return tmp.substring(index2 + 1).trim();
    }

    public static String getReason(String line) {
        int index = line.indexOf(':');
        return index >= 0 ? line.substring(index + 1).trim() : "";
    }

}