KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > attachment > AttachmentProxyTestCase


1 /*
2   * JBoss, Home of Professional Open Source
3   * Copyright 2005, JBoss Inc., and individual contributors as indicated
4   * by the @authors tag. See the copyright.txt in the distribution for a
5   * full listing of individual contributors.
6   *
7   * This is free software; you can redistribute it and/or modify it
8   * under the terms of the GNU Lesser General Public License as
9   * published by the Free Software Foundation; either version 2.1 of
10   * the License, or (at your option) any later version.
11   *
12   * This software is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15   * Lesser General Public License for more details.
16   *
17   * You should have received a copy of the GNU Lesser General Public
18   * License along with this software; if not, write to the Free
19   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
20   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
21   */

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 JavaDoc;
28 import javax.activation.FileDataSource JavaDoc;
29 import javax.mail.internet.MimeBodyPart JavaDoc;
30 import javax.mail.internet.MimeMultipart JavaDoc;
31 import javax.naming.InitialContext JavaDoc;
32 import javax.xml.rpc.Service JavaDoc;
33 import javax.xml.transform.Source JavaDoc;
34 import javax.xml.transform.stream.StreamSource JavaDoc;
35 import java.awt.*;
36 import java.io.File JavaDoc;
37 import java.io.FileInputStream JavaDoc;
38 import java.net.URL JavaDoc;
39
40 /**
41  * Test SOAP with Attachements (SwA) through the JAXRPC dynamic proxy layer.
42  *
43  * [ 1039881 ] Cannot create multipart/mixed SOAP attachment
44  *
45  * @author Thomas.Diesler@jboss.org
46  * @since 16-Nov-2004
47  */

48 public class AttachmentProxyTestCase extends WebserviceTestBase
49 {
50    private static Attachment port;
51
52    public AttachmentProxyTestCase(String JavaDoc name)
53    {
54       super(name);
55    }
56
57    /** Deploy the test ear */
58    public static Test suite() throws Exception JavaDoc
59    {
60       return getDeploySetup(AttachmentProxyTestCase.class, "ws4ee-attachment.war, ws4ee-attachment-client.jar");
61    }
62
63    protected void setUp() throws Exception JavaDoc
64    {
65       if (port == null)
66       {
67          InitialContext JavaDoc iniCtx = getClientContext();
68          Service JavaDoc service = (Service JavaDoc)iniCtx.lookup("java:comp/env/service/AttachmentService");
69          port = (Attachment)service.getPort(Attachment.class);
70       }
71    }
72
73    public void testSendMimeImageGIF() throws Exception JavaDoc
74    {
75       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.gif").toURL();
76
77       // On Linux the X11 server must be installed properly to create images successfully.
78
// If the image cannot be created in the test VM, we assume it cannot be done on the
79
// server either, so we just skip the test
80
Image image = null;
81       try
82       {
83          image = Toolkit.getDefaultToolkit().createImage(url);
84       }
85       catch (Throwable JavaDoc th)
86       {
87          log.warn("Cannot create Image: " + th);
88       }
89
90       if (image != null)
91       {
92          String JavaDoc value = port.sendMimeImageGIF("Some text message", new DataHandler JavaDoc(url));
93          assertEquals("[pass]", value);
94       }
95    }
96
97    public void testSendMimeImageJPEG() throws Exception JavaDoc
98    {
99       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.jpeg").toURL();
100
101       // On Linux the X11 server must be installed properly to create images successfully.
102
// If the image cannot be created in the test VM, we assume it cannot be done on the
103
// server either, so we just skip the test
104
Image image = null;
105       try
106       {
107          image = Toolkit.getDefaultToolkit().createImage(url);
108       }
109       catch (Throwable JavaDoc th)
110       {
111          log.warn("Cannot create Image: " + th);
112       }
113
114       if (image != null)
115       {
116          String JavaDoc value = port.sendMimeImageJPEG("Some text message", new DataHandler JavaDoc(url));
117          assertEquals("[pass]", value);
118       }
119    }
120
121    public void testSendMimeTextPlain() throws Exception JavaDoc
122    {
123       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.txt").toURL();
124       String JavaDoc value = port.sendMimeTextPlain("Some text message", new DataHandler JavaDoc(url));
125       assertEquals("[pass]", value);
126    }
127
128    public void testSendMimeMultipart() throws Exception JavaDoc
129    {
130       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.txt").toURL();
131       MimeMultipart JavaDoc multipart = new MimeMultipart JavaDoc("mixed");
132       MimeBodyPart JavaDoc bodyPart = new MimeBodyPart JavaDoc();
133       bodyPart.setDataHandler(new DataHandler JavaDoc(url));
134       String JavaDoc bpct = bodyPart.getContentType();
135       bodyPart.setHeader("Content-Type", bpct);
136       multipart.addBodyPart(bodyPart);
137
138       String JavaDoc value = port.sendMimeMultipart("Some text message", multipart);
139       assertEquals("[pass]", value);
140    }
141
142    public void testSendMimeTextXML() throws Exception JavaDoc
143    {
144       // URLDataSource uses the JVM /lib/content-types.properties,
145
// which defines all xml files as application/xml.
146
ExFileDataSource dataSource = new ExFileDataSource("resources/webservice/attachment/attach.xml", "text/xml");
147       String JavaDoc value = port.sendMimeTextXML("Some text message", new DataHandler JavaDoc(dataSource));
148       assertEquals("[pass]", value);
149    }
150
151    public void testSendMimeApplicationXML() throws Exception JavaDoc
152    {
153       ExFileDataSource dataSource = new ExFileDataSource("resources/webservice/attachment/attach.xml", "application/xml");
154       String JavaDoc value = port.sendMimeApplicationXML("Some text message", new DataHandler JavaDoc(dataSource));
155       assertEquals("[pass]", value);
156    }
157
158    /* Test JPEG return values.
159     *
160     * Note that we do not test echoing a GIF because the SUN JVM does not support
161     * outputing GIF files because of patent issues
162     */

163    public void testEchoMimeImageJPEG() throws Exception JavaDoc
164    {
165       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.jpeg").toURL();
166
167       // On Linux the X11 server must be installed properly to create images successfully.
168
// If the image cannot be created in the test VM, we assume it cannot be done on the
169
// server either, so we just skip the test
170
Image image = null;
171       try
172       {
173          image = Toolkit.getDefaultToolkit().createImage(url);
174       }
175       catch (Throwable JavaDoc th)
176       {
177          log.warn("Cannot create Image: " + th);
178       }
179
180       if (image != null)
181       {
182          Object JavaDoc retValue = port.echoMimeImageJPEG(new DataHandler JavaDoc(url));
183          assertTrue("Unexpected: " + retValue, retValue instanceof Image);
184       }
185    }
186
187    public void testEchoMimeTextPlain() throws Exception JavaDoc
188    {
189       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.txt").toURL();
190       Object JavaDoc retValue = port.echoMimeTextPlain(new DataHandler JavaDoc(url));
191       assertTrue("Unexpected: " + retValue, retValue instanceof String JavaDoc);
192       assertEquals("This is a plain text attachment.", ((String JavaDoc)retValue).trim());
193    }
194
195    public void testEchoMimeMultipart() throws Exception JavaDoc
196    {
197       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.txt").toURL();
198       MimeMultipart JavaDoc multipart = new MimeMultipart JavaDoc("mixed");
199       MimeBodyPart JavaDoc bodyPart = new MimeBodyPart JavaDoc();
200       bodyPart.setDataHandler(new DataHandler JavaDoc(url));
201       String JavaDoc bpct = bodyPart.getContentType();
202       bodyPart.setHeader("Content-Type", bpct);
203       multipart.addBodyPart(bodyPart);
204
205       Object JavaDoc retValue = port.echoMimeMultipart(multipart);
206       assertTrue("Unexpected: " + retValue, retValue instanceof MimeMultipart JavaDoc);
207    }
208
209    public void testEchoMimeTextXML() throws Exception JavaDoc
210    {
211       ExFileDataSource dataSource = new ExFileDataSource("resources/webservice/attachment/attach.xml", "text/xml");
212       Object JavaDoc retValue = port.echoMimeTextXML(new DataHandler JavaDoc(dataSource));
213       assertTrue("Unexpected: " + retValue, retValue instanceof Source JavaDoc);
214    }
215
216    /** Send a multipart message with a text/plain attachment part
217     */

218    public void testEchoMimeApplicationXML() throws Exception JavaDoc
219    {
220       FileInputStream JavaDoc stream = new FileInputStream JavaDoc("resources/webservice/attachment/attach.xml");
221       Object JavaDoc retValue = port.echoMimeApplicationXML(new StreamSource JavaDoc(stream));
222       assertTrue("Unexpected: " + retValue, retValue instanceof Source JavaDoc);
223    }
224
225    public void testEchoHandler() throws Exception JavaDoc
226    {
227       ExFileDataSource dataSource = new ExFileDataSource("resources/webservice/attachment/attach.xml", "text/xml");
228       DataHandler JavaDoc retValue = port.echoHandler(new DataHandler JavaDoc(dataSource));
229       String JavaDoc mimeType = retValue.getContentType();
230       assertEquals("Invalid mime-type: ", "text/xml", mimeType);
231    }
232
233    static class ExFileDataSource extends FileDataSource JavaDoc
234    {
235       private String JavaDoc contentType;
236
237       public ExFileDataSource(String JavaDoc source, String JavaDoc contentType)
238       {
239          super(source);
240          this.contentType = contentType;
241       }
242
243       public String JavaDoc getContentType()
244       {
245          return contentType;
246       }
247    }
248 }
Popular Tags