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