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 mime;
057:
058: import javax.xml.soap.*;
059: import javax.xml.transform.stream.StreamSource;
060:
061: import java.io.StringReader;
062: import java.io.ByteArrayOutputStream;
063: import java.io.ByteArrayInputStream;
064:
065: import javax.activation.DataHandler;
066:
067: import junit.framework.TestCase;
068:
069: /**
070: * Tests for Bug ID: 4991879
071: */
072: public class XmlAttachmentTest extends TestCase {
073:
074: public XmlAttachmentTest(String name) {
075: super (name);
076: }
077:
078: public void testXmlAttachmentWithXmlDecl() throws Exception {
079: SOAPMessage msg = MessageFactory.newInstance().createMessage();
080: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <START><A>Hello</A><B>World</B></START>";
081: StringReader reader = new StringReader(xml);
082: StreamSource content = new StreamSource(reader);
083: AttachmentPart attachment = msg.createAttachmentPart(content,
084: "text/xml");
085:
086: DataHandler handler = attachment.getDataHandler();
087: ByteArrayOutputStream baos = new ByteArrayOutputStream();
088: handler.writeTo(baos);
089: String reconstructedXml = baos.toString("utf-8");
090: assertEquals("Attachment content is preserved", xml,
091: reconstructedXml);
092: }
093:
094: public void testXmlAttachmentWithoutXmlDecl() throws Exception {
095: SOAPMessage msg = MessageFactory.newInstance().createMessage();
096: String xml = "<START><A>Hello</A><B>World</B></START>";
097: StringReader reader = new StringReader(xml);
098: StreamSource content = new StreamSource(reader);
099: AttachmentPart attachment = msg.createAttachmentPart(content,
100: "text/xml");
101:
102: DataHandler handler = attachment.getDataHandler();
103: ByteArrayOutputStream baos = new ByteArrayOutputStream();
104: handler.writeTo(baos);
105: String reconstructedXml = baos.toString();
106: assertEquals("Attachment content is preserved", xml,
107: reconstructedXml);
108: }
109:
110: public void testXmlAttachmentReWriteWithReader() throws Exception {
111: SOAPMessage msg = MessageFactory.newInstance().createMessage();
112: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <START><A>Hello</A><B>World</B></START>";
113: StringReader reader = new StringReader(xml);
114: StreamSource content = new StreamSource(reader);
115: AttachmentPart attachment = msg.createAttachmentPart(content,
116: "text/xml");
117:
118: DataHandler handler = attachment.getDataHandler();
119: ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
120: ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
121: handler.writeTo(baos1);
122: handler.writeTo(baos2);
123: String reconstructedXml1 = baos1.toString("utf-8");
124: String reconstructedXml2 = baos2.toString("utf-8");
125: assertEquals(
126: "writeTo for the attachment should succeed the second time too!",
127: reconstructedXml1, reconstructedXml2);
128: }
129:
130: public void testXmlAttachmentReWriteWithIS() throws Exception {
131: SOAPMessage msg = MessageFactory.newInstance().createMessage();
132: String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?> <START><A>Hello</A><B>World</B></START>";
133: ByteArrayInputStream bais = new ByteArrayInputStream(xml
134: .getBytes("utf-8"));
135: StreamSource content = new StreamSource(bais);
136: AttachmentPart attachment = msg.createAttachmentPart(content,
137: "text/xml");
138:
139: DataHandler handler = attachment.getDataHandler();
140: ByteArrayOutputStream baos1 = new ByteArrayOutputStream();
141: ByteArrayOutputStream baos2 = new ByteArrayOutputStream();
142: handler.writeTo(baos1);
143: handler.writeTo(baos2);
144: String reconstructedXml1 = baos1.toString("utf-8");
145: String reconstructedXml2 = baos2.toString("utf-8");
146: assertEquals(
147: "writeTo for the attachment should succeed the second time too!",
148: reconstructedXml1, reconstructedXml2);
149: }
150: }
|