KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.awt.Image JavaDoc;
25 import java.awt.Toolkit JavaDoc;
26 import java.io.File JavaDoc;
27 import java.net.URL JavaDoc;
28 import java.rmi.RemoteException JavaDoc;
29
30 import javax.activation.DataHandler JavaDoc;
31 import javax.activation.DataSource JavaDoc;
32 import javax.activation.FileDataSource JavaDoc;
33 import javax.mail.internet.MimeBodyPart JavaDoc;
34 import javax.mail.internet.MimeMultipart JavaDoc;
35 import javax.xml.namespace.QName JavaDoc;
36 import javax.xml.rpc.Call JavaDoc;
37 import javax.xml.rpc.ParameterMode JavaDoc;
38 import javax.xml.rpc.Service JavaDoc;
39 import javax.xml.rpc.ServiceFactory JavaDoc;
40
41 import junit.framework.Test;
42
43 import org.jboss.test.webservice.WebserviceTestBase;
44
45 /**
46  * Test SOAP with Attachements (SwA) through the JAXRPC DII layer.
47  *
48  * [ 1039881 ] Cannot create multipart/mixed SOAP attachment
49  *
50  * @author Thomas.Diesler@jboss.org
51  * @since Nov 16, 2004
52  */

53 public class AttachmentDIITestCase extends WebserviceTestBase
54 {
55    private static final String JavaDoc NS_URI = "http://org.jboss.webservice/attachment";
56    private static final QName JavaDoc SERVICE_NAME = new QName JavaDoc(NS_URI, "Attachment");
57    private static final QName JavaDoc PORT_NAME = new QName JavaDoc(NS_URI, "AttachmentPort");
58
59    private static final QName JavaDoc XSD_STRING = new QName JavaDoc("http://www.w3.org/2001/XMLSchema", "string");
60    private static final QName JavaDoc AXIS_MIME_IMAGE = new QName JavaDoc("http://xml.apache.org/xml-soap", "Image");
61    private static final QName JavaDoc AXIS_MIME_PLAINTEXT = new QName JavaDoc("http://xml.apache.org/xml-soap", "PlainText");
62    private static final QName JavaDoc AXIS_MIME_MULTIPART = new QName JavaDoc("http://xml.apache.org/xml-soap", "Multipart");
63    private static final QName JavaDoc AXIS_MIME_SOURCE = new QName JavaDoc("http://xml.apache.org/xml-soap", "Source");
64
65    private static final String JavaDoc JBOSS_MIME_NS = "http://www.jboss.org/jbossws/attachment/mimetype";
66    private static final QName JavaDoc JBOSS_MIME_IMAGE_JPEG = new QName JavaDoc(JBOSS_MIME_NS, "image_jpeg");
67    private static final QName JavaDoc JBOSS_MIME_IMAGE_GIF = new QName JavaDoc(JBOSS_MIME_NS, "image_gif");
68    private static final QName JavaDoc JBOSS_MIME_MULTIPART_MIXED = new QName JavaDoc(JBOSS_MIME_NS, "multipart_mixed");
69    private static final QName JavaDoc JBOSS_MIME_TEXT_PLAIN = new QName JavaDoc(JBOSS_MIME_NS, "text_plain");
70    private static final QName JavaDoc JBOSS_MIME_TEXT_XML = new QName JavaDoc(JBOSS_MIME_NS, "text_xml");
71    private static final QName JavaDoc JBOSS_MIME_APPLICATION_XML = new QName JavaDoc(JBOSS_MIME_NS, "application_xml");
72
73    private final QName JavaDoc MIME_IMAGE_JPEG;
74    private final QName JavaDoc MIME_IMAGE_GIF;
75    private final QName JavaDoc MIME_MULTIPART_MIXED;
76    private final QName JavaDoc MIME_TEXT_PLAIN;
77    private final QName JavaDoc MIME_TEXT_XML;
78    private final QName JavaDoc MIME_APPLICATION_XML;
79
80    public AttachmentDIITestCase(String JavaDoc name)
81    {
82       super(name);
83
84       if (isWS4EEAvailable())
85       {
86          MIME_IMAGE_GIF = AXIS_MIME_IMAGE;
87          MIME_IMAGE_JPEG = AXIS_MIME_IMAGE;
88          MIME_MULTIPART_MIXED = AXIS_MIME_MULTIPART;
89          MIME_TEXT_PLAIN = AXIS_MIME_PLAINTEXT;
90          MIME_TEXT_XML = AXIS_MIME_SOURCE;
91          MIME_APPLICATION_XML = AXIS_MIME_SOURCE;
92       }
93       else
94       {
95          MIME_IMAGE_GIF = JBOSS_MIME_IMAGE_GIF;
96          MIME_IMAGE_JPEG = JBOSS_MIME_IMAGE_JPEG;
97          MIME_MULTIPART_MIXED = JBOSS_MIME_MULTIPART_MIXED;
98          MIME_TEXT_PLAIN = JBOSS_MIME_TEXT_PLAIN;
99          MIME_TEXT_XML = JBOSS_MIME_TEXT_XML;
100          MIME_APPLICATION_XML = JBOSS_MIME_APPLICATION_XML;
101       }
102    }
103
104    /** Deploy the test ear */
105    public static Test suite() throws Exception JavaDoc
106    {
107       return getDeploySetup(AttachmentDIITestCase.class, "ws4ee-attachment.war");
108    }
109
110    /** Send a multipart message with a text/plain attachment part
111     */

112    public void testSendMimeImageGIF() throws Exception JavaDoc
113    {
114       String JavaDoc rpcMethodName = "sendMimeImageGIF";
115       Call JavaDoc call = setupMimeMessage(rpcMethodName, "image/gif");
116
117       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.gif").toURL();
118
119       // On Linux the X11 server must be installed properly to create images successfully.
120
// If the image cannot be created in the test VM, we assume it cannot be done on the
121
// server either, so we just skip the test
122
Image JavaDoc image = null;
123       try
124       {
125          image = Toolkit.getDefaultToolkit().createImage(url);
126       }
127       catch (Throwable JavaDoc th)
128       {
129          log.warn("Cannot create Image: " + th);
130       }
131
132       if (image != null)
133       {
134          sendAndValidateMimeMessage(call, new DataHandler JavaDoc(url));
135       }
136    }
137
138    /** Send a multipart message with a text/plain attachment part
139     */

140    public void testSendMimeImageJPEG() throws Exception JavaDoc
141    {
142       String JavaDoc rpcMethodName = "sendMimeImageJPEG";
143       Call JavaDoc call = setupMimeMessage(rpcMethodName, "image/jpeg");
144
145       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.jpeg").toURL();
146
147       // On Linux the X11 server must be installed properly to create images successfully.
148
// If the image cannot be created in the test VM, we assume it cannot be done on the
149
// server either, so we just skip the test
150
Image JavaDoc image = null;
151       try
152       {
153          image = Toolkit.getDefaultToolkit().createImage(url);
154       }
155       catch (Throwable JavaDoc th)
156       {
157          log.warn("Cannot create Image: " + th);
158       }
159
160       if (image != null)
161       {
162          sendAndValidateMimeMessage(call, new DataHandler JavaDoc(url));
163       }
164    }
165
166    /** Send a multipart message with a text/plain attachment part
167     */

168    public void testSendMimeTextPlain() throws Exception JavaDoc
169    {
170       String JavaDoc rpcMethodName = "sendMimeTextPlain";
171       Call JavaDoc call = setupMimeMessage(rpcMethodName, "text/plain");
172
173       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.txt").toURL();
174       sendAndValidateMimeMessage(call, new DataHandler JavaDoc(url));
175    }
176
177    /** Send a multipart message with a text/plain attachment part
178     */

179    public void testSendMimeMultipart() throws Exception JavaDoc
180    {
181       String JavaDoc rpcMethodName = "sendMimeMultipart";
182       Call JavaDoc call = setupMimeMessage(rpcMethodName, "multipart/mixed");
183
184       URL JavaDoc url = new File JavaDoc("resources/webservice/attachment/attach.txt").toURL();
185       MimeMultipart JavaDoc mimepart = new MimeMultipart JavaDoc("mixed");
186       MimeBodyPart JavaDoc bodyPart = new MimeBodyPart JavaDoc();
187       bodyPart.setDataHandler(new DataHandler JavaDoc(url));
188       String JavaDoc bpct = bodyPart.getContentType();
189       bodyPart.setHeader("Content-Type", bpct);
190       mimepart.addBodyPart(bodyPart);
191
192       sendAndValidateMimeMessage(call, mimepart);
193    }
194
195    /** Send a multipart message with a text/plain attachment part
196     */

197    public void testSendMimeTextXML() throws Exception JavaDoc
198    {
199       String JavaDoc rpcMethodName = "sendMimeTextXML";
200       Call JavaDoc call = setupMimeMessage(rpcMethodName, "text/xml");
201
202       DataSource JavaDoc ds = new ExFileDataSource("resources/webservice/attachment/attach.xml", "text/xml");
203       sendAndValidateMimeMessage(call, new DataHandler JavaDoc(ds));
204    }
205
206    /** Send a multipart message with a text/plain attachment part
207     */

208    public void testSendMimeApplicationXML() throws Exception JavaDoc
209    {
210       String JavaDoc rpcMethodName = "sendMimeApplicationXML";
211       Call JavaDoc call = setupMimeMessage(rpcMethodName, "application/xml");
212
213       DataSource JavaDoc ds = new ExFileDataSource("resources/webservice/attachment/attach.xml", "application/xml");
214       sendAndValidateMimeMessage(call, new DataHandler JavaDoc(ds));
215    }
216
217    /** Setup the multipart/related MIME message
218     */

219    private Call JavaDoc setupMimeMessage(String JavaDoc rpcMethodName, String JavaDoc contentType)
220            throws Exception JavaDoc
221    {
222       ServiceFactory JavaDoc factory = ServiceFactory.newInstance();
223       Service JavaDoc service = factory.createService(SERVICE_NAME);
224
225       Call JavaDoc call = service.createCall();
226       call.setOperationName(new QName JavaDoc(NS_URI, rpcMethodName));
227       call.addParameter("message", XSD_STRING, ParameterMode.IN);
228
229       if (contentType.equals("image/gif"))
230          call.addParameter("mimepart", MIME_IMAGE_GIF, ParameterMode.IN);
231       else if (contentType.equals("image/jpeg"))
232          call.addParameter("mimepart", MIME_IMAGE_JPEG, ParameterMode.IN);
233       else if (contentType.equals("text/plain"))
234          call.addParameter("mimepart", MIME_TEXT_PLAIN, ParameterMode.IN);
235       else if (contentType.startsWith("multipart/"))
236          call.addParameter("mimepart", MIME_MULTIPART_MIXED, ParameterMode.IN);
237       else if (contentType.equals("text/xml"))
238          call.addParameter("mimepart", MIME_TEXT_XML, ParameterMode.IN);
239       else if (contentType.equals("application/xml"))
240          call.addParameter("mimepart", MIME_APPLICATION_XML, ParameterMode.IN);
241
242       call.setReturnType(XSD_STRING);
243
244       call.setTargetEndpointAddress("http://" + getServerHost() + ":8080/ws4ee-attachment");
245
246       return call;
247    }
248
249    /** Send the message and validate the result
250     */

251    private void sendAndValidateMimeMessage(Call JavaDoc call, Object JavaDoc mimepart)
252            throws RemoteException JavaDoc
253    {
254       String JavaDoc message = "Some text message";
255       String JavaDoc value = (String JavaDoc)call.invoke(new Object JavaDoc[]{message, mimepart});
256       
257       assertEquals("[pass]", value);
258    }
259
260    static class ExFileDataSource extends FileDataSource JavaDoc
261    {
262       private String JavaDoc contentType;
263
264       public ExFileDataSource(String JavaDoc source, String JavaDoc contentType)
265       {
266          super(source);
267          this.contentType = contentType;
268       }
269
270       public String JavaDoc getContentType()
271       {
272          return contentType;
273       }
274    }
275 }
276
Popular Tags