1 package org.jbpm.bpel.xml; 2 3 import javax.xml.transform.Transformer ; 4 import javax.xml.transform.TransformerFactory ; 5 import javax.xml.transform.dom.DOMResult ; 6 import javax.xml.transform.dom.DOMSource ; 7 import javax.xml.transform.stream.StreamSource ; 8 9 import junit.framework.TestCase; 10 11 import org.w3c.dom.Document ; 12 import org.w3c.dom.Element ; 13 import org.w3c.dom.NodeList ; 14 15 import com.ibm.wsdl.util.xml.DOMUtils; 16 17 import org.jbpm.bpel.xml.util.NodeUtil; 18 19 23 public class BpelConverterTest extends TestCase { 24 25 private Transformer t; 26 27 public void setUp() throws Exception { 28 TransformerFactory f = TransformerFactory.newInstance(); 29 t = f.newTransformer(new StreamSource (BpelReader.class.getResourceAsStream("bpel-1-1-converter.xsl"))); 30 } 31 32 34 public void testProcessDefaultQueryLanguage() throws Exception { 35 String xml = "<process queryLanguage='http://www.w3.org/TR/1999/REC-xpath-19991116'/>"; 36 Element 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 { 41 String xml = "<process expressionLanguage='http://www.w3.org/TR/1999/REC-xpath-19991116'/>"; 42 Element 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 { 47 String xml = "<scope variableAccessSerializable='yes'/>"; 48 Element scope = transform(xml); 49 assertNull( NodeUtil.getAttribute(scope, "variableAccessSerializable")); 50 assertEquals("yes", NodeUtil.getAttribute(scope, "isolated")); 51 } 52 53 public void testScopeOnMessage() throws Exception { 54 String xml = "<scope><eventHandlers><onMessage/><onMessage/></eventHandlers></scope>"; 55 Element scope = transform(xml); 56 Element 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 { 62 String xml = "<pick><onMessage/><onMessage/></pick>"; 63 Element pick = transform(xml); 64 assertEquals(2, pick.getElementsByTagNameNS(BpelConstants.NS_BPWS, "onMessage").getLength()); 65 } 66 67 public void testTerminate() throws Exception { 68 String xml = "<terminate/>"; 69 Element oldVersion = transform(xml); 70 assertEquals("exit", oldVersion.getLocalName()); 71 assertEquals(BpelConstants.NS_BPWS, oldVersion.getNamespaceURI()); 72 } 73 74 76 public void testOnAlarmExpressions() throws Exception { 77 String xml = "<onAlarm for='f' until='u'><empty/></onAlarm>"; 78 Element oldVersion = transform(xml); 79 Element 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 { 88 String xml = "<wait for='f' until='u'><empty/></wait>"; 89 Element oldVersion = transform(xml); 90 Element 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 { 99 String xml = "<while condition='c'><empty/></while>"; 100 Element oldVersion = transform(xml); 101 Element 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 { 107 String xml = "<case condition='c'><empty/></case>"; 108 Element oldVersion = transform(xml); 109 Element expression = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "condition"); 110 assertNotNull(expression); 111 assertEquals("c", DOMUtils.getChildCharacterData(expression)); 112 } 113 114 116 public void testProcessTerminationHandler() throws Exception { 117 String 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 scope = transform(xml); 126 Element faultHandlers = NodeUtil.getElement(scope, BpelConstants.NS_BPWS, "faultHandlers"); 127 assertNotNull(faultHandlers); 128 assertEquals(2, faultHandlers.getElementsByTagNameNS(BpelConstants.NS_BPWS, "faultHandler").getLength()); 129 Element 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 { 135 String xml = 136 "<scope><faultHandlers>" + 137 "<faultHandler faultName='bpws:forcedTermination'>" + 138 " <empty/>" + 139 "</faultHandler>" + 140 "</faultHandlers>" + 141 "<source linkName='aSource'/>" + 142 "</scope>"; 143 Element scope = transform(xml); 144 Element faultHandlers = NodeUtil.getElement(scope, BpelConstants.NS_BPWS, "faultHandlers"); 145 assertNull(faultHandlers); 146 Element terminationHandler = NodeUtil.getElement(scope, BpelConstants.NS_BPWS, "terminationHandler"); 147 assertNotNull(terminationHandler); 148 assertNotNull(NodeUtil.getElement(scope, BpelConstants.NS_BPWS, "sources")); 149 } 150 151 153 public void testFromExpression() throws Exception { 154 String xml = "<from expression='$quantity*$price'/>"; 155 Element from = transform(xml); 156 assertEquals("$quantity*$price", DOMUtils.getChildCharacterData(from)); 157 assertNull(NodeUtil.getAttribute(from, "expression")); 158 } 159 160 public void testFromVariable() throws Exception { 161 String xml = "<from variable='v'/>"; 162 Element from = transform(xml); 163 assertEquals("$v", DOMUtils.getChildCharacterData(from)); 164 assertNull(NodeUtil.getAttribute(from, "variable")); 165 } 166 167 public void testFromVariablePart() throws Exception { 168 String xml = "<from variable='v' part='p'/>"; 169 Element 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 { 176 String xml = "<from variable='v' part='p' query='/p/q'/>"; 177 Element 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 { 185 String xml = "<from xmlns:tns='http://www.mecachi.com' variable='v' part='p' query='/tns:q'/>"; 186 Element 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 { 194 String xml = "<from xmlns:tns='http://www.mecachi.com' variable='v' part='p' query='/tns:q/tns:child'/>"; 195 Element 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 { 203 String xml = "<from xmlns:pur='http://www.manufacture.com'><pur:order pur:name='o1'/><pur:order pur:name='o2'/></from>"; 204 Element 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 { 211 String xml = "<from>free text!!!</from>"; 212 Element 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 { 218 String xml = "<to variable='v'/>"; 219 Element to = transform(xml); 220 assertEquals("$v", DOMUtils.getChildCharacterData(to)); 221 assertNull(NodeUtil.getAttribute(to, "literal")); 222 } 223 224 public void testToVariablePart() throws Exception { 225 String xml = "<to variable='v' part='p'/>"; 226 Element 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 { 233 String xml = "<to variable='v' part='p' query='/p/q'/>"; 234 Element 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 { 242 String xml = "<to xmlns:tns='http://www.mecachi.com' variable='v' part='p' query='/tns:q'/>"; 243 Element 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 { 251 String xml = "<to xmlns:tns='http://www.mecachi.com' variable='v' part='p' query='/tns:q/tns:child'/>"; 252 Element 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 261 public void testExtensionElement() throws Exception { 262 String xml ="<empty><jbpm:extension xmlns:jbpm='http://jbpm.org/bpel'><jbpm:extensionElement/></jbpm:extension></empty>"; 263 Element activity = transform(xml); 264 Element extension = NodeUtil.getElement(activity, BpelConstants.NS_VENDOR, "extension"); 265 assertNotNull(extension); 266 } 267 268 public void testExtensionAttribute() throws Exception { 269 String xml ="<empty xmlns:jbpm='http://jbpm.org/bpel' jbpm:extension='bla'/>"; 270 Element activity = transform(xml); 271 assertNotNull(activity.getAttributeNS(BpelConstants.NS_VENDOR, "extension")); 272 } 273 274 276 public void testSources() throws Exception { 277 String xml = "<empty><source linkName='s1' transitionCondition='tc'/><source linkName='s2'/></empty>"; 278 Element oldVersion = transform(xml); 279 Element sources = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "sources"); 280 assertNotNull(sources); 281 NodeList sourceList = sources.getElementsByTagNameNS(BpelConstants.NS_BPWS, "source"); 282 assertEquals(2, sourceList.getLength()); 283 Element 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 { 289 String xml = "<empty joinCondition='jc'><target linkName='t1'/><target linkName='t2'/></empty>"; 290 Element oldVersion = transform(xml); 291 Element targets = NodeUtil.getElement(oldVersion, BpelConstants.NS_BPWS, "targets"); 292 assertNotNull(targets); 293 assertEquals(2, targets.getElementsByTagNameNS(BpelConstants.NS_BPWS, "target").getLength()); 294 Element joinCondition = NodeUtil.getElement(targets, BpelConstants.NS_BPWS, "joinCondition"); 295 assertNotNull(joinCondition); 296 assertEquals("jc", DOMUtils.getChildCharacterData(joinCondition)); 297 } 298 299 public Element transform(String oldVersion) throws Exception { 300 String textToParse = 301 "<parent xmlns='" + BpelConstants.NS_BPWS_1_1 + 302 "' xmlns:bpws='" + BpelConstants.NS_BPWS_1_1 + "'>" + oldVersion + 303 "</parent>"; 304 Document oldDoc = NodeUtil.parseElement(textToParse).getOwnerDocument(); 305 Document resultDoc = NodeUtil.createDocument(); 306 t.transform(new DOMSource (oldDoc), new DOMResult (resultDoc)); 307 return (Element ) resultDoc.getDocumentElement().getChildNodes().item(0); 308 } 309 } 310 | Popular Tags |