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: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
022: *
023: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
024: *
025: * The contents of this file are subject to the terms of either the GNU
026: * General Public License Version 2 only ("GPL") or the Common Development
027: * and Distribution License("CDDL") (collectively, the "License"). You
028: * may not use this file except in compliance with the License. You can obtain
029: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
030: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
031: * language governing permissions and limitations under the License.
032: *
033: * When distributing the software, include this License Header Notice in each
034: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
035: * Sun designates this particular file as subject to the "Classpath" exception
036: * as provided by Sun in the GPL Version 2 section of the License file that
037: * accompanied this code. If applicable, add the following below the License
038: * Header, with the fields enclosed by brackets [] replaced by your own
039: * identifying information: "Portions Copyrighted [year]
040: * [name of copyright owner]"
041: *
042: * Contributor(s):
043: *
044: * If you wish your version of this file to be governed by only the CDDL or
045: * only the GPL Version 2, indicate your decision by adding "[Contributor]
046: * elects to include this software in this distribution under the [CDDL or GPL
047: * Version 2] license." If you don't indicate a single choice of license, a
048: * recipient has the option to distribute your version of this file under
049: * either the CDDL, the GPL Version 2 or to extend the choice of license to
050: * its licensees as provided above. However, if you add GPL Version 2 code
051: * and therefore, elected the GPL Version 2 license, then the option applies
052: * only if the new code is made subject to such option by the copyright
053: * holder.
054: */
055:
056: package soap;
057:
058: import java.io.ByteArrayInputStream;
059: import java.util.Iterator;
060:
061: import javax.xml.namespace.QName;
062: import javax.xml.soap.*;
063: import javax.xml.transform.stream.StreamSource;
064:
065: import junit.framework.TestCase;
066:
067: public class SetElementNameTest extends TestCase {
068:
069: private SOAPMessage sm1;
070: private SOAPMessage sm2;
071: private SOAPEnvelope envelopeOne;
072: private SOAPEnvelope envelopeTwo;
073:
074: public SetElementNameTest(String name) {
075: super (name);
076: }
077:
078: public void setUp() throws Exception {
079: createMessageOne();
080: createMessageTwo();
081: }
082:
083: private void createMessageOne() throws Exception {
084: String testDoc = "<env:Envelope"
085: + " xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'>"
086: //+ " xmlns:ns1='http://example.com/wsdl'>"
087: + "<env:Header/>" + "<env:Body>"
088: + "<Hello xmlns='http://example.com/wsdl'>"
089: + "<String_1>Duke!</String_1>"
090: + "<String_2>Hi!</String_2>" + "</Hello>"
091: + "</env:Body>" + "</env:Envelope>";
092:
093: byte[] testDocBytes = testDoc.getBytes("UTF-8");
094: ByteArrayInputStream bais = new ByteArrayInputStream(
095: testDocBytes);
096: StreamSource strSource = new StreamSource(bais);
097: MessageFactory mf = MessageFactory.newInstance();
098: sm1 = mf.createMessage();
099: SOAPPart sp = sm1.getSOAPPart();
100: sp.setContent(strSource);
101: envelopeOne = sp.getEnvelope();
102: }
103:
104: private void createMessageTwo() throws Exception {
105: String testDoc = "<env:Envelope"
106: + " xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'"
107: + " xmlns:ns1='http://example.com/wsdl'>"
108: + "<env:Body>" + "<env:Fault>"
109: + "<faultcode>SOAP-ENV:Server</faultcode>"
110: + "<faultstring>Server Error</faultstring>"
111: + "<detail>"
112: + "<e:myfaultdetails xmlns:e=\"Some-URI\">"
113: + "<e:message>" + "My application didn't work"
114: + "</e:message>" + "<errorcode>" + "1001"
115: + "</errorcode>" + "</e:myfaultdetails>" + "</detail>"
116: + "</env:Fault>" + "</env:Body>" + "</env:Envelope>";
117:
118: byte[] testDocBytes = testDoc.getBytes("UTF-8");
119: ByteArrayInputStream bais = new ByteArrayInputStream(
120: testDocBytes);
121: StreamSource strSource = new StreamSource(bais);
122: MessageFactory mf = MessageFactory.newInstance();
123: sm2 = mf.createMessage();
124: SOAPPart sp = sm2.getSOAPPart();
125: sp.setContent(strSource);
126: envelopeTwo = sp.getEnvelope();
127: }
128:
129: public void testSetElementName() throws Exception {
130: SOAPBody body = envelopeOne.getBody();
131: SOAPHeader header = envelopeOne.getHeader();
132: QName newName = new QName("", "newName", "");
133: String exception = null;
134: try {
135: envelopeOne.setElementQName(newName);
136: } catch (SOAPException e) {
137: exception = e.getMessage();
138: }
139: assertNotNull("Cannot set a new name for envelope", exception);
140: exception = null;
141: try {
142: header.setElementQName(newName);
143: } catch (SOAPException e) {
144: exception = e.getMessage();
145: }
146: assertNotNull("Cannot set a new name for header", exception);
147: exception = null;
148: try {
149: body.setElementQName(newName);
150: } catch (SOAPException e) {
151: exception = e.getMessage();
152: }
153: assertNotNull("Cannot set a new name for body", exception);
154: Iterator soapBodyElements = body.getChildElements();
155: assertTrue("First element is there", soapBodyElements.hasNext());
156: SOAPBodyElement soapBodyElement = (SOAPBodyElement) soapBodyElements
157: .next();
158: assertFalse("First element is the only child element",
159: soapBodyElements.hasNext());
160: exception = null;
161: try {
162: soapBodyElement.setElementQName(newName);
163: } catch (SOAPException e) {
164: exception = e.getMessage();
165: }
166: /*} catch(SOAPException e) {
167: fail("Changing name of a bodyElement is allowed.");
168: }*/
169: //assertNotNull("Cannot set a new name for body", exception);
170: //soapBodyElement.addNamespaceDeclaration("", "another-uri!!");
171: //sm1.writeTo(System.out);
172: /*SOAPBodyElement sbe = (SOAPBodyElement) body.getFirstChild();
173: assertTrue("New name has been recorded",
174: sbe.getElementQName().equals(newName));
175: SOAPElement element = (SOAPElement) sbe.getFirstChild();
176: assertTrue("Children are still in place",
177: element.getFirstChild().getNodeValue()
178: .equals("Duke!"));*/
179: }
180:
181: public void testSetElementNameWithFault() throws Exception {
182: SOAPFault fault = envelopeTwo.getBody().getFault();
183: QName newName = new QName("newName");
184: String exception = null;
185: try {
186: fault.setElementQName(newName);
187: } catch (SOAPException e) {
188: exception = e.getMessage();
189: }
190: assertNotNull("Cannot set a new name for fault", exception);
191: Iterator eachChild = fault.getChildElements();
192: SOAPFaultElement faultCode = (SOAPFaultElement) eachChild
193: .next();
194: exception = null;
195: try {
196: faultCode.setElementQName(newName);
197: } catch (SOAPException e) {
198: exception = e.getMessage();
199: }
200: assertNotNull("Cannot set a new name for faultcode", exception);
201: SOAPFaultElement faultString = (SOAPFaultElement) eachChild
202: .next();
203: exception = null;
204: try {
205: faultString.setElementQName(newName);
206: } catch (SOAPException e) {
207: exception = e.getMessage();
208: }
209: assertNotNull("Cannot set a new name for faultstring",
210: exception);
211: Detail detail = (Detail) eachChild.next();
212: exception = null;
213: try {
214: detail.setElementQName(newName);
215: } catch (SOAPException e) {
216: exception = e.getMessage();
217: }
218: assertNotNull("Cannot set a new name for detail", exception);
219: Iterator detailEntries = detail.getDetailEntries();
220: DetailEntry firstEntry = (DetailEntry) detailEntries.next();
221: assertFalse("firstEntry is the lastEntry", detailEntries
222: .hasNext());
223: QName entryNewName = new QName("some-other-uri",
224: "newLocalPart", "newPrefix");
225: exception = null;
226: try {
227: firstEntry.setElementQName(entryNewName);
228: } catch (SOAPException e) {
229: fail("Changing name of DetailEntry is allowed.");
230: }
231: Iterator newDetailEntries = detail.getDetailEntries();
232: DetailEntry newFirstEntry = (DetailEntry) newDetailEntries
233: .next();
234: assertFalse("newFirstEntry is the newLastEntry",
235: newDetailEntries.hasNext());
236: Iterator children = newFirstEntry.getChildElements();
237: SOAPElement element1 = (SOAPElement) children.next();
238: SOAPElement element2 = (SOAPElement) children.next();
239: assertFalse("there are only two child elements", children
240: .hasNext());
241: assertTrue("first element has a particular text value",
242: element1.getFirstChild().getNodeValue().equals(
243: "My application didn't work"));
244: assertTrue("newDetailEntry has a particular URI", newFirstEntry
245: .getElementQName().getNamespaceURI().equals(
246: "some-other-uri"));
247: assertTrue("newDetailEntry has imported old attributes",
248: newFirstEntry.getAttributeValue(
249: new QName("", "e", "xmlns")).equals("Some-URI"));
250: }
251: }
|