KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > test > saaj > TestAttachment


1 package test.saaj;
2
3 import javax.xml.soap.AttachmentPart JavaDoc;
4 import javax.xml.soap.MessageFactory JavaDoc;
5 import javax.xml.soap.SOAPConnection JavaDoc;
6 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
7 import javax.xml.soap.SOAPMessage JavaDoc;
8 import javax.xml.soap.SOAPPart JavaDoc;
9 import javax.xml.soap.SOAPEnvelope JavaDoc;
10 import javax.xml.soap.SOAPBody JavaDoc;
11 import javax.xml.soap.SOAPHeader JavaDoc;
12 import javax.xml.soap.MimeHeaders JavaDoc;
13 import javax.activation.DataHandler JavaDoc;
14 import javax.activation.DataSource JavaDoc;
15 import java.io.InputStream JavaDoc;
16 import java.io.IOException JavaDoc;
17 import java.io.OutputStream JavaDoc;
18 import java.io.ByteArrayInputStream JavaDoc;
19 import java.io.ByteArrayOutputStream JavaDoc;
20 import java.io.File JavaDoc;
21 import java.util.Iterator JavaDoc;
22 import java.net.URL JavaDoc;
23
24 public class TestAttachment extends junit.framework.TestCase {
25
26     public TestAttachment(String JavaDoc name) {
27         super(name);
28     }
29
30     public void testStringAttachment() throws Exception JavaDoc {
31         SOAPConnectionFactory JavaDoc scFactory = SOAPConnectionFactory.newInstance();
32         SOAPConnection JavaDoc con = scFactory.createConnection();
33
34         MessageFactory JavaDoc factory = MessageFactory.newInstance();
35         SOAPMessage JavaDoc message = factory.createMessage();
36         AttachmentPart JavaDoc attachment = message.createAttachmentPart();
37         String JavaDoc stringContent = "Update address for Sunny Skies " +
38                 "Inc., to 10 Upbeat Street, Pleasant Grove, CA 95439";
39
40         attachment.setContent(stringContent, "text/plain");
41         attachment.setContentId("update_address");
42         message.addAttachmentPart(attachment);
43
44         assertTrue(message.countAttachments()==1);
45
46         java.util.Iterator JavaDoc it = message.getAttachments();
47         while (it.hasNext()) {
48             attachment = (AttachmentPart JavaDoc) it.next();
49             Object JavaDoc content = attachment.getContent();
50             String JavaDoc id = attachment.getContentId();
51             System.out.println("Attachment " + id + " contains: " + content);
52             assertEquals(content,stringContent);
53         }
54         System.out.println("Here is what the XML message looks like:");
55         message.writeTo(System.out);
56
57         message.removeAllAttachments();
58         assertTrue(message.countAttachments()==0);
59     }
60
61     public void testMultipleAttachments() throws Exception JavaDoc {
62         SOAPConnectionFactory JavaDoc scFactory = SOAPConnectionFactory.newInstance();
63         SOAPConnection JavaDoc con = scFactory.createConnection();
64
65         MessageFactory JavaDoc factory = MessageFactory.newInstance();
66         SOAPMessage JavaDoc msg = factory.createMessage();
67         java.net.URL JavaDoc url1 = new java.net.URL JavaDoc("http://slashdot.org/slashdot.xml");
68         java.net.URL JavaDoc url2 = new java.net.URL JavaDoc("http://www.apache.org/LICENSE.txt");
69
70         AttachmentPart JavaDoc a1 = msg.createAttachmentPart(new javax.activation.DataHandler JavaDoc(url1));
71         a1.setContentType("text/xml");
72         msg.addAttachmentPart(a1);
73         AttachmentPart JavaDoc a2 = msg.createAttachmentPart(new javax.activation.DataHandler JavaDoc(url1));
74         a2.setContentType("text/xml");
75         msg.addAttachmentPart(a2);
76         AttachmentPart JavaDoc a3 = msg.createAttachmentPart(new javax.activation.DataHandler JavaDoc(url2));
77         a3.setContentType("text/plain");
78         msg.addAttachmentPart(a3);
79
80         assertTrue(msg.countAttachments()==3);
81
82         javax.xml.soap.MimeHeaders JavaDoc mimeHeaders = new javax.xml.soap.MimeHeaders JavaDoc();
83         mimeHeaders.addHeader("Content-Type", "text/xml");
84
85         int nAttachments = 0;
86         java.util.Iterator JavaDoc iterator = msg.getAttachments(mimeHeaders);
87         while (iterator.hasNext()) {
88             nAttachments++;
89             AttachmentPart JavaDoc ap = (AttachmentPart JavaDoc)iterator.next();
90             assertTrue(ap.equals(a1) || ap.equals(a2));
91         }
92         assertTrue(nAttachments==2);
93     }
94     
95     public void testBadAttSize() throws Exception JavaDoc {
96         MessageFactory JavaDoc factory = MessageFactory.newInstance();
97         SOAPMessage JavaDoc message = factory.createMessage();
98
99         ByteArrayInputStream JavaDoc ins=new ByteArrayInputStream JavaDoc(new byte[5]);
100         DataHandler JavaDoc dh=new DataHandler JavaDoc(new Src(ins,"text/plain"));
101         AttachmentPart JavaDoc part = message.createAttachmentPart(dh);
102         assertEquals("Size should match",5,part.getSize());
103     }
104
105     class Src implements DataSource JavaDoc{
106         InputStream JavaDoc m_src;
107         String JavaDoc m_type;
108
109         public Src(InputStream JavaDoc data, String JavaDoc type){
110             m_src=data;
111             m_type=type;
112         }
113         public String JavaDoc getContentType(){
114             return m_type;
115         }
116         public InputStream JavaDoc getInputStream() throws IOException JavaDoc{
117             m_src.reset();
118             return m_src;
119         }
120         public String JavaDoc getName(){
121             return "Some-Data";
122         }
123         public OutputStream JavaDoc getOutputStream(){
124             throw new UnsupportedOperationException JavaDoc("I don't give output streams");
125         }
126     }
127     
128     public static void main(String JavaDoc[] args) throws Exception JavaDoc {
129         test.saaj.TestAttachment tester = new test.saaj.TestAttachment("TestSAAJ");
130         tester.testMultipleAttachments();
131         tester.testStringAttachment();
132         tester.testBadAttSize();
133     }
134 }
135
Popular Tags