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.io.StringWriter;
060:
061: import java.util.Iterator;
062:
063: import javax.xml.soap.*;
064: import javax.xml.transform.stream.StreamSource;
065: import javax.xml.transform.stream.StreamResult;
066: import javax.xml.transform.Transformer;
067: import javax.xml.transform.TransformerFactory;
068: import javax.xml.transform.dom.DOMSource;
069:
070: import junit.framework.TestCase;
071:
072: import org.w3c.dom.*;
073:
074: public class ExtractContentAsDocumentTest extends TestCase {
075:
076: private SOAPMessage sm1;
077: private SOAPEnvelope envelopeOne;
078:
079: public ExtractContentAsDocumentTest(String name) {
080: super (name);
081: }
082:
083: public void setUp() throws Exception {
084: createMessageOne();
085: }
086:
087: private void createMessageOne() throws Exception {
088: String testDoc = "<env:Envelope"
089: + " xmlns:env='http://schemas.xmlsoap.org/soap/envelope/'"
090: + " xmlns:ns1='http://example.com/wsdl'>"
091: + "<env:Header/>" + "<env:Body>" + "<ns1:Hi/>"
092: + "<ns1:Hello>" + "<String_1>Duke!</String_1>"
093: + "<String_2>Hi!</String_2>" + "</ns1:Hello>"
094: + "</env:Body>" + "</env:Envelope>";
095:
096: byte[] testDocBytes = testDoc.getBytes("UTF-8");
097: ByteArrayInputStream bais = new ByteArrayInputStream(
098: testDocBytes);
099: StreamSource strSource = new StreamSource(bais);
100: MessageFactory mf = MessageFactory.newInstance();
101: sm1 = mf.createMessage();
102: SOAPPart sp = sm1.getSOAPPart();
103: sp.setContent(strSource);
104: envelopeOne = sp.getEnvelope();
105: }
106:
107: public void testExtractContentAsDocument() throws Exception {
108: SOAPBody body = envelopeOne.getBody();
109: String exception = null;
110: Document document = null;
111:
112: try {
113: document = body.extractContentAsDocument();
114: } catch (Exception e) {
115: exception = e.getMessage();
116: }
117: assertNotNull("Body has 2 child elements so extract fails.",
118: exception);
119:
120: // Remove one out of the two body elements
121: Iterator eachChild = body.getChildElements();
122: SOAPBodyElement firstElement = (SOAPBodyElement) eachChild
123: .next();
124: firstElement.detachNode();
125: sm1.saveChanges();
126:
127: exception = null;
128: try {
129: document = body.extractContentAsDocument();
130: } catch (Exception e) {
131: fail("Body has exactly one child element.");
132: }
133:
134: Element element = document.getDocumentElement();
135: assertEquals("element has a particular tag name.", element
136: .getTagName(), "ns1:Hello");
137: assertEquals("first child element has a particular tag name",
138: ((Element) element.getFirstChild()).getTagName(),
139: "String_1");
140: //System.out.println(nodeToString(document));
141: //System.out.println(nodeToString(element));
142:
143: try {
144: document = body.extractContentAsDocument();
145: } catch (Exception e) {
146: exception = e.getMessage();
147: }
148: assertNotNull("Body is empty so extract fails.", exception);
149: }
150:
151: public String nodeToString(org.w3c.dom.Node node) throws Exception {
152: // Use a Transformer for output
153: TransformerFactory tFactory = new com.sun.org.apache.xalan.internal.xsltc.trax.TransformerFactoryImpl();
154: Transformer transformer = tFactory.newTransformer();
155: StringWriter stringWriter = new StringWriter();
156:
157: DOMSource source = new DOMSource(node);
158: StreamResult result = new StreamResult(stringWriter);
159:
160: transformer.transform(source, result);
161: return stringWriter.toString();
162: }
163: }
|