1 22 package org.jboss.test.webservice.attachment; 23 24 import junit.framework.Test; 25 import org.jboss.test.webservice.WebserviceTestBase; 26 27 import javax.activation.DataHandler ; 28 import javax.activation.FileDataSource ; 29 import javax.mail.internet.MimeBodyPart ; 30 import javax.mail.internet.MimeMultipart ; 31 import javax.naming.InitialContext ; 32 import javax.xml.rpc.Service ; 33 import javax.xml.transform.Source ; 34 import javax.xml.transform.stream.StreamSource ; 35 import java.awt.*; 36 import java.io.File ; 37 import java.io.FileInputStream ; 38 import java.net.URL ; 39 40 48 public class AttachmentProxyTestCase extends WebserviceTestBase 49 { 50 private static Attachment port; 51 52 public AttachmentProxyTestCase(String name) 53 { 54 super(name); 55 } 56 57 58 public static Test suite() throws Exception 59 { 60 return getDeploySetup(AttachmentProxyTestCase.class, "ws4ee-attachment.war, ws4ee-attachment-client.jar"); 61 } 62 63 protected void setUp() throws Exception 64 { 65 if (port == null) 66 { 67 InitialContext iniCtx = getClientContext(); 68 Service service = (Service )iniCtx.lookup("java:comp/env/service/AttachmentService"); 69 port = (Attachment)service.getPort(Attachment.class); 70 } 71 } 72 73 public void testSendMimeImageGIF() throws Exception 74 { 75 URL url = new File ("resources/webservice/attachment/attach.gif").toURL(); 76 77 Image image = null; 81 try 82 { 83 image = Toolkit.getDefaultToolkit().createImage(url); 84 } 85 catch (Throwable th) 86 { 87 log.warn("Cannot create Image: " + th); 88 } 89 90 if (image != null) 91 { 92 String value = port.sendMimeImageGIF("Some text message", new DataHandler (url)); 93 assertEquals("[pass]", value); 94 } 95 } 96 97 public void testSendMimeImageJPEG() throws Exception 98 { 99 URL url = new File ("resources/webservice/attachment/attach.jpeg").toURL(); 100 101 Image image = null; 105 try 106 { 107 image = Toolkit.getDefaultToolkit().createImage(url); 108 } 109 catch (Throwable th) 110 { 111 log.warn("Cannot create Image: " + th); 112 } 113 114 if (image != null) 115 { 116 String value = port.sendMimeImageJPEG("Some text message", new DataHandler (url)); 117 assertEquals("[pass]", value); 118 } 119 } 120 121 public void testSendMimeTextPlain() throws Exception 122 { 123 URL url = new File ("resources/webservice/attachment/attach.txt").toURL(); 124 String value = port.sendMimeTextPlain("Some text message", new DataHandler (url)); 125 assertEquals("[pass]", value); 126 } 127 128 public void testSendMimeMultipart() throws Exception 129 { 130 URL url = new File ("resources/webservice/attachment/attach.txt").toURL(); 131 MimeMultipart multipart = new MimeMultipart ("mixed"); 132 MimeBodyPart bodyPart = new MimeBodyPart (); 133 bodyPart.setDataHandler(new DataHandler (url)); 134 String bpct = bodyPart.getContentType(); 135 bodyPart.setHeader("Content-Type", bpct); 136 multipart.addBodyPart(bodyPart); 137 138 String value = port.sendMimeMultipart("Some text message", multipart); 139 assertEquals("[pass]", value); 140 } 141 142 public void testSendMimeTextXML() throws Exception 143 { 144 ExFileDataSource dataSource = new ExFileDataSource("resources/webservice/attachment/attach.xml", "text/xml"); 147 String value = port.sendMimeTextXML("Some text message", new DataHandler (dataSource)); 148 assertEquals("[pass]", value); 149 } 150 151 public void testSendMimeApplicationXML() throws Exception 152 { 153 ExFileDataSource dataSource = new ExFileDataSource("resources/webservice/attachment/attach.xml", "application/xml"); 154 String value = port.sendMimeApplicationXML("Some text message", new DataHandler (dataSource)); 155 assertEquals("[pass]", value); 156 } 157 158 163 public void testEchoMimeImageJPEG() throws Exception 164 { 165 URL url = new File ("resources/webservice/attachment/attach.jpeg").toURL(); 166 167 Image image = null; 171 try 172 { 173 image = Toolkit.getDefaultToolkit().createImage(url); 174 } 175 catch (Throwable th) 176 { 177 log.warn("Cannot create Image: " + th); 178 } 179 180 if (image != null) 181 { 182 Object retValue = port.echoMimeImageJPEG(new DataHandler (url)); 183 assertTrue("Unexpected: " + retValue, retValue instanceof Image); 184 } 185 } 186 187 public void testEchoMimeTextPlain() throws Exception 188 { 189 URL url = new File ("resources/webservice/attachment/attach.txt").toURL(); 190 Object retValue = port.echoMimeTextPlain(new DataHandler (url)); 191 assertTrue("Unexpected: " + retValue, retValue instanceof String ); 192 assertEquals("This is a plain text attachment.", ((String )retValue).trim()); 193 } 194 195 public void testEchoMimeMultipart() throws Exception 196 { 197 URL url = new File ("resources/webservice/attachment/attach.txt").toURL(); 198 MimeMultipart multipart = new MimeMultipart ("mixed"); 199 MimeBodyPart bodyPart = new MimeBodyPart (); 200 bodyPart.setDataHandler(new DataHandler (url)); 201 String bpct = bodyPart.getContentType(); 202 bodyPart.setHeader("Content-Type", bpct); 203 multipart.addBodyPart(bodyPart); 204 205 Object retValue = port.echoMimeMultipart(multipart); 206 assertTrue("Unexpected: " + retValue, retValue instanceof MimeMultipart ); 207 } 208 209 public void testEchoMimeTextXML() throws Exception 210 { 211 ExFileDataSource dataSource = new ExFileDataSource("resources/webservice/attachment/attach.xml", "text/xml"); 212 Object retValue = port.echoMimeTextXML(new DataHandler (dataSource)); 213 assertTrue("Unexpected: " + retValue, retValue instanceof Source ); 214 } 215 216 218 public void testEchoMimeApplicationXML() throws Exception 219 { 220 FileInputStream stream = new FileInputStream ("resources/webservice/attachment/attach.xml"); 221 Object retValue = port.echoMimeApplicationXML(new StreamSource (stream)); 222 assertTrue("Unexpected: " + retValue, retValue instanceof Source ); 223 } 224 225 public void testEchoHandler() throws Exception 226 { 227 ExFileDataSource dataSource = new ExFileDataSource("resources/webservice/attachment/attach.xml", "text/xml"); 228 DataHandler retValue = port.echoHandler(new DataHandler (dataSource)); 229 String mimeType = retValue.getContentType(); 230 assertEquals("Invalid mime-type: ", "text/xml", mimeType); 231 } 232 233 static class ExFileDataSource extends FileDataSource 234 { 235 private String contentType; 236 237 public ExFileDataSource(String source, String contentType) 238 { 239 super(source); 240 this.contentType = contentType; 241 } 242 243 public String getContentType() 244 { 245 return contentType; 246 } 247 } 248 } | Popular Tags |