1 package net.kwfgrid.gworkflowdl;
2
3 import junit.framework.TestCase;
4 import net.kwfgrid.gworkflowdl.protocol.structure.DefaultProtocolCreator;
5 import net.kwfgrid.gworkflowdl.protocol.structure.ProtocolPlace;
6 import net.kwfgrid.gworkflowdl.protocol.structure.ProtocolToken;
7 import net.kwfgrid.gworkflowdl.protocol.structure.ProtocolData;
8 import net.kwfgrid.gworkflowdl.protocol.xupdate.XUMethodCallMarshaller;
9 import net.kwfgrid.gworkflowdl.protocol.xupdate.XUModificationHandler;
10 import net.kwfgrid.gworkflowdl.protocol.xupdate.XUpdateSerializerPool;
11 import net.kwfgrid.gworkflowdl.structure.*;
12 import net.kwfgrid.jxupdate.xupdate.XUpdateSerializer;
13 import org.jaxen.SimpleNamespaceContext;
14 import org.jaxen.XPath;
15 import org.jaxen.jdom.JDOMXPath;
16 import org.jdom.Element;
17 import org.jdom.output.Format;
18 import org.jdom.output.XMLOutputter;
19
20 import java.io.StringWriter;
21 import java.util.Iterator;
22 import java.util.List;
23
24 public class TokenConditionTest extends TestCase {
25 private static final String DATA_XML = "<data><input xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:type=\"xsd:string\">value</input></data>";
26 private static final String CONDITION = "gwdl:place[@ID='p1']/gwdl:token[1]/gwdl:data/input='value'";
27
28 public void testConditionLikeInGWES() throws Exception {
29 Data data = Factory.newData();
30 data.fromXML(DATA_XML);
31 Token tok = Factory.newToken(data);
32
33 Place p1 = Factory.newPlace();
34 p1.setID("p1");
35 p1.addToken(tok);
36
37 Workflow wf = Factory.newWorkflow();
38 wf.addPlace(p1);
39
40 SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
41 nsContext.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
42 nsContext.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
43 nsContext.addNamespace("gwdl", "http://www.gridworkflow.org/gworkflowdl");
44
45 Element context = WorkflowJdom.java2element(wf);
46
47 XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat());
48 outp.output(context, System.out);
49
50 System.out.println("\n");
51
52 XPath xpath = new JDOMXPath("*");
53 xpath.setNamespaceContext(nsContext);
54 List nodes = xpath.selectNodes(context);
55
56 System.out.println("XPath selected " + nodes.size() + " nodes.");
57
58 Iterator i = nodes.iterator();
59 while (i.hasNext()) {
60 System.out.println("XPath \"*\" selected: " + i.next());
61 }
62
63 xpath = new JDOMXPath(CONDITION);
64 xpath.setNamespaceContext(nsContext);
65 boolean result = xpath.booleanValueOf(context);
66
67 assertTrue("XPath selection wrong.", result);
68 }
69
70 public void testConditionLikeInGWESWithClientCreatedToken() throws Exception {
71 Factory.setCreator(new DefaultProtocolCreator(new DefaultCreator()));
72
73 ProtocolPlace p1 = (ProtocolPlace) Factory.newPlace();
74 p1.setID("p1");
75
76 Workflow wf = Factory.newWorkflow();
77 wf.addPlace(p1);
78
79 ProtocolData data = (ProtocolData) Factory.newData();
80 data.fromXML(DATA_XML);
81 ProtocolToken tok = (ProtocolToken) Factory.newToken(data);
82
83
84 XUMethodCallMarshaller mrs = new XUMethodCallMarshaller();
85 XUpdateSerializer x = XUpdateSerializerPool.getInstance().getSerializer();
86 StringWriter out = new StringWriter();
87 x.setOutput(out);
88 x.startModifications();
89 mrs.marshalPlaceAddToken(x, p1, tok);
90 x.endModifications();
91 out.flush();
92 String call = out.toString();
93 x.setOutput(null);
94 XUpdateSerializerPool.getInstance().returnObject(x);
95
96 System.out.println("Serialized Call:\n" + call);
97
98 XUModificationHandler hdl = new XUModificationHandler();
99 hdl.handleModification(wf, call);
100
101
102 SimpleNamespaceContext nsContext = new SimpleNamespaceContext();
103 nsContext.addNamespace("xsd", "http://www.w3.org/2001/XMLSchema");
104 nsContext.addNamespace("xsi", "http://www.w3.org/2001/XMLSchema-instance");
105 nsContext.addNamespace("gwdl", "http://www.gridworkflow.org/gworkflowdl");
106
107 Element context = WorkflowJdom.java2element(wf);
108
109 XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat());
110 outp.output(context, System.out);
111
112 System.out.println("\n");
113
114 XPath xpath = new JDOMXPath("*");
115 xpath.setNamespaceContext(nsContext);
116 List nodes = xpath.selectNodes(context);
117
118 System.out.println("XPath selected " + nodes.size() + " nodes.");
119
120 Iterator i = nodes.iterator();
121 while (i.hasNext()) {
122 System.out.println("XPath selected: " + i.next());
123 }
124
125 xpath = new JDOMXPath(CONDITION);
126 xpath.setNamespaceContext(nsContext);
127 boolean result = xpath.booleanValueOf(context);
128
129 assertTrue("XPath selection wrong.", result);
130 }
131 }