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