KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > webservice > jbws801 > JBWS801TestCase


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.jbws801;
23
24 import java.io.IOException JavaDoc;
25 import java.io.InputStream JavaDoc;
26 import java.io.OutputStream JavaDoc;
27 import java.net.MalformedURLException JavaDoc;
28 import java.net.URL JavaDoc;
29 import java.util.Arrays JavaDoc;
30 import java.util.Iterator JavaDoc;
31
32 import javax.activation.DataHandler JavaDoc;
33 import javax.activation.DataSource JavaDoc;
34 import javax.xml.soap.AttachmentPart JavaDoc;
35 import javax.xml.soap.MessageFactory JavaDoc;
36 import javax.xml.soap.Name JavaDoc;
37 import javax.xml.soap.SOAPBody JavaDoc;
38 import javax.xml.soap.SOAPConnection JavaDoc;
39 import javax.xml.soap.SOAPConnectionFactory JavaDoc;
40 import javax.xml.soap.SOAPElement JavaDoc;
41 import javax.xml.soap.SOAPEnvelope JavaDoc;
42 import javax.xml.soap.SOAPException JavaDoc;
43 import javax.xml.soap.SOAPMessage JavaDoc;
44 import javax.xml.soap.SOAPPart JavaDoc;
45
46 import junit.framework.Test;
47
48 import org.jboss.test.webservice.WebserviceTestBase;
49
50 public class JBWS801TestCase extends WebserviceTestBase
51 {
52    private static final String JavaDoc NS_PREFIX = "ns1";
53    private static final String JavaDoc NS_URI = "http://org.jboss.webservice/jbws801";
54    private static final String JavaDoc CID_MIMEPART = "big";
55
56    public JBWS801TestCase(String JavaDoc name)
57    {
58       super(name);
59    }
60
61    /** Deploy the test ear */
62    public static Test suite() throws Exception JavaDoc
63    {
64       return getDeploySetup(JBWS801TestCase.class, "ws4ee-jbws801.war");
65    }
66
67    public void testLargeFile() throws Exception JavaDoc
68    {
69       if (isJBossWSAvailable())
70       {
71          System.out.println("FIXME: JBWS-801");
72          return;
73       }
74       long size = 600L * 1024L * 1024L;
75
76       String JavaDoc methodName = "sendLargeFile";
77       SOAPMessage JavaDoc msg = setupMimeMessage(methodName);
78       DataHandler JavaDoc handler = new DataHandler JavaDoc(new GeneratorDataSource(size));
79       addAttachmentPart(msg, handler);
80       sendAndValidateMimeMessage(methodName, msg, size);
81    }
82
83    private static class GeneratorDataSource implements DataSource JavaDoc
84    {
85       private long size;
86
87       public GeneratorDataSource(long size)
88       {
89          this.size = size;
90       }
91
92       public String JavaDoc getContentType()
93       {
94          return "application/octet-stream";
95       }
96
97       public InputStream JavaDoc getInputStream() throws IOException JavaDoc
98       {
99          return new FakeInputStream(size);
100       }
101
102       public String JavaDoc getName()
103       {
104          return null;
105       }
106
107       public OutputStream JavaDoc getOutputStream() throws IOException JavaDoc
108       {
109          return null;
110       }
111    }
112
113    private static class FakeInputStream extends InputStream JavaDoc
114    {
115       private long size;
116
117       public FakeInputStream(long size)
118       {
119          this.size = size;
120       }
121
122       public int read(byte[] b, int off, int len) throws IOException JavaDoc
123       {
124          if (len < 1)
125             return 0;
126
127          if (size == 0)
128             return -1;
129
130          int ret = (int) Math.min(size, len);
131          Arrays.fill(b, off, off + ret, (byte)1);
132          size -= ret;
133
134          return ret;
135       }
136
137       public int read() throws IOException JavaDoc
138       {
139          if (size > 0)
140          {
141             size--;
142             return 1;
143          }
144
145          return -1;
146       }
147    }
148
149    private SOAPMessage JavaDoc setupMimeMessage(String JavaDoc rpcMethodName)
150       throws Exception JavaDoc
151    {
152       MessageFactory JavaDoc mf = MessageFactory.newInstance();
153
154       // Create a soap message from the message factory.
155
SOAPMessage JavaDoc msg = mf.createMessage();
156
157       // Message creation takes care of creating the SOAPPart - a required part of the message as per the SOAP 1.1 spec.
158
SOAPPart JavaDoc sp = msg.getSOAPPart();
159
160       // Retrieve the envelope from the soap part to start building the soap message.
161
SOAPEnvelope JavaDoc envelope = sp.getEnvelope();
162
163       // Create a soap body from the envelope.
164
SOAPBody JavaDoc bdy = envelope.getBody();
165
166       // Add a soap body element
167
bdy.addBodyElement(envelope.createName(rpcMethodName, NS_PREFIX, NS_URI));
168
169       return msg;
170    }
171
172    private void addAttachmentPart(SOAPMessage JavaDoc msg, DataHandler JavaDoc dataHandler)
173    {
174       // Create the attachment part
175
AttachmentPart JavaDoc ap = msg.createAttachmentPart(dataHandler);
176       ap.setContentId(CID_MIMEPART);
177
178       // Add the attachments to the message.
179
msg.addAttachmentPart(ap);
180    }
181
182    /** Send the message and validate the result
183     */

184    private void sendAndValidateMimeMessage(String JavaDoc rpcMethodName, SOAPMessage JavaDoc msg, long count)
185       throws SOAPException JavaDoc, MalformedURLException JavaDoc
186    {
187       SOAPConnectionFactory JavaDoc conFactory = SOAPConnectionFactory.newInstance();
188       SOAPConnection JavaDoc con = conFactory.createConnection();
189       SOAPMessage JavaDoc resMessage = con.call(msg, new URL JavaDoc("http://" + getServerHost() + ":8080/ws4ee-jbws801"));
190       SOAPBody JavaDoc soapBody = resMessage.getSOAPBody();
191       SOAPEnvelope JavaDoc soapEnvelope = (SOAPEnvelope JavaDoc)soapBody.getParentElement();
192
193       Name JavaDoc rpcName = soapEnvelope.createName(rpcMethodName + "Response", NS_PREFIX, NS_URI);
194       Iterator JavaDoc childElements = soapBody.getChildElements(rpcName);
195       assertTrue("Expexted child: " + rpcName, childElements.hasNext());
196
197       SOAPElement JavaDoc bodyChild = (SOAPElement JavaDoc)childElements.next();
198       Name JavaDoc resName = soapEnvelope.createName("result");
199       SOAPElement JavaDoc resElement = (SOAPElement JavaDoc)bodyChild.getChildElements(resName).next();
200       String JavaDoc value = resElement.getValue();
201
202       assertEquals(count, Long.parseLong(value));
203    }
204 }
205
Popular Tags