
com.tascape.qa.th.SuiteRunner 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.google.common.util.concurrent.ThreadFactoryBuilder;
import com.tascape.qa.th.db.DbHandler;
import com.tascape.qa.th.db.SuiteProperty;
import com.tascape.qa.th.db.TestCase;
import com.tascape.qa.th.db.TestResult;
import com.tascape.qa.th.suite.AbstractSuite;
import java.io.File;
import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.CompletionService;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.ThreadFactory;
import javax.xml.stream.XMLStreamException;
import org.apache.commons.io.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
/**
*
* @author linsong wang
*/
public class SuiteRunner {
private static final Logger LOG = LoggerFactory.getLogger(SuiteRunner.class);
private TestSuite ts = null;
private DbHandler db = null;
private final SystemConfiguration SYS_CONFIG = SystemConfiguration.getInstance();
private final String execId = SYS_CONFIG.getExecId();
private static final Map UNSUPPORTED_TESTS = new HashMap<>();
public synchronized static void addUnspportedTestCase(TestCase tc) {
UNSUPPORTED_TESTS.put(tc.format(), tc);
}
public SuiteRunner(TestSuite testSuite) throws Exception {
LOG.info("Run suite with execution id {}", execId);
this.ts = testSuite;
this.db = DbHandler.getInstance();
db.queueSuiteExecution(ts, this.execId);
}
@SuppressWarnings("UseSpecificCatch")
public int startExecution() throws IOException, InterruptedException, SQLException, XMLStreamException {
File dir = SYS_CONFIG.getLogPath().resolve(execId).toFile();
LOG.info("Create suite execution log directory {}", dir);
if (!dir.exists() && !dir.mkdirs()) {
throw new IOException("Cannot create directory " + dir);
}
this.saveExectionProperties(dir);
int threadCount = SYS_CONFIG.getExecutionThreadCount();
LOG.info("Start execution engine with {} thread(s)", threadCount);
int len = (threadCount + "").length();
ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("t%0" + len + "d").build();
ExecutorService executorService = Executors.newFixedThreadPool(threadCount, namedThreadFactory);
CompletionService completionService = new ExecutorCompletionService<>(executorService);
LOG.info("Start to acquire test cases to execute");
int numberOfFailures = 0;
try {
List tcrs = this.filter(this.db.getQueuedTestCaseResults(this.execId, 100));
while (!tcrs.isEmpty()) {
List> futures = new ArrayList<>();
for (TestResult tcr : tcrs) {
LOG.info("Submit test case {}", tcr.getTestCase().format());
futures.add(completionService.submit(new TestRunnerJUnit4(db, tcr)));
}
LOG.debug("Total {} test cases submitted", futures.size());
for (Future f : futures) {
try {
Future future = completionService.take();
TestResult tcr = future.get();
if (tcr == null) {
continue;
}
String result = tcr.getResult().result();
LOG.info("Get result of test case {} - {}", tcr.getTestCase().format(), result);
if (!ExecutionResult.PASS.getName().equals(result) && !result.endsWith("/0")) {
numberOfFailures++;
}
} catch (Throwable ex) {
LOG.error("Error executing test thread", ex);
numberOfFailures++;
}
}
tcrs = this.filter(this.db.getQueuedTestCaseResults(this.execId, 100));
}
} finally {
AbstractSuite.getSuites().stream().forEach((suite) -> {
try {
suite.tearDown();
} catch (Exception ex) {
LOG.warn("Error tearing down suite {} - {}", suite.getClass(), ex.getMessage());
}
});
}
executorService.shutdown();
LOG.info("No more test case to run on this host, updating suite execution result");
this.db.updateSuiteExecutionResult(this.execId);
this.db.saveJunitXml(this.execId);
this.db.exportToJson(this.execId);
return numberOfFailures;
}
private void saveExectionProperties(File dir) throws IOException, SQLException {
File props = new File(dir, "execution.properties");
SYS_CONFIG.getProperties().store(FileUtils.openOutputStream(props), "Testharness");
List sps = new ArrayList<>();
SYS_CONFIG.getProperties().entrySet().stream()
.filter(key -> !key.toString().startsWith("qa.th.db."))
.forEach(entry -> {
SuiteProperty sp = new SuiteProperty();
sp.setSuiteResultId(this.execId);
sp.setPropertyName(entry.getKey().toString());
sp.setPropertyValue(entry.getValue().toString());
sps.add(sp);
});
this.db.setSuiteExecutionProperties(sps);
}
private List filter(List tcrs) {
List tcrs0 = new ArrayList<>();
tcrs.stream().filter((tcr) -> (UNSUPPORTED_TESTS.get(tcr.getTestCase().format()) == null)).forEach((tcr) -> {
tcrs0.add(tcr);
});
return tcrs0;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy