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