    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;
    17
    18  import java.util.HashSet;
    19  import java.util.Set;
    20
    21  import org.apache.commons.jexl.JexlContext;
    22  import org.apache.commons.scxml.env.SimpleContext;
    23  import org.apache.commons.scxml.env.jexl.JexlEvaluator;
    24  import org.apache.commons.scxml.model.History;
    25  import org.apache.commons.scxml.model.State;
    26  import org.apache.commons.scxml.model.TransitionTarget;
    27
    28  import junit.framework.Test;
    29  import junit.framework.TestCase;
    30  import junit.framework.TestSuite;
    31
    32  public class SCInstanceTest extends TestCase {
    33
    34      public SCInstanceTest(String testName) {
    35          super(testName);
    36      }
    37
    38      public static Test suite() {
    39          return new TestSuite(SCInstanceTest.class);
    40      }
    41
    42      public static void main(String args[]) {
    43          String[] testCaseName = { SCInstanceTest.class.getName()};
    44          junit.textui.TestRunner.main(testCaseName);
    45      }
    46
    47      private SCInstance instance;
    48
    49      public void setUp() {
    50          instance = new SCInstance(null);
    51      }
    52
    53      public void testGetRootContextNull() {
    54          assertNull(instance.getRootContext());
    55      }
    56
    57      public void testGetRootContext() {
    58          Context context = new SimpleContext();
    59          context.set("name", "value");
    60
    61          instance.setRootContext(context);
    62          assertEquals("value", instance.getRootContext().get("name"));
    63      }
    64
    65      public void testGetRootContextEvaluator() {
    66          Evaluator evaluator = new JexlEvaluator();
    67
    68          instance.setEvaluator(evaluator);
    69
    70          assertTrue(instance.getRootContext() instanceof JexlContext);
    71      }
    72
    73      public void testGetContext() {
    74          TransitionTarget target = new State();
    75          target.setId("1");
    76
    77          Context context = new SimpleContext();
    78          context.set("name", "value");
    79
    80          instance.setContext(target, context);
    81
    82          assertEquals("value", instance.getContext(target).get("name"));
    83      }
    84
    85      public void testGetContextNullParent() {
    86          TransitionTarget target = new State();
    87          target.setId("1");
    88
    89          Context context = new SimpleContext();
    90          context.set("name", "value");
    91          instance.setRootContext(context);
    92
    93          Evaluator evaluator = new JexlEvaluator();
    94          instance.setEvaluator(evaluator);
    95
    96          assertEquals("value", instance.getContext(target).get("name"));
    97          assertEquals("value", instance.lookupContext(target).get("name"));
    98      }
    99
    100     public void testGetContextParent() {
    101         TransitionTarget target = new State();
    102         target.setId("1");
    103
    104         State parent = new State();
    105         parent.setId("parent");
    106
    107         target.setParent(parent);
    108
    109         Context context = new SimpleContext();
    110         context.set("name", "value");
    111         instance.setRootContext(context);
    112
    113         Evaluator evaluator = new JexlEvaluator();
    114         instance.setEvaluator(evaluator);
    115
    116         assertEquals("value", instance.getContext(target).get("name"));
    117         assertEquals("value", instance.lookupContext(target).get("name"));
    118     }
    119
    120     public void testGetLastConfigurationNull() {
    121         History history = new History();
    122
    123         Set returnConfiguration = instance.getLastConfiguration(history);
    124
    125         assertEquals(0, returnConfiguration.size());
    126     }
    127
    128
    129     public void testGetLastConfiguration() {
    130         History history = new History();
    131         history.setId("1");
    132
    133         Set configuration = new HashSet();
    134         configuration.add("value1");
    135         configuration.add("value2");
    136
    137         instance.setLastConfiguration(history, configuration);
    138
    139         Set returnConfiguration = instance.getLastConfiguration(history);
    140
    141         assertEquals(2, returnConfiguration.size());
    142         assertTrue(returnConfiguration.contains("value1"));
    143         assertTrue(returnConfiguration.contains("value2"));
    144     }
    145
    146     public void testIsEmpty() {
    147         assertTrue(instance.isEmpty(new History()));
    148     }
    149
    150     public void testIsEmptyFalse() {
    151         History history = new History();
    152         history.setId("1");
    153
    154         Set configuration = new HashSet();
    155         configuration.add("value1");
    156         configuration.add("value2");
    157
    158         instance.setLastConfiguration(history, configuration);
    159
    160         assertFalse(instance.isEmpty(history));
    161     }
    162
    163     public void testReset() {
    164         History history = new History();
    165         history.setId("1");
    166
    167         Set configuration = new HashSet();
    168         configuration.add("value1");
    169         configuration.add("value2");
    170
    171         instance.setLastConfiguration(history, configuration);
    172
    173         instance.reset(history);
    174
    175         assertTrue(instance.isEmpty(history));
    176     }
    177
    178 }