优化了步骤动作关键字解析的代码,采用了策略模式,代码结构更清晰。之前关键字的处理逻辑没有变。

需要增加一个关键字处理时,只需实现ActionKeyWordParser接口,重写parse方法即可
This commit is contained in:
sunshaoyan 2019-05-29 15:35:40 +08:00
parent f3b3c57517
commit 87e0b6716c
13 changed files with 356 additions and 86 deletions

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.javassist:javassist:3.21.0-GA">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/javassist/javassist/3.21.0-GA/javassist-3.21.0-GA.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/javassist/javassist/3.21.0-GA/javassist-3.21.0-GA-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/javassist/javassist/3.21.0-GA/javassist-3.21.0-GA-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.reflections:reflections:0.9.11">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/reflections/reflections/0.9.11/reflections-0.9.11.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/reflections/reflections/0.9.11/reflections-0.9.11-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/reflections/reflections/0.9.11/reflections-0.9.11-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -299,5 +299,11 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.reflections</groupId>
<artifactId>reflections</artifactId>
<version>0.9.11</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,65 @@
package luckyclient.caserun.publicdispose;
import luckyclient.caserun.publicdispose.actionkeyword.Action;
import luckyclient.caserun.publicdispose.actionkeyword.ActionKeyWordParser;
import luckyclient.publicclass.LogUtil;
import org.reflections.Reflections;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
/**
* 步骤动作容器根据参数生成不同的动作关键字类型执行相应的解析
* @author: sunshaoyan@
* @date: Created on 2019/4/13
*/
public class ActionContext {
private static Logger logger = LoggerFactory.getLogger(ActionContext.class);
private static Map<String, Class> allActions;
static {
Reflections reflections = new Reflections("luckyclient.caserun.publicdispose.actionkeyword");
Set<Class<?>> annotatedClasses =
reflections.getTypesAnnotatedWith(Action.class);
allActions = new ConcurrentHashMap<String, Class>();
for (Class<?> classObject : annotatedClasses) {
Action action = (Action) classObject
.getAnnotation(Action.class);
allActions.put(action.name(), classObject);
}
allActions = Collections.unmodifiableMap(allActions);
}
private ActionKeyWordParser action;
public ActionContext(String name){
if (allActions.containsKey(name)) {
logger.info("Created Action name is {}", name);
try {
action = (ActionKeyWordParser) allActions.get(name).newInstance();
} catch (InstantiationException | IllegalAccessException ex) {
logger.error("Instantiate Action failed", ex);
}
} else {
logger.error("Specified Action name {} does not exist", name);
}
}
public String parse(String actionKeyWord,String testResult) {
if(null != action){
testResult = action.parse(actionKeyWord, testResult);
}else {
testResult="未检索到对应动作关键字,直接跳过此动作,请检查关键字:"+actionKeyWord;
LogUtil.APP.error("未检索到对应动作关键字,直接跳过此动作,请检查关键字:"+actionKeyWord);
}
return testResult;
}
}

View File

@ -1,6 +1,5 @@
package luckyclient.caserun.publicdispose;
import luckyclient.driven.SubString;
import luckyclient.publicclass.LogUtil;
/**
@ -13,12 +12,11 @@ import luckyclient.publicclass.LogUtil;
* @date 2019年1月15日
*/
public class ActionManageForSteps {
/**
* 解析用例步骤
* @param projectcase
* @param step
* @param taskid
* @param caselog
* @param stepsaction
* @param testresult
* @return
*/
public static String actionManage(String stepsaction,String testresult){
@ -28,7 +26,7 @@ public class ActionManageForSteps {
LogUtil.APP.info("Action(动作)无需处理......");
return testresult;
}
stepsaction=stepsaction.toLowerCase().trim();
stepsaction=stepsaction.trim();
String[] temp=stepsaction.split("\\|",-1);
for(String actionorder:temp){
if(null!=actionorder&&!"".equals(actionorder.trim())){
@ -40,90 +38,26 @@ public class ActionManageForSteps {
/**
* 动作关键字执行
* @param actionorder
* @param testresult
* @param actionKeyWord
* @param testResult
* @return
*/
private static String actionExecute(String actionorder,String testresult){
private static String actionExecute(String actionKeyWord,String testResult){
try{
// 处理动作事件
if(actionorder.endsWith("#wait")){
if(ChangString.isInteger(actionorder.substring(0, actionorder.lastIndexOf("#wait")))){
try {
// 获取步骤间等待时间
int time=Integer.parseInt(actionorder.substring(0, actionorder.lastIndexOf("#wait")));
if (time > 0) {
LogUtil.APP.info("Action(Wait):线程等待"+time+"秒...");
Thread.sleep(time * 1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
String[] actionArr = actionKeyWord.split("#");
if(actionArr.length == 2){
ActionContext actionContext = new ActionContext(actionArr[1]);
testResult = actionContext.parse(actionKeyWord, testResult);
}else {
LogUtil.APP.warn("使用等待关键字的参数不是整数,直接跳过此动作,请检查!");
testResult="关键字语法书写有误,请检查关键字:"+actionKeyWord;
LogUtil.APP.error("关键字语法书写有误,请检查关键字:"+actionKeyWord);
}
}else if(actionorder.endsWith("#getjv")){
String actionparams=actionorder.substring(0, actionorder.lastIndexOf("#getjv"));
String key="";
String index="1";
if(actionparams.endsWith("]")&&actionparams.contains("[")){
key=actionparams.substring(0,actionparams.lastIndexOf("["));
index=actionparams.substring(actionparams.lastIndexOf("[")+1, actionparams.lastIndexOf("]"));
testresult=SubString.getJsonValue(testresult, key, index);
}else{
key=actionparams;
testresult=SubString.getJsonValue(testresult, key, index);
}
LogUtil.APP.info("Action(getJV):获取JSON字符串指定Key的值是"+testresult);
}else if(actionorder.endsWith("#subcentrestr")){
String actionparams=actionorder.substring(0, actionorder.lastIndexOf("#subcentrestr"));
String startstr="";
String endstr="";
if(actionparams.startsWith("[")&&actionparams.endsWith("]")){
startstr=actionparams.substring(actionparams.indexOf("[")+1, actionparams.indexOf("]"));
endstr=actionparams.substring(actionparams.lastIndexOf("[")+1, actionparams.lastIndexOf("]"));
testresult=SubString.subCentreStr(testresult, startstr, endstr);
LogUtil.APP.info("Action(subCentreStr):截取测试结果指定开始及结束位置字符串:"+testresult);
}else{
testresult="步骤动作subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作关键字:"+actionorder;
LogUtil.APP.warn("步骤动作subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作关键字:"+actionorder);
}
}else if(actionorder.endsWith("#subcentrenum")){
String actionparams=actionorder.substring(0, actionorder.lastIndexOf("#subcentrenum"));
String startnum="";
String endnum="";
if(actionparams.startsWith("[")&&actionparams.endsWith("]")){
startnum=actionparams.substring(actionparams.indexOf("[")+1, actionparams.indexOf("]"));
endnum=actionparams.substring(actionparams.lastIndexOf("[")+1, actionparams.lastIndexOf("]"));
testresult=SubString.subCentreNum(testresult, startnum, endnum);
LogUtil.APP.info("Action(subCentreNum):截取测试结果指定开始及结束位置字符串:"+testresult);
}else{
testresult="步骤动作subCentreNum 必须是[\"开始字符\"][\"结束字符\"]#subCentreNum 格式,请检查您的步骤动作关键字:"+actionorder;
LogUtil.APP.warn("步骤动作subCentreNum 必须是[\"开始位置(整数)\"][\"结束位置(整数)\"]#subCentreNum 格式,请检查您的步骤动作关键字:"+actionorder);
}
}else if(actionorder.endsWith("#substrrgex")){
String actionparams=actionorder.substring(0, actionorder.lastIndexOf("#substrrgex"));
String key="";
String index="1";
if(actionparams.endsWith("]")&&actionparams.contains("[")){
key=actionparams.substring(0,actionparams.lastIndexOf("["));
index=actionparams.substring(actionparams.lastIndexOf("[")+1, actionparams.lastIndexOf("]"));
testresult=SubString.subStrRgex(testresult, key, index);
}else{
key=actionparams;
testresult=SubString.subStrRgex(testresult, key, index);
}
LogUtil.APP.info("Action(subStrRgex):获取JSON字符串指定Key的值是"+testresult);
}else{
testresult="未检索到对应动作关键字,直接跳过此动作,请检查关键字:"+actionorder;
LogUtil.APP.warn("未检索到对应动作关键字,直接跳过此动作,请检查关键字:"+actionorder);
}
return testresult;
return testResult;
}catch(Exception e){
testresult="处理步骤动作事件过程中出现异常,直接返回测试结果:"+actionorder;
LogUtil.APP.error("处理步骤动作事件过程中出现异常,直接返回测试结果:"+actionorder);
return testresult;
testResult="处理步骤动作事件过程中出现异常,直接返回测试结果:"+actionKeyWord;
LogUtil.APP.error("处理步骤动作事件过程中出现异常,直接返回测试结果:" ,e);
return testResult;
}
}

View File

@ -0,0 +1,17 @@
package luckyclient.caserun.publicdispose.actionkeyword;
/**
* 动作关键字注解定义
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
import java.lang.annotation.*;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
public @interface Action {
String name() default "";
}

View File

@ -0,0 +1,11 @@
package luckyclient.caserun.publicdispose.actionkeyword;
/**
* 动作关键字的处理接口
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
public interface ActionKeyWordParser {
String parse(String actionKeyWord, String testResult);
}

View File

@ -0,0 +1,36 @@
package luckyclient.caserun.publicdispose.actionkeyword;
import luckyclient.driven.SubString;
import luckyclient.publicclass.LogUtil;
/**
* 动作关键字的处理接口的实现类获取JSON字符串指定Key的值的
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
@Action(name="getJV")
public class GetJsonActionParser implements ActionKeyWordParser {
/**
* 获取JSON字符串指定Key的值是
* @param actionKeyWord 动作关键字
* @param testResult 测试结果
*/
@Override
public String parse(String actionKeyWord, String testResult) {
String actionparams=actionKeyWord.substring(0, actionKeyWord.lastIndexOf("#getJV"));
String key="";
String index="1";
if(actionparams.endsWith("]")&&actionparams.contains("[")){
key=actionparams.substring(0,actionparams.lastIndexOf("["));
index=actionparams.substring(actionparams.lastIndexOf("[")+1, actionparams.lastIndexOf("]"));
testResult= SubString.getJsonValue(testResult, key, index);
}else{
key=actionparams;
testResult=SubString.getJsonValue(testResult, key, index);
}
LogUtil.APP.info("Action(getJV):获取JSON字符串指定Key的值是"+testResult);
return testResult;
}
}

View File

@ -0,0 +1,29 @@
package luckyclient.caserun.publicdispose.actionkeyword;
import com.alibaba.fastjson.JSONObject;
/**
* 动作关键字的处理接口的实现类从响应header中取出某个header值
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
@Action(name="Header")
public class HeaderParser implements ActionKeyWordParser {
/**
* @param actionorder 动作关键字
*/
@Override
public String parse(String actionorder, String testResult) {
// 获取步骤间等待时间
String headerParam=actionorder.substring(0, actionorder.lastIndexOf("#Header"));
String pre = "RESPONSE_HEAD:【";
String headerStr = testResult.substring(testResult.indexOf(pre) + pre.length(), testResult.indexOf("】 RESPONSE_CODE"));
return JSONObject.parseObject(headerStr).getJSONArray(headerParam).getString(0);
}
}

View File

@ -0,0 +1,36 @@
package luckyclient.caserun.publicdispose.actionkeyword;
import luckyclient.driven.SubString;
import luckyclient.publicclass.LogUtil;
/**
* 动作关键字的处理接口的实现类截取测试结果指定开始及结束位置字符串
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
@Action(name="subCentreNum")
public class SubCentreNumActionParser implements ActionKeyWordParser {
/**
* 截取测试结果指定开始及结束位置字符串
* @param actionKeyWord 动作关键字
* @param testResult 测试结果
*/
@Override
public String parse(String actionKeyWord, String testResult) {
String actionParams=actionKeyWord.substring(0, actionKeyWord.lastIndexOf("#subCentreNum"));
if(actionParams.startsWith("[")&&actionParams.endsWith("]")){
String startnum=actionParams.substring(actionParams.indexOf("[")+1, actionParams.indexOf("]"));
String endnum=actionParams.substring(actionParams.lastIndexOf("[")+1, actionParams.lastIndexOf("]"));
testResult= SubString.subCentreNum(testResult, startnum, endnum);
LogUtil.APP.info("Action(subCentreNum):截取测试结果指定开始及结束位置字符串:"+testResult);
}else{
testResult="步骤动作subCentreNum 必须是[\"开始字符\"][\"结束字符\"]#subCentreNum 格式,请检查您的步骤动作关键字:"+actionKeyWord;
LogUtil.APP.error("步骤动作subCentreNum 必须是[\"开始位置(整数)\"][\"结束位置(整数)\"]#subCentreNum 格式,请检查您的步骤动作关键字:"+actionKeyWord);
}
return testResult;
}
}

View File

@ -0,0 +1,36 @@
package luckyclient.caserun.publicdispose.actionkeyword;
import luckyclient.driven.SubString;
import luckyclient.publicclass.LogUtil;
/**
* 动作关键字的处理接口的实现类截取测试结果指定开始及结束位置字符串
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
@Action(name="subCentreStr")
public class SubCentresStrActionParser implements ActionKeyWordParser {
/**
* 截取测试结果指定开始及结束位置字符串
* @param actionKeyWord 动作关键字
* @param testResult 测试结果
*/
@Override
public String parse(String actionKeyWord, String testResult) {
String actionparams=actionKeyWord.substring(0, actionKeyWord.lastIndexOf("#subCentreStr"));
String startstr="";
String endstr="";
if(actionparams.startsWith("[")&&actionparams.endsWith("]")){
startstr=actionparams.substring(actionparams.indexOf("[")+1, actionparams.indexOf("]"));
endstr=actionparams.substring(actionparams.lastIndexOf("[")+1, actionparams.lastIndexOf("]"));
testResult= SubString.subCentreStr(testResult, startstr, endstr);
LogUtil.APP.info("Action(subCentreStr):截取测试结果指定开始及结束位置字符串:"+testResult);
}else{
testResult="步骤动作subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作关键字:"+actionKeyWord;
LogUtil.APP.error("步骤动作subCentreStr 必须是[\"开始字符\"][\"结束字符\"]#subCentreStr 格式,请检查您的步骤动作关键字:"+actionKeyWord);
}
return testResult;
}
}

View File

@ -0,0 +1,36 @@
package luckyclient.caserun.publicdispose.actionkeyword;
import luckyclient.driven.SubString;
import luckyclient.publicclass.LogUtil;
/**
* 动作关键字的处理接口的实现类获取JSON字符串指定Key的值采用正则匹配
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
@Action(name="subStrRgex")
public class SubStrRegxActionParser implements ActionKeyWordParser {
/**
* 获取JSON字符串指定Key的值采用正则匹配
* @param actionKeyWord 动作关键字
* @param testResult 测试结果
*/
@Override
public String parse(String actionKeyWord, String testResult) {
String actionparams=actionKeyWord.substring(0, actionKeyWord.lastIndexOf("#subStrRgex"));
String key="";
String index="1";
if(actionparams.endsWith("]")&&actionparams.contains("[")){
key=actionparams.substring(0,actionparams.lastIndexOf("["));
index=actionparams.substring(actionparams.lastIndexOf("[")+1, actionparams.lastIndexOf("]"));
testResult= SubString.subStrRgex(testResult, key, index);
}else{
key=actionparams;
testResult= SubString.subStrRgex(testResult, key, index);
}
LogUtil.APP.info("Action(subStrRgex):获取JSON字符串指定Key的值是"+testResult);
return testResult;
}
}

View File

@ -0,0 +1,38 @@
package luckyclient.caserun.publicdispose.actionkeyword;
import luckyclient.caserun.publicdispose.ChangString;
import luckyclient.publicclass.LogUtil;
/**
* 动作关键字的处理接口的实现类线程等待时间
* @author: sunshaoyan
* @date: Created on 2019/4/13
*/
@Action(name="Wait")
public class ThreadWaitAction implements ActionKeyWordParser {
/**
* @param actionorder 动作关键字
*/
@Override
public String parse(String actionorder, String testResult) {
if(ChangString.isInteger(actionorder.substring(0, actionorder.lastIndexOf("#Wait")))){
try {
// 获取步骤间等待时间
int time=Integer.parseInt(actionorder.substring(0, actionorder.lastIndexOf("#Wait")));
if (time > 0) {
LogUtil.APP.info("Action(Wait):线程等待"+time+"秒...");
Thread.sleep(time * 1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}else{
LogUtil.APP.error("使用等待关键字的参数不是整数,直接跳过此动作,请检查!");
}
return testResult;
}
}