单元测试提交

This commit is contained in:
LeoLee
2021-12-11 21:39:19 +08:00
parent 273a9221e4
commit abb888e40a
12 changed files with 256 additions and 0 deletions

View File

@@ -0,0 +1,43 @@
package com.yomahub.liteflow.test.nullParam;
import com.yomahub.liteflow.core.FlowExecutor;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
/**
* 单元测试:传递null param导致NPE的优化代码
*
* @Author LeoLee
* @Date 2021/12/9 16:58
* @Version 1.0
*/
@RunWith(SpringRunner.class)
@TestPropertySource(value = "classpath:/nullParam/application.properties")
@SpringBootTest(classes = NullParamTest.class)
@EnableAutoConfiguration
@ComponentScan({"com.yomahub.liteflow.test.nullParam.cmp"})
public class NullParamTest {
@Autowired
private FlowExecutor flowExecutor;
/**
* 支持无参的flow执行以及param 为null时的异常抛出
* @Author LeoLee
* @Date 17:25 2021/12/9
*/
@Test
public void testNullParam() throws Exception {
//flowExecutor.execute("chain1", null);//NullParamException: data slot can't accept null param
flowExecutor.execute("chain1");
//flowExecutor.execute2Resp("chain1", null);//NullParamException: data slot can't accept null param
flowExecutor.execute2Resp("chain1");
}
}

View File

@@ -0,0 +1,22 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.nullParam.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
System.out.println("ACmp executed!");
System.out.println("get request data:" + this.getSlot().getRequestData());
this.getSlot().setInput("BCmp", "param for BCmp");
}
}

View File

@@ -0,0 +1,23 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.nullParam.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
System.out.println("BCmp executed!");
System.out.println("BCmp param:" + this.getSlot().getInput("BCmp"));
this.getSlot().setOutput("CCmp", "param for CCmp");
}
}

View File

@@ -0,0 +1,22 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.nullParam.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
System.out.println("CCmp executed!");
System.out.println("CCmp param:" + this.getSlot().getOutput("CCmp"));
}
}

View File

@@ -0,0 +1,2 @@
liteflow.rule-source=nullParam/flow.xml
liteflow.print-banner=true

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
<!--同步执行-->
<then value="a,b"/>
<when value="c"/>
</chain>
</flow>

View File

@@ -0,0 +1,38 @@
package com.yomahub.liteflow.test.nullParam;
import com.yomahub.liteflow.core.FlowExecutor;
import com.yomahub.liteflow.test.BaseTest;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
/**
* 单元测试:传递null param导致NPE的优化代码
*
* @Author LeoLee
* @Date 2021/12/11
* @Version V1.0
**/
@RunWith(SpringRunner.class)
@ContextConfiguration("classpath:/nullParam/application-local.xml")
public class NullParamTest extends BaseTest {
@Resource
private FlowExecutor flowExecutor;
/**
* 支持无参的flow执行以及param 为null时的异常抛出
* @Author: LeoLee
* @Date: 2021/12/11 21:38
*/
@Test
public void testNullParam() throws Exception {
//flowExecutor.execute("chain1", null);//NullParamException: data slot can't accept null param
flowExecutor.execute("chain1");
//flowExecutor.execute2Resp("chain1", null);//NullParamException: data slot can't accept null param
flowExecutor.execute2Resp("chain1");
}
}

View File

@@ -0,0 +1,22 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.nullParam.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("a")
public class ACmp extends NodeComponent {
@Override
public void process() {
System.out.println("ACmp executed!");
System.out.println("get request data:" + this.getSlot().getRequestData());
this.getSlot().setInput("BCmp", "param for BCmp");
}
}

View File

@@ -0,0 +1,23 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.nullParam.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("b")
public class BCmp extends NodeComponent {
@Override
public void process() {
System.out.println("BCmp executed!");
System.out.println("BCmp param:" + this.getSlot().getInput("BCmp"));
this.getSlot().setOutput("CCmp", "param for CCmp");
}
}

View File

@@ -0,0 +1,22 @@
/**
* <p>Title: liteflow</p>
* <p>Description: 轻量级的组件式流程框架</p>
* @author Bryan.Zhang
* @email weenyc31@163.com
* @Date 2020/4/1
*/
package com.yomahub.liteflow.test.nullParam.cmp;
import com.yomahub.liteflow.core.NodeComponent;
import org.springframework.stereotype.Component;
@Component("c")
public class CCmp extends NodeComponent {
@Override
public void process() {
System.out.println("CCmp executed!");
System.out.println("CCmp param:" + this.getSlot().getOutput("CCmp"));
}
}

View File

@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd">
<context:component-scan base-package="com.yomahub.liteflow.test.nullParam.cmp" />
<bean id="springAware" class="com.yomahub.liteflow.util.SpringAware"/>
<bean class="com.yomahub.liteflow.spring.ComponentScanner"/>
<bean id="liteflowConfig" class="com.yomahub.liteflow.property.LiteflowConfig">
<property name="ruleSource" value="nullParam/flow.xml"/>
</bean>
<bean id="flowExecutor" class="com.yomahub.liteflow.core.FlowExecutor">
<property name="liteflowConfig" ref="liteflowConfig"/>
</bean>
</beans>

View File

@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<flow>
<chain name="chain1">
<!--同步执行-->
<then value="a,b"/>
<when value="c"/>
</chain>
</flow>