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.apache.commons.logging.Log;
023: import org.apache.commons.logging.LogFactory;
024: import org.w3c.dom.Document;
025: import org.w3c.dom.Element;
026:
027: import javax.xml.namespace.QName;
028: import javax.xml.parsers.DocumentBuilder;
029: import javax.xml.parsers.DocumentBuilderFactory;
030: import javax.xml.soap.Detail;
031: import javax.xml.soap.SOAPConstants;
032: import javax.xml.soap.SOAPElement;
033: import javax.xml.soap.SOAPException;
034: import javax.xml.soap.SOAPFactory;
035: import javax.xml.soap.SOAPFault;
036: import java.util.Iterator;
037:
038: /**
039: *
040: */
041: public class SOAPFactoryTest extends TestCase {
042: private static final Log log = LogFactory
043: .getLog(SOAPFactoryTest.class);
044:
045: public void testCreateDetail() {
046: try {
047: SOAPFactory sf = SOAPFactory.newInstance();
048: if (sf == null) {
049: fail("SOAPFactory was null");
050: }
051: Detail d = sf.createDetail();
052: if (d == null) {
053: fail("Detail was null");
054: }
055: } catch (Exception e) {
056: e.printStackTrace();
057: fail("Unexpected Exception " + e);
058: }
059: }
060:
061: public void testCreateElement() {
062: try {
063: //SOAPFactory sf = SOAPFactory.newInstance();
064: SOAPFactory sf = SOAPFactory
065: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
066: if (sf == null) {
067: fail("createElementTest4() could not create SOAPFactory object");
068: }
069: //Create QName object with localName=MyName1,prefix=MyPrefix1, uri=MyUri1
070: QName name = new QName("MyUri1", "MyName1", "MyPrefix1");
071: SOAPElement se = sf.createElement(name);
072: assertNotNull(se);
073: name = se.getElementQName();
074: String localName = name.getLocalPart();
075: String prefix = name.getPrefix();
076: String uri = name.getNamespaceURI();
077: if (localName == null) {
078: fail("localName is null (expected MyName1)");
079: } else if (!localName.equals("MyName1")) {
080: fail("localName is wrong (expected MyName1)");
081: } else if (prefix == null) {
082: fail("prefix is null (expected MyPrefix1)");
083: } else if (!prefix.equals("MyPrefix1")) {
084: fail("prefix is wrong (expected MyPrefix1)");
085: } else if (uri == null) {
086: fail("uri is null (expected MyUri1)");
087: } else if (!uri.equals("MyUri1")) {
088: fail("uri is wrong (expected MyUri1)");
089: }
090: } catch (Exception e) {
091: fail();
092: }
093: }
094:
095: public void testCreateElement2() {
096: try {
097: SOAPFactory sf = SOAPFactory.newInstance();
098: //SOAPFactory sf = SOAPFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
099: if (sf == null) {
100: fail("could not create SOAPFactory object");
101: }
102: log.info("Create a DOMElement");
103: DocumentBuilderFactory dbfactory = DocumentBuilderFactory
104: .newInstance();
105: DocumentBuilder builder = dbfactory.newDocumentBuilder();
106: Document document = builder.newDocument();
107: Element de = document.createElementNS(
108: "http://MyNamespace.org/", "MyTag");
109: //Calling SOAPFactory.createElement(org.w3c.dom.Element)
110: SOAPElement se = sf.createElement(de);
111: if (!de.getNodeName().equals(se.getNodeName())
112: || !de.getNamespaceURI().equals(
113: se.getNamespaceURI())) {
114: //Node names are not equal
115: fail("Got: <URI=" + se.getNamespaceURI() + ", PREFIX="
116: + se.getPrefix() + ", NAME=" + se.getNodeName()
117: + ">" + "Expected: <URI="
118: + de.getNamespaceURI() + ", PREFIX="
119: + de.getPrefix() + ", NAME=" + de.getNodeName()
120: + ">");
121: }
122: } catch (Exception e) {
123: fail("Exception: " + e);
124: }
125: }
126:
127: public void testCreateElement3() {
128: try {
129: SOAPFactory factory = SOAPFactory.newInstance();
130: if (factory == null) {
131: fail("createFaultTest1() could not create SOAPFactory object");
132: }
133: SOAPFault sf = factory.createFault();
134: if (sf == null) {
135: fail("createFault() returned null");
136: } else if (!(sf instanceof SOAPFault)) {
137: fail("createFault() did not create a SOAPFault object");
138: }
139: } catch (Exception e) {
140: fail();
141: }
142: }
143:
144: public void testCreateElement4() {
145: try {
146: SOAPFactory sf = SOAPFactory.newInstance();
147: if (sf == null) {
148: fail("createElementTest6() could not create SOAPFactory object");
149: }
150: QName qname = new QName("http://MyNamespace.org/", "MyTag");
151: SOAPElement se1 = sf.createElement(qname);
152: //Create second SOAPElement from first SOAPElement
153: SOAPElement se2 = sf.createElement(se1);
154: //commented to support jdk 1.4 build
155: // if(!se1.isEqualNode(se2) && !se1.isSameNode(se2)) {
156: // fail("The SOAPElement's are not equal and not the same (unexpected)");
157: // }
158: if (!se1.getNodeName().equals(se2.getNodeName())
159: || !se1.getNamespaceURI().equals(
160: se2.getNamespaceURI())) {
161: fail("Got: <URI=" + se1.getNamespaceURI() + ", PREFIX="
162: + se1.getPrefix() + ", NAME="
163: + se1.getNodeName() + ">" + "Expected: <URI="
164: + se2.getNamespaceURI() + ", PREFIX="
165: + se2.getPrefix() + ", NAME="
166: + se2.getNodeName() + ">");
167: }
168: } catch (Exception e) {
169: fail();
170: }
171: }
172:
173: public void testCreateFault() {
174: try {
175: SOAPFactory factory = SOAPFactory
176: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
177: //SOAPFactory factory = SOAPFactory.newInstance();
178: SOAPFault sf = factory.createFault(
179: "This is the fault reason.",
180: SOAPConstants.SOAP_RECEIVER_FAULT);
181: assertNotNull(sf);
182: assertTrue(sf instanceof SOAPFault);
183: QName fc = sf.getFaultCodeAsQName();
184: //Expect FaultCode="+SOAPConstants.SOAP_RECEIVER_FAULT
185: Iterator i = sf.getFaultReasonTexts();
186: if (i == null) {
187: log
188: .info("Call to getFaultReasonTexts() returned null iterator");
189: }
190: String reason = "";
191: while (i.hasNext()) {
192: reason += (String) i.next();
193: }
194: assertNotNull(reason);
195: assertTrue(reason.indexOf("This is the fault reason.") > -1);
196: assertTrue(fc.equals(SOAPConstants.SOAP_RECEIVER_FAULT));
197: } catch (SOAPException e) {
198: fail("Caught unexpected SOAPException");
199: }
200: }
201:
202: public void testCreateFault1() {
203: try {
204: //SOAPFactory factory = SOAPFactory.newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
205: SOAPFactory factory = SOAPFactory.newInstance();
206: SOAPFault sf = factory.createFault(
207: "This is the fault reason.",
208: SOAPConstants.SOAP_RECEIVER_FAULT);
209: assertNotNull(sf);
210: QName fc = sf.getFaultCodeAsQName();
211: Iterator i = sf.getFaultReasonTexts();
212:
213: String reason = "";
214: while (i.hasNext()) {
215: reason += (String) i.next();
216: }
217: log.info("Actual ReasonText=" + reason);
218: assertNotNull(reason);
219: assertTrue(reason.indexOf("This is the fault reason.") > -1);
220: assertTrue(fc.equals(SOAPConstants.SOAP_RECEIVER_FAULT));
221: } catch (SOAPException e) {
222: //Caught expected SOAPException
223: } catch (Exception e) {
224: fail("Exception: " + e);
225: }
226: }
227:
228: /** for soap 1.1 */
229: public void testSOAPFaultException1() {
230: try {
231: SOAPFactory factory = SOAPFactory
232: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
233: SOAPFault fault = factory.createFault(
234: "This is the fault reason.", new QName(
235: "http://MyNamespaceURI.org/",
236: "My Fault Code"));
237: } catch (UnsupportedOperationException e) {
238: //Caught expected UnsupportedOperationException
239: } catch (SOAPException e) {
240: //Caught expected SOAPException
241: } catch (IllegalArgumentException e) {
242: //Caught expected IllegalArgumentException
243: } catch (Exception e) {
244: fail("Exception: " + e);
245: }
246: }
247:
248: /** for soap 1.2 */
249: public void testSOAPFaultException2() {
250: try {
251: SOAPFactory factory = SOAPFactory
252: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
253: SOAPFault sf = factory.createFault(
254: "This is the fault reason.", new QName(
255: "http://MyNamespaceURI.org/",
256: "My Fault Code"));
257: fail("Did not throw expected SOAPException");
258: } catch (UnsupportedOperationException e) {
259: //Caught expected UnsupportedOperationException
260: } catch (SOAPException e) {
261: //Caught expected SOAPException
262: } catch (IllegalArgumentException e) {
263: //Caught expected IllegalArgumentException
264: } catch (Exception e) {
265: fail("Exception: " + e);
266: }
267: }
268: }
|