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: */package org.apache.cxf.attachment;
019:
020: import java.io.ByteArrayInputStream;
021: import java.io.ByteArrayOutputStream;
022: import java.io.InputStream;
023: import java.util.Collection;
024: import java.util.Iterator;
025:
026: import org.apache.cxf.helpers.IOUtils;
027: import org.apache.cxf.message.Attachment;
028: import org.apache.cxf.message.Message;
029: import org.apache.cxf.message.MessageImpl;
030: import org.junit.Assert;
031: import org.junit.Test;
032:
033: public class AttachmentDeserializerTest extends Assert {
034:
035: @Test
036: public void testDeserializerMtom() throws Exception {
037: InputStream is = getClass().getResourceAsStream("mimedata");
038: String ct = "multipart/related; type=\"application/xop+xml\"; "
039: + "start=\"<soap.xml@xfire.codehaus.org>\"; "
040: + "start-info=\"text/xml; charset=utf-8\"; "
041: + "boundary=\"----=_Part_4_701508.1145579811786\"";
042:
043: MessageImpl msg = new MessageImpl();
044: msg.put(Message.CONTENT_TYPE, ct);
045: msg.setContent(InputStream.class, is);
046:
047: AttachmentDeserializer deserializer = new AttachmentDeserializer(
048: msg);
049: deserializer.initializeAttachments();
050:
051: InputStream attBody = msg.getContent(InputStream.class);
052: assertTrue(attBody != is);
053: assertTrue(attBody instanceof DelegatingInputStream);
054:
055: Collection<Attachment> atts = msg.getAttachments();
056: assertNotNull(atts);
057:
058: Iterator<Attachment> itr = atts.iterator();
059: assertTrue(itr.hasNext());
060:
061: Attachment a = itr.next();
062: assertNotNull(a);
063:
064: InputStream attIs = a.getDataHandler().getInputStream();
065:
066: assertTrue(((DelegatingInputStream) attIs).getInputStream() instanceof MimeBodyPartInputStream);
067: assertTrue(((DelegatingInputStream) attBody).getInputStream() instanceof ByteArrayInputStream);
068:
069: // check the cached output stream
070: ByteArrayOutputStream out = new ByteArrayOutputStream();
071: IOUtils.copy(attBody, out);
072: assertTrue(out.toString().startsWith("<env:Envelope"));
073:
074: // try streaming a character off the wire
075: assertTrue(attIs.read() == '/');
076: assertTrue(attIs.read() == '9');
077:
078: // Attachment invalid = atts.get("INVALID");
079: // assertNull(invalid.getDataHandler().getInputStream());
080: //
081: // assertTrue(attIs instanceof ByteArrayInputStream);
082: }
083:
084: @Test
085: public void testDeserializerMtomWithAxis2StyleBoundaries()
086: throws Exception {
087: InputStream is = getClass().getResourceAsStream(
088: "axis2_mimedata");
089: String ct = "multipart/related; type=\"application/xop+xml\"; "
090: + "start=\"<soap.xml@xfire.codehaus.org>\"; "
091: + "start-info=\"text/xml; charset=utf-8\"; "
092: + "boundary=MIMEBoundaryurn_uuid_6BC4984D5D38EB283C1177616488109";
093:
094: MessageImpl msg = new MessageImpl();
095: msg.put(Message.CONTENT_TYPE, ct);
096: msg.setContent(InputStream.class, is);
097:
098: AttachmentDeserializer deserializer = new AttachmentDeserializer(
099: msg);
100: deserializer.initializeAttachments();
101:
102: InputStream attBody = msg.getContent(InputStream.class);
103: assertTrue(attBody != is);
104: assertTrue(attBody instanceof DelegatingInputStream);
105:
106: Collection<Attachment> atts = msg.getAttachments();
107: assertNotNull(atts);
108:
109: Iterator<Attachment> itr = atts.iterator();
110: assertTrue(itr.hasNext());
111:
112: Attachment a = itr.next();
113: assertNotNull(a);
114:
115: InputStream attIs = a.getDataHandler().getInputStream();
116:
117: assertTrue(((DelegatingInputStream) attIs).getInputStream() instanceof MimeBodyPartInputStream);
118: assertTrue(((DelegatingInputStream) attBody).getInputStream() instanceof ByteArrayInputStream);
119:
120: // check the cached output stream
121: ByteArrayOutputStream out = new ByteArrayOutputStream();
122: IOUtils.copy(attBody, out);
123: assertTrue(out.toString().startsWith("<env:Envelope"));
124:
125: // try streaming a character off the wire
126: assertTrue(attIs.read() == '/');
127: assertTrue(attIs.read() == '9');
128:
129: // Attachment invalid = atts.get("INVALID");
130: // assertNull(invalid.getDataHandler().getInputStream());
131: //
132: // assertTrue(attIs instanceof ByteArrayInputStream);
133: }
134:
135: @Test
136: public void testDeserializerSwA() throws Exception {
137: InputStream is = getClass().getResourceAsStream("swadata");
138: String ct = "multipart/related; type=\"text/xml\"; "
139: + "start=\"<86048FF3556694F7DA1918466DDF8143>\"; "
140: + "boundary=\"----=_Part_0_14158819.1167275505862\"";
141:
142: MessageImpl msg = new MessageImpl();
143: msg.put(Message.CONTENT_TYPE, ct);
144: msg.setContent(InputStream.class, is);
145:
146: AttachmentDeserializer deserializer = new AttachmentDeserializer(
147: msg);
148: deserializer.initializeAttachments();
149:
150: InputStream attBody = msg.getContent(InputStream.class);
151: assertTrue(attBody != is);
152: assertTrue(attBody instanceof DelegatingInputStream);
153:
154: Collection<Attachment> atts = msg.getAttachments();
155: assertNotNull(atts);
156:
157: Iterator<Attachment> itr = atts.iterator();
158: assertTrue(itr.hasNext());
159:
160: Attachment a = itr.next();
161: assertNotNull(a);
162:
163: InputStream attIs = a.getDataHandler().getInputStream();
164:
165: assertTrue(((DelegatingInputStream) attIs).getInputStream() instanceof MimeBodyPartInputStream);
166: assertTrue(((DelegatingInputStream) attBody).getInputStream() instanceof ByteArrayInputStream);
167:
168: // check the cached output stream
169: ByteArrayOutputStream out = new ByteArrayOutputStream();
170: IOUtils.copy(attBody, out);
171: assertTrue(out.toString().startsWith("<?xml"));
172:
173: // try streaming a character off the wire
174: assertTrue(attIs.read() == 'f');
175: assertTrue(attIs.read() == 'o');
176: assertTrue(attIs.read() == 'o');
177: assertTrue(attIs.read() == 'b');
178: assertTrue(attIs.read() == 'a');
179: assertTrue(attIs.read() == 'r');
180: assertTrue(attIs.read() == -1);
181: }
182:
183: @Test
184: public void testDeserializerSwAWithoutBoundryInContentType()
185: throws Exception {
186: InputStream is = getClass().getResourceAsStream("swadata");
187: String ct = "multipart/related; type=\"text/xml\"; ";
188:
189: MessageImpl msg = new MessageImpl();
190: msg.put(Message.CONTENT_TYPE, ct);
191: msg.setContent(InputStream.class, is);
192:
193: AttachmentDeserializer deserializer = new AttachmentDeserializer(
194: msg);
195: deserializer.initializeAttachments();
196:
197: InputStream attBody = msg.getContent(InputStream.class);
198: assertTrue(attBody != is);
199: assertTrue(attBody instanceof DelegatingInputStream);
200:
201: Collection<Attachment> atts = msg.getAttachments();
202: assertNotNull(atts);
203:
204: Iterator<Attachment> itr = atts.iterator();
205: assertTrue(itr.hasNext());
206:
207: Attachment a = itr.next();
208: assertNotNull(a);
209:
210: InputStream attIs = a.getDataHandler().getInputStream();
211:
212: assertTrue(((DelegatingInputStream) attIs).getInputStream() instanceof MimeBodyPartInputStream);
213: assertTrue(((DelegatingInputStream) attBody).getInputStream() instanceof ByteArrayInputStream);
214:
215: // check the cached output stream
216: ByteArrayOutputStream out = new ByteArrayOutputStream();
217: IOUtils.copy(attBody, out);
218: assertTrue(out.toString().startsWith("<?xml"));
219:
220: // try streaming a character off the wire
221: assertTrue(attIs.read() == 'f');
222: assertTrue(attIs.read() == 'o');
223: assertTrue(attIs.read() == 'o');
224: assertTrue(attIs.read() == 'b');
225: assertTrue(attIs.read() == 'a');
226: assertTrue(attIs.read() == 'r');
227: assertTrue(attIs.read() == -1);
228: }
229: }
|