KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestPrefixes


1 package test.saaj;
2
3 import javax.xml.soap.MessageFactory JavaDoc;
4 import javax.xml.soap.SOAPBody JavaDoc;
5 import javax.xml.soap.SOAPElement JavaDoc;
6 import javax.xml.soap.SOAPEnvelope JavaDoc;
7 import javax.xml.soap.SOAPMessage JavaDoc;
8 import javax.xml.soap.SOAPPart JavaDoc;
9 import javax.xml.soap.MimeHeaders JavaDoc;
10 import javax.xml.soap.Name JavaDoc;
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.io.ByteArrayInputStream JavaDoc;
13 import java.util.Iterator JavaDoc;
14
15 /**
16  * Test case for Prefixes
17  */

18 public class TestPrefixes extends junit.framework.TestCase {
19
20     public TestPrefixes(String JavaDoc name) {
21         super(name);
22     }
23
24     /**
25      * Test for Bug 18274 - prefix name not set during adding child element
26      * @throws Exception
27      */

28     public void testAddingPrefixesForChildElements() throws Exception JavaDoc {
29         MessageFactory JavaDoc factory = MessageFactory.newInstance();
30         SOAPMessage JavaDoc msg = factory.createMessage();
31         SOAPPart JavaDoc sp = msg.getSOAPPart();
32         SOAPEnvelope JavaDoc se = sp.getEnvelope();
33         SOAPBody JavaDoc sb = se.getBody();
34         SOAPElement JavaDoc el1 = sb.addBodyElement(se.createName
35                 ("element1", "prefix1", "http://www.sun.com"));
36         SOAPElement JavaDoc el2 = el1.addChildElement(se.createName
37                 ("element2", "prefix2", "http://www.apache.org"));
38         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
39         msg.writeTo(baos);
40         String JavaDoc xml = new String JavaDoc(baos.toByteArray());
41         assertTrue(xml.indexOf("prefix1") != -1);
42         assertTrue(xml.indexOf("prefix2") != -1);
43         assertTrue(xml.indexOf("http://www.sun.com") != -1);
44         assertTrue(xml.indexOf("http://www.apache.org") != -1);
45     }
46     
47     public void testAttribute() throws Exception JavaDoc {
48         String JavaDoc soappacket = "<SOAP-ENV:Envelope xmlns:SOAP-ENV =\"http://schemas.xmlsoap.org/soap/envelope/\"" +
49                             " xmlns:xsi =\"http://www.w3.org/1999/XMLSchema-instance\"" +
50                             " xmlns:xsd =\"http://www.w3.org/1999/XMLSchema\">" +
51                             " <SOAP-ENV:Body> " +
52                             " <helloworld name=\"tester\" />" +
53                             " </SOAP-ENV:Body>" +
54                             "</SOAP-ENV:Envelope>";
55         SOAPMessage JavaDoc msg = MessageFactory.newInstance().createMessage(new MimeHeaders JavaDoc(), new ByteArrayInputStream JavaDoc(soappacket.getBytes()));
56         SOAPBody JavaDoc body = msg.getSOAPPart().getEnvelope().getBody();
57
58         SOAPElement JavaDoc ele = (SOAPElement JavaDoc) body.getChildElements().next();
59         Iterator JavaDoc attit = ele.getAllAttributes();
60
61         Name JavaDoc n = (Name JavaDoc) attit.next();
62         assertEquals("Test fail prefix problem",n.getQualifiedName(),"name");
63     }
64 }
65
Popular Tags