    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;
    18
    19  import java.util.HashSet;
    20  import java.util.Set;
    21
    22  import junit.framework.Test;
    23  import junit.framework.TestCase;
    24  import junit.framework.TestSuite;
    25
    26  import org.apache.commons.scxml.env.MockErrorReporter;
    27  import org.apache.commons.scxml.env.SimpleErrorReporter;
    28  import org.apache.commons.scxml.model.Parallel;
    29  import org.apache.commons.scxml.model.State;
    30  import org.apache.commons.scxml.model.TransitionTarget;
    31  import org.apache.commons.scxml.semantics.ErrorConstants;
    32
    33  public class SCXMLHelperTest extends TestCase {
    34
    35      public SCXMLHelperTest(String testName) {
    36          super(testName);
    37      }
    38
    39      public static Test suite() {
    40          return new TestSuite(SCXMLHelperTest.class);
    41      }
    42
    43      public static void main(String args[]) {
    44          String[] testCaseName = { SCXMLHelperTest.class.getName()};
    45          junit.textui.TestRunner.main(testCaseName);
    46      }
    47
    48      public void testIsStringEmptyNull() {
    49          assertTrue(SCXMLHelper.isStringEmpty(null));
    50      }
    51
    52      public void testIsStringEmptyZeroLength() {
    53          assertTrue(SCXMLHelper.isStringEmpty(""));
    54      }
    55
    56      public void testIsStringEmpty() {
    57          assertFalse(SCXMLHelper.isStringEmpty("value"));
    58      }
    59
    60      public void testIsDescendantNullParent() {
    61          TransitionTarget state = new State();
    62          TransitionTarget context = new State();
    63
    64          assertFalse(SCXMLHelper.isDescendant(state, context));
    65      }
    66
    67      public void testIsDescendantNotEqual() {
    68          TransitionTarget state = new State();
    69          state.setParent(new State());
    70          TransitionTarget context = new State();
    71
    72          assertFalse(SCXMLHelper.isDescendant(state, context));
    73      }
    74
    75      public void testIsDescendantEqual() {
    76          TransitionTarget state = new State();
    77          TransitionTarget context = new State();
    78          state.setParent(context);
    79
    80          assertTrue(SCXMLHelper.isDescendant(state, context));
    81      }
    82
    83      public void testIsDescendantParentEqual() {
    84          TransitionTarget state = new State();
    85          TransitionTarget context = new State();
    86          TransitionTarget parent = new State();
    87
    88          parent.setParent(context);
    89          state.setParent(parent);
    90
    91          assertTrue(SCXMLHelper.isDescendant(state, context));
    92      }
    93
    94      public void testGetAncestorClosureEmptySet() {
    95          Set states = new HashSet();
    96
    97          Set returnValue = SCXMLHelper.getAncestorClosure(states, new HashSet());
    98
    99          assertEquals(0, returnValue.size());
    100     }
    101
    102     public void testGetAncestorClosureUpperBoundNotNullAndContains() {
    103         Set states = new HashSet();
    104         TransitionTarget state = new State();
    105         state.setId("1");
    106         states.add(state);
    107
    108         Set upperBounds = new HashSet();
    109         upperBounds.add(state);
    110
    111         Set returnValue = SCXMLHelper.getAncestorClosure(states, upperBounds);
    112
    113         assertEquals(1, returnValue.size());
    114         assertEquals("1", ((TransitionTarget)returnValue.toArray()[0]).getId());
    115     }
    116
    117     public void testGetAncestorClosureContainsParent() {
    118         Set states = new HashSet();
    119         TransitionTarget state = new State();
    120         state.setId("1");
    121         state.setParent(state);
    122         states.add(state);
    123
    124         Set upperBounds = new HashSet();
    125
    126         Set returnValue = SCXMLHelper.getAncestorClosure(states, upperBounds);
    127
    128         assertEquals(1, returnValue.size());
    129         assertEquals("1", ((TransitionTarget)returnValue.toArray()[0]).getId());
    130     }
    131
    132     public void testIsLegalConfigNoStates() {
    133         Set states = new HashSet();
    134
    135         assertTrue(SCXMLHelper.isLegalConfig(states, new SimpleErrorReporter()));
    136     }
    137
    138     public void testIsLegalConfigInvalidParallel() {
    139         Set states = new HashSet();
    140         Parallel parallel = new Parallel();
    141
    142         Parallel parent = new Parallel();
    143         parent.setId("4");
    144
    145         State state1 = new State();
    146         state1.setId("1");
    147         State state2 = new State();
    148         state2.setId("2");
    149
    150         parent.addChild(state1);
    151         parent.addChild(state2);
    152
    153         parallel.setParent(parent);
    154
    155         states.add(parallel);
    156
    157         MockErrorReporter errorReporter = new MockErrorReporter();
    158
    159         assertFalse(SCXMLHelper.isLegalConfig(states, errorReporter));
    160         assertEquals(ErrorConstants.ILLEGAL_CONFIG, errorReporter.getErrCode());
    161         assertEquals("Not all AND states active for parallel 4", errorReporter.getErrDetail());
    162     }
    163
    164     public void testIsLegalConfigMultipleTopLevel() {
    165         Set states = new HashSet();
    166
    167         State state1 = new State();
    168         state1.setId("1");
    169         State state2 = new State();
    170         state2.setId("2");
    171
    172         states.add(state1);
    173         states.add(state2);
    174
    175         MockErrorReporter errorReporter = new MockErrorReporter();
    176
    177         assertTrue(SCXMLHelper.isLegalConfig(states, errorReporter));
    178         assertEquals(ErrorConstants.ILLEGAL_CONFIG, errorReporter.getErrCode());
    179         assertEquals("Multiple top-level OR states active!", errorReporter.getErrDetail());
    180     }
    181
    182     public void testIsLegalConfigMultipleStatesActive() {
    183         Set states = new HashSet();
    184
    185         State state1 = new State();
    186         state1.setId("1");
    187
    188         State state2 = new State();
    189         state2.setId("2");
    190
    191         State parent = new State();
    192         parent.setId("parentid");
    193
    194         state2.setParent(parent);
    195         state1.setParent(parent);
    196
    197         states.add(state1);
    198         states.add(state2);
    199
    200         MockErrorReporter errorReporter = new MockErrorReporter();
    201
    202         assertFalse(SCXMLHelper.isLegalConfig(states, errorReporter));
    203         assertEquals(ErrorConstants.ILLEGAL_CONFIG, errorReporter.getErrCode());
    204         assertEquals("Multiple OR states active for state parentid", errorReporter.getErrDetail());
    205     }
    206
    207     public void testGetLCASameTarget() {
    208         TransitionTarget target = new State();
    209         target.setId("1");
    210
    211         TransitionTarget returnValue = SCXMLHelper.getLCA(target, target);
    212
    213         assertEquals("1", returnValue.getId());
    214     }
    215
    216     public void testGetLCAIsDescendant() {
    217         TransitionTarget target = new State();
    218         target.setId("1");
    219
    220         TransitionTarget parent = new State();
    221         parent.setId("2");
    222
    223         target.setParent(parent);
    224
    225         TransitionTarget returnValue = SCXMLHelper.getLCA(target, parent);
    226
    227         assertEquals("2", returnValue.getId());
    228     }
    229
    230     public void testGetLCAIsDescendantReverse() {
    231         TransitionTarget target = new State();
    232         target.setId("1");
    233
    234         TransitionTarget parent = new State();
    235         parent.setId("2");
    236
    237         parent.setParent(target); // reversed
    238
    239         TransitionTarget returnValue = SCXMLHelper.getLCA(target, parent);
    240
    241         assertEquals("1", returnValue.getId());
    242     }
    243
    244     public void testGetLCANull() {
    245         TransitionTarget target = new State();
    246         target.setId("1");
    247
    248         TransitionTarget notParent = new State();
    249         notParent.setId("2");
    250
    251         TransitionTarget returnValue = SCXMLHelper.getLCA(target, notParent);
    252
    253         assertNull(returnValue);
    254     }
    255
    256     public void testGetLCADistantAncestor() {
    257         TransitionTarget target1 = new State();
    258         target1.setId("1");
    259
    260         TransitionTarget target2 = new State();
    261         target2.setId("2");
    262
    263         TransitionTarget parent = new State();
    264         parent.setId("3");
    265
    266         target1.setParent(parent);
    267         target2.setParent(parent);
    268
    269         TransitionTarget returnValue = SCXMLHelper.getLCA(target1, target2);
    270
    271         assertEquals("3", returnValue.getId());
    272     }
    273 }