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.semantics;
18  
19  import junit.framework.Test;
20  import junit.framework.TestCase;
21  import junit.framework.TestSuite;
22  
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  public class TransitionTargetComparatorTest extends TestCase {
28  
29      public TransitionTargetComparatorTest(String testName) {
30          super(testName);
31      }
32  
33      public static Test suite() {
34          return new TestSuite(TransitionTargetComparatorTest.class);
35      }
36  
37      public static void main(String args[]) {
38          String[] testCaseName = { TransitionTargetComparatorTest.class.getName()};
39          junit.textui.TestRunner.main(testCaseName);
40      }
41  
42      private TransitionTargetComparator comparator;
43      
44      public void setUp() {
45          comparator = new TransitionTargetComparator();
46      }
47      
48      public void testComparatorEquals() {
49          TransitionTarget target = new State();
50          
51          assertEquals(0, comparator.compare(target, target));
52      }
53      
54      public void testComparatorNegative() {
55          TransitionTarget target1 = new State();
56          TransitionTarget target2 = new State();
57          
58          target1.setParent(target2);
59          
60          assertEquals(-1, comparator.compare(target1, target2));
61      }
62      
63      public void testComparatorPositive() {
64          TransitionTarget target1 = new State();
65          TransitionTarget target2 = new State();
66          
67          target2.setParent(target1);
68          
69          assertEquals(1, comparator.compare(target1, target2));
70      }
71      
72      public void testComparatorFirstMoreParents() {
73          TransitionTarget target1 = new State();
74          TransitionTarget parent1 = new State();
75          TransitionTarget parent2 = new State();
76  
77          parent1.setParent(parent2);
78          target1.setParent(parent1);
79          
80          TransitionTarget target2 = new State();
81          TransitionTarget parent3 = new State();
82          
83          target2.setParent(parent3);
84          
85          assertEquals(-1, comparator.compare(target1, target2));
86      }
87      
88      public void testComparatorSecondMoreParents() {
89          TransitionTarget target1 = new State();
90          TransitionTarget parent1 = new State();
91          TransitionTarget parent2 = new State();
92  
93          parent1.setParent(parent2);
94          target1.setParent(parent1);
95          
96          TransitionTarget target2 = new State();
97          TransitionTarget parent3 = new State();
98          
99          target2.setParent(parent3);
100         
101         assertEquals(1, comparator.compare(target2, target1)); // reversed
102     }
103     
104     public void testComparatorSameParent() {
105         State target1 = new State();
106         Parallel parent = new Parallel();
107         target1.setParent(parent);
108         parent.addChild(target1);
109         
110         State target2 = new State();
111         target2.setParent(parent);
112         parent.addChild(target2);
113         
114         assertEquals(1, comparator.compare(target1, target2));
115     }
116 }