1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.commons.scxml2.io;
18
19 import java.io.IOException;
20 import java.util.LinkedHashMap;
21 import java.util.Map;
22
23 import org.apache.commons.scxml2.model.Parallel;
24 import org.apache.commons.scxml2.model.SCXML;
25 import org.apache.commons.scxml2.model.Script;
26 import org.apache.commons.scxml2.model.State;
27 import org.junit.Assert;
28 import org.junit.Test;
29
30 import javax.xml.stream.XMLStreamException;
31
32 public class SCXMLWriterTest {
33
34 @Test
35 public void testSerializeSCXMLNoStates() throws IOException, XMLStreamException {
36 SCXML scxml = new SCXML();
37 Map<String, String> namespaces = new LinkedHashMap<String, String>();
38 namespaces.put("", "http://www.w3.org/2005/07/scxml");
39 namespaces.put("cs", "http://commons.apache.org/scxml");
40 namespaces.put("foo", "http://f.o.o");
41 namespaces.put("bar", "http://b.a.r");
42 scxml.setNamespaces(namespaces);
43 scxml.setVersion("version1");
44 scxml.setInitial("off");
45 State s = new State();
46 s.setId(scxml.generateTransitionTargetId());
47 scxml.addChild(s);
48
49 String assertValue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
50 + "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" xmlns:cs=\"http://commons.apache.org/scxml\" "
51 + "xmlns:foo=\"http://f.o.o\" xmlns:bar=\"http://b.a.r\" "
52 + "version=\"version1\" initial=\"off\"><!--http://commons.apache.org/scxml--><state></state>"
53 + "</scxml>";
54
55 Assert.assertEquals(assertValue, SCXMLWriter.write(scxml, new SCXMLWriter.Configuration(true, false)));
56 }
57
58 @Test
59 public void testSerializeSCXMLState() throws IOException, XMLStreamException {
60 SCXML scxml = new SCXML();
61 Map<String, String> namespaces = new LinkedHashMap<String, String>();
62 scxml.setNamespaces(namespaces);
63 scxml.setVersion("1.0");
64 scxml.setInitial("S1");
65
66 State s1 = new State();
67 s1.setId("S1");
68
69 scxml.addChild(s1);
70
71 String assertValue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
72 + "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" "
73 + "xmlns:cs=\"http://commons.apache.org/scxml\" version=\"1.0\" initial=\"S1\">"
74 + "<!--http://commons.apache.org/scxml--><state id=\"S1\"></state></scxml>";
75
76 Assert.assertEquals(assertValue, SCXMLWriter.write(scxml, new SCXMLWriter.Configuration(true, false)));
77 }
78
79 @Test
80 public void testSerializeParallel() throws IOException, XMLStreamException {
81
82 SCXML scxml = new SCXML();
83 Map<String, String> namespaces = new LinkedHashMap<String, String>();
84 scxml.setNamespaces(namespaces);
85 scxml.setVersion("1.0");
86 scxml.setInitial("par");
87
88 Parallel par = new Parallel();
89 par.setId("par");
90
91 State s1 = new State();
92 s1.setId("S1");
93
94 State s11 = new State();
95 s11.setId("S11");
96
97 s1.addChild(s11);
98
99 State s2 = new State();
100 s2.setId("S2");
101
102 State s21 = new State();
103 s21.setId("S21");
104
105 s2.addChild(s21);
106
107 par.addChild(s1);
108 par.addChild(s2);
109
110 scxml.addChild(par);
111
112 String assertValue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
113 + "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" xmlns:cs=\"http://commons.apache.org/scxml\" "
114 + "version=\"1.0\" initial=\"par\">"
115 + "<!--http://commons.apache.org/scxml-->"
116 + "<parallel id=\"par\">"
117 + "<state id=\"S1\">"
118 + "<state id=\"S11\"></state>"
119 + "</state>"
120 + "<state id=\"S2\">"
121 + "<state id=\"S21\"></state>"
122 + "</state>"
123 + "</parallel>"
124 + "</scxml>";
125
126 Assert.assertEquals(assertValue, SCXMLWriter.write(scxml, new SCXMLWriter.Configuration(true, false)));
127 }
128
129 @Test
130 public void testSerializeGlobalScript() throws IOException, XMLStreamException {
131 SCXML scxml = new SCXML();
132 Map<String, String> namespaces = new LinkedHashMap<String, String>();
133 scxml.setNamespaces(namespaces);
134 scxml.setVersion("1.0");
135 scxml.setInitial("S1");
136
137 Script script = new Script();
138 script.setGlobalScript(true);
139 script.setBody("foo=\"abc\"");
140 scxml.setGlobalScript(script);
141
142 State s1 = new State();
143 s1.setId("S1");
144
145 scxml.addChild(s1);
146
147 String assertValue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
148 + "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" "
149 + "xmlns:cs=\"http://commons.apache.org/scxml\" version=\"1.0\" initial=\"S1\">"
150 + "<!--http://commons.apache.org/scxml--><script><![CDATA[foo=\"abc\"]]></script><state id=\"S1\"></state></scxml>";
151
152 Assert.assertEquals(assertValue, SCXMLWriter.write(scxml, new SCXMLWriter.Configuration(true, false)));
153 }
154 }