Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat/runtime container #11

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions hrun4j-cli/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -63,8 +63,7 @@
<dependency>
<groupId>vip.lematech</groupId>
<artifactId>hrun4j-core</artifactId>
<version>1.0.2</version>
<scope>compile</scope>
<version>1.0.3-SNAPSHOT</version>
</dependency>
</dependencies>
</project>
</project>
38 changes: 20 additions & 18 deletions hrun4j-cli/src/main/java/vip/lematech/hrun4j/cli/commands/Run.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,11 @@

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.ReUtil;
import cn.hutool.core.util.StrUtil;
import vip.lematech.hrun4j.helper.FilesHelper;
import vip.lematech.hrun4j.helper.JavaIdentifierHelper;
import vip.lematech.hrun4j.helper.LittleHelper;
import vip.lematech.hrun4j.helper.LogHelper;
import vip.lematech.hrun4j.cli.testsuite.TestNGEngine;
import vip.lematech.hrun4j.config.Env;
import vip.lematech.hrun4j.config.NamespaceMap;
import vip.lematech.hrun4j.config.RunnerConfig;
import vip.lematech.hrun4j.core.converter.ObjectConverter;
Expand All @@ -26,7 +23,6 @@
import org.kohsuke.args4j.Option;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.*;

Expand Down Expand Up @@ -65,9 +61,14 @@ public String description() {
String i18n = Constant.I18N_CN;

private Searcher searcher;

private RunnerConfig runnerConfig;
@Override
public int execute(PrintWriter out, PrintWriter err) {
RunnerConfig.getInstance().setRunMode(RunnerConfig.RunMode.CLI);

//TODO 初始化 runnerConfig,保证初始化的名字是 testSuiteName
runnerConfig = RunnerConfig.getInstance();
runnerConfig.setRunMode(RunnerConfig.RunMode.CLI);
LogHelper.info("Run mode: {}", RunnerConfig.RunMode.CLI);


Expand All @@ -86,7 +87,7 @@ public int execute(PrintWriter out, PrintWriter err) {


validateOrSetParams();
String workDirPath = FileUtil.getAbsolutePath(RunnerConfig.getInstance().getWorkDirectory());
String workDirPath = FileUtil.getAbsolutePath(runnerConfig.getWorkDirectory());

File testSuitePath = null ;
if(Objects.nonNull(testSuitePathValue)){
Expand All @@ -95,7 +96,7 @@ public int execute(PrintWriter out, PrintWriter err) {
LogHelper.info("The workspace path:{}", workDirPath);
initEnvPath(workDirPath);
TestSuite testSuite = new TestSuite();
searcher = new Searcher();
searcher = new Searcher(runnerConfig);
List<String> testCaseMapFiles = new ArrayList<>();
if (CollUtil.isNotEmpty(testCasePaths)) {
Config config = new Config();
Expand Down Expand Up @@ -154,7 +155,7 @@ public int execute(PrintWriter out, PrintWriter err) {
testCaseConfig.setParameters(null);
Config resultConfig = (Config) ObjectConverter.objectsExtendsPropertyValue(testSuiteConfig, testCaseConfig);
testCase.setConfig(resultConfig);
Map environment = Env.getEnvMap();
Map environment = runnerConfig.getEnv().getEnvMap();
if (environment.containsKey(namespace)) {
String exceptionMsg = String.format("If the same path case %s already exists, only the last one can be retained", caseRelativePath);
LogHelper.warn(exceptionMsg);
Expand All @@ -166,8 +167,9 @@ public int execute(PrintWriter out, PrintWriter err) {
LogHelper.error(exceptionMsg);
return 1;
}
RunnerConfig.getInstance().setTestCasePaths(testCaseMapFiles);
TestNGEngine.run();
runnerConfig.setTestCasePaths(testCaseMapFiles);
TestNGEngine engine = new TestNGEngine(runnerConfig);
engine.run();
return 0;
}

Expand Down Expand Up @@ -213,9 +215,9 @@ private void initEnvPath(String workDirPath) {
}
if (!FileUtil.isAbsolutePath(dotEnvPath.getPath())) {
File dotFilePath = new File(workDirPath, Constant.ENV_FILE_NAME);
RunnerConfig.getInstance().setDotEnvPath(FileUtil.getAbsolutePath(dotFilePath));
runnerConfig.setDotEnvPath(FileUtil.getAbsolutePath(dotFilePath));
} else {
RunnerConfig.getInstance().setDotEnvPath(FileUtil.getAbsolutePath(dotEnvPath));
runnerConfig.setDotEnvPath(FileUtil.getAbsolutePath(dotEnvPath));
}
} else {
File dotFilePath = new File(workDirPath);
Expand All @@ -224,7 +226,7 @@ private void initEnvPath(String workDirPath) {
, FileUtil.getAbsolutePath(dotFilePath));
LogHelper.warn(exceptionMsg);
}
RunnerConfig.getInstance().setDotEnvPath(FileUtil.getAbsolutePath(dotFilePath));
runnerConfig.setDotEnvPath(FileUtil.getAbsolutePath(dotFilePath));
}
}

Expand All @@ -234,7 +236,7 @@ private void initEnvPath(String workDirPath) {
private void validateOrSetParams() {

if (Objects.isNull(rootBsh)) {
RunnerConfig.getInstance().setWorkDirectory(new File(Constant.DOT_PATH));
runnerConfig.setWorkDirectory(new File(Constant.DOT_PATH));
} else {
if (!rootBsh.exists() || !rootBsh.isFile() || !Constant.BEANSHELL_BSH_END_SUFFIX.endsWith(FileUtil.extName(rootBsh))) {
String exceptionMsg = String.format("The rootBsh file %s does not exist or the suffix does not end in `.bsh`"
Expand All @@ -245,16 +247,16 @@ private void validateOrSetParams() {
String workDirPath = FileUtil.getAbsolutePath(workFile);
Properties property = System.getProperties();
property.setProperty("user.dir", workDirPath);
RunnerConfig.getInstance().setWorkDirectory(new File(workDirPath));
runnerConfig.setWorkDirectory(new File(workDirPath));
}
if (!StrUtil.isEmpty(pkgName)) {
RunnerConfig.getInstance().setPkgName(pkgName);
runnerConfig.setPkgName(pkgName);
}
if (!StrUtil.isEmpty(extName)) {
RunnerConfig.getInstance().setTestCaseExtName(extName);
runnerConfig.setTestCaseExtName(extName);
}
if (!StrUtil.isEmpty(i18n)) {
RunnerConfig.getInstance().setI18n(i18n);
RunnerConfig.i18n = i18n;
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,8 +52,8 @@ public int execute(PrintWriter out, PrintWriter err) {
JavaIdentifierHelper.isValidJavaFullClassName(groupId);
ProjectInfo projectInfo = new ProjectInfo(groupId, projectName
, version, projectName, String.format("Demo project for %s", projectName));
RunnerConfig.getInstance().setWorkDirectory(new File(Constant.DOT_PATH));
String projectRoot = FileUtil.getAbsolutePath(RunnerConfig.getInstance().getWorkDirectory()) + File.separator;

String projectRoot = FileUtil.getAbsolutePath(new File(Constant.DOT_PATH)) + File.separator;
LogHelper.info("工作区路径:{}", projectRoot);
IProjectGenerator projectGenerator = new ProjectGeneratorImpl();
if (CliConstants.SRPINGBOOT_PROJECT_TYPE.equalsIgnoreCase(type)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,49 +33,51 @@

@Data
public class TestNGEngine {
private static TestNG testNG;
private static String suiteName;
public static Map<String, Set<String>> testCasePkgGroup = new HashMap<>();

public static TestNG getInstance() {
if (testNG == null) {
testNG = new TestNG();
setDefaultProperties();
}
return testNG;
private TestNG testNG;
private String suiteName;
private RunnerConfig runnerConfig;
public Map<String, Set<String>> testCasePkgGroup = new HashMap<>();

public TestNGEngine(RunnerConfig runnerConfig) {
this.runnerConfig = runnerConfig;
this.testNG = new TestNG();
setDefaultProperties();
}

/**
* Setting Default Properties
*/
private static void setDefaultProperties() {
testNG.setDefaultSuiteName("hrun4j");
private void setDefaultProperties() {
testNG.setDefaultSuiteName(RunnerConfig.DEFAULT_TEST_SUITE_NAME);
HTMLReporter htmlReporter = new HTMLReporter();
JUnitXMLReporter jUnitXMLReporter = new JUnitXMLReporter();
testNG.addListener(htmlReporter);
testNG.addListener(jUnitXMLReporter);
}

public void setSuiteName(String name) {
testNG.setDefaultSuiteName(name);
}

/**
* self-defined add listener
* @param testListenerList testListenerList
* @return TestNG
*/
public static TestNG addListener(List<ITestListener> testListenerList) {
getInstance();
public TestNGEngine addListener(List<ITestListener> testListenerList) {
if (testListenerList.size() > 0) {
for (ITestListener testListener : testListenerList) {
testNG.addListener(testListener);
}
}
return testNG;
return this;
}

/**
* init testng classes and testng run
*/
public static void run() {
List<String> testCasePaths = RunnerConfig.getInstance().getTestCasePaths();
public void run() {
List<String> testCasePaths = runnerConfig.getTestCasePaths();
testCasePkgGroup = fileList2TestClass(testCasePaths);
if (MapUtil.isEmpty(testCasePkgGroup)) {
LogHelper.warn("No valid test cases were found on the current path: {}", testCasePaths);
Expand All @@ -84,14 +86,14 @@ public static void run() {
runNG();
}

private static void runNG() {
getInstance().run();
private void runNG() {
testNG.run();
}

/**
* Dynamically construct test cases
*/
private static void addTestClasses() {
private void addTestClasses() {
List<Class> classes = new ArrayList<>();
for (Map.Entry<String, Set<String>> entry : testCasePkgGroup.entrySet()) {
String fullTestClassName = entry.getKey();
Expand All @@ -109,7 +111,7 @@ private static void addTestClasses() {
LogHelper.debug("Class full path:'{}',package path:'{}',class name:{} added done.", fullTestClassName, pkgName, className);
}
Class[] execClass = classes.toArray(new Class[0]);
getInstance().setTestClasses(execClass);
testNG.setTestClasses(execClass);
}


Expand All @@ -118,7 +120,7 @@ private static void addTestClasses() {
*/
private static boolean filePathFlag = true;

private static void fileToTestClassMap(Map<String, Set<String>> fileTestClassMap, File file) {
private void fileToTestClassMap(Map<String, Set<String>> fileTestClassMap, File file) {
String extName = FileUtil.extName(file);
if (Constant.SUPPORT_TEST_CASE_FILE_EXT_JSON_NAME.equalsIgnoreCase(extName)
|| Constant.SUPPORT_TEST_CASE_FILE_EXT_YML_NAME.equalsIgnoreCase(extName)) {
Expand All @@ -130,11 +132,11 @@ private static void fileToTestClassMap(Map<String, Set<String>> fileTestClassMap
}
String fileParentCanonicalPath = FileUtil.getAbsolutePath(file.getParentFile());
StringBuffer pkgName = new StringBuffer();
pkgName.append(RunnerConfig.getInstance().getPkgName());
pkgName.append(runnerConfig.getPkgName());
String filePath;
if (filePathFlag) {
pkgName.append("_");
String workDirPath = FileUtil.getAbsolutePath(RunnerConfig.getInstance().getWorkDirectory());
String workDirPath = FileUtil.getAbsolutePath(runnerConfig.getWorkDirectory());
workDirPath = LittleHelper.escapeRegexTransfer(workDirPath);
filePath = fileParentCanonicalPath.replaceFirst(workDirPath, "");
} else {
Expand Down Expand Up @@ -171,7 +173,7 @@ private static void fileToTestClassMap(Map<String, Set<String>> fileTestClassMap
* @param files
* @param fileTestClassMap
*/
private static void fileTraverse(File files, Map<String, Set<String>> fileTestClassMap) {
private void fileTraverse(File files, Map<String, Set<String>> fileTestClassMap) {
if (files.isFile()) {
fileToTestClassMap(fileTestClassMap, files);
} else {
Expand All @@ -191,15 +193,15 @@ private static void fileTraverse(File files, Map<String, Set<String>> fileTestCl
* @param listFile directories file list
* @return file to map
*/
public static Map<String, Set<String>> fileList2TestClass(List<String> listFile) {
public Map<String, Set<String>> fileList2TestClass(List<String> listFile) {
Map<String, Set<String>> fileTestClassMap = Maps.newHashMap();
for (String fileStr : listFile) {
File file = new File(fileStr);
if (FileUtil.isAbsolutePath(file.getPath())) {
filePathFlag = false;
}
if (filePathFlag) {
file = new File(RunnerConfig.getInstance().getWorkDirectory().getPath(), file.getPath());
file = new File(runnerConfig.getWorkDirectory().getPath(), file.getPath());
}
FilesHelper.checkFileExists(file);
fileTraverse(file, fileTestClassMap);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ import vip.lematech.hrun4j.entity.testcase.TestCase;
public class GetTest extends Hrun4j {
@Test(dataProvider = "dataProvider")
public void getScene(TestCase testCase) {
TestCaseExecutorEngine.getInstance().execute(testCase);
getTestCaseRunner().execute(testCase);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,6 @@ import vip.lematech.hrun4j.entity.testcase.TestCase;
public class PostTest extends Hrun4j {
@Test(dataProvider = "dataProvider")
public void postScene(TestCase testCase) {
TestCaseExecutorEngine.getInstance().execute(testCase);
getTestCaseRunner().execute(testCase);
}
}
2 changes: 1 addition & 1 deletion hrun4j-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<modelVersion>4.0.0</modelVersion>
<groupId>vip.lematech</groupId>
<artifactId>hrun4j-core</artifactId>
<version>1.0.2</version>
<version>1.0.3-SNAPSHOT</version>
<name>hrun4j-core</name>
<url>https://lematech.vip</url>
<properties>
Expand Down
25 changes: 22 additions & 3 deletions hrun4j-core/src/main/java/vip/lematech/hrun4j/base/TestBase.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
package vip.lematech.hrun4j.base;

import cn.hutool.core.io.FileUtil;
import org.testng.ITestContext;
import vip.lematech.hrun4j.common.DefinedException;
import vip.lematech.hrun4j.config.RunnerConfig;
import vip.lematech.hrun4j.core.provider.NGDataProvider;
import vip.lematech.hrun4j.core.runner.TestCaseRunner;
import vip.lematech.hrun4j.helper.LogHelper;
import org.testng.annotations.*;

import java.lang.reflect.Method;
import java.util.Objects;

/**
* Test base classes for pre -, post -, and data loading
Expand All @@ -19,8 +23,11 @@

public class TestBase {
private String testCaseName;
private String testSuiteName;

@BeforeSuite
public void beforeSuite(){
public void beforeSuite(ITestContext context){
this.testSuiteName = context.getSuite().getName();
LogHelper.info("[========================================]@beforeSuite()");
}
@BeforeMethod
Expand All @@ -36,12 +43,16 @@ public void afterSuite(){
LogHelper.info("[========================================]@afterSuite()");
}
@DataProvider
public Object[][] dataProvider(Method method) {
public Object[][] dataProvider(ITestContext context, Method method) {
if (Objects.isNull(testSuiteName)) {
this.testSuiteName = context.getSuite().getName();
}
Object[][] objects;
this.testCaseName = method.getName();

String packageName = FileUtil.mainName(method.getDeclaringClass().getName());
try {
objects = new NGDataProvider().dataProvider(packageName, testCaseName);
objects = new NGDataProvider(testSuiteName).dataProvider(packageName, testCaseName);
} catch (DefinedException e) {
throw e;
} catch (Exception e) {
Expand All @@ -50,4 +61,12 @@ public Object[][] dataProvider(Method method) {
}
return objects;
}

public RunnerConfig getRunnerConfig() {
return RunnerConfig.getInstanceBySuiteName(this.testSuiteName);
}

public TestCaseRunner getTestCaseRunner() {
return getRunnerConfig().getTestCaseRunner();
}
}
Loading