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: /**
057: * $Id: MimeRecreateTest.java,v 1.2 2007/07/16 16:41:28 ofung Exp $
058: */package mime;
059:
060: import java.io.FileOutputStream;
061: import java.net.URL;
062:
063: import javax.activation.*;
064: import javax.xml.soap.*;
065:
066: import junit.framework.TestCase;
067:
068: /*
069: * These code snippets taken from Krishna's test workspace.
070: * The whole aim of the test is to create a message, and write it
071: * out to a .msg file and write the mime headers to a .mh file
072: *
073: * Later use these to recreate the message and verify that the
074: * original and the recreated messages are indeed the same.
075: *
076: * @author Manveen Kaur (manveen.kaur@sun.com)
077: */
078:
079: public class MimeRecreateTest extends TestCase {
080:
081: public MimeRecreateTest(String name) {
082: super (name);
083: }
084:
085: public SOAPMessage createMessage() throws Exception {
086:
087: MessageBuilder mBuilder = new MessageBuilder();
088:
089: // Create a message factory.
090: MessageFactory mf = MessageFactory.newInstance();
091: SOAPMessage msg = mf.createMessage();
092: SOAPPart sp = msg.getSOAPPart();
093:
094: SOAPEnvelope envelope = sp.getEnvelope();
095:
096: SOAPHeader hdr = envelope.getHeader();
097: SOAPBody bdy = envelope.getBody();
098:
099: SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
100: "GetLastTradePrice", "ztrade",
101: "http://wombat.ztrade.com"));
102:
103: gltp.addChildElement(
104: envelope.createName("symbol", "ztrade",
105: "http://wombat.ztrade.com"))
106: .addTextNode("SUNW");
107:
108: URL url = new URL("file", null,
109: "src/test/mime/data/attach1.xml");
110:
111: //-----This code is written as a work around for one problem.
112:
113: class XmlDataSource extends URLDataSource {
114: public XmlDataSource(URL u) {
115: super (u);
116: }
117:
118: public String getContentType() {
119: return "text/xml";
120: }
121: }
122: ;
123: DataSource xmlDataSource = new XmlDataSource(url);
124:
125: AttachmentPart ap = msg.createAttachmentPart(new DataHandler(
126: xmlDataSource));
127:
128: /*
129: AttachmentPart ap =
130: msg.createAttachmentPart(new DataHandler(url));
131:
132: ap.setContentType("text/xml");
133: */
134: //-----
135: msg.addAttachmentPart(ap);
136:
137: /*
138: AttachmentPart ap = msg.createAttachmentPart();
139: ap.setContent("<foo>hello</foo>","text/plain");
140: msg.addAttachmentPart(ap);
141: */
142:
143: FileOutputStream sentFile = new FileOutputStream(
144: "src/test/mime/data/golden.msg");
145:
146: msg.saveChanges();
147:
148: mBuilder.saveMimeHeaders(msg, "src/test/mime/data/golden.mh");
149:
150: msg.writeTo(sentFile);
151: sentFile.close();
152:
153: //System.out.println("\n\n------Original message----------");
154: //msg.writeTo(System.out);
155:
156: return msg;
157: }
158:
159: public void testMessageRecreation() {
160: try {
161:
162: SOAPMessage originalMsg = createMessage();
163:
164: MessageBuilder mBuilder = new MessageBuilder();
165:
166: SOAPMessage newMsg = mBuilder.constructMessage(
167: "src/test/mime/data/golden.mh",
168: "src/test/mime/data/golden.msg");
169:
170: //System.out.println("\n\n------Recreated message---------");
171: //newMsg.writeTo(System.out);
172:
173: assertTrue("Messages must match", mBuilder.verifyMessage(
174: originalMsg, newMsg));
175:
176: } catch (Exception e) {
177: e.printStackTrace();
178: fail("No exception should have been thrown");
179: }
180:
181: }
182:
183: public static void main(String argv[]) {
184:
185: junit.textui.TestRunner.run(MimeRecreateTest.class);
186:
187: }
188:
189: }
|