KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestEnvelope


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.Node JavaDoc;
7 import javax.xml.soap.SOAPBody JavaDoc;
8 import javax.xml.soap.SOAPConnection JavaDoc;
9 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
10 import javax.xml.soap.SOAPElement JavaDoc;
11 import javax.xml.soap.SOAPEnvelope JavaDoc;
12 import javax.xml.soap.SOAPFault JavaDoc;
13 import javax.xml.soap.SOAPHeader JavaDoc;
14 import javax.xml.soap.SOAPHeaderElement JavaDoc;
15 import javax.xml.soap.SOAPMessage JavaDoc;
16 import javax.xml.soap.SOAPPart JavaDoc;
17 import javax.xml.soap.Text JavaDoc;
18 import javax.xml.soap.Detail JavaDoc;
19 import javax.xml.soap.DetailEntry JavaDoc;
20 import java.io.ByteArrayInputStream JavaDoc;
21 import java.io.ByteArrayOutputStream JavaDoc;
22 import java.util.Iterator JavaDoc;
23
24 public class TestEnvelope extends junit.framework.TestCase {
25
26     public TestEnvelope(String JavaDoc name) {
27         super(name);
28     }
29
30
31     String JavaDoc xmlString =
32             "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
33             "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\"\n" +
34             " xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"\n" +
35             " xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n" +
36             " <soapenv:Header>\n" +
37             " <shw:Hello xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n" +
38             " <shw:Myname>Tony</shw:Myname>\n" +
39             " </shw:Hello>\n" +
40             " </soapenv:Header>\n" +
41             " <soapenv:Body>\n" +
42             " <shw:Address xmlns:shw=\"http://www.jcommerce.net/soap/ns/SOAPHelloWorld\">\n" +
43             " <shw:City>GENT</shw:City>\n" +
44             " </shw:Address>\n" +
45             " </soapenv:Body>\n" +
46             "</soapenv:Envelope>";
47
48     // Test JAXM methods...
49
public void testEnvelope() throws Exception JavaDoc {
50         MessageFactory JavaDoc mf = MessageFactory.newInstance();
51         SOAPMessage JavaDoc smsg =
52                 mf.createMessage(new MimeHeaders JavaDoc(), new ByteArrayInputStream JavaDoc(xmlString.getBytes()));
53         SOAPPart JavaDoc sp = smsg.getSOAPPart();
54         SOAPEnvelope JavaDoc se = (SOAPEnvelope JavaDoc)sp.getEnvelope();
55         //smsg.writeTo(System.out);
56
assertTrue(se != null);
57     }
58
59     // Test JAXM methods...
60
public void testEnvelope2() throws Exception JavaDoc {
61         MessageFactory JavaDoc mf = MessageFactory.newInstance();
62         SOAPMessage JavaDoc smsg =
63                 mf.createMessage(new MimeHeaders JavaDoc(), new ByteArrayInputStream JavaDoc(xmlString.getBytes()));
64         SOAPPart JavaDoc sp = smsg.getSOAPPart();
65         SOAPEnvelope JavaDoc se = (SOAPEnvelope JavaDoc)sp.getEnvelope();
66         ByteArrayOutputStream JavaDoc baos = new ByteArrayOutputStream JavaDoc();
67         smsg.writeTo(baos);
68         SOAPBody JavaDoc body = smsg.getSOAPPart().getEnvelope().getBody();
69         assertTrue(body != null);
70     }
71     
72     public void testEnvelopeWithLeadingComment() throws Exception JavaDoc {
73         String JavaDoc soapMessageWithLeadingComment =
74             "<?xml version='1.0' encoding='UTF-8'?>" +
75             "<!-- Comment -->" +
76             "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>" +
77             "<env:Body><echo><arg0>Hello</arg0></echo></env:Body>" +
78             "</env:Envelope>";
79         
80         SOAPConnectionFactory JavaDoc scFactory = SOAPConnectionFactory.newInstance();
81         SOAPConnection JavaDoc con = scFactory.createConnection();
82         
83         MessageFactory JavaDoc factory = MessageFactory.newInstance();
84         SOAPMessage JavaDoc message =
85             factory.createMessage(new MimeHeaders JavaDoc(),
86                     new ByteArrayInputStream JavaDoc(soapMessageWithLeadingComment.getBytes()));
87         SOAPPart JavaDoc part = message.getSOAPPart();
88         SOAPEnvelope JavaDoc envelope = (SOAPEnvelope JavaDoc) part.getEnvelope();
89         //message.writeTo(System.out);
90
assertTrue(envelope != null);
91     }
92     
93     private SOAPEnvelope JavaDoc getSOAPEnvelope() throws Exception JavaDoc {
94         SOAPConnectionFactory JavaDoc scFactory = SOAPConnectionFactory.newInstance();
95         SOAPConnection JavaDoc con = scFactory.createConnection();
96
97         MessageFactory JavaDoc factory = MessageFactory.newInstance();
98         SOAPMessage JavaDoc message = factory.createMessage();
99         SOAPEnvelope JavaDoc envelope = message.getSOAPPart().getEnvelope();
100         return envelope;
101     }
102
103     public void testAttributes() throws Exception JavaDoc {
104         SOAPEnvelope JavaDoc envelope = getSOAPEnvelope();
105         SOAPBody JavaDoc body = envelope.getBody();
106
107         Name JavaDoc name1 = envelope.createName("MyAttr1");
108         String JavaDoc value1 = "MyValue1";
109         Name JavaDoc name2 = envelope.createName("MyAttr2");
110         String JavaDoc value2 = "MyValue2";
111         Name JavaDoc name3 = envelope.createName("MyAttr3");
112         String JavaDoc value3 = "MyValue3";
113         body.addAttribute(name1, value1);
114         body.addAttribute(name2, value2);
115         body.addAttribute(name3, value3);
116         java.util.Iterator JavaDoc iterator = body.getAllAttributes();
117         assertTrue(getIteratorCount(iterator) == 3);
118         iterator = body.getAllAttributes();
119         boolean foundName1 = false;
120         boolean foundName2 = false;
121         boolean foundName3 = false;
122         while (iterator.hasNext()) {
123             Name JavaDoc name = (Name JavaDoc) iterator.next();
124             if (name.equals(name1))
125                 foundName1 = true;
126             else if (name.equals(name2))
127                 foundName2 = true;
128             else if (name.equals(name3))
129                 foundName3 = true;
130         }
131         assertTrue(foundName1 && foundName2 && foundName3);
132     }
133
134     public void testFaults() throws Exception JavaDoc {
135         SOAPEnvelope JavaDoc envelope = getSOAPEnvelope();
136         SOAPBody JavaDoc body = envelope.getBody();
137         SOAPFault JavaDoc sf = body.addFault();
138         sf.setFaultCode("myFault");
139         String JavaDoc fc = sf.getFaultCode();
140         assertTrue(fc.equals("myFault"));
141     }
142
143     public void testFaults2() throws Exception JavaDoc {
144         SOAPEnvelope JavaDoc envelope = getSOAPEnvelope();
145         SOAPBody JavaDoc body = envelope.getBody();
146         SOAPFault JavaDoc sf = body.addFault();
147
148         assertTrue(body.getFault() != null);
149
150         Detail JavaDoc d1 = sf.addDetail();
151         Name JavaDoc name = envelope.createName("GetLastTradePrice", "WOMBAT",
152             "http://www.wombat.org/trader");
153         d1.addDetailEntry(name);
154         
155         Detail JavaDoc d2 = sf.getDetail();
156         assertTrue(d2 != null);
157         Iterator JavaDoc i = d2.getDetailEntries();
158         assertTrue(getIteratorCount(i) == 1);
159         i = d2.getDetailEntries();
160         while(i.hasNext()) {
161             DetailEntry JavaDoc de = (DetailEntry JavaDoc)i.next();
162             assertEquals(de.getElementName(),name);
163         }
164     }
165     
166     public void testHeaderElements() throws Exception JavaDoc {
167         SOAPEnvelope JavaDoc envelope = getSOAPEnvelope();
168         SOAPBody JavaDoc body = envelope.getBody();
169         SOAPHeader JavaDoc hdr = envelope.getHeader();
170
171         SOAPHeaderElement JavaDoc she1 = hdr.addHeaderElement(envelope.createName("foo1", "f1", "foo1-URI"));
172         she1.setActor("actor-URI");
173         java.util.Iterator JavaDoc iterator = hdr.extractHeaderElements("actor-URI");
174         int cnt = 0;
175         while (iterator.hasNext()) {
176             cnt++;
177             SOAPHeaderElement JavaDoc she = (SOAPHeaderElement JavaDoc) iterator.next();
178             assertTrue(she.equals(she1));
179         }
180         assertTrue(cnt == 1);
181         iterator = hdr.extractHeaderElements("actor-URI");
182         assertTrue(!iterator.hasNext());
183     }
184
185     public void testText1() throws Exception JavaDoc {
186         SOAPEnvelope JavaDoc envelope = getSOAPEnvelope();
187         Iterator JavaDoc iStart = envelope.getChildElements();
188         int countStart = getIteratorCount(iStart);
189         SOAPElement JavaDoc se = envelope.addTextNode("<txt>This is text</txt>");
190         assertTrue(se != null);
191         assertTrue(envelope.getValue().equals("<txt>This is text</txt>"));
192         Iterator JavaDoc i = envelope.getChildElements();
193         int count = getIteratorCount(i);
194         assertTrue(count == countStart + 1);
195     }
196
197     public void testText2() throws Exception JavaDoc {
198         SOAPEnvelope JavaDoc envelope = getSOAPEnvelope();
199         SOAPElement JavaDoc se = envelope.addTextNode("This is text");
200         Iterator JavaDoc iterator = se.getChildElements();
201         Node JavaDoc n = null;
202         while (iterator.hasNext()) {
203             n = (Node JavaDoc)iterator.next();
204             if (n instanceof Text JavaDoc)
205                 break;
206         }
207         assertTrue(n instanceof Text JavaDoc);
208         Text JavaDoc t = (Text JavaDoc)n;
209         assertTrue(!t.isComment());
210     }
211
212     public void testText3() throws Exception JavaDoc {
213         SOAPEnvelope JavaDoc envelope = getSOAPEnvelope();
214         SOAPElement JavaDoc se = envelope.addTextNode("<!-- This is a comment -->");
215         Iterator JavaDoc iterator = se.getChildElements();
216         Node JavaDoc n = null;
217         while (iterator.hasNext()) {
218             n = (Node JavaDoc)iterator.next();
219             if (n instanceof Text JavaDoc)
220                 break;
221         }
222         assertTrue(n instanceof Text JavaDoc);
223         Text JavaDoc t = (Text JavaDoc)n;
224         assertTrue(t.isComment());
225     }
226
227     private int getIteratorCount(java.util.Iterator JavaDoc i) {
228         int count = 0;
229         while (i.hasNext()) {
230             count++;
231             i.next();
232         }
233         return count;
234     }
235
236     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
237         test.saaj.TestEnvelope tester = new test.saaj.TestEnvelope("TestEnvelope");
238         tester.testFaults2();
239         tester.testEnvelope();
240         tester.testText3();
241         tester.testText2();
242         tester.testText1();
243         tester.testHeaderElements();
244         tester.testFaults();
245         tester.testAttributes();
246     }
247 }
248
Popular Tags