package org.jboss.test.webservice.attachment;
import junit.framework.Test;
import org.jboss.test.webservice.WebserviceTestBase;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.naming.InitialContext;
import javax.xml.rpc.Service;
import javax.xml.transform.Source;
import javax.xml.transform.stream.StreamSource;
import java.awt.*;
import java.io.File;
import java.io.FileInputStream;
import java.net.URL;
public class AttachmentProxyTestCase extends WebserviceTestBase
{
private static Attachment port;
public AttachmentProxyTestCase(String name)
{
super(name);
}
public static Test suite() throws Exception
{
return getDeploySetup(AttachmentProxyTestCase.class, "ws4ee-attachment.war, ws4ee-attachment-client.jar");
}
protected void setUp() throws Exception
{
if (port == null)
{
InitialContext iniCtx = getClientContext();
Service service = (Service)iniCtx.lookup("java:comp/env/service/AttachmentService");
port = (Attachment)service.getPort(Attachment.class);
}
}
public void testSendMimeImageGIF() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.gif").toURL();
Image image = null;
try
{
image = Toolkit.getDefaultToolkit().createImage(url);
}
catch (Throwable th)
{
log.warn("Cannot create Image: " + th);
}
if (image != null)
{
String value = port.sendMimeImageGIF("Some text message", new DataHandler(url));
assertEquals("[pass]", value);
}
}
public void testSendMimeImageJPEG() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.jpeg").toURL();
Image image = null;
try
{
image = Toolkit.getDefaultToolkit().createImage(url);
}
catch (Throwable th)
{
log.warn("Cannot create Image: " + th);
}
if (image != null)
{
String value = port.sendMimeImageJPEG("Some text message", new DataHandler(url));
assertEquals("[pass]", value);
}
}
public void testSendMimeTextPlain() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.txt").toURL();
String value = port.sendMimeTextPlain("Some text message", new DataHandler(url));
assertEquals("[pass]", value);
}
public void testSendMimeMultipart() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.txt").toURL();
MimeMultipart multipart = new MimeMultipart("mixed");
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(url));
String bpct = bodyPart.getContentType();
bodyPart.setHeader("Content-Type", bpct);
multipart.addBodyPart(bodyPart);
String value = port.sendMimeMultipart("Some text message", multipart);
assertEquals("[pass]", value);
}
public void testSendMimeTextXML() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.xml").toURL();
String value = port.sendMimeTextXML("Some text message", new DataHandler(url));
assertEquals("[pass]", value);
}
public void testSendMimeApplicationXML() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.xml").toURL();
String value = port.sendMimeApplicationXML("Some text message", new DataHandler(url));
assertEquals("[pass]", value);
}
public void testEchoMimeImageGIF() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.gif").toURL();
Image image = null;
try
{
image = Toolkit.getDefaultToolkit().createImage(url);
}
catch (Throwable th)
{
log.warn("Cannot create Image: " + th);
}
if (image != null)
{
Object retValue = port.echoMimeImageGIF(new DataHandler(url));
assertTrue("Unexpected: " + retValue, retValue instanceof Image);
}
}
public void testEchoMimeImageJPEG() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.jpeg").toURL();
Image image = null;
try
{
image = Toolkit.getDefaultToolkit().createImage(url);
}
catch (Throwable th)
{
log.warn("Cannot create Image: " + th);
}
if (image != null)
{
Object retValue = port.echoMimeImageJPEG(new DataHandler(url));
assertTrue("Unexpected: " + retValue, retValue instanceof Image);
}
}
public void testEchoMimeTextPlain() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.txt").toURL();
Object retValue = port.echoMimeTextPlain(new DataHandler(url));
assertTrue("Unexpected: " + retValue, retValue instanceof String);
assertEquals("This is a plain text attachment.", ((String)retValue).trim());
}
public void testEchoMimeMultipart() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.txt").toURL();
MimeMultipart multipart = new MimeMultipart("mixed");
MimeBodyPart bodyPart = new MimeBodyPart();
bodyPart.setDataHandler(new DataHandler(url));
String bpct = bodyPart.getContentType();
bodyPart.setHeader("Content-Type", bpct);
multipart.addBodyPart(bodyPart);
Object retValue = port.echoMimeMultipart(multipart);
assertTrue("Unexpected: " + retValue, retValue instanceof MimeMultipart);
}
public void testEchoMimeTextXML() throws Exception
{
URL url = new File("resources/webservice/attachment/attach.xml").toURL();
Object retValue = port.echoMimeTextXML(new DataHandler(url));
assertTrue("Unexpected: " + retValue, retValue instanceof Source);
}
public void testEchoMimeApplicationXML() throws Exception
{
FileInputStream stream = new FileInputStream("resources/webservice/attachment/attach.xml");
Object retValue = port.echoMimeApplicationXML(new StreamSource(stream));
assertTrue("Unexpected: " + retValue, retValue instanceof Source);
}
public void testEchoHandler() throws Exception
{
ExFileDataSource dataSource = new ExFileDataSource("resources/webservice/attachment/attach.xml", "text/xml");
DataHandler retValue = port.echoHandler(new DataHandler(dataSource));
String mimeType = retValue.getContentType();
assertEquals("Invalid mime-type: ", "text/xml", mimeType);
}
static class ExFileDataSource extends FileDataSource
{
private String contentType;
public ExFileDataSource(String source, String contentType)
{
super(source);
this.contentType = contentType;
}
public String getContentType()
{
return contentType;
}
}
}