    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.scxml.io;
    18
    19  import java.util.ArrayList;
    20  import java.util.List;
    21
    22  import junit.framework.Test;
    23  import junit.framework.TestCase;
    24  import junit.framework.TestSuite;
    25
    26  import org.apache.commons.scxml.model.Assign;
    27  import org.apache.commons.scxml.model.Cancel;
    28  import org.apache.commons.scxml.model.Else;
    29  import org.apache.commons.scxml.model.ElseIf;
    30  import org.apache.commons.scxml.model.Exit;
    31  import org.apache.commons.scxml.model.If;
    32  import org.apache.commons.scxml.model.Log;
    33  import org.apache.commons.scxml.model.OnEntry;
    34  import org.apache.commons.scxml.model.OnExit;
    35  import org.apache.commons.scxml.model.SCXML;
    36  import org.apache.commons.scxml.model.Send;
    37  import org.apache.commons.scxml.model.State;
    38  import org.apache.commons.scxml.model.TransitionTarget;
    39  import org.apache.commons.scxml.model.Var;
    40
    41  public class SCXMLSerializerTest extends TestCase {
    42
    43      public SCXMLSerializerTest(String testName) {
    44          super(testName);
    45      }
    46
    47      public static Test suite() {
    48          return new TestSuite(SCXMLSerializerTest.class);
    49      }
    50
    51      public static void main(String args[]) {
    52          String[] testCaseName = { SCXMLSerializerTest.class.getName()};
    53          junit.textui.TestRunner.main(testCaseName);
    54      }
    55
    56      public void testSerializeSCXMLNoStates() {
    57          SCXML scxml = new SCXML();
    58          scxml.setXmlns("namespace");
    59          scxml.setVersion("version1");
    60          scxml.setInitialstate("off");
    61          scxml.addState(new State());
    62
    63          String assertValue = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"
    64              + "<scxml xmlns=\"namespace\" version=\"version1\" "
    65              + "initialstate=\"off\">\n <state>\n </state>\n</scxml>\n";
    66
    67          assertEquals(assertValue, SCXMLSerializer.serialize(scxml));
    68      }
    69
    70      public void testSerializeSend() {
    71          Send send = new Send();
    72          send.setSendid("1");
    73          send.setTarget("newTarget");
    74          send.setTargettype("newTargetType");
    75          send.setNamelist("names");
    76          send.setDelay("4 secs");
    77          send.setEvent("turnoff");
    78          send.setHints("guess");
    79
    80          String assertValue = " <send sendid=\"1\" " +
    81                  "target=\"newTarget\" " +
    82                  "targetType=\"newTargetType\" " +
    83                  "namelist=\"names\" " +
    84                  "delay=\"4 secs\" " +
    85                  "events=\"turnoff\" " +
    86                  "hints=\"guess\">\n </send>\n";
    87
    88          StringBuffer returnValue = new StringBuffer();
    89          SCXMLSerializer.serializeSend(returnValue, send, " ");
    90
    91          assertEquals(assertValue.toString(), returnValue.toString());
    92      }
    93
    94      public void testSerializeActionsListNull() {
    95          TransitionTarget target = new State();
    96          target.setId("1");
    97
    98          StringBuffer returnValue = new StringBuffer();
    99          boolean returnBoolean = SCXMLSerializer.serializeActions(returnValue, null, " ");
    100
    101         assertFalse(returnBoolean);
    102         assertEquals(0, returnValue.length());
    103     }
    104
    105     public void testSerializeActionsVar() {
    106         Var var = new Var();
    107         var.setName("newName");
    108         var.setExpr("newExpression");
    109
    110         List values = new ArrayList();
    111         values.add(var);
    112
    113         String actualValue = " <var name=\"newName\" expr=\"newExpression\"/>\n";
    114
    115         StringBuffer returnValue = new StringBuffer();
    116         boolean returnBoolean = SCXMLSerializer.serializeActions(returnValue, values, " ");
    117
    118         assertFalse(returnBoolean);
    119         assertEquals(actualValue, returnValue.toString());
    120     }
    121
    122     public void testSerializeActionsAssign() {
    123         Assign assign = new Assign();
    124         assign.setName("newName");
    125         assign.setExpr("newExpression");
    126
    127         List values = new ArrayList();
    128         values.add(assign);
    129
    130         String actualValue = " <assign name=\"newName\" expr=\"newExpression\"/>\n";
    131
    132         StringBuffer returnValue = new StringBuffer();
    133         boolean returnBoolean = SCXMLSerializer.serializeActions(returnValue, values, " ");
    134
    135         assertFalse(returnBoolean);
    136         assertEquals(actualValue, returnValue.toString());
    137     }
    138
    139     public void testSerializeActionsCancel() {
    140         Cancel cancel = new Cancel();
    141         cancel.setSendid("1");
    142
    143         List values = new ArrayList();
    144         values.add(cancel);
    145
    146         String actualValue = " <cancel sendid=\"1\"/>\n";
    147
    148         StringBuffer returnValue = new StringBuffer();
    149         boolean returnBoolean = SCXMLSerializer.serializeActions(returnValue, values, " ");
    150
    151         assertFalse(returnBoolean);
    152         assertEquals(actualValue, returnValue.toString());
    153     }
    154
    155     public void testSerializeActionsLog() {
    156         Log log = new Log();
    157         log.setExpr("newExpression");
    158
    159         List values = new ArrayList();
    160         values.add(log);
    161
    162         String actualValue = " <log expr=\"newExpression\"/>\n";
    163
    164         StringBuffer returnValue = new StringBuffer();
    165         boolean returnBoolean = SCXMLSerializer.serializeActions(returnValue, values, " ");
    166
    167         assertFalse(returnBoolean);
    168         assertEquals(actualValue, returnValue.toString());
    169     }
    170
    171     public void testSerializeActionsExit() {
    172         Exit exit = new Exit();
    173         exit.setExpr("newExpression");
    174         exit.setNamelist("names");
    175
    176         List values = new ArrayList();
    177         values.add(exit);
    178
    179         String actualValue = " <exit expr=\"newExpression\" namelist=\"names\"/>\n";
    180
    181         StringBuffer returnValue = new StringBuffer();
    182         boolean returnBoolean = SCXMLSerializer.serializeActions(returnValue, values, " ");
    183
    184         assertTrue(returnBoolean);
    185         assertEquals(actualValue, returnValue.toString());
    186     }
    187
    188     public void testSerializeActionsElse() {
    189         Else elseValue = new Else();
    190
    191         List values = new ArrayList();
    192         values.add(elseValue);
    193
    194         String actualValue = " <else/>\n";
    195
    196         StringBuffer returnValue = new StringBuffer();
    197         boolean returnBoolean = SCXMLSerializer.serializeActions(returnValue, values, " ");
    198
    199         assertFalse(returnBoolean);
    200         assertEquals(actualValue, returnValue.toString());
    201     }
    202
    203     public void testSerializeActionsElseIf() {
    204         ElseIf elseIf = new ElseIf();
    205         elseIf.setCond("newCondition");
    206
    207         List values = new ArrayList();
    208         values.add(elseIf);
    209
    210         String actualValue = " <elseif cond=\"newCondition\" />\n";
    211
    212         StringBuffer returnValue = new StringBuffer();
    213         boolean returnBoolean = SCXMLSerializer.serializeActions(returnValue, values, " ");
    214
    215         assertFalse(returnBoolean);
    216         assertEquals(actualValue, returnValue.toString());
    217     }
    218
    219     public void testSerializeIf() {
    220         If ifValue = new If();
    221         ifValue.setCond("newCondition");
    222
    223         List values = new ArrayList();
    224         values.add(ifValue);
    225
    226         String actualValue = " <if cond=\"newCondition\">\n </if>\n";
    227
    228         StringBuffer returnValue = new StringBuffer();
    229         boolean returnBoolean = SCXMLSerializer.serializeActions(returnValue, values, " ");
    230
    231         assertFalse(returnBoolean);
    232         assertEquals(actualValue, returnValue.toString());
    233     }
    234
    235     public void testSerializeOnEntrySizeZero() {
    236         TransitionTarget target = new State();
    237         target.setOnEntry(new OnEntry());
    238
    239         String actualValue = "";
    240
    241         StringBuffer returnValue = new StringBuffer();
    242         SCXMLSerializer.serializeOnEntry(returnValue, target, " ");
    243
    244         assertEquals(actualValue, returnValue.toString());
    245     }
    246
    247     public void testSerializeOnEntry() {
    248         TransitionTarget target = new State();
    249
    250         OnEntry onEntry = new OnEntry();
    251         onEntry.addAction(new Else());
    252
    253         target.setOnEntry(onEntry);
    254
    255         String actualValue = " <onentry>\n  <else/>\n </onentry>\n";
    256
    257         StringBuffer returnValue = new StringBuffer();
    258         SCXMLSerializer.serializeOnEntry(returnValue, target, " ");
    259
    260         assertEquals(actualValue, returnValue.toString());
    261     }
    262
    263     public void testSerializeOnExitSizeZero() {
    264         TransitionTarget target = new State();
    265         target.setOnExit(new OnExit());
    266
    267         String actualValue = "";
    268
    269         StringBuffer returnValue = new StringBuffer();
    270         SCXMLSerializer.serializeOnExit(returnValue, target, " ");
    271
    272         assertEquals(actualValue, returnValue.toString());
    273     }
    274
    275     public void testSerializeOnExit() {
    276         TransitionTarget target = new State();
    277
    278         OnExit onExit = new OnExit();
    279         onExit.addAction(new Else());
    280
    281         target.setOnExit(onExit);
    282
    283         String actualValue = " <onexit>\n  <else/>\n </onexit>\n";
    284
    285         StringBuffer returnValue = new StringBuffer();
    286         SCXMLSerializer.serializeOnExit(returnValue, target, " ");
    287
    288         assertEquals(actualValue, returnValue.toString());
    289     }
    290
    291 }