com.tascape.qa.th.AbstractTestRunner Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of testharness Show documentation
Show all versions of testharness Show documentation
Testharness - Test Automation Framework
/*
* Copyright 2015.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.tascape.qa.th;
import com.tascape.qa.th.db.DbHandler;
import com.tascape.qa.th.db.TestResult;
import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author linsong wang
*/
public abstract class AbstractTestRunner {
private static final Logger LOG = LoggerFactory.getLogger(AbstractTestRunner.class);
private static final ThreadLocal TEST_LOG_PATH = new ThreadLocal() {
@Override
protected Path initialValue() {
String execId = SystemConfiguration.getInstance().getExecId();
Path testLogPath = SystemConfiguration.getInstance().getLogPath().resolve(execId);
testLogPath.toFile().mkdirs();
return testLogPath;
}
};
public static void setTestLogPath(Path testLogPath) {
LOG.trace("Setting runtime log directory {}", testLogPath);
TEST_LOG_PATH.set(testLogPath);
}
public static Path getTestLogPath() {
return TEST_LOG_PATH.get();
}
private static final ThreadLocal TEST_CASE_RESULT = new ThreadLocal<>();
public static void setTestCaseResult(TestResult testCaseResult) {
TEST_CASE_RESULT.set(testCaseResult);
}
public static TestResult getTestCaseResult() {
return TEST_CASE_RESULT.get();
}
protected SystemConfiguration sysConfig = SystemConfiguration.getInstance();
protected DbHandler db = null;
protected TestResult tcr = null;
protected String execId = "";
public abstract void runTestCase() throws Exception;
protected void generateHtml(Path logFile) {
Pattern http = Pattern.compile("((http|https)://\\S+)");
Path html = logFile.getParent().resolve("log.html");
LOG.trace("creating file {}", html);
try (PrintWriter pw = new PrintWriter(html.toFile())) {
pw.println("");
pw.println("Suite Log Directory
Test Log Directory");
pw.println();
List lines = FileUtils.readLines(logFile.toFile());
List files = new ArrayList<>(Arrays.asList(logFile.getParent().toFile().listFiles()));
for (String line : lines) {
String newline = line.replaceAll(">", ">");
newline = newline.replaceAll("<", "<");
if (newline.contains(" WARN ")) {
newline = "" + newline + " ";
} else if (newline.contains(" ERROR ")
|| newline.contains("Failure in test")
|| newline.contains("AssertionError")) {
newline = "" + newline + " ";
} else {
Matcher m = http.matcher(line);
if (m.find()) {
String url = m.group(1);
String a = String.format("%s", url, url);
newline = newline.replace(url, a);
}
}
pw.println(newline);
for (File file : files) {
String path = file.getAbsolutePath();
String name = file.getName();
if (newline.contains(path)) {
if (name.endsWith(".png")) {
pw.printf("",
name, name);
}
String a = String.format("%s", name, name);
int len = newline.indexOf(" ");
pw.printf((len > 0 ? newline.substring(0, len + 5) : "") + a);
pw.println();
files.remove(file);
break;
}
}
}
pw.println("
");
logFile.toFile().delete();
} catch (IOException ex) {
LOG.warn(ex.getMessage());
}
}
}