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: import org.w3c.dom.Document;
023: import org.w3c.dom.Element;
024:
025: import javax.xml.namespace.QName;
026: import javax.xml.parsers.DocumentBuilder;
027: import javax.xml.parsers.DocumentBuilderFactory;
028: import javax.xml.soap.MessageFactory;
029: import javax.xml.soap.Name;
030: import javax.xml.soap.Node;
031: import javax.xml.soap.SOAPBody;
032: import javax.xml.soap.SOAPBodyElement;
033: import javax.xml.soap.SOAPConstants;
034: import javax.xml.soap.SOAPElement;
035: import javax.xml.soap.SOAPEnvelope;
036: import javax.xml.soap.SOAPFault;
037: import javax.xml.soap.SOAPHeader;
038: import javax.xml.soap.SOAPMessage;
039: import javax.xml.soap.SOAPPart;
040: import javax.xml.soap.Text;
041: import java.io.File;
042: import java.util.Iterator;
043:
044: public class SOAPBodyTest extends TestCase {
045:
046: /**
047: * Method suite
048: *
049: * @return
050: */
051: /* public static Test suite() {
052: return new TestSuite(test.message.TestSOAPBody.class);
053: }
054: */
055:
056: /**
057: * Constructor TestSOAPBody
058: *
059: * @param name
060: */
061: public SOAPBodyTest(String name) {
062: super (name);
063: }
064:
065: /**
066: * Method testSoapBodyBUG
067: *
068: * @throws Exception
069: */
070: public void testSoapBody() throws Exception {
071:
072: MessageFactory fact = MessageFactory.newInstance();
073: SOAPMessage message = fact.createMessage();
074: SOAPPart soapPart = message.getSOAPPart();
075: SOAPEnvelopeImpl env = (SOAPEnvelopeImpl) soapPart
076: .getEnvelope();
077: SOAPHeader header = env.getHeader();
078: Name hns = env.createName("Hello", "shw",
079: "http://www.jcommerce.net/soap/ns/SOAPHelloWorld");
080: SOAPElement headElmnt = header.addHeaderElement(hns);
081: Name hns1 = env.createName("Myname", "shw",
082: "http://www.jcommerce.net/soap/ns/SOAPHelloWorld");
083: SOAPElement myName = headElmnt.addChildElement(hns1);
084: myName.addTextNode("Tony");
085: Name ns = env.createName("Address", "shw",
086: "http://www.jcommerce.net/soap/ns/SOAPHelloWorld");
087: SOAPBody body = env.getBody();
088: SOAPElement bodyElmnt = body.addBodyElement(ns);
089: Name ns1 = env.createName("City", "shw",
090: "http://www.jcommerce.net/soap/ns/SOAPHelloWorld");
091: SOAPElement city = bodyElmnt.addChildElement(ns1);
092: city.addTextNode("GENT");
093:
094: SOAPElement city2 = body.addChildElement(ns1);
095: assertTrue(city2 instanceof SOAPBodyElement);
096: city2.addTextNode("CIT2");
097:
098: Iterator it = body.getChildElements();
099: int count = 0;
100:
101: while (it.hasNext()) {
102: Object o = it.next();
103: assertTrue(o instanceof SOAPBodyElement);
104: SOAPBodyElement bodyElement = (SOAPBodyElement) o;
105: assertEquals(
106: "http://www.jcommerce.net/soap/ns/SOAPHelloWorld",
107: bodyElement.getNamespaceURI());
108: assertEquals("shw", bodyElement.getPrefix());
109: assertTrue(bodyElement.getLocalName().equals("City")
110: || bodyElement.getLocalName().equals("Address"));
111: count++;
112: }
113: assertEquals(2, count);
114: }
115:
116: public void _testAddDocument() {
117: try {
118: Document document = null;
119: DocumentBuilderFactory factory = DocumentBuilderFactory
120: .newInstance();
121: factory.setNamespaceAware(true);
122:
123: DocumentBuilder builder = factory.newDocumentBuilder();
124: document = builder.parse(new File(System.getProperty(
125: "basedir", ".")
126: + "/"
127: + "test-resources"
128: + File.separator
129: + "soap-body.xml"));
130: MessageFactory fact = MessageFactory.newInstance();
131: SOAPMessage message = fact.createMessage();
132:
133: message.getSOAPHeader().detachNode();
134: // assertNull(message.getSOAPHeader());
135: // TODO:this fails. Header is always being created if it doesnt exist in DOOM
136:
137: SOAPBody soapBody = message.getSOAPBody();
138: soapBody.addDocument(document);
139: message.saveChanges();
140:
141: // Get contents using SAAJ APIs
142: Iterator iter1 = soapBody.getChildElements();
143: getContents(iter1, "");
144: } catch (Exception e) {
145: e.printStackTrace();
146: fail("Unexpected Exception : " + e);
147: }
148: }
149:
150: private void getContents(Iterator iterator, String indent) {
151: while (iterator.hasNext()) {
152: Node node = (Node) iterator.next();
153: SOAPElement element = null;
154: Text text = null;
155:
156: if (node instanceof SOAPElement) {
157: element = (SOAPElement) node;
158: Name name = element.getElementName();
159: Iterator attrs = element.getAllAttributes();
160:
161: /*
162: while (attrs.hasNext()) {
163: Name attrName = (Name) attrs.next();
164: System.out.println(indent + " Attribute name is " +
165: attrName.getQualifiedName());
166: System.out.println(indent + " Attribute value is " +
167: element.getAttributeValue(attrName));
168: }
169: */
170:
171: Iterator iter2 = element.getChildElements();
172: getContents(iter2, indent + " ");
173: } else {
174: text = (Text) node;
175: assertNotNull(text);
176: }
177: }
178: }
179:
180: //TODO : fix
181: public void testExtractContentAsDocument() {
182: try {
183: MessageFactory fact = MessageFactory.newInstance();
184: SOAPMessage message = fact.createMessage();
185: SOAPBody soapBody = message.getSOAPBody();
186:
187: QName qname1 = new QName("http://wombat.ztrade.com",
188: "GetLastTradePrice", "ztrade");
189: SOAPElement child1 = soapBody.addChildElement(qname1);
190: Document document = soapBody.extractContentAsDocument();
191:
192: assertNotNull(document);
193: assertTrue(document instanceof Document);
194: Element element = document.getDocumentElement();
195: String elementName = element.getTagName();
196:
197: //Retreive the children of the SOAPBody (should be none)
198: Iterator childElements = soapBody.getChildElements();
199: int childCount = 0;
200: while (childElements.hasNext()) {
201: Object object = childElements.next();
202: childCount++;
203: }
204: assertEquals(childCount, 0);
205: } catch (Exception e) {
206: fail("Unexpected Exception : " + e);
207: }
208: }
209:
210: /*
211: * For SOAP 1.1 message
212: */
213: public void testAddAttribute() {
214: try {
215: MessageFactory fact = MessageFactory.newInstance();
216: SOAPMessage message = fact.createMessage();
217: SOAPBody soapBody = message.getSOAPBody();
218: QName qname = new QName("http://test.apache.org/",
219: "Child1", "ch");
220: String value = "MyValue1";
221: soapBody.addAttribute(qname, value);
222: message.saveChanges();
223:
224: } catch (Exception e) {
225: fail("Unexpected Exception : " + e);
226: }
227: }
228:
229: /*
230: * For SOAP 1.2 message
231: */
232: public void testAddAttribute2() {
233: try {
234: MessageFactory fact = MessageFactory
235: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
236: SOAPMessage message = fact.createMessage();
237: SOAPBody soapBody = message.getSOAPBody();
238: QName qname = new QName("http://test.apache.org/",
239: "Child1", "ch");
240: String value = "MyValue1";
241: soapBody.addAttribute(qname, value);
242: message.saveChanges();
243: } catch (Exception e) {
244: fail("Unexpected Exception : " + e);
245: }
246: }
247:
248: /*
249: * For SOAP 1.2 message
250: */
251: public void testAddFault() {
252: try {
253: MessageFactory fact = MessageFactory.newInstance();
254: SOAPMessage message = fact.createMessage();
255: SOAPPart soapPart = message.getSOAPPart();
256: SOAPEnvelope soapEnvelope = soapPart.getEnvelope();
257: SOAPBody soapBody = soapEnvelope.getBody();
258:
259: QName qname = new QName("http://test.apache.org/",
260: "Child1", "ch");
261: String value = "MyFault";
262: SOAPFault soapFault = soapBody.addFault(qname, value);
263: message.saveChanges();
264: assertNotNull(soapFault);
265: assertTrue(soapFault instanceof SOAPFault);
266: } catch (Exception e) {
267: fail("Unexpected Exception : " + e);
268: }
269: }
270:
271: }
|