1 package test.utils; 2 3 import junit.framework.Test; 4 import junit.framework.TestSuite; 5 import org.apache.axis.encoding.DeserializationContext; 6 import test.AxisTestBase; 7 import org.apache.axis.utils.XMLUtils; 8 import org.apache.axis.message.PrefixedQName; 9 import org.apache.axis.message.MessageElement; 10 import org.apache.axis.Constants; 11 import org.w3c.dom.Document ; 12 import org.w3c.dom.Element ; 13 import org.w3c.dom.NodeList ; 14 import org.xml.sax.InputSource ; 15 16 import javax.xml.parsers.SAXParser ; 17 import javax.xml.soap.SOAPEnvelope ; 18 import javax.xml.soap.SOAPBodyElement ; 19 import java.io.ByteArrayInputStream ; 20 import java.io.InputStream ; 21 import java.io.OutputStreamWriter ; 22 import java.io.PipedOutputStream ; 23 import java.io.Reader ; 24 import java.io.StringReader ; 25 import java.util.Iterator ; 26 27 public class TestXMLUtils extends AxisTestBase 28 { 29 30 public TestXMLUtils (String name) { 31 super(name); 32 } 33 34 public static Test suite() { 35 return new TestSuite(TestXMLUtils.class); 36 } 37 38 public void setup() { 39 } 40 41 public void testNewDocumentNoArgConstructor() throws Exception 42 { 43 Document doc = XMLUtils.newDocument(); 44 assertNotNull("Did not get a new Document", doc); 45 } 46 47 public void testNewDocumentInputSource() throws Exception 48 { 49 Reader reader = (Reader )this.getTestXml("reader"); 50 InputSource inputsrc = new InputSource (reader); 51 Document doc = XMLUtils.newDocument(inputsrc); 52 assertNotNull("Did not get a new Document", doc); 53 } 54 55 public void testNewDocumentInputStream() throws Exception 56 { 57 InputStream iostream = (InputStream )this.getTestXml("inputstream"); 58 InputSource inputsrc = new InputSource (iostream); 59 Document doc = XMLUtils.newDocument(inputsrc); 60 assertNotNull("Did not get a new Document", doc); 61 } 62 63 67 68 public void testNewDocumentURI() throws Exception 69 { 70 if(isOnline()) { 71 String uri = "http://java.sun.com/j2ee/dtds/web-app_2.2.dtd"; 72 Document doc = XMLUtils.newDocument(uri); 73 assertNotNull("Did not get a new Document", doc); 74 } 75 } 76 77 public void testDocumentToString() throws Exception 78 { 79 Reader reader = (Reader )this.getTestXml("reader"); 80 InputSource inputsrc = new InputSource (reader); 81 Document doc = XMLUtils.newDocument(inputsrc); 82 83 String xmlString = (String )this.getTestXml("string"); 84 String result = XMLUtils.DocumentToString(doc); 85 assertXMLEqual("xmlString is not the same as result", xmlString, result); 86 } 87 88 94 public void testElementToWriter() throws Exception 95 { 96 97 Reader xmlReader = (Reader )this.getTestXml("reader"); 98 InputSource inputsrc = new InputSource (xmlReader); 99 Document doc = XMLUtils.newDocument(inputsrc); 100 NodeList nl = doc.getElementsByTagName("display-name"); 101 Element elem = (Element)nl.item(0); 102 String expected = "<display-name>Apache-Axis</display-name>"; 103 104 109 PipedOutputStream out = new PipedOutputStream (); 110 OutputStreamWriter writer = new OutputStreamWriter (out); 111 ConsumerPipe cpipe = new ConsumerPipe(out); 112 113 117 XMLUtils.ElementToWriter(elem, writer); 118 119 124 out.flush(); 125 String result = cpipe.getResult(); 126 out.close(); 128 129 assertEquals("Did not get the expected result", expected, result); 130 } 131 132 136 public void testDocumentToStream() throws Exception 137 { 138 Reader reader = (Reader )this.getTestXml("reader"); 139 InputSource inputsrc = new InputSource (reader); 140 Document doc = XMLUtils.newDocument(inputsrc); 141 142 PipedOutputStream out = new PipedOutputStream (); 143 ConsumerPipe cpipe = new ConsumerPipe(out); 144 145 XMLUtils.DocumentToStream(doc, out); 146 out.flush(); 147 String result = cpipe.getResult(); 148 out.close(); 149 150 String expected = (String )this.getTestXml("string"); 151 assertXMLEqual("Did not get the expected result", expected, result); 152 } 153 154 public void testElementToString() throws Exception 155 { 156 Reader reader = (Reader )this.getTestXml("reader"); 157 InputSource inputsrc = new InputSource (reader); 158 Document doc = XMLUtils.newDocument(inputsrc); 159 160 NodeList nl = doc.getElementsByTagName("display-name"); 161 Element elem = (Element)nl.item(0); 162 String expected = "<display-name>Apache-Axis</display-name>"; 163 String result = XMLUtils.ElementToString(elem); 164 assertEquals("Element tag name is not 'display-name', it is: " + elem.getTagName(), 165 "display-name", elem.getTagName()); 166 assertEquals("Did not get the expected result", expected, result); 167 } 168 169 public void testGetInnerXMLString() throws Exception 170 { 171 Reader reader = (Reader )this.getTestXml("reader"); 172 InputSource inputsrc = new InputSource (reader); 173 Document doc = XMLUtils.newDocument(inputsrc); 174 175 NodeList nl = doc.getElementsByTagName("display-name"); 176 Element elem = (Element)nl.item(0); 177 String expected = "Apache-Axis"; 178 String result = XMLUtils.getInnerXMLString(elem); 179 assertEquals(expected, result); 180 } 181 182 public void testGetPrefix() throws Exception 183 { 184 Document doc = XMLUtils.newDocument(); 185 186 Element elem = doc.createElementNS("", "svg"); 187 elem.setAttribute("xmlns:svg", "\"http://www.w3.org/2000/svg\""); 188 elem.setAttribute("xmlns:xlink", "\"http://www.w3.org/1999/xlink\""); 189 elem.setAttribute("xmlns:xhtml", "\"http://www.w3.org/1999/xhtml\""); 190 191 String expected = "svg"; 192 String result = XMLUtils.getPrefix("\"http://www.w3.org/2000/svg\"", elem); 193 assertEquals("Did not get the expected result", expected, result); 194 expected = "xlink"; 195 result = XMLUtils.getPrefix("\"http://www.w3.org/1999/xlink\"", elem); 196 assertEquals("Did not get the expected result", expected, result); 197 expected = "xhtml"; 198 result = XMLUtils.getPrefix("\"http://www.w3.org/1999/xhtml\"", elem); 199 assertEquals("Did not get the expected result", expected, result); 200 } 201 202 public void testGetNamespace() throws Exception 203 { 204 String testDoc = "<svg xmlns:svg=\"http://www.w3.org/2000/svg\"/>"; 205 InputSource inputsrc = new InputSource (new StringReader (testDoc)); 206 Document doc = XMLUtils.newDocument(inputsrc); 207 assertNotNull("Got a null document", doc); 208 209 NodeList nl = doc.getElementsByTagName("svg"); 210 Element elem = (Element)nl.item(0); 211 212 String expected = "http://www.w3.org/2000/svg"; 213 String result = XMLUtils.getNamespace("svg", elem); 214 assertEquals("Did not get the expected result", expected, result); 215 } 216 217 225 public Object getTestXml(String gimme) 226 { 227 String lineSep = System.getProperty("line.separator"); 228 StringBuffer sb = new StringBuffer (); 229 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + lineSep) 230 .append("<web-app>" + lineSep) 235 .append("<display-name>Apache-Axis</display-name>" + lineSep) 236 .append("<servlet>" + lineSep) 237 .append("<servlet-name>AxisServlet</servlet-name>" + lineSep) 238 .append("<display-name>Apache-Axis Servlet</display-name>" + lineSep) 239 .append("<servlet-class>" + lineSep) 240 .append("org.apache.axis.transport.http.AxisServlet" + lineSep) 241 .append("</servlet-class>" + lineSep) 242 .append("</servlet>" + lineSep) 243 .append("<servlet-mapping>" + lineSep) 244 .append("<servlet-name>AxisServlet</servlet-name>" + lineSep) 245 .append("<url-pattern>servlet/AxisServlet</url-pattern>" + lineSep) 246 .append("<url-pattern>*.jws</url-pattern>" + lineSep) 247 .append("</servlet-mapping>" + lineSep) 248 .append("</web-app>"); 249 250 String xmlString = sb.toString(); 251 252 if (gimme.equals("string")) 253 { 254 return xmlString; 255 } 256 else if (gimme.equals("reader")) 257 { 258 StringReader strReader = new StringReader (xmlString); 259 return strReader; 260 } 261 else if (gimme.equals("inputstream")) 262 { 263 ByteArrayInputStream byteStream = new ByteArrayInputStream (xmlString.getBytes()); 264 return byteStream; 265 } 266 else return null; 267 } 268 269 public void testDOM2Writer() throws Exception 270 { 271 StringBuffer sb = new StringBuffer (); 272 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 273 sb.append("<xsd:schema targetNamespace=\"http://tempuri.org\""); 274 sb.append(" xmlns=\"http://tempuri.org\""); 275 sb.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"); 276 sb.append(" <xsd:annotation>"); 277 sb.append(" <xsd:documentation xml:lang=\"en\">"); 278 sb.append(" Purchase order schema for Example.com."); 279 sb.append(" Copyright 2000 Example.com. All rights reserved."); 280 sb.append(" </xsd:documentation>"); 281 sb.append(" </xsd:annotation>"); 282 sb.append("</xsd:schema>"); 283 284 StringReader strReader = new StringReader (sb.toString()); 285 InputSource inputsrc = new InputSource (strReader); 286 Document doc = XMLUtils.newDocument(inputsrc); 287 288 String output = org.apache.axis.utils.DOM2Writer.nodeToString(doc,false); 289 assertTrue(output.indexOf("http://www.w3.org/XML/1998/namespace")==-1); 290 } 291 292 public void testDOMXXE() throws Exception 293 { 294 StringBuffer sb = new StringBuffer (); 295 sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>"); 296 sb.append("<!DOCTYPE project ["); 297 sb.append("<!ENTITY buildxml SYSTEM \"file:build.xml\">"); 298 sb.append("]>"); 299 sb.append("<xsd:schema targetNamespace=\"http://tempuri.org\""); 300 sb.append(" xmlns=\"http://tempuri.org\""); 301 sb.append(" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\">"); 302 sb.append(" <xsd:annotation>"); 303 sb.append(" <xsd:documentation xml:lang=\"en\">"); 304 sb.append(" &buildxml;"); 305 sb.append(" Purchase order schema for Example.com."); 306 sb.append(" Copyright 2000 Example.com. All rights reserved."); 307 sb.append(" </xsd:documentation>"); 308 sb.append(" </xsd:annotation>"); 309 sb.append("</xsd:schema>"); 310 311 StringReader strReader = new StringReader (sb.toString()); 312 InputSource inputsrc = new InputSource (strReader); 313 Document doc = XMLUtils.newDocument(inputsrc); 314 String output = org.apache.axis.utils.DOM2Writer.nodeToString(doc,false); 315 } 316 317 String msg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 318 "<!DOCTYPE project [" + 319 "<!ENTITY buildxml SYSTEM \"file:build.xml\">" + 320 "]>" + 321 "<SOAP-ENV:Envelope " + 322 "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " + 323 "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" > " + 324 "<SOAP-ENV:Body>\n" + 325 "&buildxml;" + 326 "<echo:Echo xmlns:echo=\"EchoService\">\n" + 327 "<symbol>IBM</symbol>\n" + 328 "</echo:Echo>\n" + 329 "</SOAP-ENV:Body></SOAP-ENV:Envelope>\n"; 330 331 public void testSAXXXE1() throws Exception 332 { 333 StringReader strReader = new StringReader (msg); 334 InputSource inputsrc = new InputSource (strReader); 335 SAXParser parser = XMLUtils.getSAXParser(); 336 parser.getParser().parse(inputsrc); 337 } 338 339 public void testSAXXXE2() throws Exception 340 { 341 StringReader strReader2 = new StringReader (msg); 342 InputSource inputsrc2 = new InputSource (strReader2); 343 SAXParser parser2 = XMLUtils.getSAXParser(); 344 parser2.getXMLReader().parse(inputsrc2); 345 } 346 347 String msg2 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 351 "<SOAP-ENV:Envelope " + 352 "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" " + 353 "xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" > " + 354 "<SOAP-ENV:Body>\n" + 355 "<echo:Echo xmlns:echo=\"EchoService\">\n" + 356 "<symbol xml:lang=\"en\">IBM</symbol>\n" + 357 "</echo:Echo>\n" + 358 "</SOAP-ENV:Body></SOAP-ENV:Envelope>\n"; 359 360 366 public void testSAXXXE3() throws Exception 367 { 368 StringReader strReader3 = new StringReader (msg2); 369 DeserializationContext dser = new DeserializationContext( 370 new InputSource (strReader3), null, org.apache.axis.Message.REQUEST); 371 dser.parse(); 372 SOAPEnvelope env = dser.getEnvelope(); 373 SOAPBodyElement body = (SOAPBodyElement )env.getBody().getChildElements().next(); 374 assertNotNull(body); 375 MessageElement child = (MessageElement)body.getChildElements().next(); 376 assertNotNull(child); 377 Iterator i = child.getAllAttributes(); 378 assertNotNull(i); 379 PrefixedQName attr = (PrefixedQName)i.next(); 380 assertNotNull(attr); 381 assertEquals("Prefix for attribute was not 'xml'", attr.getPrefix(), "xml"); 382 assertEquals("Namespace for attribute was not correct", attr.getURI(), 383 Constants.NS_URI_XML); 384 } 385 386 String msg3 = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 387 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" " + 388 " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" " + 389 " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + 390 " <soapenv:Header>" + 391 " <TrackingHeader xmlns=\"http://testns.org/test\">1234</TrackingHeader>" + 392 " <ns1:Security soapenv:mustUnderstand=\"0\" xmlns:ns1=\"http://schemas.xmlsoap.org/ws/2002/07/secext\">" + 393 " <ns1:UsernameToken>" + 394 " <ns1:Username xsi:type=\"xsd:string\">foobar</ns1:Username>" + 395 " <ns1:Password xsi:type=\"xsd:string\">wibble</ns1:Password>" + 396 " </ns1:UsernameToken>" + 397 " </ns1:Security>" + 398 " </soapenv:Header>" + 399 " <soapenv:Body>" + 400 " <EchoString>asdadsf</EchoString>" + 401 " </soapenv:Body>" + 402 "</soapenv:Envelope>"; 403 404 408 public void testNSStack() throws Exception 409 { 410 StringReader strReader3 = new StringReader (msg3); 411 DeserializationContext dser = new DeserializationContext( 412 new InputSource (strReader3), null, org.apache.axis.Message.REQUEST); 413 dser.parse(); 414 org.apache.axis.message.SOAPEnvelope env = dser.getEnvelope(); 415 String xml = env.toString(); 416 assertXMLEqual(xml,msg3); 417 } 418 419 public static void main(String [] args) throws Exception 420 { 421 TestXMLUtils test = new TestXMLUtils("TestXMLUtils"); 422 test.testSAXXXE3(); 423 test.testNSStack(); 424 } 425 } 426 | Popular Tags |