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: AttachImageTest.java,v 1.3 2007/07/16 16:41:27 ofung Exp $
058: */package mime;
059:
060: import java.awt.Image;
061: import java.awt.Toolkit;
062: import java.io.*;
063: import java.net.URL;
064: import java.util.Iterator;
065:
066: import javax.activation.DataHandler;
067: import javax.xml.soap.*;
068: import javax.xml.transform.stream.StreamSource;
069:
070: import junit.framework.TestCase;
071:
072: import javax.activation.FileDataSource;
073:
074: import com.sun.xml.messaging.saaj.packaging.mime.internet.MimeUtility;
075: import com.sun.xml.messaging.saaj.soap.AttachmentPartImpl;
076:
077: /*
078: * Attaches an image object and verifies whether it gets the image object back
079: */
080:
081: public class AttachImageTest extends TestCase {
082:
083: public AttachImageTest(String name) {
084: super (name);
085: }
086:
087: public void testAddImageAndVerify() throws Exception {
088:
089: MessageFactory mf = MessageFactory.newInstance();
090: SOAPMessage msg = mf.createMessage();
091: SOAPPart sp = msg.getSOAPPart();
092:
093: SOAPEnvelope envelope = sp.getEnvelope();
094:
095: SOAPHeader hdr = envelope.getHeader();
096: SOAPBody bdy = envelope.getBody();
097:
098: // Add to body
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: // Attach Image
109: Image img = Toolkit.getDefaultToolkit().getImage(
110: "src/test/mime/data/java_logo.jpg");
111: AttachmentPart ap = msg.createAttachmentPart(img, "image/jpeg");
112: AttachmentPart ap1 = msg
113: .createAttachmentPart(img, "image/jpeg");
114: msg.addAttachmentPart(ap);
115: msg.addAttachmentPart(ap1);
116: msg.saveChanges();
117:
118: // Save the soap message to file
119: FileOutputStream sentFile = new FileOutputStream(
120: "src/test/mime/data/java_logo_sent.jpg");
121: msg.writeTo(sentFile);
122: sentFile.close();
123:
124: // See if we get the image object back
125: FileInputStream fin = new FileInputStream(
126: "src/test/mime/data/java_logo_sent.jpg");
127: SOAPMessage newMsg = mf
128: .createMessage(msg.getMimeHeaders(), fin);
129:
130: SOAPElement elment = newMsg.getSOAPBody();
131: newMsg.writeTo(new ByteArrayOutputStream());
132:
133: Iterator i = newMsg.getAttachments();
134: while (i.hasNext()) {
135: AttachmentPart att = (AttachmentPart) i.next();
136: Object obj = att.getContent();
137: if (!(obj instanceof Image)) {
138: fail("Didn't get the image type, instead got:"
139: + obj.getClass());
140: }
141: }
142: fin.close();
143: }
144:
145: /*
146: public void testAddGifImageAndVerify() throws Exception {
147:
148: MessageFactory mf = MessageFactory.newInstance();
149: SOAPMessage msg = mf.createMessage();
150: SOAPPart sp = msg.getSOAPPart();
151:
152: SOAPEnvelope envelope = sp.getEnvelope();
153:
154: SOAPHeader hdr = envelope.getHeader();
155: SOAPBody bdy = envelope.getBody();
156:
157: // Add to body
158: SOAPBodyElement gltp = bdy.addBodyElement(
159: envelope.createName("GetLastTradePrice", "ztrade",
160: "http://wombat.ztrade.com"));
161:
162: gltp.addChildElement(envelope.createName("symbol", "ztrade",
163: "http://wombat.ztrade.com")).addTextNode("SUNW");
164:
165: System.out.println("Setting content via Image");
166:
167: Image image = Toolkit.getDefaultToolkit().createImage("src/test/mime/data/java.gif");
168:
169: AttachmentPart ap = msg.createAttachmentPart(image, "image/gif");
170: ap.setContentType("image/gif");
171:
172: System.out.println("Created the IMAGE object OK");
173:
174: Object o = ap.getContent();
175: System.out.println("Content type " + ap.getContentType());
176: if (o!=null) {
177: if (o instanceof Image) {
178: System.out.println("Image object was found");
179: } else {
180: System.out.println("Unexpected object was found" + o);
181: }
182: } else {
183: System.out.println("null was returned");
184: }
185:
186: msg.addAttachmentPart(ap);
187: msg.saveChanges();
188: //msg.writeTo(System.out);
189:
190: }
191: */
192:
193: public void testGetAttachmentByHref() throws Exception {
194:
195: MessageFactory mf = MessageFactory.newInstance();
196: SOAPMessage msg = mf.createMessage();
197: SOAPPart sp = msg.getSOAPPart();
198:
199: SOAPEnvelope envelope = sp.getEnvelope();
200:
201: SOAPHeader hdr = envelope.getHeader();
202: SOAPBody bdy = envelope.getBody();
203:
204: // Add to body
205: SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
206: "GetLastTradePrice", "ztrade",
207: "http://wombat.ztrade.com"));
208:
209: gltp.addChildElement(
210: envelope.createName("symbol", "ztrade",
211: "http://wombat.ztrade.com"))
212: .addTextNode("SUNW");
213: gltp.setAttribute("href", "cid:MYIMG");
214:
215: // Attach Image
216: Image img = Toolkit.getDefaultToolkit().getImage(
217: "src/test/mime/data/java_logo.jpg");
218: AttachmentPart ap = msg.createAttachmentPart(img, "image/jpeg");
219: ap.setContentId("<MYIMG>");
220: msg.addAttachmentPart(ap);
221: msg.saveChanges();
222: AttachmentPart bp = msg.getAttachment(gltp);
223: assertTrue(bp != null);
224: }
225:
226: public void testGetAttachmentBySwaRef() throws Exception {
227:
228: MessageFactory mf = MessageFactory.newInstance();
229: SOAPMessage msg = mf.createMessage();
230: SOAPPart sp = msg.getSOAPPart();
231:
232: SOAPEnvelope envelope = sp.getEnvelope();
233:
234: SOAPHeader hdr = envelope.getHeader();
235: SOAPBody bdy = envelope.getBody();
236: // Add to body
237: SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
238: "GetLastTradePrice", "ztrade",
239: "http://wombat.ztrade.com"));
240:
241: gltp.addTextNode("cid:MYIMG");
242:
243: // Attach Image
244: Image img = Toolkit.getDefaultToolkit().getImage(
245: "src/test/mime/data/java_logo.jpg");
246: AttachmentPart ap = msg.createAttachmentPart(img, "image/jpeg");
247: ap.setContentId("<MYIMG>");
248: msg.addAttachmentPart(ap);
249: msg.saveChanges();
250: AttachmentPart bp = msg.getAttachment(gltp);
251: assertTrue(bp != null);
252: }
253:
254: public void testAddXmlAndVerify() throws Exception {
255: MessageFactory mf = MessageFactory.newInstance();
256: SOAPMessage msg = mf.createMessage();
257: SOAPPart sp = msg.getSOAPPart();
258:
259: SOAPEnvelope envelope = sp.getEnvelope();
260:
261: SOAPHeader hdr = envelope.getHeader();
262: SOAPBody bdy = envelope.getBody();
263:
264: // Add to body
265: SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
266: "GetLastTradePrice", "ztrade",
267: "http://wombat.ztrade.com"));
268: gltp.addChildElement(
269: envelope.createName("symbol", "ztrade",
270: "http://wombat.ztrade.com"))
271: .addTextNode("SUNW");
272:
273: // Attachment1 : XML from DataHandler(URL)
274: {
275: URL url1 = new URL("file:src/test/mime/data/attach1.xml");
276: AttachmentPart ap1 = msg
277: .createAttachmentPart(new DataHandler(url1));
278: ap1.setContentType("text/xml");
279: msg.addAttachmentPart(ap1);
280: }
281:
282: // Attachment1 : XML from DataHandler(URL)
283: {
284: URL url1 = new URL("file:src/test/mime/data/attach1.xml");
285: AttachmentPart ap1 = msg
286: .createAttachmentPart(new DataHandler(url1));
287: ap1.setContentType("text/xml");
288: msg.addAttachmentPart(ap1);
289: }
290:
291: // Attachment2: XML from reader. The text has XML decl
292: {
293: String xml2 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
294: + "<START><A>Hello</A><B>World</B></START>";
295: StringReader reader2 = new StringReader(xml2);
296: StreamSource source2 = new StreamSource(reader2);
297: AttachmentPart ap2 = msg.createAttachmentPart();
298: ap2.setContent(source2, "text/xml");
299: msg.addAttachmentPart(ap2);
300: }
301:
302: // Attachment3: XML from reader. The text has no XML decl
303: {
304: String xml3 = "<START><A>Hello</A><B>World</B></START>";
305: StringReader reader3 = new StringReader(xml3);
306: StreamSource source3 = new StreamSource(reader3);
307: AttachmentPart ap3 = msg.createAttachmentPart(source3,
308: "text/xml");
309: msg.addAttachmentPart(ap3);
310: }
311:
312: // Attachment4: XML from stream
313: {
314: String xml4 = "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>"
315: + "<START><A>Hello</A><B>World</B></START>";
316: InputStream stream4 = new ByteArrayInputStream(xml4
317: .getBytes());
318: StreamSource source4 = new StreamSource(stream4);
319: AttachmentPart ap4 = msg.createAttachmentPart(source4,
320: "text/xml");
321: msg.addAttachmentPart(ap4);
322: }
323:
324: // Save the soap message to file
325: FileOutputStream sentFile = new FileOutputStream(
326: "src/test/mime/data/saving.cnt");
327: msg.writeTo(sentFile);
328: sentFile.close();
329:
330: // See if we get the attachment back
331: FileInputStream fin = new FileInputStream(
332: "src/test/mime/data/saving.cnt");
333: SOAPMessage newMsg = mf
334: .createMessage(msg.getMimeHeaders(), fin);
335: Iterator i = newMsg.getAttachments();
336: while (i.hasNext()) {
337: AttachmentPart att = (AttachmentPart) i.next();
338: Object obj = att.getContent();
339: if (!(obj instanceof StreamSource)) {
340: fail("Didn't get StreamSource, instead got:"
341: + obj.getClass());
342: }
343: }
344: fin.close();
345: }
346:
347: public void testAddTextPlainAndVerify() throws Exception {
348: MessageFactory mf = MessageFactory.newInstance();
349: SOAPMessage msg = mf.createMessage();
350: SOAPPart sp = msg.getSOAPPart();
351:
352: SOAPEnvelope envelope = sp.getEnvelope();
353:
354: SOAPHeader hdr = envelope.getHeader();
355: SOAPBody bdy = envelope.getBody();
356:
357: // Add to body
358: SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
359: "GetLastTradePrice", "ztrade",
360: "http://wombat.ztrade.com"));
361: gltp.addChildElement(
362: envelope.createName("symbol", "ztrade",
363: "http://wombat.ztrade.com"))
364: .addTextNode("SUNW");
365:
366: // Attachment1 : XML from DataHandler(URL)
367: {
368: URL url1 = new URL("file:src/test/mime/data/attach1.xml");
369: AttachmentPart ap1 = msg
370: .createAttachmentPart(new DataHandler(url1));
371: ap1.setContentType("text/plain");
372: msg.addAttachmentPart(ap1);
373: }
374:
375: // Save the soap message to file
376: FileOutputStream sentFile = new FileOutputStream(
377: "src/test/mime/data/saving_text.cnt");
378: msg.writeTo(sentFile);
379: sentFile.close();
380:
381: // See if we get the attachment back
382: FileInputStream fin = new FileInputStream(
383: "src/test/mime/data/saving_text.cnt");
384: SOAPMessage newMsg = mf
385: .createMessage(msg.getMimeHeaders(), fin);
386: Iterator i = newMsg.getAttachments();
387: while (i.hasNext()) {
388: AttachmentPart att = (AttachmentPart) i.next();
389: Object obj = att.getContent();
390: if (!(obj instanceof String)) {
391: fail("Didn't get String, instead got:" + obj.getClass());
392: }
393: }
394: fin.close();
395: }
396:
397: public void testSetGetRawContent() throws Exception {
398: MessageFactory mf = MessageFactory.newInstance();
399: SOAPMessage msg = mf.createMessage();
400: SOAPPart sp = msg.getSOAPPart();
401:
402: SOAPEnvelope envelope = sp.getEnvelope();
403:
404: SOAPHeader hdr = envelope.getHeader();
405: SOAPBody bdy = envelope.getBody();
406:
407: // Add to body
408: SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
409: "GetLastTradePrice", "ztrade",
410: "http://wombat.ztrade.com"));
411: gltp.addChildElement(
412: envelope.createName("symbol", "ztrade",
413: "http://wombat.ztrade.com"))
414: .addTextNode("SUNW");
415:
416: // Attachment1 : XML from file
417: FileInputStream fis = new FileInputStream(
418: "src/test/mime/data/attach1.xml");
419: AttachmentPart ap1 = msg.createAttachmentPart();
420: ap1.setRawContent(fis, "text/xml");
421: msg.addAttachmentPart(ap1);
422: InputStream content = ap1.getRawContent();
423: assertTrue(content != null);
424: }
425:
426: public void testSetContentGetRawContent() throws Exception {
427:
428: byte buf1[] = new byte[30000];
429: byte buf2[] = new byte[30000];
430: byte buf3[] = new byte[30000];
431: byte buf4[] = new byte[30000];
432:
433: MessageFactory mf = MessageFactory.newInstance();
434: SOAPMessage msg = mf.createMessage();
435: AttachmentPart ap = msg.createAttachmentPart();
436:
437: FileInputStream is1 = new FileInputStream(
438: "src/test/mime/data/java_logo.jpg");
439: int length1 = is1.read(buf1, 0, 30000);
440: //System.out.println("Length1 =" + length1);
441: is1.close();
442:
443: FileInputStream is11 = new FileInputStream(
444: "src/test/mime/data/java_logo.jpg");
445: ap.setRawContent(is11, "image/jpeg");
446: InputStream is12 = ap.getRawContent();
447: int length11 = ap.getSize();
448: //System.out.println("Length11 =" + length11);
449: assertTrue(length11 == length1);
450:
451: Image img = Toolkit.getDefaultToolkit().getImage(
452: "src/test/mime/data/java_logo.jpg");
453: ap.setContent(img, "image/jpeg");
454: InputStream is3 = ap.getRawContent();
455: int length2 = is3.read(buf2, 0, 30000);
456: //System.out.println("Length2 =" + length2);
457:
458: Image img1 = Toolkit.getDefaultToolkit().getImage(
459: "src/test/mime/data/java_logo.jpg");
460: ap.setContent(img1, "image/jpeg");
461: InputStream is4 = ap.getDataHandler().getInputStream();
462: int length3 = is4.read(buf3, 0, 30000);
463: //System.out.println("Length3 =" + length3);
464:
465: Image img2 = Toolkit.getDefaultToolkit().getImage(
466: "src/test/mime/data/java_logo.jpg");
467: ap.setContent(img2, "image/jpeg");
468: int length4 = ap.getSize();
469: //System.out.println("Length4 =" + length4);
470:
471: Image img3 = Toolkit.getDefaultToolkit().getImage(
472: "src/test/mime/data/java_logo.jpg");
473: DataHandler dh = new DataHandler(img3, "image/jpeg");
474: InputStream is5 = dh.getInputStream();
475: int length5 = is5.read(buf4, 0, 30000);
476: //System.out.println("Length5 =" + length5);
477: }
478:
479: public void testSetContentGetRawContent2() throws Exception {
480:
481: byte buf1[] = new byte[30000];
482: byte buf2[] = new byte[30000];
483: byte buf3[] = new byte[30000];
484: byte buf4[] = new byte[30000];
485:
486: MessageFactory mf = MessageFactory.newInstance();
487: SOAPMessage msg = mf.createMessage();
488: AttachmentPart ap = msg.createAttachmentPart();
489:
490: FileInputStream is1 = new FileInputStream(
491: "src/test/mime/data/attach1.xml");
492: int length1 = is1.read(buf1, 0, 30000);
493: //System.out.println("Length1 =" + length1);
494: is1.close();
495:
496: FileInputStream is2 = new FileInputStream(
497: "src/test/mime/data/attach1.xml");
498: ap.setRawContent(is2, "text/xml");
499: InputStream is3 = ap.getRawContent();
500: int length2 = is3.read(buf2, 0, 30000);
501: //System.out.println("Length2 =" + length2);
502: assertTrue(length2 == length1);
503:
504: FileInputStream fis2 = new FileInputStream(
505: "src/test/mime/data/attach1.xml");
506: ap.setContent(fis2, "text/xml");
507: InputStream is4 = ap.getDataHandler().getInputStream();
508: int length3 = is4.read(buf3, 0, 30000);
509: //System.out.println("Length3 =" + length3);
510:
511: /*
512: FileDataSource fis3 = new FileDataSource("src/test/mime/data/attach1.xml");
513: ap.setContent(fis3 ,"text/xml");
514: int length4 = ap.getSize();
515: System.out.println("Length4 =" + length4);
516: */
517:
518: FileInputStream fis4 = new FileInputStream(
519: "src/test/mime/data/attach1.xml");
520: DataHandler dh = new DataHandler(fis4, "text/xml");
521: InputStream is5 = dh.getInputStream();
522: int length5 = is5.read(buf4, 0, 30000);
523: //System.out.println("Length5 =" + length5);
524: }
525:
526: public void testSetRawContentBytes() throws Exception {
527:
528: byte buf1[] = new byte[30000];
529: byte buf2[] = new byte[30000];
530: byte buf3[] = new byte[30000];
531: byte buf4[] = new byte[30000];
532:
533: MessageFactory mf = MessageFactory.newInstance();
534: SOAPMessage msg = mf.createMessage();
535: AttachmentPart ap = msg.createAttachmentPart();
536:
537: FileInputStream is1 = new FileInputStream(
538: "src/test/mime/data/attach1.xml");
539: int length1 = is1.read(buf1, 0, 30000);
540: is1.close();
541:
542: ap.setRawContentBytes(buf1, 0, length1, "text/xml");
543: msg.addAttachmentPart(ap);
544: msg.saveChanges();
545: byte[] bufx = ap.getRawContentBytes();
546: // on Win2k CRLF changes into CRCRLF
547: assertTrue((bufx.length == 1634) || (bufx.length == 1710));
548:
549: }
550:
551: public void testGetBase64Content() throws Exception {
552: MessageFactory mf = MessageFactory.newInstance();
553: SOAPMessage msg = mf.createMessage();
554: SOAPPart sp = msg.getSOAPPart();
555:
556: SOAPEnvelope envelope = sp.getEnvelope();
557:
558: SOAPHeader hdr = envelope.getHeader();
559: SOAPBody bdy = envelope.getBody();
560:
561: // Add to body
562: SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
563: "GetLastTradePrice", "ztrade",
564: "http://wombat.ztrade.com"));
565: gltp.addChildElement(
566: envelope.createName("symbol", "ztrade",
567: "http://wombat.ztrade.com"))
568: .addTextNode("SUNW");
569:
570: // Attachment1 : XML from file
571: FileInputStream fis = new FileInputStream(
572: "src/test/mime/data/attach1.xml");
573: AttachmentPart ap1 = msg.createAttachmentPart();
574: ap1.setRawContent(fis, "text/xml");
575: msg.addAttachmentPart(ap1);
576: InputStream content = ap1.getBase64Content();
577: assertTrue(content != null);
578: /*
579: int len;
580: int size = 1024;
581: byte [] buf;
582:
583: buf = new byte[size];
584: while ((len = content.read(buf, 0, size)) != -1) {
585: System.out.println(new String(buf));
586: }*/
587:
588: }
589:
590: public void testSetBase64Content() throws Exception {
591: MessageFactory mf = MessageFactory.newInstance();
592: SOAPMessage msg = mf.createMessage();
593: SOAPPart sp = msg.getSOAPPart();
594:
595: SOAPEnvelope envelope = sp.getEnvelope();
596:
597: SOAPHeader hdr = envelope.getHeader();
598: SOAPBody bdy = envelope.getBody();
599:
600: // Add to body
601: SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
602: "GetLastTradePrice", "ztrade",
603: "http://wombat.ztrade.com"));
604: gltp.addChildElement(
605: envelope.createName("symbol", "ztrade",
606: "http://wombat.ztrade.com"))
607: .addTextNode("SUNW");
608:
609: // Attachment1 : XML from file
610: FileInputStream fis = new FileInputStream(
611: "src/test/mime/data/attach1.xml");
612: AttachmentPart ap1 = msg.createAttachmentPart();
613:
614: InputStream stream = null;
615: int len;
616: int size = 1024;
617: byte[] buf;
618:
619: ByteArrayOutputStream bos = new ByteArrayOutputStream();
620: OutputStream ret = MimeUtility.encode(bos, "base64");
621: buf = new byte[size];
622:
623: while ((len = fis.read(buf, 0, size)) != -1) {
624: ret.write(buf, 0, len);
625: }
626: ret.flush();
627: buf = bos.toByteArray();
628: stream = new ByteArrayInputStream(buf);
629:
630: ap1.setBase64Content(stream, "text/xml");
631: msg.addAttachmentPart(ap1);
632: InputStream content = ap1.getRawContent();
633: assertTrue(content != null);
634:
635: /*
636: buf = new byte[size];
637: while ((len = content.read(buf, 0, size)) != -1) {
638: System.out.println(new String(buf));
639: }*/
640:
641: }
642:
643: public void testRemoveImageAndVerify() throws Exception {
644:
645: MessageFactory mf = MessageFactory.newInstance();
646: SOAPMessage msg = mf.createMessage();
647: SOAPPart sp = msg.getSOAPPart();
648:
649: SOAPEnvelope envelope = sp.getEnvelope();
650:
651: SOAPHeader hdr = envelope.getHeader();
652: SOAPBody bdy = envelope.getBody();
653:
654: // Add to body
655: SOAPBodyElement gltp = bdy.addBodyElement(envelope.createName(
656: "GetLastTradePrice", "ztrade",
657: "http://wombat.ztrade.com"));
658:
659: gltp.addChildElement(
660: envelope.createName("symbol", "ztrade",
661: "http://wombat.ztrade.com"))
662: .addTextNode("SUNW");
663:
664: // Attach Image
665: Image img = Toolkit.getDefaultToolkit().getImage(
666: "src/test/mime/data/java_logo.jpg");
667: AttachmentPart ap = msg.createAttachmentPart(img, "image/jpeg");
668: msg.addAttachmentPart(ap);
669: AttachmentPart ap1 = msg
670: .createAttachmentPart(img, "image/jpeg");
671: msg.addAttachmentPart(ap1);
672: FileInputStream fis = new FileInputStream(
673: "src/test/mime/data/attach1.xml");
674: AttachmentPart ap2 = msg.createAttachmentPart();
675: ap2.setRawContent(fis, "text/xml");
676: msg.addAttachmentPart(ap2);
677: msg.saveChanges();
678:
679: // Save the soap message to file
680: FileOutputStream sentFile = new FileOutputStream(
681: "src/test/mime/data/java_logo_sent.jpg");
682: msg.writeTo(sentFile);
683: sentFile.close();
684:
685: // See if we get the image object back
686: FileInputStream fin = new FileInputStream(
687: "src/test/mime/data/java_logo_sent.jpg");
688: SOAPMessage newMsg = mf
689: .createMessage(msg.getMimeHeaders(), fin);
690: Iterator i = newMsg.getAttachments();
691: //System.out.println("Count before remove:" + newMsg.countAttachments());
692: while (i.hasNext()) {
693: AttachmentPart att = (AttachmentPart) i.next();
694: newMsg.removeAttachments(((AttachmentPartImpl) att)
695: .getMimeHeaders());
696: break;
697: }
698: //System.out.println("Count after remove:" + newMsg.countAttachments());
699: assertTrue(newMsg.countAttachments() == 1);
700: fin.close();
701: }
702:
703: }
|