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.io;
018
019import java.io.IOException;
020import java.util.LinkedHashMap;
021import java.util.Map;
022
023import org.apache.commons.scxml2.model.Parallel;
024import org.apache.commons.scxml2.model.SCXML;
025import org.apache.commons.scxml2.model.Script;
026import org.apache.commons.scxml2.model.State;
027import org.junit.Assert;
028import org.junit.Test;
029
030import javax.xml.stream.XMLStreamException;
031
032public class SCXMLWriterTest {
033
034    @Test
035    public void testSerializeSCXMLNoStates() throws IOException, XMLStreamException {
036        SCXML scxml = new SCXML();
037        Map<String, String> namespaces = new LinkedHashMap<String, String>();
038        namespaces.put("", "http://www.w3.org/2005/07/scxml");
039        namespaces.put("cs", "http://commons.apache.org/scxml");
040        namespaces.put("foo", "http://f.o.o");
041        namespaces.put("bar", "http://b.a.r");
042        scxml.setNamespaces(namespaces);
043        scxml.setVersion("version1");
044        scxml.setInitial("off");
045        State s = new State();
046        s.setId(scxml.generateTransitionTargetId());
047        scxml.addChild(s);
048        
049        String assertValue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
050            + "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" xmlns:cs=\"http://commons.apache.org/scxml\" "
051            + "xmlns:foo=\"http://f.o.o\" xmlns:bar=\"http://b.a.r\" "
052            + "version=\"version1\" initial=\"off\"><!--http://commons.apache.org/scxml--><state></state>"
053            + "</scxml>";
054
055        Assert.assertEquals(assertValue, SCXMLWriter.write(scxml, new SCXMLWriter.Configuration(true, false)));
056    }
057    
058    @Test
059    public void testSerializeSCXMLState() throws IOException, XMLStreamException {
060        SCXML scxml = new SCXML();
061        Map<String, String> namespaces = new LinkedHashMap<String, String>();
062        scxml.setNamespaces(namespaces);
063        scxml.setVersion("1.0");
064        scxml.setInitial("S1");
065
066        State s1 = new State();
067        s1.setId("S1");
068
069        scxml.addChild(s1);
070
071        String assertValue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
072            + "<scxml xmlns=\"http://www.w3.org/2005/07/scxml\" "
073            + "xmlns:cs=\"http://commons.apache.org/scxml\" version=\"1.0\" initial=\"S1\">"
074            + "<!--http://commons.apache.org/scxml--><state id=\"S1\"></state></scxml>";
075
076        Assert.assertEquals(assertValue, SCXMLWriter.write(scxml, new SCXMLWriter.Configuration(true, false)));
077    }
078    
079    @Test
080    public void testSerializeParallel() throws IOException, XMLStreamException {
081
082        SCXML scxml = new SCXML();
083        Map<String, String> namespaces = new LinkedHashMap<String, String>();
084        scxml.setNamespaces(namespaces);
085        scxml.setVersion("1.0");
086        scxml.setInitial("par");
087
088        Parallel par = new Parallel();
089        par.setId("par");
090
091        State s1 = new State();
092        s1.setId("S1");
093
094        State s11 = new State();
095        s11.setId("S11");
096
097        s1.addChild(s11);
098
099        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}