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 javax.xml.soap.*;
059: import javax.xml.transform.stream.StreamSource;
060:
061: import java.io.ByteArrayInputStream;
062: import java.io.ByteArrayOutputStream;
063: import java.io.StringReader;
064:
065: import junit.framework.TestCase;
066:
067: /**
068: * These unit tests are based on one of the TCK tests
069: */
070: public class XmlDeclarationUtf16Test extends TestCase {
071:
072: private static final String GoodSoapMessage = "<soap:Envelope xmlns:soap=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:soapenc=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:tns=\"http://helloservice.org/wsdl\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\"><soap:Body soap:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\"><tns:hello><String_1 xsi:type=\"xsd:string\"><Bozo></String_1></tns:hello></soap:Body></soap:Envelope>";
073:
074: public XmlDeclarationUtf16Test(String name) {
075: super (name);
076: }
077:
078: public void test1VerifyXmlDeclarationUtf16() throws Exception {
079: MessageFactory factory = MessageFactory.newInstance();
080: SOAPMessage message = factory.createMessage();
081: SOAPPart sp = message.getSOAPPart();
082: SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
083: SOAPHeader header = envelope.getHeader();
084: ByteArrayInputStream bais = new ByteArrayInputStream(
085: GoodSoapMessage.getBytes());
086: StreamSource ssrc = new StreamSource(bais);
087: sp.setContent(ssrc);
088: message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
089: message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING,
090: "utf-16");
091: ByteArrayOutputStream baos = new ByteArrayOutputStream();
092: message.writeTo(baos);
093: String decoded = baos.toString("utf-16");
094: //String decoded = baos.toString();
095: assertTrue(decoded.indexOf("<?xml") != -1
096: && decoded.indexOf("utf-16") != -1);
097: }
098:
099: public void test2VerifyXmlDeclarationUtf16() throws Exception {
100: MessageFactory factory = MessageFactory.newInstance();
101: SOAPMessage message = factory.createMessage();
102: SOAPPart sp = message.getSOAPPart();
103: SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
104: SOAPHeader header = envelope.getHeader();
105: StringReader reader = new StringReader(GoodSoapMessage);
106: StreamSource ssrc = new StreamSource(reader);
107: sp.setContent(ssrc);
108: message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "true");
109: message.setProperty(SOAPMessage.CHARACTER_SET_ENCODING,
110: "utf-16");
111: ByteArrayOutputStream baos = new ByteArrayOutputStream();
112: message.writeTo(baos);
113: String decoded = baos.toString("utf-16");
114: //String decoded = baos.toString();
115: assertTrue(decoded.indexOf("<?xml") != -1
116: && decoded.indexOf("utf-16") != -1);
117: }
118:
119: public void testVerifyNoXmlDeclarationOutput() throws Exception {
120: MessageFactory factory = MessageFactory.newInstance();
121: SOAPMessage message = factory.createMessage();
122: SOAPPart sp = message.getSOAPPart();
123: SOAPEnvelope envelope = message.getSOAPPart().getEnvelope();
124: SOAPHeader header = envelope.getHeader();
125: ByteArrayInputStream bais = new ByteArrayInputStream(
126: GoodSoapMessage.getBytes());
127: StreamSource ssrc = new StreamSource(bais);
128: sp.setContent(ssrc);
129: message.setProperty(SOAPMessage.WRITE_XML_DECLARATION, "false");
130: ByteArrayOutputStream baos = new ByteArrayOutputStream();
131: message.writeTo(baos);
132: String soapmessage = new String(baos.toByteArray());
133: assertFalse(soapmessage.indexOf("<?xml") != -1);
134: }
135:
136: //CR:4952752
137: public void testVerifyXmlDeclUtf16() throws Exception {
138: MessageFactory factory = MessageFactory.newInstance();
139: String xml = "<?xml version=\"1.0\" encoding=\"UTF-16\"?><SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\"><SOAP-ENV:Header/><SOAP-ENV:Body><p:content SOAP-ENV:encodingStyle=\"http://example.com/encoding\" xmlns:p=\"some-uri\">Jeu universel de caractères codés à plusieurs octets</p:content></SOAP-ENV:Body></SOAP-ENV:Envelope>";
140: SOAPMessage msg = factory.createMessage();
141: msg.getMimeHeaders().setHeader("Content-Type",
142: "text/xml; charset=utf-16");
143: msg.getSOAPPart().setContent(
144: new StreamSource(new ByteArrayInputStream(xml
145: .getBytes("utf-16"))));
146: msg.saveChanges();
147: String[] contentType = msg.getMimeHeaders().getHeader(
148: "Content-Type");
149: if ((contentType != null) && (contentType.length > 0)
150: && (!contentType[0].equals("text/xml; charset=utf-16"))) {
151: fail();
152: }
153: }
154: }
|