浏览代码

bug #I6URNQ 在CATCH表达中写单独的组件,SLOT中会拿不到异常

everywhere.z 2 年之前
父节点
当前提交
385b69ba98

+ 10 - 1
liteflow-core/src/main/java/com/yomahub/liteflow/builder/el/operator/CatchOperator.java

@@ -3,7 +3,9 @@ package com.yomahub.liteflow.builder.el.operator;
 import com.yomahub.liteflow.builder.el.operator.base.BaseOperator;
 import com.yomahub.liteflow.builder.el.operator.base.OperatorHelper;
 import com.yomahub.liteflow.flow.element.Executable;
+import com.yomahub.liteflow.flow.element.Node;
 import com.yomahub.liteflow.flow.element.condition.CatchCondition;
+import com.yomahub.liteflow.flow.element.condition.ThenCondition;
 
 /**
  * EL规则中的CATCH的操作符 用法:CATCH...DO...
@@ -20,8 +22,15 @@ public class CatchOperator extends BaseOperator<CatchCondition> {
 		Executable catchItem = OperatorHelper.convert(objects[0], Executable.class);
 
 		CatchCondition catchCondition = new CatchCondition();
-		catchCondition.setCatchItem(catchItem);
 
+		//如果是单个Node的话,要包装成THEN的CONDITION模式,否则CATCH不到异常
+		if (catchItem instanceof Node){
+			ThenCondition thenCondition = new ThenCondition();
+			thenCondition.addExecutable(catchItem);
+			catchCondition.setCatchItem(thenCondition);
+		}else{
+			catchCondition.setCatchItem(catchItem);
+		}
 		return catchCondition;
 	}
 

+ 11 - 0
liteflow-testcase-el/liteflow-testcase-el-script-groovy-springboot/src/test/java/com/yomahub/liteflow/test/script/groovy/loop/LiteFlowXmlScriptLoopGroovyELTest.java

@@ -2,6 +2,7 @@ package com.yomahub.liteflow.test.script.groovy.loop;
 
 import com.yomahub.liteflow.core.FlowExecutor;
 import com.yomahub.liteflow.flow.LiteflowResponse;
+import com.yomahub.liteflow.slot.DefaultContext;
 import com.yomahub.liteflow.test.BaseTest;
 import org.junit.Assert;
 import org.junit.Test;
@@ -65,4 +66,14 @@ public class LiteFlowXmlScriptLoopGroovyELTest extends BaseTest {
 				response.getExecuteStepStr());
 	}
 
+	// 测试在脚本中取到循环对象
+	@Test
+	public void testLoop6() throws Exception {
+		LiteflowResponse response = flowExecutor.execute2Resp("chain6", "arg");
+		Assert.assertTrue(response.isSuccess());
+		Assert.assertEquals("e==>w==>w==>w", response.getExecuteStepStr());
+		DefaultContext context = response.getFirstContextBean();
+		Assert.assertEquals("jack-tom-frank", context.getData("test"));
+	}
+
 }

+ 17 - 0
liteflow-testcase-el/liteflow-testcase-el-script-groovy-springboot/src/test/java/com/yomahub/liteflow/test/script/groovy/loop/cmp/ECmp.java

@@ -0,0 +1,17 @@
+package com.yomahub.liteflow.test.script.groovy.loop.cmp;
+
+import cn.hutool.core.collection.CollUtil;
+import com.yomahub.liteflow.core.NodeIteratorComponent;
+import org.springframework.stereotype.Component;
+
+import java.util.Iterator;
+import java.util.List;
+
+@Component("e")
+public class ECmp extends NodeIteratorComponent {
+    @Override
+    public Iterator<?> processIterator() throws Exception {
+        List<String> list = CollUtil.toList("jack","tom","frank");
+        return list.iterator();
+    }
+}

+ 15 - 0
liteflow-testcase-el/liteflow-testcase-el-script-groovy-springboot/src/test/resources/loop/flow.xml

@@ -26,6 +26,17 @@
                 }
             ]]>
         </node>
+
+        <node id="w" type="script" language="groovy">
+            <![CDATA[
+                def key = "test"
+                if (defaultContext.hasData(key)){
+                    defaultContext.setData(key, defaultContext.getData(key) + "-" + _meta.loopObject);
+                }else{
+                    defaultContext.setData(key, _meta.loopObject);
+                }
+            ]]>
+        </node>
     </nodes>
 
     <chain name="chain1">
@@ -47,4 +58,8 @@
     <chain name="chain5">
         WHILE(z).DO(THEN(a,d)).BREAK(y);
     </chain>
+
+    <chain name="chain6">
+        ITERATOR(e).DO(w);
+    </chain>
 </flow>

+ 6 - 0
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/java/com/yomahub/liteflow/test/catchcase/CatchELSpringbootTest.java

@@ -58,4 +58,10 @@ public class CatchELSpringbootTest extends BaseTest {
 		Assert.assertEquals("LOOP_3==>a==>b==>a==>b==>a==>b", response.getExecuteStepStrWithoutTime());
 	}
 
+	@Test
+	public void testCatch5() throws Exception {
+		LiteflowResponse response = flowExecutor.execute2Resp("chain5", "arg");
+		Assert.assertFalse(response.isSuccess());
+		Assert.assertEquals("a==>d", response.getExecuteStepStrWithoutTime());
+	}
 }

+ 8 - 2
liteflow-testcase-el/liteflow-testcase-el-springboot/src/test/resources/catchcase/flow.el.xml

@@ -4,13 +4,13 @@
     <chain name="chain1">
         CATCH(
             THEN(a,b)
-        ).DO(c)
+        ).DO(c);
     </chain>
 
     <chain name="chain2">
         CATCH(
             THEN(a,b)
-        ).DO(d)
+        ).DO(d);
     </chain>
 
     <chain name="chain3">
@@ -26,4 +26,10 @@
             )
         );
     </chain>
+
+    <chain name="chain5">
+        CATCH(
+            a
+        ).DO(d);
+    </chain>
 </flow>