KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jbpm > bpel > xml > BpelConverterTest


1 package org.jbpm.bpel.xml;
2
3 import javax.xml.transform.Transformer JavaDoc;
4 import javax.xml.transform.TransformerFactory JavaDoc;
5 import javax.xml.transform.dom.DOMResult JavaDoc;
6 import javax.xml.transform.dom.DOMSource JavaDoc;
7 import javax.xml.transform.stream.StreamSource JavaDoc;
8
9 import junit.framework.TestCase;
10
11 import org.w3c.dom.Document JavaDoc;
12 import org.w3c.dom.Element JavaDoc;
13 import org.w3c.dom.NodeList JavaDoc;
14
15 import com.ibm.wsdl.util.xml.DOMUtils;
16
17 import org.jbpm.bpel.xml.util.NodeUtil;
18
19 /**
20  * @author Juan Cantú
21  * @version $Revision: 1.11 $ $Date: 2005/06/23 02:17:26 $
22  */

23 public class BpelConverterTest extends TestCase {
24
25   private Transformer JavaDoc t;
26   
27   public void setUp() throws Exception JavaDoc {
28     TransformerFactory JavaDoc f = TransformerFactory.newInstance();
29     t = f.newTransformer(new StreamSource JavaDoc(BpelReader.class.getResourceAsStream("bpel-1-1-converter.xsl")));
30   }
31   
32   /////////////////////// Renamed Elements
33

34   public void testProcessDefaultQueryLanguage() throws Exception JavaDoc {
35     String JavaDoc xml = "<process queryLanguage='http://www.w3.org/TR/1999/REC-xpath-19991116'/>";
36     Element JavaDoc scope = transform(xml);
37     assertEquals("urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0", NodeUtil.getAttribute(scope, "queryLanguage"));
38   }
39
40   public void testProcessDefaultExpressionLanguage() throws Exception JavaDoc {
41     String JavaDoc xml = "<process expressionLanguage='http://www.w3.org/TR/1999/REC-xpath-19991116'/>";
42     Element JavaDoc scope = transform(xml);
43     assertEquals("urn:oasis:names:tc:wsbpel:2.0:sublang:xpath1.0", NodeUtil.getAttribute(scope, "expressionLanguage"));
44   }
45   
46   public void testScopeVariableAccessSerializable() throws Exception JavaDoc {
47     String JavaDoc xml = "<scope variableAccessSerializable='yes'/>";
48     Element JavaDoc scope = transform(xml);
49     assertNull( NodeUtil.getAttribute(scope, "variableAccessSerializable"));
50     assertEquals("yes", NodeUtil.getAttribute(scope, "isolated"));
51   }
52   
53   public void testScopeOnMessage() throws Exception JavaDoc {
54     String JavaDoc xml = "<scope><eventHandlers><onMessage/><onMessage/></eventHandlers></scope>";
55     Element JavaDoc scope = transform(xml);
56     Element JavaDoc events = NodeUtil.getElement(scope, BpelConstants.NS_BPWS, "eventHandlers");
57     assertNotNull(events);
58     assertEquals(2, events.getElementsByTagNameNS(BpelConstants.NS_BPWS, "onEvent").getLength());
59   }
60   
61   public void testPickOnMessage() throws Exception JavaDoc {
62     String JavaDoc xml = "<pick><onMessage/><onMessage/></pick>";
63     Element JavaDoc pick = transform(xml);
64     assertEquals(2, pick.getElementsByTagNameNS(BpelConstants.NS_BPWS, "onMessage").getLength());
65   }
66   
67   public void testTerminate() throws Exception JavaDoc {
68     String JavaDoc xml = "<terminate/>";
69     Element JavaDoc oldVersion = transform(xml);
70     assertEquals("exit", oldVersion.getLocalName());
71     assertEquals(BpelConstants.NS_BPWS, oldVersion.getNamespaceURI());
72   }
73   
74   /////////////////////// Attributes that changed into elements
75

76   public void testOnAlarmExpressions() throws Exception JavaDoc {
77     String JavaDoc xml = "<onAlarm for='f' until='u'><empty/></onAlarm>";
78     Element JavaDoc oldVersion = transform(xml);
79     Element JavaDoc expression = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "for");
80     assertNotNull(expression);
81     assertEquals("f", DOMUtils.getChildCharacterData(expression));
82     expression = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "until");
83     assertNotNull(expression);
84     assertEquals("u", DOMUtils.getChildCharacterData(expression));
85   }
86   
87   public void testWaitExpressions() throws Exception JavaDoc {
88     String JavaDoc xml = "<wait for='f' until='u'><empty/></wait>";
89     Element JavaDoc oldVersion = transform(xml);
90     Element JavaDoc expression = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "for");
91     assertNotNull(expression);
92     assertEquals("f", DOMUtils.getChildCharacterData(expression));
93     expression = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "until");
94     assertNotNull(expression);
95     assertEquals("u", DOMUtils.getChildCharacterData(expression));
96   }
97   
98   public void testWhileCondition() throws Exception JavaDoc {
99     String JavaDoc xml = "<while condition='c'><empty/></while>";
100     Element JavaDoc oldVersion = transform(xml);
101     Element JavaDoc expression = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "condition");
102     assertNotNull(expression);
103     assertEquals("c", DOMUtils.getChildCharacterData(expression));
104   }
105   
106   public void testCaseCondition() throws Exception JavaDoc {
107     String JavaDoc xml = "<case condition='c'><empty/></case>";
108     Element JavaDoc oldVersion = transform(xml);
109     Element JavaDoc expression = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "condition");
110     assertNotNull(expression);
111     assertEquals("c", DOMUtils.getChildCharacterData(expression));
112   }
113   
114   /////////////////////// Termination handler
115

116   public void testProcessTerminationHandler() throws Exception JavaDoc {
117     String JavaDoc xml =
118       "<process><faultHandlers>" +
119       "<faultHandler faultName='bpws:forcedTermination'>" +
120       " <empty/>" +
121       "</faultHandler>" +
122       "<faultHandler faultName='other'><empty/></faultHandler>" +
123       "<faultHandler faultVariable='var'><empty/></faultHandler>" +
124       "</faultHandlers></process>";
125     Element JavaDoc scope = transform(xml);
126     Element JavaDoc faultHandlers = NodeUtil.getElement(scope, BpelConstants.NS_BPWS, "faultHandlers");
127     assertNotNull(faultHandlers);
128     assertEquals(2, faultHandlers.getElementsByTagNameNS(BpelConstants.NS_BPWS, "faultHandler").getLength());
129     Element JavaDoc terminationHandler = NodeUtil.getElement(scope, BpelConstants.NS_BPWS, "terminationHandler");
130     assertNotNull(terminationHandler);
131     assertNotNull(NodeUtil.getElement(terminationHandler, BpelConstants.NS_BPWS, "empty"));
132   }
133   
134   public void testScopeTerminationHandler() throws Exception JavaDoc {
135     String JavaDoc xml =
136       "<scope><faultHandlers>" +
137       "<faultHandler faultName='bpws:forcedTermination'>" +
138       " <empty/>" +
139       "</faultHandler>" +
140       "</faultHandlers>" +
141       "<source linkName='aSource'/>" +
142       "</scope>";
143     Element JavaDoc scope = transform(xml);
144     Element JavaDoc faultHandlers = NodeUtil.getElement(scope, BpelConstants.NS_BPWS, "faultHandlers");
145     assertNull(faultHandlers);
146     Element JavaDoc terminationHandler = NodeUtil.getElement(scope, BpelConstants.NS_BPWS, "terminationHandler");
147     assertNotNull(terminationHandler);
148     assertNotNull(NodeUtil.getElement(scope, BpelConstants.NS_BPWS, "sources"));
149   }
150   
151   /////////////////////// Copy
152

153   public void testFromExpression() throws Exception JavaDoc {
154     String JavaDoc xml = "<from expression='$quantity*$price'/>";
155     Element JavaDoc from = transform(xml);
156     assertEquals("$quantity*$price", DOMUtils.getChildCharacterData(from));
157     assertNull(NodeUtil.getAttribute(from, "expression"));
158   }
159   
160   public void testFromVariable() throws Exception JavaDoc {
161     String JavaDoc xml = "<from variable='v'/>";
162     Element JavaDoc from = transform(xml);
163     assertEquals("$v", DOMUtils.getChildCharacterData(from));
164     assertNull(NodeUtil.getAttribute(from, "variable"));
165   }
166   
167   public void testFromVariablePart() throws Exception JavaDoc {
168     String JavaDoc xml = "<from variable='v' part='p'/>";
169     Element JavaDoc from = transform(xml);
170     assertEquals("$v/p", DOMUtils.getChildCharacterData(from));
171     assertNull(NodeUtil.getAttribute(from, "variable"));
172     assertNull(NodeUtil.getAttribute(from, "part"));
173   }
174   
175   public void testFromVariableQuery() throws Exception JavaDoc {
176     String JavaDoc xml = "<from variable='v' part='p' query='/p/q'/>";
177     Element JavaDoc from = transform(xml);
178     assertEquals("$v/p/q", DOMUtils.getChildCharacterData(from));
179     assertNull(NodeUtil.getAttribute(from, "variable"));
180     assertNull(NodeUtil.getAttribute(from, "part"));
181     assertNull(NodeUtil.getAttribute(from, "query"));
182   }
183   
184   public void testFromVariableRootElementQuery() throws Exception JavaDoc {
185     String JavaDoc xml = "<from xmlns:tns='http://www.mecachi.com' variable='v' part='p' query='/tns:q'/>";
186     Element JavaDoc from = transform(xml);
187     assertEquals("$v/p/tns:q", DOMUtils.getChildCharacterData(from));
188     assertNull(NodeUtil.getAttribute(from, "variable"));
189     assertNull(NodeUtil.getAttribute(from, "part"));
190     assertNull(NodeUtil.getAttribute(from, "query"));
191   }
192   
193   public void testFromVariableElementQuery() throws Exception JavaDoc {
194     String JavaDoc xml = "<from xmlns:tns='http://www.mecachi.com' variable='v' part='p' query='/tns:q/tns:child'/>";
195     Element JavaDoc from = transform(xml);
196     assertEquals("$v/p/tns:q/tns:child", DOMUtils.getChildCharacterData(from));
197     assertNull(NodeUtil.getAttribute(from, "variable"));
198     assertNull(NodeUtil.getAttribute(from, "part"));
199     assertNull(NodeUtil.getAttribute(from, "query"));
200   }
201   
202   public void testFromLiteralElements() throws Exception JavaDoc {
203     String JavaDoc xml = "<from xmlns:pur='http://www.manufacture.com'><pur:order pur:name='o1'/><pur:order pur:name='o2'/></from>";
204     Element JavaDoc from = transform(xml);
205     assertTrue(NodeUtil.getNamespaceDeclarations(from).containsKey("pur"));
206     assertEquals("yes", NodeUtil.getAttribute(from, "literal"));
207     assertEquals(2, from.getElementsByTagNameNS("http://www.manufacture.com", "order").getLength());
208   }
209   
210   public void testFromLiteralText() throws Exception JavaDoc {
211     String JavaDoc xml = "<from>free text!!!</from>";
212     Element JavaDoc from = transform(xml);
213     assertEquals("yes", NodeUtil.getAttribute(from, "literal"));
214     assertEquals("free text!!!", DOMUtils.getChildCharacterData(from));
215   }
216   
217   public void testToVariable() throws Exception JavaDoc {
218     String JavaDoc xml = "<to variable='v'/>";
219     Element JavaDoc to = transform(xml);
220     assertEquals("$v", DOMUtils.getChildCharacterData(to));
221     assertNull(NodeUtil.getAttribute(to, "literal"));
222   }
223   
224   public void testToVariablePart() throws Exception JavaDoc {
225     String JavaDoc xml = "<to variable='v' part='p'/>";
226     Element JavaDoc to = transform(xml);
227     assertEquals("$v/p", DOMUtils.getChildCharacterData(to));
228     assertNull(NodeUtil.getAttribute(to, "variable"));
229     assertNull(NodeUtil.getAttribute(to, "part"));
230   }
231   
232   public void testToVariableQuery() throws Exception JavaDoc {
233     String JavaDoc xml = "<to variable='v' part='p' query='/p/q'/>";
234     Element JavaDoc to = transform(xml);
235     assertEquals("$v/p/q", DOMUtils.getChildCharacterData(to));
236     assertNull(NodeUtil.getAttribute(to, "variable"));
237     assertNull(NodeUtil.getAttribute(to, "part"));
238     assertNull(NodeUtil.getAttribute(to, "query"));
239   }
240
241   public void testToVariableRootElementQuery() throws Exception JavaDoc {
242     String JavaDoc xml = "<to xmlns:tns='http://www.mecachi.com' variable='v' part='p' query='/tns:q'/>";
243     Element JavaDoc to = transform(xml);
244     assertEquals("$v/p/tns:q", DOMUtils.getChildCharacterData(to));
245     assertNull(NodeUtil.getAttribute(to, "variable"));
246     assertNull(NodeUtil.getAttribute(to, "part"));
247     assertNull(NodeUtil.getAttribute(to, "query"));
248   }
249   
250   public void testToVariableElementQuery() throws Exception JavaDoc {
251     String JavaDoc xml = "<to xmlns:tns='http://www.mecachi.com' variable='v' part='p' query='/tns:q/tns:child'/>";
252     Element JavaDoc to = transform(xml);
253     assertEquals("$v/p/tns:q/tns:child", DOMUtils.getChildCharacterData(to));
254     assertNull(NodeUtil.getAttribute(to, "variable"));
255     assertNull(NodeUtil.getAttribute(to, "part"));
256     assertNull(NodeUtil.getAttribute(to, "query"));
257   }
258   
259   /////////////////////// Extensions
260

261   public void testExtensionElement() throws Exception JavaDoc {
262     String JavaDoc xml ="<empty><jbpm:extension xmlns:jbpm='http://jbpm.org/bpel'><jbpm:extensionElement/></jbpm:extension></empty>";
263     Element JavaDoc activity = transform(xml);
264     Element JavaDoc extension = NodeUtil.getElement(activity, BpelConstants.NS_VENDOR, "extension");
265     assertNotNull(extension);
266   }
267   
268   public void testExtensionAttribute() throws Exception JavaDoc {
269     String JavaDoc xml ="<empty xmlns:jbpm='http://jbpm.org/bpel' jbpm:extension='bla'/>";
270     Element JavaDoc activity = transform(xml);
271     assertNotNull(activity.getAttributeNS(BpelConstants.NS_VENDOR, "extension"));
272   }
273   
274   /////////////////////// Activity source and targets
275

276   public void testSources() throws Exception JavaDoc {
277     String JavaDoc xml = "<empty><source linkName='s1' transitionCondition='tc'/><source linkName='s2'/></empty>";
278     Element JavaDoc oldVersion = transform(xml);
279     Element JavaDoc sources = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "sources");
280     assertNotNull(sources);
281     NodeList JavaDoc sourceList = sources.getElementsByTagNameNS(BpelConstants.NS_BPWS, "source");
282     assertEquals(2, sourceList.getLength());
283     Element JavaDoc transitionCondition = NodeUtil.getElement(sourceList.item(0), BpelConstants.NS_BPWS, "transitionCondition");
284     assertNotNull(transitionCondition);
285     assertEquals("tc", DOMUtils.getChildCharacterData(transitionCondition));
286   }
287  
288   public void testTargets() throws Exception JavaDoc {
289     String JavaDoc xml = "<empty joinCondition='jc'><target linkName='t1'/><target linkName='t2'/></empty>";
290     Element JavaDoc oldVersion = transform(xml);
291     Element JavaDoc targets = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "targets");
292     assertNotNull(targets);
293     assertEquals(2, targets.getElementsByTagNameNS(BpelConstants.NS_BPWS, "target").getLength());
294     Element JavaDoc joinCondition = NodeUtil.getElement(targets, BpelConstants.NS_BPWS, "joinCondition");
295     assertNotNull(joinCondition);
296     assertEquals("jc", DOMUtils.getChildCharacterData(joinCondition));
297   }
298   
299   public Element JavaDoc transform(String JavaDoc oldVersion) throws Exception JavaDoc {
300     String JavaDoc textToParse =
301       "<parent xmlns='" + BpelConstants.NS_BPWS_1_1 +
302       "' xmlns:bpws='" + BpelConstants.NS_BPWS_1_1 + "'>" + oldVersion +
303       "</parent>";
304     Document JavaDoc oldDoc = NodeUtil.parseElement(textToParse).getOwnerDocument();
305     Document JavaDoc resultDoc = NodeUtil.createDocument();
306     t.transform(new DOMSource JavaDoc(oldDoc), new DOMResult JavaDoc(resultDoc));
307     return (Element JavaDoc) resultDoc.getDocumentElement().getChildNodes().item(0);
308   }
309 }
310
Popular Tags