001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: */
019:
020: package org.apache.axis2.saaj;
021:
022: import junit.framework.TestCase;
023:
024: import javax.activation.DataHandler;
025: import javax.activation.FileDataSource;
026: import javax.xml.soap.AttachmentPart;
027: import javax.xml.soap.MessageFactory;
028: import javax.xml.soap.MimeHeaders;
029: import javax.xml.soap.SOAPBody;
030: import javax.xml.soap.SOAPElement;
031: import javax.xml.soap.SOAPEnvelope;
032: import javax.xml.soap.SOAPHeader;
033: import javax.xml.soap.SOAPMessage;
034: import javax.xml.soap.SOAPPart;
035: import java.io.ByteArrayOutputStream;
036: import java.io.File;
037: import java.io.FileInputStream;
038: import java.io.FileOutputStream;
039: import java.io.InputStream;
040: import java.io.OutputStream;
041: import java.util.Iterator;
042:
043: public class AttachmentSerializationTest extends TestCase {
044:
045: public static final String MIME_MULTIPART_RELATED = "multipart/related";
046: public static final String MIME_APPLICATION_DIME = "application/dime";
047: public static final String NS_PREFIX = "jaxmtst";
048: public static final String NS_URI = "http://www.jcommerce.net/soap/jaxm/TestJaxm";
049:
050: public AttachmentSerializationTest(String name) {
051: super (name);
052: }
053:
054: public void testAttachments() throws Exception {
055: try {
056: ByteArrayOutputStream bais = new ByteArrayOutputStream();
057: int count = saveMsgWithAttachments(bais);
058: assertEquals(count, 2);
059: } catch (Exception e) {
060: e.printStackTrace();
061: fail("Unexpected Exception : " + e);
062: }
063: }
064:
065: public int saveMsgWithAttachments(OutputStream os) throws Exception {
066: MessageFactory mf = MessageFactory.newInstance();
067: SOAPMessage msg = mf.createMessage();
068:
069: SOAPPart soapPart = msg.getSOAPPart();
070: SOAPEnvelope envelope = soapPart.getEnvelope();
071: SOAPHeader header = envelope.getHeader();
072: SOAPBody body = envelope.getBody();
073:
074: SOAPElement el = header.addHeaderElement(envelope.createName(
075: "field4", NS_PREFIX, NS_URI));
076:
077: SOAPElement el2 = el.addChildElement("field4b", NS_PREFIX);
078: el2.addTextNode("field4value");
079:
080: el = body.addBodyElement(envelope.createName("bodyfield3",
081: NS_PREFIX, NS_URI));
082: el2 = el.addChildElement("bodyfield3a", NS_PREFIX);
083: el2.addTextNode("bodyvalue3a");
084: el2 = el.addChildElement("bodyfield3b", NS_PREFIX);
085: el2.addTextNode("bodyvalue3b");
086: el.addChildElement("datefield", NS_PREFIX);
087:
088: // First Attachment
089: AttachmentPart ap = msg.createAttachmentPart();
090: final String testText = "some attachment text...";
091: ap.setContent(testText, "text/plain");
092: msg.addAttachmentPart(ap);
093:
094: // Second attachment
095: String jpgfilename = System.getProperty("basedir", ".") + "/"
096: + "test-resources/axis2.jpg";
097: File myfile = new File(jpgfilename);
098: FileDataSource fds = new FileDataSource(myfile);
099: DataHandler dh = new DataHandler(fds);
100: AttachmentPart ap2 = msg.createAttachmentPart(dh);
101: ap2.setContentType("image/jpg");
102: msg.addAttachmentPart(ap2);
103:
104: MimeHeaders headers = msg.getMimeHeaders();
105: assertTrue(headers != null);
106: String[] contentType = headers.getHeader("Content-Type");
107: assertTrue(contentType != null);
108:
109: int i = 0;
110: for (Iterator iter = msg.getAttachments(); iter.hasNext();) {
111: AttachmentPart attachmentPart = (AttachmentPart) iter
112: .next();
113: final Object content = attachmentPart.getDataHandler()
114: .getContent();
115: if (content instanceof String) {
116: assertEquals(testText, (String) content);
117: } else if (content instanceof FileInputStream) {
118:
119: // try to write to a File and check whether it is ok
120: final FileInputStream fis = (FileInputStream) content;
121: byte[] b = new byte[15000];
122: final int lengthRead = fis.read(b);
123: FileOutputStream fos = new FileOutputStream(new File(
124: System.getProperty("basedir", ".") + "/"
125: + "target/test-resources/atts-op"
126: + (i++) + ".jpg"));
127: fos.write(b, 0, lengthRead);
128: fos.flush();
129: fos.close();
130: }
131: }
132:
133: msg.writeTo(os);
134: os.flush();
135: return msg.countAttachments();
136: }
137:
138: public int loadMsgWithAttachments(InputStream is) throws Exception {
139: MimeHeaders headers = new MimeHeaders();
140: headers.setHeader("Content-Type", MIME_MULTIPART_RELATED);
141: MessageFactory mf = MessageFactory.newInstance();
142: SOAPMessage msg = mf.createMessage(headers, is);
143:
144: SOAPPart sp = msg.getSOAPPart();
145: assertTrue(sp != null);
146:
147: SOAPEnvelope envelope = sp.getEnvelope();
148: assertTrue(envelope != null);
149: return msg.countAttachments();
150: }
151: }
|