You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@commons.apache.org by wo...@apache.org on 2013/12/04 15:36:27 UTC
svn commit: r1547807 - in /commons/proper/scxml/trunk/src:
main/java/org/apache/commons/scxml2/model/
main/java/org/apache/commons/scxml2/semantics/
test/java/org/apache/commons/scxml2/
Author: woonsan
Date: Wed Dec 4 14:36:26 2013
New Revision: 1547807
URL: http://svn.apache.org/r1547807
Log:
SCXML-180: treat the cond expression result as false even when the result is null or non-boolean object, or even when catching an exception while evaluating
Added:
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/transitions-with-cond-01.xml
Modified:
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/If.java
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/semantics/SCXMLSemanticsImpl.java
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/SCXMLExecutorTest.java
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/SCXMLTestHelper.java
Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/If.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/If.java?rev=1547807&r1=1547806&r2=1547807&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/If.java (original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/If.java Wed Dec 4 14:36:26 2013
@@ -28,6 +28,7 @@ import org.apache.commons.scxml2.EventDi
import org.apache.commons.scxml2.SCInstance;
import org.apache.commons.scxml2.SCXMLExpressionException;
import org.apache.commons.scxml2.TriggerEvent;
+import org.apache.commons.scxml2.semantics.ErrorConstants;
/**
* The class in this SCXML object model that corresponds to the
@@ -121,7 +122,21 @@ public class If extends Action {
Context ctx = scInstance.getContext(parentTarget);
Evaluator eval = scInstance.getEvaluator();
ctx.setLocal(getNamespacesKey(), getNamespaces());
- execute = eval.evalCond(ctx, cond).booleanValue();
+ Boolean rslt = Boolean.FALSE;
+ try {
+ rslt = eval.evalCond(ctx, cond);
+ if (rslt == null) {
+ if (appLog.isDebugEnabled()) {
+ appLog.debug("Treating as false because the cond expression was evaluated as null: '" + cond + "'");
+ }
+ rslt = Boolean.FALSE;
+ }
+ } catch (SCXMLExpressionException e) {
+ rslt = Boolean.FALSE;
+ errRep.onError(ErrorConstants.EXPRESSION_ERROR, "Treating as false due to error: " + e.getMessage(), this);
+ // TODO: place the error 'error.execution' in the internal event queue. (section "3.12.2 Errors")
+ }
+ execute = rslt.booleanValue();
ctx.setLocal(getNamespacesKey(), null);
// The "if" statement is a "container"
for (Action aa : actions) {
Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/semantics/SCXMLSemanticsImpl.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/semantics/SCXMLSemanticsImpl.java?rev=1547807&r1=1547806&r2=1547807&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/semantics/SCXMLSemanticsImpl.java (original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/semantics/SCXMLSemanticsImpl.java Wed Dec 4 14:36:26 2013
@@ -395,7 +395,7 @@ public class SCXMLSemanticsImpl implemen
continue; //makes no sense to eval guard cond.
}
// guard condition check
- Boolean rslt;
+ Boolean rslt = Boolean.FALSE;
String expr = t.getCond();
if (SCXMLHelper.isStringEmpty(expr)) {
rslt = Boolean.TRUE;
@@ -405,11 +405,17 @@ public class SCXMLSemanticsImpl implemen
ctx.setLocal(NAMESPACES_KEY, t.getNamespaces());
rslt = scInstance.getEvaluator().evalCond(ctx,
t.getCond());
+ if (rslt == null) {
+ if (appLog.isDebugEnabled()) {
+ appLog.debug("Treating as false because the cond expression was evaluated as null: '" + t.getCond() + "'");
+ }
+ rslt = Boolean.FALSE;
+ }
ctx.setLocal(NAMESPACES_KEY, null);
} catch (SCXMLExpressionException e) {
rslt = Boolean.FALSE;
- errRep.onError(ErrorConstants.EXPRESSION_ERROR, e
- .getMessage(), t);
+ errRep.onError(ErrorConstants.EXPRESSION_ERROR, "Treating as false due to error: " + e.getMessage(), t);
+ // TODO: place the error 'error.execution' in the internal event queue. (section "3.12.2 Errors")
}
}
if (!rslt.booleanValue()) {
Modified: commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/SCXMLExecutorTest.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/SCXMLExecutorTest.java?rev=1547807&r1=1547806&r2=1547807&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/SCXMLExecutorTest.java (original)
+++ commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/SCXMLExecutorTest.java Wed Dec 4 14:36:26 2013
@@ -17,7 +17,9 @@
package org.apache.commons.scxml2;
import java.net.URL;
+import java.util.HashMap;
import java.util.HashSet;
+import java.util.Map;
import java.util.Set;
import org.apache.commons.scxml2.env.SimpleContext;
@@ -37,7 +39,8 @@ public class SCXMLExecutorTest {
// Test data
private URL microwave01jsp, microwave02jsp, microwave01jexl,
microwave02jexl, microwave03jexl, microwave04jexl, microwave05jexl, transitions01,
- transitions02, transitions03, transitions04, transitions05, transitions06, prefix01, send01, send02;
+ transitions02, transitions03, transitions04, transitions05, transitions06, prefix01, send01, send02,
+ transitionsWithCond01;
private SCXMLExecutor exec;
/**
@@ -77,6 +80,8 @@ public class SCXMLExecutorTest {
getResource("org/apache/commons/scxml2/send-01.xml");
send02 = this.getClass().getClassLoader().
getResource("org/apache/commons/scxml2/send-02.xml");
+ transitionsWithCond01 = this.getClass().getClassLoader().
+ getResource("org/apache/commons/scxml2/transitions-with-cond-01.xml");
}
/**
@@ -86,7 +91,8 @@ public class SCXMLExecutorTest {
public void tearDown() {
microwave01jsp = microwave02jsp = microwave01jexl = microwave02jexl =
microwave04jexl = microwave05jexl = transitions01 = transitions02 = transitions03 =
- transitions04 = transitions05 = transitions06 = prefix01 = send01 = send02 = null;
+ transitions04 = transitions05 = transitions06 = prefix01 = send01 = send02 =
+ transitionsWithCond01 = null;
}
/**
@@ -276,6 +282,38 @@ public class SCXMLExecutorTest {
Assert.assertTrue(exec.getCurrentStatus().isFinal());
}
+ @Test
+ public void testSCXMLExecutorTransitionsWithCond01Sample() throws Exception {
+ SCXML scxml = SCXMLTestHelper.parse(transitionsWithCond01);
+ Assert.assertNotNull(scxml);
+ exec = SCXMLTestHelper.getExecutor(scxml);
+ Assert.assertNotNull(exec);
+
+ Map<String, Object> payload = new HashMap<String, Object>();
+
+ // with _eventdata['keyed'] set to true, transition should happen as expected.
+ payload.put("keyed", Boolean.TRUE);
+ SCXMLTestHelper.assertPostTriggerState(exec, "open", payload, "opened");
+ // turn back to closed
+ SCXMLTestHelper.assertPostTriggerState(exec, "close", payload, "closed");
+
+ // with _eventdata['keyed'] set to false, transition shouldn't happen as expected.
+ payload.put("keyed", Boolean.FALSE);
+ SCXMLTestHelper.assertPostTriggerState(exec, "open", payload, "closed");
+
+ // with _eventdata['keyed'] set to null, transition shouldn't happen as expected.
+ payload.clear();
+ SCXMLTestHelper.assertPostTriggerState(exec, "open", payload, "closed");
+
+ // with _eventdata set to null, transition shouldn't happen as expected.
+ SCXMLTestHelper.assertPostTriggerState(exec, "open", null, "closed");
+
+ // transition to locked for testing
+ SCXMLTestHelper.assertPostTriggerState(exec, "lock", null, "locked");
+ // due to intentional expression syntax error, it catches an exception and so treat the cond as false
+ SCXMLTestHelper.assertPostTriggerState(exec, "unlock", null, "locked");
+ }
+
private void checkMicrowave01Sample() throws Exception {
Set<TransitionTarget> currentStates = SCXMLTestHelper.fireEvent(exec, "turn_on");
Assert.assertEquals(1, currentStates.size());
@@ -288,5 +326,6 @@ public class SCXMLExecutorTest {
String id = ((State)currentStates.iterator().next()).getId();
Assert.assertTrue(id.equals("closed") || id.equals("cooking"));
}
+
}
Modified: commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/SCXMLTestHelper.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/SCXMLTestHelper.java?rev=1547807&r1=1547806&r2=1547807&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/SCXMLTestHelper.java (original)
+++ commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/SCXMLTestHelper.java Wed Dec 4 14:36:26 2013
@@ -195,8 +195,12 @@ public class SCXMLTestHelper {
}
public static Set<TransitionTarget> fireEvent(SCXMLExecutor exec, String name) throws Exception {
+ return fireEvent(exec, name, null);
+ }
+
+ public static Set<TransitionTarget> fireEvent(SCXMLExecutor exec, String name, Object payload) throws Exception {
TriggerEvent[] evts = {new TriggerEvent(name,
- TriggerEvent.SIGNAL_EVENT, null)};
+ TriggerEvent.SIGNAL_EVENT, payload)};
exec.triggerEvents(evts);
return exec.getCurrentStatus().getStates();
}
@@ -213,14 +217,24 @@ public class SCXMLTestHelper {
public static void assertPostTriggerState(SCXMLExecutor exec,
String triggerEventName, String expectedStateId) throws Exception {
+ assertPostTriggerState(exec, triggerEventName, null, expectedStateId);
+ }
+
+ public static void assertPostTriggerState(SCXMLExecutor exec,
+ String triggerEventName, Object payload, String expectedStateId) throws Exception {
assertPostTriggerState(exec, new TriggerEvent(triggerEventName,
- TriggerEvent.SIGNAL_EVENT), expectedStateId);
+ TriggerEvent.SIGNAL_EVENT, payload), expectedStateId);
}
public static void assertPostTriggerStates(SCXMLExecutor exec,
String triggerEventName, String[] expectedStateIds) throws Exception {
+ assertPostTriggerStates(exec, triggerEventName, null, expectedStateIds);
+ }
+
+ public static void assertPostTriggerStates(SCXMLExecutor exec,
+ String triggerEventName, Object payload, String[] expectedStateIds) throws Exception {
assertPostTriggerStates(exec, new TriggerEvent(triggerEventName,
- TriggerEvent.SIGNAL_EVENT), expectedStateIds);
+ TriggerEvent.SIGNAL_EVENT, payload), expectedStateIds);
}
public static void assertPostTriggerState(SCXMLExecutor exec,
Added: commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/transitions-with-cond-01.xml
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/transitions-with-cond-01.xml?rev=1547807&view=auto
==============================================================================
--- commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/transitions-with-cond-01.xml (added)
+++ commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/transitions-with-cond-01.xml Wed Dec 4 14:36:26 2013
@@ -0,0 +1,44 @@
+<?xml version="1.0"?>
+<!--
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.
+-->
+<!--
+ Uses SCXMLReader
+-->
+<scxml xmlns="http://www.w3.org/2005/07/scxml"
+ version="1.0"
+ initial="closed">
+
+ <state id="closed">
+ <transition event="open" cond="_eventdata['keyed']" target="opened" />
+ <transition event="lock" target="locked" />
+ </state>
+
+ <state id="locked">
+ <transition event="unlock" cond="_eventdata :-( intendedSyntaxError" target="closed" />
+ </state>
+
+ <state id="opened">
+ <onentry>
+ <if cond="_eventdata['newface']">
+ <log expr="'Welcome!'" />
+ </if>
+ </onentry>
+ <transition event="close" target="closed" />
+ </state>
+
+</scxml>
+