001: /*
002: * The contents of this file are subject to the terms
003: * of the Common Development and Distribution License
004: * (the "License"). You may not use this file except
005: * in compliance with the License.
006: *
007: * You can obtain a copy of the license at
008: * https://jwsdp.dev.java.net/CDDLv1.0.html
009: * See the License for the specific language governing
010: * permissions and limitations under the License.
011: *
012: * When distributing Covered Code, include this CDDL
013: * HEADER in each file and include the License file at
014: * https://jwsdp.dev.java.net/CDDLv1.0.html If applicable,
015: * add the following below this CDDL HEADER, with the
016: * fields enclosed by brackets "[]" replaced with your
017: * own identifying information: Portions Copyright [yyyy]
018: * [name of copyright owner]
019: */
020: /*
021: * $Id: FaultTest.java,v 1.2 2007/07/16 16:42:00 ofung Exp $
022: */
023:
024: /*
025: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
026: *
027: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
028: *
029: * The contents of this file are subject to the terms of either the GNU
030: * General Public License Version 2 only ("GPL") or the Common Development
031: * and Distribution License("CDDL") (collectively, the "License"). You
032: * may not use this file except in compliance with the License. You can obtain
033: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
034: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
035: * language governing permissions and limitations under the License.
036: *
037: * When distributing the software, include this License Header Notice in each
038: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
039: * Sun designates this particular file as subject to the "Classpath" exception
040: * as provided by Sun in the GPL Version 2 section of the License file that
041: * accompanied this code. If applicable, add the following below the License
042: * Header, with the fields enclosed by brackets [] replaced by your own
043: * identifying information: "Portions Copyrighted [year]
044: * [name of copyright owner]"
045: *
046: * Contributor(s):
047: *
048: * If you wish your version of this file to be governed by only the CDDL or
049: * only the GPL Version 2, indicate your decision by adding "[Contributor]
050: * elects to include this software in this distribution under the [CDDL or GPL
051: * Version 2] license." If you don't indicate a single choice of license, a
052: * recipient has the option to distribute your version of this file under
053: * either the CDDL, the GPL Version 2 or to extend the choice of license to
054: * its licensees as provided above. However, if you add GPL Version 2 code
055: * and therefore, elected the GPL Version 2 license, then the option applies
056: * only if the new code is made subject to such option by the copyright
057: * holder.
058: */
059:
060: /**
061: *
062: * @author SAAJ RI Development Team
063: */package soap;
064:
065: import java.io.ByteArrayInputStream;
066: import java.util.Iterator;
067:
068: import javax.xml.soap.*;
069: import javax.xml.transform.stream.*;
070: import javax.xml.namespace.QName;
071:
072: import junit.framework.TestCase;
073:
074: public class FaultTest extends TestCase {
075:
076: public FaultTest(String name) {
077: super (name);
078: }
079:
080: public void testGetDetail() throws Exception {
081: MessageFactory factory = MessageFactory.newInstance();
082: SOAPMessage message = factory.createMessage();
083: SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
084: SOAPBody body = envelope.getBody();
085: SOAPFault fault = body.addFault();
086: Detail detail = fault.addDetail();
087: String detailEntryLocalName = "name";
088: Name detailEntryName = envelope.createName(
089: detailEntryLocalName, "prefix", "uri");
090: detail.addDetailEntry(detailEntryName);
091:
092: SOAPFault extractedFault = body.getFault();
093: assertTrue(extractedFault != null);
094: Detail extractedDetail = extractedFault.getDetail();
095: assertTrue(extractedDetail != null);
096: Iterator eachDetailEntry = extractedDetail.getDetailEntries();
097: assertTrue(eachDetailEntry.hasNext());
098: DetailEntry extractedEntry = (DetailEntry) eachDetailEntry
099: .next();
100: assertEquals(detailEntryLocalName, extractedEntry
101: .getLocalName());
102: assertFalse(eachDetailEntry.hasNext());
103:
104: extractedFault.setFaultActor(SOAPConstants.URI_SOAP_ACTOR_NEXT);
105: Detail d = extractedFault.getDetail();
106: d.detachNode();
107: d = extractedFault.getDetail();
108: assertTrue(d == null);
109: }
110:
111: public void testFaultWithAmp() throws Exception {
112: String testDoc = "<?xml version='1.0' encoding='UTF-8'?>"
113: + "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\">"
114: + "<soap:Body>" + "<soap:Fault>" + "<faultcode>"
115: + "code>code" + "</faultcode>" + "<faultstring>"
116: + "string>string" + "</faultstring>"
117: + "<faultactor>" + "actor>actor" + "</faultactor>"
118: + "<detail />" + "</soap:Fault>" + "</soap:Body>"
119: + "</soap:Envelope>";
120:
121: byte[] testDocBytes = testDoc.getBytes("UTF-8");
122: ByteArrayInputStream bais = new ByteArrayInputStream(
123: testDocBytes);
124: StreamSource strSource = new StreamSource(bais);
125:
126: MessageFactory msgFactory = MessageFactory.newInstance();
127: SOAPMessage message = msgFactory.createMessage();
128: SOAPPart soapPart = message.getSOAPPart();
129:
130: soapPart.setContent(strSource);
131: message.saveChanges();
132: SOAPEnvelope envelope = soapPart.getEnvelope();
133: SOAPBody body = envelope.getBody();
134: SOAPFault fault = body.getFault();
135: assertTrue(fault.getFaultCode().equals("code>code"));
136: assertTrue(fault.getFaultString().equals("string>string"));
137: assertTrue(fault.getFaultActor().equals("actor>actor"));
138: }
139:
140: /**
141: * Test case for CR ID 6212709
142: */
143: public void testSetFaultCodeWithNameArg() throws Exception {
144: MessageFactory factory = MessageFactory.newInstance();
145: SOAPMessage message = factory.createMessage();
146: SOAPBody body = message.getSOAPBody();
147: SOAPFault fault = body.addFault();
148: Name faultCodeName = SOAPFactory.newInstance().createName(
149: "Client", "env", null);
150: try {
151: fault.setFaultCode(faultCodeName);
152: } catch (SOAPException se) {
153: return;
154: }
155: fail("Invalid fault code allowed to be set");
156: }
157:
158: public void testSetFaultCodeWithPrefixEmpty() throws Exception {
159: MessageFactory factory = MessageFactory.newInstance();
160: SOAPMessage message = factory.createMessage();
161: SOAPBody body = message.getSOAPBody();
162: body.addFault(new QName("some-uri", "code"), "Some string");
163: }
164:
165: public void testSetFaultStringAndLocale() throws Exception {
166: MessageFactory factory = MessageFactory.newInstance();
167: SOAPMessage message = factory.createMessage();
168: SOAPBody body = message.getSOAPBody();
169: body.addFault(new QName("some-uri", "code"), "Some string");
170: SOAPFault fault = body.getFault();
171: fault
172: .setFaultString("EN faultString",
173: java.util.Locale.ENGLISH);
174: //System.out.println(fault.getFaultString() + " : " + fault.getFaultStringLocale());
175: fault.setFaultString("No Locale faultString");
176: //System.out.println(fault.getFaultString() + " : " + fault.getFaultStringLocale());
177: assertTrue(fault.getFaultStringLocale() == null);
178: }
179: }
|