001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *     http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.commons.scxml2.model;
018
019import static org.junit.Assert.assertEquals;
020import static org.junit.Assert.assertNull;
021import static org.junit.Assert.fail;
022
023import java.io.StringReader;
024
025import org.apache.commons.scxml2.SCXMLExecutor;
026import org.apache.commons.scxml2.SCXMLTestHelper;
027import org.junit.Test;
028
029public class ScxmlInitialAttributeTest {
030
031    private static final String SCXML_WITH_LEGAL_INITIAL =
032            "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" version=\"1.0\" initial=\"s1\">\n" +
033            "  <state id=\"s1\">\n" +
034            "    <transition event=\"end\" target=\"fine\" />\n" +
035            "  </state>\n" +
036            "  <final id=\"fine\"/>\n" +
037            "</scxml>";
038
039    private static final String SCXML_WITH_NO_INITIAL =
040            "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" version=\"1.0\">\n" +
041            "  <state id=\"s1\">\n" +
042            "    <transition event=\"end\" target=\"fine\" />\n" +
043            "  </state>\n" +
044            "  <final id=\"fine\"/>\n" +
045            "</scxml>";
046
047    private static final String SCXML_WITH_ILLEGAL_INITIAL =
048            "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" version=\"1.0\" initial=\"nonexisting\">\n" +
049            "  <state id=\"s1\">\n" +
050            "    <transition event=\"end\" target=\"fine\" />\n" +
051            "  </state>\n" +
052            "  <final id=\"fine\"/>\n" +
053            "</scxml>";
054
055    @Test
056    public void testInitial() throws Exception {
057        SCXML scxml = SCXMLTestHelper.parse(new StringReader(SCXML_WITH_LEGAL_INITIAL), null);
058        assertEquals("The initial state ID reading was wrong.", "s1", scxml.getInitial());
059        TransitionTarget tt = scxml.getInitialTransition().getTargets().iterator().next();
060        assertEquals("The initial state resolution was wrong.", "s1", tt.getId());
061        SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml);
062        exec.go();
063        assertEquals(scxml.getTargets().get("s1"), exec.getStatus().getStates().iterator().next());
064    }
065
066    @Test
067    public void testNoInitial() throws Exception {
068        SCXML scxml = SCXMLTestHelper.parse(new StringReader(SCXML_WITH_NO_INITIAL), null);
069        assertNull(scxml.getInitial());
070        TransitionTarget tt = scxml.getInitialTransition().getTargets().iterator().next();
071        assertEquals("The initial state resolution was wrong.", "s1", tt.getId());
072        SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml);
073        exec.go();
074        assertEquals(scxml.getTargets().get("s1"), exec.getStatus().getStates().iterator().next());
075    }
076
077    @Test
078    public void testIllegalInitial() throws Exception {
079        try {
080            SCXMLTestHelper.parse(new StringReader(SCXML_WITH_ILLEGAL_INITIAL), null);
081            fail("SCXML reading should have failed due to the illegal state ID in SCXML.");
082        } catch (ModelException e) {
083            // expected because of the non-existing initial state id
084        }
085    }
086}