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