KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > jpdl > xml > ActionXmlTest


1 package org.jbpm.jpdl.xml;
2
3 import java.io.StringReader JavaDoc;
4 import java.util.List JavaDoc;
5
6 import org.dom4j.Element;
7 import org.jbpm.graph.def.Action;
8 import org.jbpm.graph.def.Event;
9 import org.jbpm.graph.def.Node;
10 import org.jbpm.graph.def.ProcessDefinition;
11 import org.jbpm.graph.def.Transition;
12 import org.jbpm.graph.node.State;
13 import org.jbpm.instantiation.Delegation;
14
15 public class ActionXmlTest extends AbstractXmlTestCase {
16   
17   public void testReadProcessDefinitionAction() {
18     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
19       "<process-definition>" +
20       " <event type='node-enter'>" +
21       " <action class='one'/>" +
22       " <action class='two'/>" +
23       " <action class='three'/>" +
24       " </event>" +
25       "</process-definition>"
26     );
27     Event event = processDefinition.getEvent("node-enter");
28     assertEquals(3, event.getActions().size());
29     assertEquals("one", ((Action)event.getActions().get(0)).getActionDelegation().getClassName());
30     assertEquals("two", ((Action)event.getActions().get(1)).getActionDelegation().getClassName());
31     assertEquals("three", ((Action)event.getActions().get(2)).getActionDelegation().getClassName());
32   }
33
34   public void testWriteProcessDefinitionAction() throws Exception JavaDoc {
35     ProcessDefinition processDefinition = new ProcessDefinition();
36     Event event = new Event("node-enter");
37     processDefinition.addEvent(event);
38     event.addAction(new Action(new Delegation("one")));
39     event.addAction(new Action(new Delegation("two")));
40     event.addAction(new Action(new Delegation("three")));
41     
42     Element eventElement = toXmlAndParse( processDefinition, "/process-definition/event" );
43     
44     List JavaDoc actionElements = eventElement.elements("action");
45
46     assertEquals(3, actionElements.size());
47     assertEquals("one", ((Element)actionElements.get(0)).attributeValue("class"));
48     assertEquals("two", ((Element)actionElements.get(1)).attributeValue("class"));
49     assertEquals("three", ((Element)actionElements.get(2)).attributeValue("class"));
50   }
51
52   public void testReadActionConfigType() {
53     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
54       "<process-definition>" +
55       " <action name='burps' class='org.foo.Burps' config-type='bean' />" +
56       "</process-definition>"
57     );
58   
59     assertEquals("bean", processDefinition.getAction("burps").getActionDelegation().getConfigType() );
60   }
61
62   public void testWriteActionConfigType() throws Exception JavaDoc {
63     ProcessDefinition processDefinition = new ProcessDefinition();
64     Delegation actionDelegate = new Delegation("one");
65     actionDelegate.setConfigType("bean");
66     Action action = new Action(actionDelegate);
67     action.setName("a");
68     processDefinition.addAction(action);
69     Element actionElement = toXmlAndParse( processDefinition, "/process-definition/action" );
70     assertEquals("bean", actionElement.attributeValue("config-type"));
71   }
72
73   public void testReadActionXmlConfiguration() {
74     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
75       "<process-definition>" +
76       " <action name='burps' class='org.foo.Burps' config-type='bean'>" +
77       " <id>63</id>" +
78       " <greeting>aloha</greeting>" +
79       " </action>" +
80       "</process-definition>"
81     );
82   
83     Action action = processDefinition.getAction("burps");
84     Delegation instantiatableDelegate = action.getActionDelegation();
85     assertTrue(instantiatableDelegate.getConfiguration().indexOf("<id>63</id>")!=-1);
86     assertTrue(instantiatableDelegate.getConfiguration().indexOf("<greeting>aloha</greeting>")!=-1 );
87   }
88
89   public void testWriteActionXmlConfiguration() throws Exception JavaDoc {
90     ProcessDefinition processDefinition = new ProcessDefinition();
91     Delegation actionDelegate = new Delegation("one");
92     actionDelegate.setConfiguration("<id>63</id><greeting>aloha</greeting>");
93     Action action = new Action(actionDelegate);
94     action.setName("a");
95     processDefinition.addAction(action);
96     Element actionElement = toXmlAndParse( processDefinition, "/process-definition/action" );
97     assertEquals("63", actionElement.elementTextTrim("id"));
98     assertEquals("aloha", actionElement.elementTextTrim("greeting"));
99   }
100
101   public void testReadActionTextConfiguration() {
102     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
103       "<process-definition>" +
104       " <action name='burps' class='org.foo.Burps' config-type='constructor'>" +
105       " a piece of configuration text" +
106       " </action>" +
107       "</process-definition>"
108     );
109   
110     Action action = processDefinition.getAction("burps");
111     Delegation instantiatableDelegate = action.getActionDelegation();
112     assertTrue(instantiatableDelegate.getConfiguration().indexOf("a piece of configuration text")!=-1);
113   }
114
115   public void testWriteActionTextConfiguration() throws Exception JavaDoc {
116     ProcessDefinition processDefinition = new ProcessDefinition();
117     Delegation actionDelegate = new Delegation("one");
118     actionDelegate.setConfiguration("a piece of configuration text");
119     actionDelegate.setConfigType("constructor");
120     Action action = new Action(actionDelegate);
121     action.setName("a");
122     processDefinition.addAction(action);
123     Element actionElement = toXmlAndParse( processDefinition, "/process-definition/action" );
124     assertEquals("a piece of configuration text", actionElement.getTextTrim());
125   }
126
127   public void testReadActionAcceptPropagatedEventsDefault() {
128     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
129       "<process-definition>" +
130       " <action name='burps' class='org.foo.Burps' />" +
131       "</process-definition>"
132     );
133   
134     Action action = processDefinition.getAction("burps");
135     assertTrue(action.acceptsPropagatedEvents());
136   }
137
138   public void testReadActionAcceptPropagatedEventsTrue() {
139     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
140       "<process-definition>" +
141       " <action name='burps' class='org.foo.Burps' accept-propagated-events='true' />" +
142       "</process-definition>"
143     );
144   
145     Action action = processDefinition.getAction("burps");
146     assertTrue(action.acceptsPropagatedEvents());
147   }
148
149   public void testReadActionAcceptPropagatedEventsFalse() {
150     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
151       "<process-definition>" +
152       " <action name='burps' class='org.foo.Burps' accept-propagated-events='false' />" +
153       "</process-definition>"
154     );
155   
156     Action action = processDefinition.getAction("burps");
157     assertFalse(action.acceptsPropagatedEvents());
158   }
159
160   public void testWriteActionAcceptPropagatedEventsDefault() throws Exception JavaDoc {
161     ProcessDefinition processDefinition = new ProcessDefinition();
162     Delegation actionDelegate = new Delegation("one");
163     Action action = new Action(actionDelegate);
164     action.setName("a");
165     processDefinition.addAction(action);
166     Element actionElement = toXmlAndParse( processDefinition, "/process-definition/action" );
167     assertNull(actionElement.attribute("accept-propagated-events"));
168   }
169
170   public void testWriteActionAcceptPropagatedEventsTrue() throws Exception JavaDoc {
171     ProcessDefinition processDefinition = new ProcessDefinition();
172     Delegation actionDelegate = new Delegation("one");
173     Action action = new Action(actionDelegate);
174     action.setName("a");
175     action.setPropagationAllowed(true);
176     processDefinition.addAction(action);
177     Element actionElement = toXmlAndParse( processDefinition, "/process-definition/action" );
178     assertNull(actionElement.attribute("accept-propagated-events"));
179   }
180
181   public void testWriteActionAcceptPropagatedEventsFalse() throws Exception JavaDoc {
182     ProcessDefinition processDefinition = new ProcessDefinition();
183     Delegation actionDelegate = new Delegation("one");
184     Action action = new Action(actionDelegate);
185     action.setName("a");
186     action.setPropagationAllowed(false);
187     processDefinition.addAction(action);
188     Element actionElement = toXmlAndParse( processDefinition, "/process-definition/action" );
189     assertEquals("false", actionElement.attributeValue("accept-propagated-events"));
190   }
191
192   public void testReadNodeActionName() {
193     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
194       "<process-definition>" +
195       " <node name='a'>" +
196       " <event type='node-enter'>" +
197       " <action name='burps' class='org.foo.Burps'/>" +
198       " </event>" +
199       " </node>" +
200       "</process-definition>"
201     );
202     Action burps = (Action)processDefinition.getNode("a").getEvent("node-enter").getActions().get(0);
203     assertEquals("burps", burps.getName());
204   }
205
206   public void testWriteNodeActionName() throws Exception JavaDoc {
207     ProcessDefinition processDefinition = new ProcessDefinition();
208     Node node = processDefinition.addNode( new Node() );
209     Delegation instantiatableDelegate = new Delegation();
210     instantiatableDelegate.setClassName("com.foo.Fighting");
211     node.setAction(new Action(instantiatableDelegate));
212     
213     Element actionElement = AbstractXmlTestCase.toXmlAndParse( processDefinition, "/process-definition/node/action" );
214     assertNotNull(actionElement);
215     assertEquals("action", actionElement.getName());
216     assertEquals("com.foo.Fighting", actionElement.attributeValue("class"));
217   }
218
219   public void testReadNodeEnterAction() {
220     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
221       "<process-definition>" +
222       " <node name='a'>" +
223       " <event type='node-enter'>" +
224       " <action class='org.foo.Burps'/>" +
225       " </event>" +
226       " </node>" +
227       "</process-definition>"
228     );
229     assertEquals("org.foo.Burps", ((Action)processDefinition.getNode("a").getEvent("node-enter").getActions().get(0)).getActionDelegation().getClassName());
230   }
231
232   public void testWriteNodeEnterAction() throws Exception JavaDoc {
233     ProcessDefinition processDefinition = new ProcessDefinition();
234     Node node = processDefinition.addNode( new Node() );
235     Delegation instantiatableDelegate = new Delegation();
236     instantiatableDelegate.setClassName("com.foo.Fighting");
237     node.addEvent(new Event("node-enter")).addAction(new Action(instantiatableDelegate));
238     Element element = AbstractXmlTestCase.toXmlAndParse( processDefinition, "/process-definition/node[1]/event[1]" );
239     
240     assertNotNull(element);
241     assertEquals("event", element.getName());
242     assertEquals("node-enter", element.attributeValue("type"));
243     assertEquals(1, element.elements("action").size());
244
245     element = element.element("action");
246     assertNotNull(element);
247     assertEquals("action", element.getName());
248     assertEquals("com.foo.Fighting", element.attributeValue("class"));
249   }
250
251   public void testParseAndWriteOfNamedEventActions() throws Exception JavaDoc {
252     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
253       "<process-definition>" +
254       " <node name='a'>" +
255       " <event type='node-enter'>" +
256       " <action name='burps' class='org.foo.Burps'/>" +
257       " </event>" +
258       " </node>" +
259       "</process-definition>"
260     );
261     Action burps = (Action)processDefinition.getNode("a").getEvent("node-enter").getActions().get(0);
262     assertSame(burps, processDefinition.getAction("burps"));
263     Element processDefinitionElement = toXmlAndParse( processDefinition, "/process-definition" );
264     assertEquals(0, processDefinitionElement.elements("action").size());
265   }
266
267   public void testParseStateAction() {
268     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
269       "<process-definition>" +
270       " <state name='a'>" +
271       " <event type='node-enter'>" +
272       " <action class='org.foo.Burps' config-type='constructor-text'>" +
273       " this text should be passed in the constructor" +
274       " </action>" +
275       " </event>" +
276       " </state>" +
277       "</process-definition>"
278     );
279     
280     Node node = processDefinition.getNode("a");
281     Event event = node.getEvent("node-enter");
282     Action action = (Action) event.getActions().iterator().next();
283     Delegation instantiatableDelegate = action.getActionDelegation();
284     assertEquals("org.foo.Burps", instantiatableDelegate.getClassName());
285     assertEquals("constructor-text", instantiatableDelegate.getConfigType());
286     assertTrue(instantiatableDelegate.getConfiguration().indexOf("this text should be passed in the constructor")!=-1 );
287   }
288
289   public void testParseTransitionAction() {
290     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
291       "<process-definition>" +
292       " <state name='a'>" +
293       " <transition to='b'>" +
294       " <action class='org.foo.Burps'/>" +
295       " </transition>" +
296       " </state>" +
297       " <state name='b' />" +
298       "</process-definition>"
299     );
300     
301     Node node = processDefinition.getNode("a");
302     assertEquals( 1, node.getLeavingTransitionsMap().size() );
303     Transition transition = node.getDefaultLeavingTransition();
304     Event event = transition.getEvent(Event.EVENTTYPE_TRANSITION);
305     Action action = (Action) event.getActions().iterator().next();
306     Delegation instantiatableDelegate = action.getActionDelegation();
307     assertEquals("org.foo.Burps", instantiatableDelegate.getClassName());
308   }
309
310   public void testParseReferencedAction() {
311     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
312       "<process-definition>" +
313       " <node name='a'>" +
314       " <transition to='b'>" +
315       " <action ref-name='scratch'/>" +
316       " </transition>" +
317       " </node>" +
318       " <node name='b' />" +
319       " <action name='scratch' class='com.itch.Scratch' />" +
320       "</process-definition>"
321     );
322     
323     Node node = processDefinition.getNode("a");
324     Transition transition = node.getDefaultLeavingTransition();
325     Event event = transition.getEvent(Event.EVENTTYPE_TRANSITION);
326     Action transitionAction = (Action) event.getActions().iterator().next();
327     
328     Action processAction = processDefinition.getAction("scratch");
329     assertEquals("scratch", processAction.getName());
330     assertSame(processAction, transitionAction.getReferencedAction());
331     
332     Delegation instantiatableDelegate = processAction.getActionDelegation();
333     assertEquals("com.itch.Scratch", instantiatableDelegate.getClassName());
334   }
335   
336   public void testParseActionWithoutClass() {
337     StringReader JavaDoc stringReader = new StringReader JavaDoc(
338       "<process-definition>" +
339       " <node>" +
340       " <event type='node-enter'>" +
341       " <action />" +
342       " </event>" +
343       " </node>" +
344       "</process-definition>");
345     JpdlXmlReader jpdlReader = new JpdlXmlReader(stringReader);
346     jpdlReader.readProcessDefinition();
347     assertTrue(Problem.containsProblemsOfLevel(jpdlReader.problems, Problem.LEVEL_WARNING));
348   }
349
350   public void testParseActionWithInvalidReference() {
351     StringReader JavaDoc stringReader = new StringReader JavaDoc(
352       "<process-definition>" +
353       " <node>" +
354       " <event type='node-enter'>" +
355       " <action ref-name='non-existing-action-name'/>" +
356       " </event>" +
357       " </node>" +
358       "</process-definition>");
359     JpdlXmlReader jpdlReader = new JpdlXmlReader(stringReader);
360     jpdlReader.readProcessDefinition();
361     assertTrue(Problem.containsProblemsOfLevel(jpdlReader.problems, Problem.LEVEL_WARNING));
362   }
363
364   public void testWriteTransitionAction() throws Exception JavaDoc {
365     ProcessDefinition processDefinition = ProcessDefinition.parseXmlString(
366       "<process-definition>" +
367       " <state name='a'>" +
368       " <transition to='b' />" +
369       " </state>" +
370       " <state name='b' />" +
371       "</process-definition>"
372     );
373     Transition transition = processDefinition.getNode("a").getDefaultLeavingTransition();
374     
375     Delegation instantiatableDelegate = new Delegation();
376     instantiatableDelegate.setClassName("com.foo.Fighting");
377     transition.addEvent(new Event(Event.EVENTTYPE_TRANSITION)).addAction(new Action(instantiatableDelegate));
378     Element element = AbstractXmlTestCase.toXmlAndParse( processDefinition, "/process-definition/state/transition" );
379
380     assertNotNull(element);
381     assertEquals("transition", element.getName());
382     assertEquals(1, element.elements("action").size());
383
384     element = element.element("action");
385     assertNotNull(element);
386     assertEquals("action", element.getName());
387     assertEquals("com.foo.Fighting", element.attributeValue("class"));
388   }
389
390   public void testWriteConfigurableAction() throws Exception JavaDoc {
391     ProcessDefinition processDefinition = new ProcessDefinition();
392     State state = (State) processDefinition.addNode( new State("a") );
393     Delegation instantiatableDelegate = new Delegation();
394     instantiatableDelegate.setClassName("com.foo.Fighting");
395     instantiatableDelegate.setConfigType("bean");
396     instantiatableDelegate.setConfiguration("<id>4</id><greeting>aloha</greeting>");
397     state.addEvent(new Event("node-enter")).addAction(new Action(instantiatableDelegate));
398     Element element = AbstractXmlTestCase.toXmlAndParse( processDefinition, "/process-definition/state[1]/event[1]/action[1]" );
399
400     assertNotNull(element);
401     assertEquals("action", element.getName());
402     assertEquals("bean", element.attributeValue("config-type"));
403     assertEquals("4", element.element("id").getTextTrim() );
404     assertEquals("aloha", element.element("greeting").getTextTrim() );
405   }
406
407   public void testWriteReferenceAction() throws Exception JavaDoc {
408     ProcessDefinition processDefinition = new ProcessDefinition();
409
410     // add a global action with name 'pina colada'
411
Delegation instantiatableDelegate = new Delegation();
412     instantiatableDelegate.setClassName("com.foo.Fighting");
413     instantiatableDelegate.setConfigType("bean");
414     instantiatableDelegate.setConfiguration("<id>4</id><greeting>aloha</greeting>");
415     Action action = new Action();
416     action.setName("pina colada");
417     action.setActionDelegation(instantiatableDelegate);
418     processDefinition.addAction(action);
419     
420     // now create a reference to it from event node-enter on state 'a'
421
State state = (State) processDefinition.addNode( new State() );
422     Action refAction = new Action();
423     refAction.setReferencedAction(action);
424     state.addEvent(new Event(Event.EVENTTYPE_NODE_ENTER)).addAction(refAction);
425     
426     AbstractXmlTestCase.toXmlAndParse( processDefinition, "/process-definition/state[1]/event[1]/action[1]" );
427   }
428 }
429
Popular Tags