001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: /*
038: * The contents of this file are subject to the terms
039: * of the Common Development and Distribution License
040: * (the "License"). You may not use this file except
041: * in compliance with the License.
042: *
043: * You can obtain a copy of the license at
044: * https://jwsdp.dev.java.net/CDDLv1.0.html
045: * See the License for the specific language governing
046: * permissions and limitations under the License.
047: *
048: * When distributing Covered Code, include this CDDL
049: * HEADER in each file and include the License file at
050: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
051: * add the following below this CDDL HEADER, with the
052: * fields enclosed by brackets "[]" replaced with your
053: * own identifying information: Portions Copyright [yyyy]
054: * [name of copyright owner]
055: */
056: package soap12;
057:
058: import javax.xml.soap.*;
059: import javax.xml.namespace.QName;
060: import javax.xml.transform.stream.StreamSource;
061:
062: import java.io.*;
063:
064: import java.util.Locale;
065: import java.util.Iterator;
066:
067: import junit.framework.TestCase;
068:
069: public class SOAPFaultTests extends TestCase {
070:
071: public SOAPFaultTests(String name) throws Exception {
072: super (name);
073: }
074:
075: public void testIncomingFault() throws Exception {
076:
077: MessageFactory mFactory = MessageFactory
078: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
079: SOAPMessage msg = mFactory.createMessage();
080: SOAPPart soapPart = msg.getSOAPPart();
081: soapPart.setContent(new StreamSource(new FileInputStream(
082: "src/test/soap12/data/fault.xml")));
083: SOAPBody body = msg.getSOAPBody();
084: assertTrue(body.hasFault());
085: SOAPFault fault = body.getFault();
086:
087: // Check fault code
088: QName faultcode = fault.getFaultCodeAsQName();
089: assertEquals(faultcode, SOAPConstants.SOAP_SENDER_FAULT);
090:
091: // Check sub-code
092: Iterator subcodes = fault.getFaultSubcodes();
093: assertTrue("There's a subcode", subcodes.hasNext());
094: assertEquals((QName) subcodes.next(), new QName(
095: "http://www.w3.org/2003/05/soap-rpc", "BadArguments"));
096: assertFalse("There's exactly one subcode", subcodes.hasNext());
097:
098: try {
099: subcodes.remove();
100: fail();
101: } catch (Exception e) {
102: //System.out.println("Subcodes.remove() " + e.getMessage());
103: }
104: // Check for Reason Texts
105: String englishText = fault.getFaultReasonText(Locale.US);
106: assertNotNull(englishText);
107: assertEquals("The reason text is as expected", englishText,
108: "Processing error");
109: Iterator locales = fault.getFaultReasonLocales();
110: Iterator texts = fault.getFaultReasonTexts();
111: assertTrue(locales.hasNext() && texts.hasNext());
112: assertEquals(fault.getFaultReasonText((Locale) locales.next()),
113: texts.next());
114: assertTrue(locales.hasNext() && texts.hasNext());
115: assertEquals(fault.getFaultReasonText((Locale) locales.next()),
116: texts.next());
117: assertFalse(locales.hasNext() || texts.hasNext());
118:
119: // Check for Detail
120: assertTrue("Detail is present", fault.hasDetail());
121: Detail detail = fault.getDetail();
122: Iterator detailEntries = detail.getDetailEntries();
123: assertTrue("There's a detail entry", detailEntries.hasNext());
124: assertEquals(((DetailEntry) detailEntries.next())
125: .getElementQName(), new QName(
126: "http://travelcompany.example.org/faults",
127: "myFaultDetails"));
128:
129: assertNull(fault.getFaultRole());
130: assertNull(fault.getFaultNode());
131: }
132:
133: public void testSetFaultReasonText() throws Exception {
134:
135: MessageFactory mFactory = MessageFactory
136: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
137: SOAPMessage msg = mFactory.createMessage();
138: SOAPBody body = msg.getSOAPBody();
139: SOAPFault fault = body.addFault();
140: fault.setFaultCode(SOAPConstants.SOAP_RECEIVER_FAULT);
141: fault.addFaultReasonText("Some Text", Locale.US);
142:
143: Iterator faultElements = fault.getChildElements();
144:
145: SOAPFaultElement code = (SOAPFaultElement) faultElements.next();
146: assertEquals("Code has the right name", code.getElementQName(),
147: new QName(SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE,
148: "Code"));
149:
150: SOAPFaultElement reason = (SOAPFaultElement) faultElements
151: .next();
152: assertEquals("Reason has the right name", reason
153: .getElementQName(), new QName(
154: SOAPConstants.URI_NS_SOAP_1_2_ENVELOPE, "Reason"));
155:
156: assertFalse("There should be only two fault elements",
157: faultElements.hasNext());
158: }
159:
160: public void testAppendFaultSubcode() throws Exception {
161: SOAPMessage msg = MessageFactory.newInstance(
162: SOAPConstants.SOAP_1_2_PROTOCOL).createMessage();
163: SOAPBody body = msg.getSOAPBody();
164: SOAPFault fault = body.addFault();
165: fault
166: .appendFaultSubcode(new QName("another-uri", "name2",
167: "q"));
168: fault.appendFaultSubcode(new QName("yet-another-uri", "name3",
169: "r"));
170:
171: Iterator faultElements = fault.getChildElements();
172: SOAPFaultElement code = (SOAPFaultElement) faultElements.next();
173:
174: Iterator eachCodeChild = code.getChildElements();
175: eachCodeChild.next();
176: SOAPElement subcode = (SOAPElement) eachCodeChild.next();
177:
178: Iterator eachSubcodeChild = subcode.getChildElements();
179: SOAPElement subcodeValue = (SOAPElement) eachSubcodeChild
180: .next();
181: assertEquals(subcodeValue.getValue(), "q:name2");
182: SOAPElement subSubcode = (SOAPElement) eachSubcodeChild.next();
183:
184: Iterator eachSubSubcodeChild = subSubcode.getChildElements();
185: SOAPElement subSubcodeValue = (SOAPElement) eachSubSubcodeChild
186: .next();
187: assertEquals(subSubcodeValue.getValue(), "r:name3");
188: assertEquals(subSubcodeValue.createQName("name3", "r"),
189: new QName("yet-another-uri", "name3"));
190: }
191:
192: public void testCreateDetail() throws Exception {
193: MessageFactory factory = MessageFactory
194: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
195: SOAPFactory sFactory = SOAPFactory
196: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
197: SOAPMessage msg = factory.createMessage();
198: SOAPBody body = msg.getSOAPBody();
199: SOAPFault sf = body.addFault();
200: Detail detail = sFactory.createDetail();
201: detail.addDetailEntry(sFactory.createName("de2", "dePrefix",
202: "deUri"));
203: assertTrue(sf.addChildElement(detail) instanceof Detail);
204: }
205:
206: public void testBug6235490() throws Exception {
207: MessageFactory factory = MessageFactory
208: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
209: SOAPMessage msg = factory.createMessage();
210: SOAPBody body = msg.getSOAPBody();
211: SOAPFault sf = body.addFault();
212: sf.addFaultReasonText("String for Locale UK", Locale.UK);
213: if (!Locale.getDefault().equals(Locale.UK)) {
214: assertNull(sf.getFaultReasonText(Locale.getDefault()));
215: }
216: }
217:
218: public void testAppendFaultSubcodeWithPrefixEmpty()
219: throws Exception {
220: MessageFactory factory = MessageFactory
221: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
222: SOAPMessage message = factory.createMessage();
223: SOAPBody body = message.getSOAPBody();
224: SOAPFault fault = body.addFault(
225: SOAPConstants.SOAP_SENDER_FAULT, "Some string");
226: fault.appendFaultSubcode(new QName("another-uri", "subcode"));
227: }
228:
229: public void testOrderOfFaultElements() throws Exception {
230: MessageFactory factory = MessageFactory
231: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
232: SOAPMessage msg = factory.createMessage();
233: SOAPBody body = msg.getSOAPBody();
234: SOAPFault sf = body.addFault();
235: sf.addDetail();
236: sf.setFaultRole("some-role");
237: sf.setFaultNode("some-node");
238: Iterator it = sf.getChildElements();
239: assertTrue(((SOAPFaultElement) it.next()).getLocalName()
240: .equals("Code"));
241: assertTrue(((SOAPFaultElement) it.next()).getLocalName()
242: .equals("Reason"));
243: assertTrue(((SOAPFaultElement) it.next()).getLocalName()
244: .equals("Node"));
245: assertTrue(((SOAPFaultElement) it.next()).getLocalName()
246: .equals("Role"));
247: assertTrue(it.next() instanceof Detail);
248: assertFalse(it.hasNext());
249: }
250:
251: public void testFaultOnlyChildElementofBody() throws Exception {
252: MessageFactory factory = MessageFactory
253: .newInstance(SOAPConstants.SOAP_1_2_PROTOCOL);
254: SOAPMessage msg = factory.createMessage();
255: SOAPBody body = msg.getSOAPBody();
256: body.addChildElement("foo");
257: try {
258: body.addFault();
259: } catch (SOAPException se) {
260: return;
261: }
262: fail("Fault shouldn't have got added when there was already a child element of soap body");
263: }
264: }
|