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.model;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
23  public class PathTest extends TestCase {
24  
25      public PathTest(String testName) {
26          super(testName);
27      }
28  
29      public static Test suite() {
30          return new TestSuite(PathTest.class);
31      }
32  
33      public static void main(String args[]) {
34          String[] testCaseName = { PathTest.class.getName()};
35          junit.textui.TestRunner.main(testCaseName);
36      }
37      
38      public void testConstructorNull() {
39          Path path = new Path(null, null);
40          
41          assertNull(path.getScope());
42      }
43  
44      public void testConstructorNullState() {
45          Path path = new Path(new State(), null);
46          
47          assertTrue(path.getScope() instanceof State);
48      }
49  
50      public void testConstructorStates() {
51          TransitionTarget source = new State();
52          source.setId("1");
53  
54          TransitionTarget target = new State();
55          target.setId("2");
56          
57          Path path = new Path(source, target);
58          
59          assertNull(path.getScope());
60          assertEquals(1, path.getUpwardSegment().size());
61          assertEquals("1", ((State)path.getUpwardSegment().get(0)).getId());
62  
63          assertEquals(1, path.getDownwardSegment().size());
64          assertEquals("2", ((State)path.getDownwardSegment().get(0)).getId());
65          
66          assertFalse(path.isCrossRegion());
67      }
68      
69      public void testConstructorSourceCrossRegion() {
70          Parallel region = new Parallel();
71          
72          TransitionTarget source = new State();
73          source.setId("1");
74          source.setParent(region);
75          
76          TransitionTarget target = new State();
77          target.setId("2");
78          
79          Path path = new Path(source, target);
80          
81          assertTrue(path.isCrossRegion());
82      }
83  
84      public void testConstructorTargetCrossRegion() {
85          Parallel region = new Parallel();
86          
87          TransitionTarget source = new State();
88          source.setId("1");
89          
90          TransitionTarget target = new State();
91          target.setId("2");
92          target.setParent(region);
93          
94          Path path = new Path(source, target);
95          
96          assertTrue(path.isCrossRegion());
97      }
98  
99      public void testConstructorParentTarget() {
100         TransitionTarget source = new State();
101         source.setId("1");
102 
103         TransitionTarget target = new State();
104         target.setId("2");
105 
106         source.setParent(target);
107 
108         Path path = new Path(source, target);
109 
110         assertNull(path.getScope());
111     }
112 
113     public void testConstructorParentSource() {
114         TransitionTarget source = new State();
115         source.setId("1");
116 
117         TransitionTarget target = new State();
118         target.setId("2");
119 
120         target.setParent(source);
121 
122         Path path = new Path(source, target);
123 
124         assertNull(path.getScope());
125     }
126     
127     public void testConstructorParent() {
128         TransitionTarget source = new State();
129         source.setId("1");
130 
131         TransitionTarget target = new State();
132         target.setId("2");
133 
134         State parent = new State();
135         parent.setId("parentid");
136         
137         target.setParent(parent);
138         source.setParent(parent);
139 
140         Path path = new Path(source, target);
141 
142         assertEquals("parentid", path.getScope().getId());
143     }
144     
145     public void testConstructorParentParallel() {
146         TransitionTarget source = new State();
147         source.setId("1");
148 
149         TransitionTarget target = new State();
150         target.setId("2");
151 
152         Parallel parent = new Parallel();
153         parent.setId("parentid");
154         
155         target.setParent(parent);
156         source.setParent(parent);
157 
158         Path path = new Path(source, target);
159 
160         assertNull(path.getScope());
161     }
162     
163     public void testConstructorParentParallelParent() {
164         TransitionTarget source = new State();
165         source.setId("1");
166 
167         TransitionTarget target = new State();
168         target.setId("2");
169 
170         Parallel parent = new Parallel();
171         parent.setId("parentid");
172         
173         State parentOfParent = new State();
174         parentOfParent.setId("superParent");
175  
176         parent.setParent(parentOfParent);
177         
178         target.setParent(parent);
179         source.setParent(parent);
180 
181         Path path = new Path(source, target);
182 
183         assertEquals("superParent", path.getScope().getId());
184     }
185     
186     public void testGetRegionsExitedNull() {
187         Path path = new Path(new State(), null);
188 
189         assertEquals(0, path.getRegionsExited().size());
190     }
191     
192     public void testGetRegionsExitedNotRegion() {
193         TransitionTarget source = new State();
194         source.setId("1");
195 
196         TransitionTarget target = new State();
197         target.setId("2");
198         
199         Path path = new Path(source, target);
200 
201         assertEquals(0, path.getRegionsExited().size());
202     }
203     
204     public void testGetRegionsExitedParallel() {
205         TransitionTarget source = new Parallel();
206         source.setId("1");
207 
208         TransitionTarget target = new Parallel();
209         target.setId("2");
210         
211         Path path = new Path(source, target);
212 
213         assertEquals(0, path.getRegionsExited().size());
214     }
215     
216     public void testGetRegionsExited() {
217         Parallel region = new Parallel();
218         
219         TransitionTarget source = new State();
220         source.setId("1");
221         source.setParent(region);
222         
223         TransitionTarget target = new State();
224         target.setId("2");
225         
226         Path path = new Path(source, target);
227         
228         assertEquals(1, path.getRegionsExited().size());
229         assertEquals("1", ((State)path.getRegionsExited().get(0)).getId());
230     }
231 
232     public void testGetRegionsEnteredNull() {
233         Path path = new Path(new State(), null);
234 
235         assertEquals(0, path.getRegionsEntered().size());
236     }
237     
238     public void testGetRegionsEnteredNotRegion() {
239         TransitionTarget source = new State();
240         source.setId("1");
241 
242         TransitionTarget target = new State();
243         target.setId("2");
244         
245         Path path = new Path(source, target);
246 
247         assertEquals(0, path.getRegionsEntered().size());
248     }
249     
250     public void testGetRegionsEnteredParallel() {
251         TransitionTarget source = new Parallel();
252         source.setId("1");
253 
254         TransitionTarget target = new Parallel();
255         target.setId("2");
256         
257         Path path = new Path(source, target);
258 
259         assertEquals(0, path.getRegionsEntered().size());
260     }
261     
262     public void testGetRegionsEntered() {
263         Parallel region = new Parallel();
264         
265         TransitionTarget source = new State();
266         source.setId("1");
267         
268         TransitionTarget target = new State();
269         target.setId("2");
270         target.setParent(region);
271         
272         Path path = new Path(source, target);
273         
274         assertEquals(1, path.getRegionsEntered().size());
275         assertEquals("2", ((State)path.getRegionsEntered().get(0)).getId());
276     }
277 
278 }