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