1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
84 }
85 }
86 }