commons-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From woon...@apache.org
Subject svn commit: r1546294 - in /commons/proper/scxml/trunk/src: main/java/org/apache/commons/scxml2/io/ main/java/org/apache/commons/scxml2/model/ test/java/org/apache/commons/scxml2/ test/java/org/apache/commons/scxml2/model/
Date Thu, 28 Nov 2013 05:59:10 GMT
Author: woonsan
Date: Thu Nov 28 05:59:10 2013
New Revision: 1546294

URL: http://svn.apache.org/r1546294
Log:
SCXML-178: fixing bug when initial is not specified; if initial is not specified, the first
child target must be the initial target instead.

Added:
    commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/model/ScxmlInitialAttributeTest.java
Modified:
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/ModelUpdater.java
    commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.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/io/ModelUpdater.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/ModelUpdater.java?rev=1546294&r1=1546293&r2=1546294&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/ModelUpdater.java
(original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/io/ModelUpdater.java
Thu Nov 28 05:59:10 2013
@@ -58,14 +58,23 @@ final class ModelUpdater {
      */
    static void updateSCXML(final SCXML scxml) throws ModelException {
        String initial = scxml.getInitial();
-       //we have to use getTargets() here since the initialTarget can be
-       //an indirect descendant
-       TransitionTarget initialTarget = scxml.getTargets().get(initial);
-       if (initialTarget == null) {
-           // Where do we, where do we go?
-           logAndThrowModelError(ERR_SCXML_NO_INIT, new Object[] {
-               initial });
+       TransitionTarget initialTarget = null;
+
+       if (initial != null) {
+           //we have to use getTargets() here since the initialTarget can be
+           //an indirect descendant
+           initialTarget = scxml.getTargets().get(initial);
+           if (initialTarget == null) {
+               // Where do we, where do we go?
+               logAndThrowModelError(ERR_SCXML_NO_INIT, new Object[] {
+                   initial });
+           }
+       } else {
+           // If 'initial' is not specified, the default initial state is
+           // the first child state in document order.
+           initialTarget = scxml.getFirstChild();
        }
+
        scxml.setInitialTarget(initialTarget);
        Map<String, TransitionTarget> targets = scxml.getTargets();
        Map<String, TransitionTarget> children = scxml.getChildren();

Modified: commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java?rev=1546294&r1=1546293&r2=1546294&view=diff
==============================================================================
--- commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java (original)
+++ commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml2/model/SCXML.java Thu
Nov 28 05:59:10 2013
@@ -162,6 +162,17 @@ public class SCXML implements Serializab
     }
 
     /**
+     * Get the first immediate child target of the SCXML root.
+     *
+     * @return TransitionTarget Returns the first immediate child target of the SCXML root.
+     *
+     * @since 2.0
+     */
+    public final TransitionTarget getFirstChild() {
+        return children.values().iterator().next();
+    }
+
+    /**
      * Add an immediate child target of the SCXML root.
      *
      * @param tt The transition target to be added to the states Map.

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=1546294&r1=1546293&r2=1546294&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
Thu Nov 28 05:59:10 2013
@@ -21,6 +21,7 @@ import java.io.FileInputStream;
 import java.io.FileOutputStream;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.io.Reader;
 import java.io.StringReader;
 import java.net.URL;
 import java.util.ArrayList;
@@ -30,8 +31,6 @@ import java.util.Set;
 
 import javax.xml.parsers.DocumentBuilderFactory;
 
-import org.junit.Assert;
-
 import org.apache.commons.scxml2.env.SimpleDispatcher;
 import org.apache.commons.scxml2.env.Tracer;
 import org.apache.commons.scxml2.env.jexl.JexlEvaluator;
@@ -41,6 +40,7 @@ import org.apache.commons.scxml2.model.C
 import org.apache.commons.scxml2.model.ModelException;
 import org.apache.commons.scxml2.model.SCXML;
 import org.apache.commons.scxml2.model.TransitionTarget;
+import org.junit.Assert;
 import org.w3c.dom.Document;
 import org.xml.sax.InputSource;
 /**
@@ -79,6 +79,16 @@ public class SCXMLTestHelper {
         return roundtrip;
     }
 
+    public static SCXML parse(final Reader scxmlReader, final List<CustomAction> customActions)
throws Exception {
+        Assert.assertNotNull(scxmlReader);
+        SCXML scxml = null;
+        Configuration configuration = new Configuration(null, null, customActions);
+        scxml = SCXMLReader.read(scxmlReader, configuration);
+        Assert.assertNotNull(scxml);
+        SCXML roundtrip = testModelSerializability(scxml);
+        return roundtrip;
+    }
+
     public static SCXMLExecutor getExecutor(final URL url) throws Exception {
         SCXML scxml = parse(url);
         Evaluator evaluator = new JexlEvaluator();

Added: commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/model/ScxmlInitialAttributeTest.java
URL: http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/model/ScxmlInitialAttributeTest.java?rev=1546294&view=auto
==============================================================================
--- commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/model/ScxmlInitialAttributeTest.java
(added)
+++ commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml2/model/ScxmlInitialAttributeTest.java
Thu Nov 28 05:59:10 2013
@@ -0,0 +1,94 @@
+/*
+ * 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.
+ */
+package org.apache.commons.scxml2.model;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.fail;
+
+import java.io.StringReader;
+
+import org.apache.commons.scxml2.SCXMLExecutor;
+import org.apache.commons.scxml2.SCXMLTestHelper;
+import org.apache.commons.scxml2.env.jexl.JexlContext;
+import org.apache.commons.scxml2.env.jexl.JexlEvaluator;
+import org.junit.Test;
+
+public class ScxmlInitialAttributeTest {
+
+    private static final String SCXML_WITH_LEGAL_INITIAL =
+            "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" initial=\"s1\">\n" +
+            "  <state id=\"s1\">\n" +
+            "    <transition event=\"end\" target=\"fine\" />\n" +
+            "  </state>\n" +
+            "  <final id=\"fine\"/>\n" +
+            "</scxml>";
+
+    private static final String SCXML_WITH_NO_INITIAL =
+            "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\">\n" +
+            "  <state id=\"s1\">\n" +
+            "    <transition event=\"end\" target=\"fine\" />\n" +
+            "  </state>\n" +
+            "  <final id=\"fine\"/>\n" +
+            "</scxml>";
+
+    private static final String SCXML_WITH_ILLEGAL_INITIAL =
+            "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" initial=\"nonexisting\">\n"
+
+            "  <state id=\"s1\">\n" +
+            "    <transition event=\"end\" target=\"fine\" />\n" +
+            "  </state>\n" +
+            "  <final id=\"fine\"/>\n" +
+            "</scxml>";
+
+    @Test
+    public void testInitial() throws Exception {
+        SCXML scxml = SCXMLTestHelper.parse(new StringReader(SCXML_WITH_LEGAL_INITIAL), null);
+        assertEquals("The initial state ID reading was wrong.", "s1", scxml.getInitial());
+        assertNotNull(scxml.getInitialTarget());
+        assertEquals("The initial state resolution was wrong.", "s1", scxml.getInitialTarget().getId());
+        SCXMLExecutor exec = executeSCXML(scxml);
+        assertEquals(scxml.getTargets().get("s1"), exec.getCurrentStatus().getStates().iterator().next());
+    }
+
+    @Test
+    public void testNoInitial() throws Exception {
+        SCXML scxml = SCXMLTestHelper.parse(new StringReader(SCXML_WITH_NO_INITIAL), null);
+        assertNull(scxml.getInitial());
+        assertNotNull("The initial state ID reading was wrong.", scxml.getInitialTarget());
+        assertEquals("The initial state resolution was wrong.", "s1", scxml.getInitialTarget().getId());
+        SCXMLExecutor exec = executeSCXML(scxml);
+        assertEquals(scxml.getTargets().get("s1"), exec.getCurrentStatus().getStates().iterator().next());
+    }
+
+    @Test
+    public void testIllegalInitial() throws Exception {
+        try {
+            SCXMLTestHelper.parse(new StringReader(SCXML_WITH_ILLEGAL_INITIAL), null);
+            fail("SCXML reading should have failed due to the illegal state ID in SCXML.");
+        } catch (ModelException e) {
+            // expected because of the non-existing initial state id
+        }
+    }
+
+    private SCXMLExecutor executeSCXML(SCXML scxml) throws Exception {
+        SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml, new JexlContext(), new JexlEvaluator());
+        exec.go();
+        return exec;
+    }
+
+}



Mime
View raw message