KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestHeaders


1 package test.saaj;
2
3 import javax.xml.soap.MessageFactory JavaDoc;
4 import javax.xml.soap.MimeHeaders JavaDoc;
5 import javax.xml.soap.Name JavaDoc;
6 import javax.xml.soap.SOAPEnvelope JavaDoc;
7 import javax.xml.soap.SOAPHeader JavaDoc;
8 import javax.xml.soap.SOAPHeaderElement JavaDoc;
9 import javax.xml.soap.SOAPMessage JavaDoc;
10 import java.io.ByteArrayInputStream JavaDoc;
11 import java.io.ByteArrayOutputStream JavaDoc;
12 import java.util.Iterator JavaDoc;
13
14 public class TestHeaders extends junit.framework.TestCase {
15
16     public TestHeaders(String JavaDoc name) {
17         super(name);
18     }
19
20     public void testAddingHeaderElements() throws Exception JavaDoc {
21         SOAPMessage JavaDoc soapMessage = MessageFactory.newInstance().createMessage();
22         SOAPEnvelope JavaDoc soapEnv = soapMessage.getSOAPPart().getEnvelope();
23         SOAPHeader JavaDoc header = soapEnv.getHeader();
24         header.addChildElement("ebxmlms");
25         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
26         soapMessage.writeTo(baos);
27         String JavaDoc xml = new String JavaDoc(baos.toByteArray());
28         assertTrue(xml.indexOf("ebxmlms") != -1);
29     }
30
31     
32     private final String JavaDoc actor = "ACTOR#1";
33     private final String JavaDoc localName = "Local1";
34     private final String JavaDoc namespace = "http://ws.apache.org";
35     private final String JavaDoc prefix = "P1";
36     
37     String JavaDoc xmlString =
38             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
39             "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
40             " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
41             " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
42             " <soapenv:Body>\n" +
43             " <shw:Address xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n" +
44             " <shw:City>GENT</shw:City>\n" +
45             " </shw:Address>\n" +
46             " </soapenv:Body>\n" +
47             "</soapenv:Envelope>";
48     
49     public void testAddingHeaderElements2() throws Exception JavaDoc {
50         MessageFactory JavaDoc mf = MessageFactory.newInstance();
51         SOAPMessage JavaDoc soapMessage = mf.createMessage(new MimeHeaders JavaDoc(), new ByteArrayInputStream JavaDoc(xmlString.getBytes()));
52         SOAPEnvelope JavaDoc soapEnv = soapMessage.getSOAPPart().getEnvelope();
53         SOAPHeader JavaDoc header = soapEnv.getHeader();
54
55         Name JavaDoc headerName = soapEnv.createName(localName, prefix, prefix);
56         SOAPHeaderElement JavaDoc he = header.addHeaderElement(headerName);
57         he.setActor(actor);
58
59         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
60         soapMessage.writeTo(baos);
61         
62         String JavaDoc xml = new String JavaDoc(baos.toByteArray());
63         assertTrue(xml.indexOf(localName) != -1);
64     }
65     
66     public void testExtractAllHeaders() throws Exception JavaDoc {
67         MessageFactory JavaDoc mf = MessageFactory.newInstance();
68         SOAPMessage JavaDoc soapMessage = mf.createMessage();
69         SOAPEnvelope JavaDoc envelope = soapMessage.getSOAPPart().getEnvelope();
70         SOAPHeader JavaDoc hdr = envelope.getHeader();
71         SOAPHeaderElement JavaDoc she1 = hdr.addHeaderElement(envelope.createName("foo1", "f1", "foo1-URI"));
72         she1.setActor("actor-URI");
73
74         Iterator JavaDoc iterator = hdr.extractAllHeaderElements();
75         SOAPHeaderElement JavaDoc she = null;
76         int cnt = 0;
77         while (iterator.hasNext()) {
78             cnt++;
79             she = (SOAPHeaderElement JavaDoc) iterator.next();
80             assertEquals(she, she1);
81         }
82         assertEquals(1, cnt);
83         iterator = hdr.extractAllHeaderElements();
84         assertTrue(!iterator.hasNext());
85     }
86
87     public void testExamineAllHeaders() throws Exception JavaDoc {
88         MessageFactory JavaDoc mf = MessageFactory.newInstance();
89         SOAPMessage JavaDoc soapMessage = mf.createMessage();
90         SOAPEnvelope JavaDoc envelope = soapMessage.getSOAPPart().getEnvelope();
91         SOAPHeader JavaDoc hdr = envelope.getHeader();
92         SOAPHeaderElement JavaDoc she1 = hdr.addHeaderElement(envelope.createName("foo1", "f1", "foo1-URI"));
93         she1.setActor("actor-URI");
94
95         Iterator JavaDoc iterator = hdr.examineAllHeaderElements();
96         SOAPHeaderElement JavaDoc she = null;
97         int cnt = 0;
98         while (iterator.hasNext()) {
99             cnt++;
100             she = (SOAPHeaderElement JavaDoc) iterator.next();
101             assertEquals(she, she1);
102         }
103         assertEquals(1, cnt);
104         iterator = hdr.examineAllHeaderElements();
105         assertTrue(iterator.hasNext());
106     }
107 }
108
Popular Tags