001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019: package org.apache.axis2.saaj;
020:
021: import junit.framework.TestCase;
022:
023: import javax.xml.namespace.QName;
024: import javax.xml.soap.MessageFactory;
025: import javax.xml.soap.Name;
026: import javax.xml.soap.SOAPBody;
027: import javax.xml.soap.SOAPConstants;
028: import javax.xml.soap.SOAPElement;
029: import javax.xml.soap.SOAPEnvelope;
030: import javax.xml.soap.SOAPException;
031: import javax.xml.soap.SOAPFactory;
032: import javax.xml.soap.SOAPHeader;
033: import javax.xml.soap.SOAPHeaderElement;
034: import javax.xml.soap.SOAPMessage;
035: import javax.xml.soap.SOAPPart;
036: import java.util.ArrayList;
037: import java.util.Iterator;
038:
039: public class SOAPHeaderTest extends TestCase {
040: private MessageFactory mf = null;
041: private SOAPMessage msg = null;
042: private SOAPPart sp = null;
043: private SOAPEnvelope envelope = null;
044: private SOAPHeader hdr = null;
045: private SOAPHeaderElement she1 = null;
046: private SOAPHeaderElement she2 = null;
047:
048: public SOAPHeaderTest(String name) {
049: super (name);
050: }
051:
052: public void testAddHeaderElements() throws Exception {
053: javax.xml.soap.SOAPMessage soapMessage = javax.xml.soap.MessageFactory
054: .newInstance().createMessage();
055: javax.xml.soap.SOAPEnvelope soapEnv = soapMessage.getSOAPPart()
056: .getEnvelope();
057: javax.xml.soap.SOAPHeader header = soapEnv.getHeader();
058: try {
059: header.addChildElement("ebxmlms1");
060: } catch (Exception e) {
061: assertTrue(e instanceof SOAPException);
062: }
063:
064: assertTrue(header.addChildElement("ebxmlms1", "ns-prefix",
065: "http://test.apache.org") instanceof SOAPHeaderElement);
066: ((SOAPHeaderElement) header.getFirstChild())
067: .addTextNode("test add");
068:
069: assertTrue(header.addHeaderElement(soapEnv.createName(
070: "ebxmlms2", "ns-prefix", "http://test2.apache.org")) != null);
071: assertTrue(header.addHeaderElement(new PrefixedQName(
072: "http://test3.apache.org", "ebxmlms3", "ns-prefix")) != null);
073:
074: SOAPHeaderElement firstChild = (SOAPHeaderElement) header
075: .getFirstChild();
076: assertEquals("ebxmlms1", firstChild.getLocalName());
077: assertEquals("ns-prefix", firstChild.getPrefix());
078: assertEquals("http://test.apache.org", firstChild
079: .getNamespaceURI());
080:
081: SOAPHeaderElement secondChild = (SOAPHeaderElement) firstChild
082: .getNextSibling();
083: assertEquals("ebxmlms2", secondChild.getLocalName());
084: assertEquals("ns-prefix", secondChild.getPrefix());
085: assertEquals("http://test2.apache.org", secondChild
086: .getNamespaceURI());
087:
088: SOAPHeaderElement lastChild = (SOAPHeaderElement) header
089: .getLastChild();
090: assertEquals("ebxmlms3", lastChild.getLocalName());
091: assertEquals("ns-prefix", lastChild.getPrefix());
092: assertEquals("http://test3.apache.org", lastChild
093: .getNamespaceURI());
094:
095: SOAPHeaderElement fourthChild = (SOAPHeaderElement) lastChild
096: .getPreviousSibling();
097: assertEquals("ebxmlms2", fourthChild.getLocalName());
098: assertEquals("ns-prefix", fourthChild.getPrefix());
099: assertEquals("http://test2.apache.org", fourthChild
100: .getNamespaceURI());
101:
102: Iterator it = header.getChildElements();
103: int numOfHeaderElements = 0;
104: while (it.hasNext()) {
105: Object o = it.next();
106: assertTrue(o instanceof SOAPHeaderElement);
107: SOAPHeaderElement el = (SOAPHeaderElement) o;
108: String lName = el.getLocalName();
109: assertTrue(lName.equals("ebxmlms" + ++numOfHeaderElements));
110: }
111: assertEquals(3, numOfHeaderElements);
112: }
113:
114: public void testHeaders() {
115: try {
116: // Create message factory and SOAP factory
117: MessageFactory messageFactory = MessageFactory
118: .newInstance();
119: SOAPFactory soapFactory = SOAPFactory.newInstance();
120:
121: // Create a message
122: SOAPMessage message = messageFactory.createMessage();
123:
124: // Get the SOAP header from the message and
125: // add headers to it
126: SOAPHeader header = message.getSOAPHeader();
127:
128: String nameSpace = "ns";
129: String nameSpaceURI = "http://gizmos.com/NSURI";
130:
131: Name order = soapFactory.createName("orderDesk", nameSpace,
132: nameSpaceURI);
133: SOAPHeaderElement orderHeader = header
134: .addHeaderElement(order);
135: orderHeader.setActor("http://gizmos.com/orders");
136:
137: Name shipping = soapFactory.createName("shippingDesk",
138: nameSpace, nameSpaceURI);
139: SOAPHeaderElement shippingHeader = header
140: .addHeaderElement(shipping);
141: shippingHeader.setActor("http://gizmos.com/shipping");
142:
143: Name confirmation = soapFactory.createName(
144: "confirmationDesk", nameSpace, nameSpaceURI);
145: SOAPHeaderElement confirmationHeader = header
146: .addHeaderElement(confirmation);
147: confirmationHeader
148: .setActor("http://gizmos.com/confirmations");
149:
150: Name billing = soapFactory.createName("billingDesk",
151: nameSpace, nameSpaceURI);
152: SOAPHeaderElement billingHeader = header
153: .addHeaderElement(billing);
154: billingHeader.setActor("http://gizmos.com/billing");
155:
156: // Add header with mustUnderstand attribute
157: Name tName = soapFactory.createName("Transaction", "t",
158: "http://gizmos.com/orders");
159:
160: SOAPHeaderElement transaction = header
161: .addHeaderElement(tName);
162: transaction.setMustUnderstand(true);
163: transaction.addTextNode("5");
164:
165: // Get the SOAP body from the message but leave
166: // it empty
167: SOAPBody body = message.getSOAPBody();
168:
169: message.saveChanges();
170:
171: // Display the message that would be sent
172: //System.out.println("\n----- Request Message ----\n");
173: //message.writeTo(System.out);
174:
175: // Look at the headers
176: Iterator allHeaders = header.examineAllHeaderElements();
177:
178: while (allHeaders.hasNext()) {
179: SOAPHeaderElement headerElement = (SOAPHeaderElement) allHeaders
180: .next();
181: Name headerName = headerElement.getElementName();
182: //System.out.println("\nHeader name is " +
183: // headerName.getQualifiedName());
184: //System.out.println("Actor is " + headerElement.getActor());
185: //System.out.println("mustUnderstand is " +
186: // headerElement.getMustUnderstand());
187: }
188: } catch (Exception e) {
189: fail("Enexpected Exception " + e);
190: }
191: }
192:
193: protected void setUp() throws Exception {
194: msg = MessageFactory.newInstance().createMessage();
195: sp = msg.getSOAPPart();
196: envelope = sp.getEnvelope();
197: hdr = envelope.getHeader();
198: }
199:
200: public void testExamineHeader() {
201: SOAPHeaderElement she = null;
202:
203: try {
204: she1 = hdr.addHeaderElement(envelope.createName("foo1",
205: "f1", "foo1-URI"));
206: she1.setActor("actor-URI");
207: Iterator iterator = hdr.examineAllHeaderElements();
208: int cnt = 0;
209: while (iterator.hasNext()) {
210: cnt++;
211: she = (SOAPHeaderElement) iterator.next();
212: if (!she.equals(she1)) {
213: fail("SOAP Header Elements do not match");
214: }
215: }
216:
217: if (cnt != 1) {
218: fail("SOAPHeaderElement count mismatch: expected 1, received "
219: + cnt);
220: }
221:
222: iterator = hdr.examineAllHeaderElements();
223: if (!iterator.hasNext()) {
224: fail("no elements in iterator - unexpected");
225: }
226:
227: } catch (Exception e) {
228: fail("Unexpected Exception: " + e);
229: }
230: }
231:
232: public void testAddNotUnderstoodHeaderElement() throws Exception {
233: javax.xml.soap.SOAPMessage soapMessage = javax.xml.soap.MessageFactory
234: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)
235: .createMessage();
236:
237: javax.xml.soap.SOAPEnvelope soapEnv = soapMessage.getSOAPPart()
238: .getEnvelope();
239: javax.xml.soap.SOAPHeader header = soapEnv.getHeader();
240:
241: SOAPElement soapElement = header
242: .addNotUnderstoodHeaderElement(new QName(
243: "http://foo.org", "foo", "f"));
244:
245: assertNotNull(soapElement);
246: Name name = soapElement.getElementName();
247: String uri = name.getURI();
248: String localName = name.getLocalName();
249: assertEquals(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, uri);
250: //Validate the LocalName which must be NotUnderstood
251: assertEquals("NotUnderstood", localName);
252: }
253:
254: public void testAddUpgradeHeaderElement() throws Exception {
255: javax.xml.soap.SOAPMessage soapMessage = javax.xml.soap.MessageFactory
256: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)
257: .createMessage();
258:
259: javax.xml.soap.SOAPEnvelope soapEnv = soapMessage.getSOAPPart()
260: .getEnvelope();
261: javax.xml.soap.SOAPHeader header = soapEnv.getHeader();
262:
263: // create a list of supported URIs.
264: ArrayList supported = new ArrayList();
265: supported.add(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE);
266: supported.add(SOAPConstants.URI_NS_SOAP_ENVELOPE);
267:
268: SOAPElement soapElement = header
269: .addUpgradeHeaderElement(supported.iterator());
270: assertNotNull(soapElement);
271: Name name = soapElement.getElementName();
272: String uri = name.getURI();
273: String localName = name.getLocalName();
274:
275: //Validate the URI which must be SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE);
276: assertTrue(uri.equals(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE));
277: assertTrue(localName.equals("Upgrade"));
278: }
279:
280: public void testExamineHeaderElements() throws Exception {
281: javax.xml.soap.SOAPMessage soapMessage = javax.xml.soap.MessageFactory
282: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL)
283: .createMessage();
284:
285: javax.xml.soap.SOAPEnvelope soapEnv = soapMessage.getSOAPPart()
286: .getEnvelope();
287: javax.xml.soap.SOAPHeader header = soapEnv.getHeader();
288:
289: SOAPHeaderElement soapHeaderElement = header
290: .addHeaderElement(envelope.createName("foo1", "f1",
291: "foo1-URI"));
292:
293: Iterator iterator = null;
294: soapHeaderElement.setRole("role-URI");
295:
296: iterator = header.examineHeaderElements("role1-URI");
297:
298: int count = 0;
299: while (iterator.hasNext()) {
300: count++;
301: iterator.next();
302: }
303: assertEquals(0, count);
304: }
305:
306: public void testExamineHeaderElements2() throws Exception {
307: javax.xml.soap.SOAPMessage soapMessage = javax.xml.soap.MessageFactory
308: .newInstance().createMessage();
309:
310: javax.xml.soap.SOAPEnvelope soapEnv = soapMessage.getSOAPPart()
311: .getEnvelope();
312: javax.xml.soap.SOAPHeader header = soapEnv.getHeader();
313: SOAPHeaderElement soapHeaderElement = null;
314:
315: try {
316: // Add some soap header elements
317: SOAPElement se = header.addHeaderElement(
318: envelope.createName("Header1", "prefix",
319: "http://myuri")).addTextNode(
320: "This is Header1");
321: soapHeaderElement = (SOAPHeaderElement) se;
322: soapHeaderElement.setMustUnderstand(true);
323:
324: se = header.addHeaderElement(
325: envelope.createName("Header2", "prefix",
326: "http://myuri")).addTextNode(
327: "This is Header2");
328: soapHeaderElement = (SOAPHeaderElement) se;
329: soapHeaderElement.setMustUnderstand(false);
330:
331: se = header.addHeaderElement(
332: envelope.createName("Header3", "prefix",
333: "http://myuri")).addTextNode(
334: "This is Header3");
335: soapHeaderElement = (SOAPHeaderElement) se;
336: soapHeaderElement.setMustUnderstand(true);
337:
338: se = header.addHeaderElement(
339: envelope.createName("Header4", "prefix",
340: "http://myuri")).addTextNode(
341: "This is Header4");
342: soapHeaderElement = (SOAPHeaderElement) se;
343: soapHeaderElement.setMustUnderstand(false);
344:
345: Iterator iterator = header.examineAllHeaderElements();
346:
347: //validating Iterator count .... should be 4");
348: int cnt = 0;
349: while (iterator.hasNext()) {
350: cnt++;
351: soapHeaderElement = (SOAPHeaderElement) iterator.next();
352: }
353: assertEquals(cnt, 4);
354: iterator = header
355: .examineHeaderElements(SOAPConstants.URI_SOAP_ACTOR_NEXT);
356: cnt = 0;
357: while (iterator.hasNext()) {
358: cnt++;
359: soapHeaderElement = (SOAPHeaderElement) iterator.next();
360: }
361: assertEquals(cnt, 0);
362: } catch (Exception e) {
363: fail("Unexpected Exception: " + e);
364: }
365: }
366:
367: public void testQNamesOnHeader() {
368: SOAPHeaderElement headerElement = null;
369: try {
370: //SOAP1.1 and SOAP1.2 requires all HeaderElements to be namespace qualified
371: //Try adding HeaderElement with unqualified QName not belonging to any namespace
372: //(expect SOAPException)
373: headerElement = hdr.addHeaderElement(envelope
374: .createName("Transaction"));
375: fail("Did not throw expected SOAPException");
376: } catch (SOAPException e) {
377: //Did throw expected SOAPException
378: } catch (Exception e) {
379: fail("Unexpected Exception: " + e.getMessage());
380: }
381: }
382: }
|