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.io.ByteArrayInputStream;
059:
060: import javax.xml.parsers.DocumentBuilderFactory;
061: import javax.xml.soap.*;
062: import javax.xml.transform.*;
063: import javax.xml.transform.dom.DOMSource;
064: import javax.xml.transform.stream.StreamResult;
065: import javax.xml.transform.stream.StreamSource;
066:
067: import junit.framework.TestCase;
068:
069: import org.w3c.dom.*;
070: import org.w3c.dom.Node;
071: import org.xml.sax.InputSource;
072:
073: /*
074: * Tests to check for namespace rules being followed in SOAP message creation.
075: */
076:
077: public class NSDeclTest extends TestCase {
078:
079: public static final String NamespaceSpecNS = "http://www.w3.org/2000/xmlns/";
080:
081: public NSDeclTest(String name) {
082: super (name);
083: }
084:
085: public void testAttributes() throws Exception {
086:
087: String testDoc = "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'\n"
088: + " xmlns:xsd='http://www.w3.org/2001/XMLSchema'\n"
089: + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n"
090: + " xmlns:enc='http://schemas.xmlsoap.org/soap/encoding/'\n"
091: + " xmlns:ns0='http://example.com/wsdl'>\n"
092: + " <env:Body>\n"
093: + " <ns0:sayHello>\n"
094: + " <String_1>to Duke!</String_1>\n"
095: + " </ns0:sayHello>\n"
096: + " </env:Body>\n"
097: + "</env:Envelope>\n";
098:
099: byte[] testDocBytes = testDoc.getBytes("UTF-8");
100: ByteArrayInputStream bais = new ByteArrayInputStream(
101: testDocBytes);
102: StreamSource strSource = new StreamSource(bais);
103:
104: MessageFactory mf = MessageFactory.newInstance();
105: SOAPMessage sm = mf.createMessage();
106: SOAPPart sp = sm.getSOAPPart();
107: sp.setContent(strSource);
108: Document doc = sp;
109:
110: // Workaround for SAAJ bug 4871599
111: //sp.getEnvelope();
112:
113: // Uncomment the following to enable viewing DOM Node.
114: // dumpDomNode(doc);
115: // This test fails
116: doTest(doc);
117:
118: // The following code which does not use SAAJ works correctly...
119: byte[] testDocBytes2 = testDoc.getBytes("UTF-8");
120: ByteArrayInputStream bais2 = new ByteArrayInputStream(
121: testDocBytes2);
122: InputSource is = new InputSource(bais2);
123: DocumentBuilderFactory dbf = new com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl();
124: dbf.setNamespaceAware(true);
125: Document doc2 = dbf.newDocumentBuilder().parse(is);
126: // Uncomment to enable dumping to see the DOM Node
127: // dumpDomNode(doc2);
128: doTest(doc2);
129: }
130:
131: private static boolean doTest(Document doc) {
132: Element documentElement = doc.getDocumentElement();
133:
134: // Set dumpAttrs to true to print out root element attributes
135: boolean dumpAttrs = false;
136: if (dumpAttrs) {
137: NamedNodeMap nnm = documentElement.getAttributes();
138: for (int i = 0; i < nnm.getLength(); i++) {
139: Node node = nnm.item(i);
140: System.err.println("type=" + node.getNodeType()
141: + ", name=" + node.getNodeName()
142: + ", namespaceUri=" + node.getNamespaceURI()
143: + ", localName=" + node.getLocalName());
144: }
145: }
146:
147: // DOM Level 2 Core says that this should return the "xmlns:env"
148: // namespace declaration but it does not. See first "Note" under
149: // http://www.w3.org/TR/DOM-Level-2-Core/core.html#Namespaces-Considerations
150: Attr xmlnsAttr = documentElement.getAttributeNodeNS(
151: NamespaceSpecNS, "env");
152:
153: if (xmlnsAttr == null) {
154: fail("Error: a DOM Attr should have been returned");
155: return false;
156: } else {
157: //System.err.println("Success: a DOM Attr was returned");
158: }
159: return true;
160: }
161:
162: static void dumpDomNode(Node node) throws TransformerException {
163: System.err
164: .println("==== DebugUtil.dumpDomNode(...) Start ====");
165: DOMSource domSource = new DOMSource(node);
166: TransformerFactory tf = new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl();
167: Transformer xform = null;
168: xform = tf.newTransformer();
169: xform.transform(domSource, new StreamResult(System.err));
170: System.err.println();
171: System.err.println("==== DebugUtil.dumpDomNode(...) End ====");
172: }
173:
174: // test for bug id 4871599
175: public void testGetDocElement() throws Exception {
176:
177: String testDoc = "<env:Envelope xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'\n"
178: + " xmlns:xsd='http://www.w3.org/2001/XMLSchema'\n"
179: + " xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'\n"
180: + " xmlns:enc='http://schemas.xmlsoap.org/soap/encoding/'\n"
181: + " xmlns:ns0='http://example.com/wsdl'>\n"
182: + " <env:Body>\n"
183: + " <ns0:sayHello>\n"
184: + " <String_1>to Duke!</String_1>\n"
185: + " </ns0:sayHello>\n"
186: + " </env:Body>\n"
187: + "</env:Envelope>\n";
188:
189: byte[] testDocBytes = testDoc.getBytes("UTF-8");
190: ByteArrayInputStream bais = new ByteArrayInputStream(
191: testDocBytes);
192: StreamSource strSource = new StreamSource(bais);
193:
194: MessageFactory mf = MessageFactory.newInstance();
195: SOAPMessage sm = mf.createMessage();
196: SOAPPart sp = sm.getSOAPPart();
197: sp.setContent(strSource);
198:
199: // Uncomment following line for a workaround
200: //sp.getEnvelope();
201: Element docElement = sp.getDocumentElement();
202: if (docElement == null)
203: fail("The following value should not be null: "
204: + docElement);
205: }
206:
207: public static void main(String argv[]) {
208:
209: junit.textui.TestRunner.run(NSDeclTest.class);
210:
211: }
212:
213: }
|