1 22 package org.jboss.test.webservice.jbws801; 23 24 import java.io.IOException ; 25 import java.io.InputStream ; 26 import java.io.OutputStream ; 27 import java.net.MalformedURLException ; 28 import java.net.URL ; 29 import java.util.Arrays ; 30 import java.util.Iterator ; 31 32 import javax.activation.DataHandler ; 33 import javax.activation.DataSource ; 34 import javax.xml.soap.AttachmentPart ; 35 import javax.xml.soap.MessageFactory ; 36 import javax.xml.soap.Name ; 37 import javax.xml.soap.SOAPBody ; 38 import javax.xml.soap.SOAPConnection ; 39 import javax.xml.soap.SOAPConnectionFactory ; 40 import javax.xml.soap.SOAPElement ; 41 import javax.xml.soap.SOAPEnvelope ; 42 import javax.xml.soap.SOAPException ; 43 import javax.xml.soap.SOAPMessage ; 44 import javax.xml.soap.SOAPPart ; 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 NS_PREFIX = "ns1"; 53 private static final String NS_URI = "http://org.jboss.webservice/jbws801"; 54 private static final String CID_MIMEPART = "big"; 55 56 public JBWS801TestCase(String name) 57 { 58 super(name); 59 } 60 61 62 public static Test suite() throws Exception 63 { 64 return getDeploySetup(JBWS801TestCase.class, "ws4ee-jbws801.war"); 65 } 66 67 public void testLargeFile() throws Exception 68 { 69 if (isJBossWSAvailable()) 70 { 71 System.out.println("FIXME: JBWS-801"); 72 return; 73 } 74 long size = 600L * 1024L * 1024L; 75 76 String methodName = "sendLargeFile"; 77 SOAPMessage msg = setupMimeMessage(methodName); 78 DataHandler handler = new DataHandler (new GeneratorDataSource(size)); 79 addAttachmentPart(msg, handler); 80 sendAndValidateMimeMessage(methodName, msg, size); 81 } 82 83 private static class GeneratorDataSource implements DataSource 84 { 85 private long size; 86 87 public GeneratorDataSource(long size) 88 { 89 this.size = size; 90 } 91 92 public String getContentType() 93 { 94 return "application/octet-stream"; 95 } 96 97 public InputStream getInputStream() throws IOException 98 { 99 return new FakeInputStream(size); 100 } 101 102 public String getName() 103 { 104 return null; 105 } 106 107 public OutputStream getOutputStream() throws IOException 108 { 109 return null; 110 } 111 } 112 113 private static class FakeInputStream extends InputStream 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 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 138 { 139 if (size > 0) 140 { 141 size--; 142 return 1; 143 } 144 145 return -1; 146 } 147 } 148 149 private SOAPMessage setupMimeMessage(String rpcMethodName) 150 throws Exception 151 { 152 MessageFactory mf = MessageFactory.newInstance(); 153 154 SOAPMessage msg = mf.createMessage(); 156 157 SOAPPart sp = msg.getSOAPPart(); 159 160 SOAPEnvelope envelope = sp.getEnvelope(); 162 163 SOAPBody bdy = envelope.getBody(); 165 166 bdy.addBodyElement(envelope.createName(rpcMethodName, NS_PREFIX, NS_URI)); 168 169 return msg; 170 } 171 172 private void addAttachmentPart(SOAPMessage msg, DataHandler dataHandler) 173 { 174 AttachmentPart ap = msg.createAttachmentPart(dataHandler); 176 ap.setContentId(CID_MIMEPART); 177 178 msg.addAttachmentPart(ap); 180 } 181 182 184 private void sendAndValidateMimeMessage(String rpcMethodName, SOAPMessage msg, long count) 185 throws SOAPException , MalformedURLException 186 { 187 SOAPConnectionFactory conFactory = SOAPConnectionFactory.newInstance(); 188 SOAPConnection con = conFactory.createConnection(); 189 SOAPMessage resMessage = con.call(msg, new URL ("http://" + getServerHost() + ":8080/ws4ee-jbws801")); 190 SOAPBody soapBody = resMessage.getSOAPBody(); 191 SOAPEnvelope soapEnvelope = (SOAPEnvelope )soapBody.getParentElement(); 192 193 Name rpcName = soapEnvelope.createName(rpcMethodName + "Response", NS_PREFIX, NS_URI); 194 Iterator childElements = soapBody.getChildElements(rpcName); 195 assertTrue("Expexted child: " + rpcName, childElements.hasNext()); 196 197 SOAPElement bodyChild = (SOAPElement )childElements.next(); 198 Name resName = soapEnvelope.createName("result"); 199 SOAPElement resElement = (SOAPElement )bodyChild.getChildElements(resName).next(); 200 String value = resElement.getValue(); 201 202 assertEquals(count, Long.parseLong(value)); 203 } 204 } 205 | Popular Tags |