Return-Path: X-Original-To: apmail-commons-commits-archive@minotaur.apache.org Delivered-To: apmail-commons-commits-archive@minotaur.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 26801106D1 for ; Thu, 28 Nov 2013 05:59:47 +0000 (UTC) Received: (qmail 53183 invoked by uid 500); 28 Nov 2013 05:59:42 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 53131 invoked by uid 500); 28 Nov 2013 05:59:38 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 53122 invoked by uid 99); 28 Nov 2013 05:59:36 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Nov 2013 05:59:36 +0000 X-ASF-Spam-Status: No, hits=-2000.0 required=5.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 28 Nov 2013 05:59:32 +0000 Received: from eris.apache.org (localhost [127.0.0.1]) by eris.apache.org (Postfix) with ESMTP id 14159238883D; Thu, 28 Nov 2013 05:59:11 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit 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 -0000 To: commits@commons.apache.org From: woonsan@apache.org X-Mailer: svnmailer-1.0.9 Message-Id: <20131128055911.14159238883D@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org 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 targets = scxml.getTargets(); Map 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 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 = + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + + private static final String SCXML_WITH_NO_INITIAL = + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + + private static final String SCXML_WITH_ILLEGAL_INITIAL = + "\n" + + " \n" + + " \n" + + " \n" + + " \n" + + ""; + + @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; + } + +}