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 namespace;
057:
058: import java.util.Iterator;
059:
060: import javax.xml.soap.*;
061: import javax.xml.transform.stream.StreamSource;
062:
063: import junit.framework.TestCase;
064:
065: import org.w3c.dom.Document;
066:
067: import com.sun.xml.messaging.saaj.soap.name.NameImpl;
068:
069: import java.io.*;
070:
071: /*
072: * Tests to check for namespace rules being followed in SOAP message creation.
073: */
074:
075: public class NamespaceTest extends TestCase {
076:
077: private SOAPEnvelope envelope;
078: private SOAPFactory sFactory;
079: private MessageFactory msgFactory;
080:
081: public NamespaceTest(String name) {
082: super (name);
083: }
084:
085: protected void setUp() {
086:
087: try {
088: msgFactory = MessageFactory.newInstance();
089: sFactory = SOAPFactory.newInstance();
090: SOAPMessage msg = msgFactory.createMessage();
091: envelope = msg.getSOAPPart().getEnvelope();
092: } catch (Exception ex) {
093: ex.printStackTrace();
094: }
095: }
096:
097: private SOAPMessage createMessage(Name name, String attrib)
098: throws SOAPException {
099: /*
100: * Creating a message.
101: */
102: SOAPMessage msg = msgFactory.createMessage();
103:
104: envelope = msg.getSOAPPart().getEnvelope();
105:
106: SOAPHeader hdr = envelope.getHeader();
107: SOAPBody bdy = envelope.getBody();
108:
109: SOAPElement elem = sFactory.createElement(name);
110:
111: if (attrib != null)
112: elem.addAttribute(name, attrib);
113:
114: SOAPHeaderElement she = hdr.addHeaderElement(envelope
115: .createName("HeaderElement1", "he1",
116: "http://foo.xyz.com"));
117: she.addChildElement(elem);
118:
119: return msg;
120: }
121:
122: /*
123: * xmlns prefix is reserved and cannot be declared
124: */
125: public void testXmlnsAsPrefix() {
126:
127: String exception = null;
128: try {
129:
130: Name name1 = envelope.createName("foo", "xmlns",
131: "http://www.w3.org/2000/xmlns/");
132:
133: SOAPMessage msg = createMessage(name1, "myNameSpaceURI");
134:
135: //msg.writeTo(System.out);
136:
137: } catch (Exception e) {
138: exception = e.getMessage();
139: }
140:
141: // exception should be thrown if someone tried to declare a namespace prefix with the
142: // reserved 'xmlns' word.
143: assertTrue("An exception should have been thrown",
144: (exception != null));
145: }
146:
147: /*
148: * Qualified name cannot be xmlns.
149: */
150: public void testQNameAsPrefix() {
151:
152: String exception = null;
153: try {
154:
155: Name name2 = envelope.createName("xmlns", null,
156: "http://www.w3.org/2000/xmlns/");
157:
158: SOAPMessage msg = createMessage(name2, "");
159:
160: //msg.writeTo(System.out);
161:
162: } catch (Exception e) {
163: exception = e.getMessage();
164: }
165:
166: assertTrue("An exception should have been thrown",
167: (exception != null));
168: }
169:
170: /*
171: * URI cannot be null.
172: */
173: public void testNullUriInName() {
174:
175: String exception = null;
176: try {
177:
178: Name name3 = envelope.createName("test", "prefix", null);
179:
180: SOAPMessage msg = createMessage(name3, null);
181:
182: //msg.writeTo(System.out);
183:
184: } catch (Exception e) {
185: exception = e.getMessage();
186: }
187:
188: assertTrue("An exception should have been thrown",
189: (exception != null));
190: }
191:
192: /*
193: * Test to verify SOAPFactory.createElement(localname);
194: */
195: public void testCreateElementString() {
196:
197: try {
198: SOAPMessage msg = msgFactory.createMessage();
199: envelope = msg.getSOAPPart().getEnvelope();
200:
201: SOAPHeader hdr = envelope.getHeader();
202: SOAPBody bdy = envelope.getBody();
203:
204: SOAPElement elem = sFactory.createElement("localname");
205: bdy.addChildElement(elem);
206:
207: } catch (Exception e) {
208: // e.printStackTrace();
209: fail("No exception should be thrown" + e.getMessage());
210: }
211: }
212:
213: public void testGetNamespacePrefixes() throws Exception {
214: SOAPMessage message = msgFactory.createMessage();
215: envelope = message.getSOAPPart().getEnvelope();
216: SOAPBody body = envelope.getBody();
217: body.addNamespaceDeclaration("prefix", "http://aUri");
218:
219: Iterator eachPrefix = body.getNamespacePrefixes();
220:
221: String prefix;
222:
223: assertTrue(eachPrefix.hasNext());
224: prefix = (String) eachPrefix.next();
225: assertTrue("wrong first prefix: \"" + prefix + "\"", "prefix"
226: .equalsIgnoreCase(prefix));
227: // assertTrue(eachPrefix.hasNext());
228: // prefix = (String) eachPrefix.next();
229: // assertTrue("wrong second prefix: \""+ prefix+"\"", "SOAP-ENV".equalsIgnoreCase(prefix));
230:
231: if (eachPrefix.hasNext()) {
232: String errorString = "";
233: int count = 0;
234: while (eachPrefix.hasNext() && count < 10) {
235: prefix = (String) eachPrefix.next();
236: errorString = errorString + "Unexpected prefix: "
237: + prefix + "\n";
238: }
239: if (count == 10) {
240: errorString = errorString + "more...";
241: }
242:
243: fail(errorString);
244: }
245:
246: eachPrefix = body.getNamespacePrefixes();
247: eachPrefix.next();
248: eachPrefix.remove();
249:
250: // eachPrefix = body.getNamespacePrefixes();
251: // assertTrue(eachPrefix.hasNext());
252: // prefix = (String) eachPrefix.next();
253: // assertTrue("wrong prefix found after removal: \""+ prefix+"\"", "SOAP-ENV".equalsIgnoreCase(prefix));
254: assertTrue(!eachPrefix.hasNext());
255: }
256:
257: public void testBodyPrefix() throws Exception {
258: // Create Envelope element
259: SOAPElement env = SOAPFactory.newInstance().createElement(
260: "Envelope", "env",
261: "http://schemas.xmlsoap.org/soap/envelope/");
262: env.addNamespaceDeclaration("xsd",
263: "http://www.w3.org/2001/XMLSchema");
264: env.addNamespaceDeclaration("xsi",
265: "http://www.w3.org/2001/XMLSchema-instance");
266: env.addNamespaceDeclaration("enc",
267: "http://schemas.xmlsoap.org/soap/encoding/");
268:
269: // Insert Envelope element
270: SOAPMessage soapMsg = msgFactory.createMessage();
271: Document dom = soapMsg.getSOAPPart();
272: SOAPElement elem = (SOAPElement) dom.importNode(env, true);
273: dom.insertBefore(elem, null);
274:
275: //Insert Body element
276: elem.addChildElement("Body", "env",
277: "http://schemas.xmlsoap.org/soap/envelope/");
278: soapMsg.saveChanges();
279:
280: // Is namespace of Body "env" ?
281: SOAPBody body = soapMsg.getSOAPBody();
282: assertTrue(body.getPrefix().equals("env"));
283: }
284:
285: public void testAttrPrefix() throws Exception {
286: String msgStr = "<env:Envelope "
287: + "xmlns:enc=\"http://schemas.xmlsoap.org/soap/encoding/\" "
288: + "xmlns:env=\"http://schemas.xmlsoap.org/soap/envelope/\" "
289: + "xmlns:ns0=\"http://echoservice.org/types4\" "
290: + "xmlns:ns1=\"http://java.sun.com/jax-rpc-ri/internal\" "
291: + "xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" "
292: + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">"
293: + "<SOAP-ENV:Body "
294: + "xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">"
295: + "<ans1:echoMapEntryResponse "
296: + "env:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" "
297: + "xmlns:ans1=\"http://echoservice.org/wsdl\">"
298: + "<result xsi:nil=\"1\" xsi:type=\"ns1:mapEntry\"/>"
299: + "</ans1:echoMapEntryResponse>" + "</SOAP-ENV:Body>"
300: + "</env:Envelope>";
301:
302: SOAPMessage soapMsg = msgFactory.createMessage();
303: soapMsg.getSOAPPart().setContent(
304: new StreamSource(new java.io.StringReader(msgStr)));
305: soapMsg.saveChanges();
306: SOAPBody body = soapMsg.getSOAPBody();
307: Iterator i = body.getChildElements();
308: SOAPElement elem = null;
309: if (i.hasNext()) {
310: elem = (SOAPElement) i.next(); // elem=ans1:echoMapEntryResponse
311: }
312: if (elem != null) {
313: i = elem.getChildElements();
314: elem = null;
315: if (i.hasNext()) {
316: elem = (SOAPElement) i.next(); // elem=result
317: String got = elem.getNamespaceURI("ns1");
318: String expected = "http://java.sun.com/jax-rpc-ri/internal";
319: assertTrue(got.equals(expected));
320: }
321: }
322: }
323:
324: public void testNamespaceDeclarationAsAttribute() throws Exception {
325: SOAPElement element = SOAPFactory.newInstance().createElement(
326: "Envelope", "env",
327: "http://schemas.xmlsoap.org/soap/envelope/");
328: element.addAttribute(NameImpl
329: .createFromTagName("xmlns:fooName"), "http://foo");
330:
331: Iterator eachDeclaration = element.getNamespacePrefixes();
332: assertTrue(eachDeclaration.hasNext());
333: assertEquals("fooName", (String) eachDeclaration.next());
334: if (eachDeclaration.hasNext()) {
335: String extraPrefix = (String) eachDeclaration.next();
336: fail("An extra namespace declaration was added for: "
337: + extraPrefix);
338: }
339: }
340:
341: public void testLookupNamespaceURIDOML3() throws Exception {
342: String msg = "<?xml version='1.0' ?><S:Envelope xmlns:S='http://schemas.xmlsoap.org/soap/envelope/'><S:Body><ns2:Fault xmlns:ns2='http://schemas.xmlsoap.org/soap/envelope/' xmlns:ns3='http://www.w3.org/2003/05/soap-envelope'><faultcode>ns3:VersionMismatch</faultcode><faultstring>Couldn't create SOAP message. Expecting Envelope in namespace http://schemas.xmlsoap.org/soap/envelope/, but got http://wrongname.org </faultstring></ns2:Fault></S:Body></S:Envelope>";
343:
344: MessageFactory messageFactory = MessageFactory.newInstance();
345: MimeHeaders headers = new MimeHeaders();
346: headers.addHeader("Content-Type", "text/xml");
347:
348: SOAPMessage soapMsg = messageFactory.createMessage(headers,
349: new ByteArrayInputStream(msg.getBytes()));
350: soapMsg.writeTo(System.out);
351: SOAPBody body = soapMsg.getSOAPPart().getEnvelope().getBody();
352:
353: SOAPFault fault = (SOAPFault) body.getFault();
354: String uri = fault.lookupNamespaceURI("ns3");
355: assertTrue(uri
356: .equals("http://www.w3.org/2003/05/soap-envelope"));
357:
358: }
359:
360: public static void main(String argv[]) {
361:
362: junit.textui.TestRunner.run(NamespaceTest.class);
363:
364: }
365:
366: }
|