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