KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > graph > def > SuperStateTest


1 package org.jbpm.graph.def;
2
3 import java.util.List JavaDoc;
4
5 import junit.framework.TestCase;
6
7 public class SuperStateTest extends TestCase {
8   
9   public void testChildNodeAdditions() {
10     SuperState superState = new SuperState();
11     superState.addNode(new Node("one"));
12     superState.addNode(new Node("two"));
13     superState.addNode(new Node("three"));
14     
15     assertEquals(3, superState.getNodes().size());
16     assertEquals(superState.getNode("one"), superState.getNodes().get(0));
17     assertEquals(superState.getNode("two"), superState.getNodes().get(1));
18     assertEquals(superState.getNode("three"), superState.getNodes().get(2));
19   }
20
21   public void testChildNodeRemoval() {
22     SuperState superState = new SuperState();
23     superState.addNode(new Node("one"));
24     superState.addNode(new Node("two"));
25     superState.addNode(new Node("three"));
26     superState.removeNode(superState.getNode("two"));
27     
28     assertEquals(2, superState.getNodes().size());
29     assertEquals(superState.getNode("one"), superState.getNodes().get(0));
30     assertEquals(superState.getNode("three"), superState.getNodes().get(1));
31   }
32
33   public void testSuperStateXmlParsing() {
34     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
35       "<process-definition>" +
36       " <super-state name='phase one'>" +
37       " <node name='ignition' />" +
38       " <node name='explosion' />" +
39       " <node name='cleanup' />" +
40       " <node name='repare' />" +
41       " </super-state>" +
42       "</process-definition>"
43     );
44
45     assertEquals(1, processDefinition.getNodes().size());
46     
47     SuperState phaseOne = (SuperState) processDefinition.getNode("phase one");
48     assertNotNull(phaseOne);
49     assertEquals(4, phaseOne.getNodes().size());
50     
51     assertSame(phaseOne.getNode("ignition"), phaseOne.getNodes().get(0));
52     assertSame(phaseOne.getNode("explosion"), phaseOne.getNodes().get(1));
53     assertSame(phaseOne.getNode("cleanup"), phaseOne.getNodes().get(2));
54     assertSame(phaseOne.getNode("repare"), phaseOne.getNodes().get(3));
55
56     // check parents
57
assertSame(processDefinition, phaseOne.getParent());
58     assertSame(phaseOne, phaseOne.getNode("ignition").getParent());
59     assertSame(phaseOne, phaseOne.getNode("explosion").getParent());
60     assertSame(phaseOne, phaseOne.getNode("cleanup").getParent());
61     assertSame(phaseOne, phaseOne.getNode("repare").getParent());
62     
63     // check process definition references
64
assertSame(processDefinition, phaseOne.getParent());
65     assertSame(processDefinition, phaseOne.getNode("ignition").getProcessDefinition());
66     assertSame(processDefinition, phaseOne.getNode("explosion").getProcessDefinition());
67     assertSame(processDefinition, phaseOne.getNode("cleanup").getProcessDefinition());
68     assertSame(processDefinition, phaseOne.getNode("repare").getProcessDefinition());
69   }
70
71   public void testNestedSuperStateXmlParsing() {
72     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
73       "<process-definition>" +
74       " <super-state name='phase one'>" +
75       " <node name='ignition' />" +
76       " <node name='explosion' />" +
77       " <super-state name='cleanup'>" +
78       " <node name='take brush' />" +
79       " <node name='sweep floor' />" +
80       " </super-state>" +
81       " <node name='repare' />" +
82       " </super-state>" +
83       "</process-definition>"
84     );
85     
86     SuperState phaseOne = (SuperState) processDefinition.getNode("phase one");
87     assertNotNull(phaseOne);
88     // check phase one parent
89
assertSame(processDefinition, phaseOne.getParent());
90     
91     // check phase one child nodes
92
List JavaDoc phaseOneNodes = phaseOne.getNodes();
93     assertNotNull(phaseOneNodes);
94     assertEquals(4, phaseOneNodes.size());
95     assertEquals("ignition", ((Node)phaseOneNodes.get(0)).getName());
96     assertEquals("explosion", ((Node)phaseOneNodes.get(1)).getName());
97     assertEquals("cleanup", ((Node)phaseOneNodes.get(2)).getName());
98     assertEquals("repare", ((Node)phaseOneNodes.get(3)).getName());
99     // check phase one child nodes parent
100
assertEquals(phaseOne, ((Node)phaseOneNodes.get(0)).getParent());
101     assertEquals(phaseOne, ((Node)phaseOneNodes.get(1)).getParent());
102     assertEquals(phaseOne, ((Node)phaseOneNodes.get(2)).getParent());
103     assertEquals(phaseOne, ((Node)phaseOneNodes.get(3)).getParent());
104
105     SuperState cleanUp = (SuperState) processDefinition.findNode("phase one/cleanup");
106     assertSame( cleanUp, phaseOneNodes.get(2));
107     // check clea up child nodes
108
List JavaDoc cleanUpNodes = cleanUp.getNodes();
109     assertNotNull(cleanUpNodes);
110     assertEquals(2, cleanUpNodes.size());
111     assertEquals("take brush", ((Node)cleanUpNodes.get(0)).getName());
112     assertEquals("sweep floor", ((Node)cleanUpNodes.get(1)).getName());
113     // check clean up child nodes parent
114
assertEquals(cleanUp, ((Node)cleanUpNodes.get(0)).getParent());
115     assertEquals(cleanUp, ((Node)cleanUpNodes.get(1)).getParent());
116
117     assertEquals("take brush", processDefinition.findNode("phase one/cleanup/take brush").getName());
118   }
119
120   public void testNestedSuperStateXmlTransitionParsing() {
121     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
122       "<process-definition>" +
123       " <node name='preparation'>" +
124       " <transition name='local' to='phase one' />" +
125       " <transition name='superstate-node' to='phase one/cleanup' />" +
126       " <transition name='nested-superstate-node' to='phase one/cleanup/take brush' />" +
127       " </node>" +
128       " <super-state name='phase one'>" +
129       " <node name='ignition'>" +
130       " <transition name='parent' to='../preparation' />" +
131       " <transition name='local' to='explosion' />" +
132       " <transition name='superstate-node' to='cleanup/take brush' />" +
133       " </node>" +
134       " <node name='explosion' />" +
135       " <super-state name='cleanup'>" +
136       " <node name='take brush'>" +
137       " <transition name='recursive-parent' to='../../preparation' />" +
138       " <transition name='parent' to='../explosion' />" +
139       " <transition name='local' to='take brush' />" +
140       " <transition name='absolute-superstate' to='/phase one' />" +
141       " <transition name='absolute-node' to='/phase two' />" +
142       " </node>" +
143       " <node name='sweep floor' />" +
144       " </super-state>" +
145       " <node name='repare' />" +
146       " </super-state>" +
147       " <node name='phase two' />" +
148       "</process-definition>"
149     );
150
151     Node preparation = processDefinition.getNode("preparation");
152     assertNotNull(preparation);
153     assertEquals("phase one", preparation.getLeavingTransition("local").getTo().getName() );
154     assertEquals("cleanup", preparation.getLeavingTransition("superstate-node").getTo().getName() );
155     assertEquals("take brush", preparation.getLeavingTransition("nested-superstate-node").getTo().getName() );
156
157     Node ignition = processDefinition.findNode("phase one/ignition");
158     assertNotNull(ignition);
159     assertEquals("preparation", ignition.getLeavingTransition("parent").getTo().getName() );
160     assertEquals("explosion", ignition.getLeavingTransition("local").getTo().getName() );
161     assertEquals("take brush", ignition.getLeavingTransition("superstate-node").getTo().getName() );
162
163     Node cleanup = processDefinition.findNode("phase one/cleanup/take brush");
164     assertNotNull(ignition);
165     assertEquals("preparation", cleanup.getLeavingTransition("recursive-parent").getTo().getName() );
166     assertEquals("explosion", cleanup.getLeavingTransition("parent").getTo().getName() );
167     assertEquals("take brush", cleanup.getLeavingTransition("local").getTo().getName() );
168     assertEquals("phase one", cleanup.getLeavingTransition("absolute-superstate").getTo().getName() );
169     assertEquals("phase two", cleanup.getLeavingTransition("absolute-node").getTo().getName() );
170   }
171
172   public void testSuperStateTransitionsParsing() {
173     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
174       "<process-definition>" +
175       " <node name='preparation'>" +
176       " <transition to='phase one' />" +
177       " </node>" +
178       " <super-state name='phase one'>" +
179       " <transition name='to-node' to='preparation' />" +
180       " <transition name='self' to='phase one' />" +
181       " </super-state>" +
182       "</process-definition>"
183     );
184     
185     assertEquals("preparation", processDefinition.getNode("phase one").getLeavingTransition("to-node").getTo().getName() );
186     assertEquals("phase one", processDefinition.getNode("phase one").getLeavingTransition("self").getTo().getName() );
187     assertEquals("phase one", processDefinition.getNode("preparation").getDefaultLeavingTransition().getTo().getName() );
188   }
189
190   public void testLeavingTransitionOfSuperState() {
191     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
192       "<process-definition>" +
193       " <super-state name='super'>" +
194       " <node name='child' />" +
195       " <transition name='take me' to='super' />" +
196       " </super-state>" +
197       "</process-definition>"
198     );
199     
200     Node child = processDefinition.findNode("super/child");
201     Transition takeMe = processDefinition.getNode("super").getLeavingTransition("take me");
202     
203     assertSame(takeMe, child.getLeavingTransition("take me") );
204   }
205 }
206
Popular Tags