View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *     http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
16   */
17  package org.apache.commons.scxml2.model;
18  
19  import static org.junit.Assert.assertEquals;
20  import static org.junit.Assert.assertNull;
21  import static org.junit.Assert.fail;
22  
23  import java.io.StringReader;
24  
25  import org.apache.commons.scxml2.SCXMLExecutor;
26  import org.apache.commons.scxml2.SCXMLTestHelper;
27  import org.junit.Test;
28  
29  public class ScxmlInitialAttributeTest {
30  
31      private static final String SCXML_WITH_LEGAL_INITIAL =
32              "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" version=\"1.0\" initial=\"s1\">\n" +
33              "  <state id=\"s1\">\n" +
34              "    <transition event=\"end\" target=\"fine\" />\n" +
35              "  </state>\n" +
36              "  <final id=\"fine\"/>\n" +
37              "</scxml>";
38  
39      private static final String SCXML_WITH_NO_INITIAL =
40              "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" version=\"1.0\">\n" +
41              "  <state id=\"s1\">\n" +
42              "    <transition event=\"end\" target=\"fine\" />\n" +
43              "  </state>\n" +
44              "  <final id=\"fine\"/>\n" +
45              "</scxml>";
46  
47      private static final String SCXML_WITH_ILLEGAL_INITIAL =
48              "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" version=\"1.0\" initial=\"nonexisting\">\n" +
49              "  <state id=\"s1\">\n" +
50              "    <transition event=\"end\" target=\"fine\" />\n" +
51              "  </state>\n" +
52              "  <final id=\"fine\"/>\n" +
53              "</scxml>";
54  
55      @Test
56      public void testInitial() throws Exception {
57          SCXML scxml = SCXMLTestHelper.parse(new StringReader(SCXML_WITH_LEGAL_INITIAL), null);
58          assertEquals("The initial state ID reading was wrong.", "s1", scxml.getInitial());
59          TransitionTarget tt = scxml.getInitialTransition().getTargets().iterator().next();
60          assertEquals("The initial state resolution was wrong.", "s1", tt.getId());
61          SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml);
62          exec.go();
63          assertEquals(scxml.getTargets().get("s1"), exec.getStatus().getStates().iterator().next());
64      }
65  
66      @Test
67      public void testNoInitial() throws Exception {
68          SCXML scxml = SCXMLTestHelper.parse(new StringReader(SCXML_WITH_NO_INITIAL), null);
69          assertNull(scxml.getInitial());
70          TransitionTarget tt = scxml.getInitialTransition().getTargets().iterator().next();
71          assertEquals("The initial state resolution was wrong.", "s1", tt.getId());
72          SCXMLExecutor exec = SCXMLTestHelper.getExecutor(scxml);
73          exec.go();
74          assertEquals(scxml.getTargets().get("s1"), exec.getStatus().getStates().iterator().next());
75      }
76  
77      @Test
78      public void testIllegalInitial() throws Exception {
79          try {
80              SCXMLTestHelper.parse(new StringReader(SCXML_WITH_ILLEGAL_INITIAL), null);
81              fail("SCXML reading should have failed due to the illegal state ID in SCXML.");
82          } catch (ModelException e) {
83              // expected because of the non-existing initial state id
84          }
85      }
86  }