1 57 58 package docStyle; 59 60 import java.io.BufferedInputStream ; 61 import java.io.ByteArrayInputStream ; 62 import java.io.File ; 63 import java.io.FileInputStream ; 64 import java.io.FileNotFoundException ; 65 import java.io.IOException ; 66 import java.io.InputStream ; 67 import java.io.StringReader ; 68 import java.io.StringWriter ; 69 import java.util.Arrays ; 70 71 import javax.activation.DataHandler ; 72 import javax.activation.FileDataSource ; 73 import junit.framework.Test; 74 import junit.framework.TestCase; 75 import junit.framework.TestSuite; 76 77 import org.apache.wsif.WSIFMessage; 78 import org.apache.wsif.WSIFOperation; 79 import org.apache.wsif.WSIFPort; 80 import org.apache.wsif.WSIFService; 81 import org.apache.wsif.WSIFServiceFactory; 82 import org.apache.xerces.parsers.DOMParser; 83 import org.apache.xml.serialize.OutputFormat; 84 import org.apache.xml.serialize.XMLSerializer; 85 import org.w3c.dom.Element ; 86 import org.w3c.dom.Node ; 87 import org.w3c.dom.NodeList ; 88 import org.xml.sax.InputSource ; 89 import util.TestUtilities; 90 91 94 public class MessagingAttachmentsTest extends TestCase { 95 String wsdlLocation = 96 TestUtilities.getWsdlPath("java\\test\\docStyle\\wsifservice") 97 + "mime.wsdl"; 98 static String server = TestUtilities.getSoapServer().toUpperCase(); 99 100 private final static String IMAGE_LOCATION = 101 TestUtilities.getWsdlPath("java\\test\\mime") 102 + "axis.jpg"; 103 104 public MessagingAttachmentsTest(String name) { 105 super(name); 106 } 107 108 public static void main(String [] args) { 109 junit.textui.TestRunner.run(suite()); 114 } 116 117 public static Test suite() { 118 return new TestSuite(MessagingAttachmentsTest.class); 119 } 120 121 public void setUp() { 122 TestUtilities.setUpExtensionsAndProviders(); 123 } 124 125 public void testMsgAxisRcvAtt() { 126 doitMsgRcvAtt(server+"Port", "axis"); 127 } 128 public void testMsgAxisSendAtt() { 129 doitMsgSendAtt(server+"Port", "axis"); 130 } 131 132 private void doitMsgRcvAtt(String portName, String protocol) { 133 if (portName.toUpperCase().indexOf("JMS") != -1 134 && !TestUtilities.areWeTesting("jms")) 135 return; 136 137 TestUtilities.setProviderForProtocol(protocol); 138 139 try { 140 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 141 WSIFService service = 142 factory.getService( 143 wsdlLocation, 144 null, 145 null, 146 "http://mime/", 147 "Mime"); 148 149 WSIFPort port = service.getPort(portName); 150 WSIFOperation operation = port.createOperation("stringToDataHandler"); 151 WSIFMessage inMsg = operation.createInputMessage(); 152 WSIFMessage outMsg = operation.createOutputMessage(); 153 WSIFMessage faultMsg = operation.createFaultMessage(); 154 155 String content = "The owl and the pussy cat went to sea in a beautiful pea-green boat,"; 156 157 String inputDoc = 158 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 159 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + 160 " <soapenv:Body>" + 161 " <ns1:stringToDataHandler soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://mime/\">" + 162 " <ns1:buff xsi:type=\"xsd:string\">" + 163 content + 164 "</ns1:buff>" + 165 " </ns1:stringToDataHandler>" + 166 " </soapenv:Body>" + 167 "</soapenv:Envelope>"; 168 169 DOMParser parser = new DOMParser(); 170 171 parser.parse(new InputSource (new StringReader (inputDoc))); 172 Element element = parser.getDocument().getDocumentElement(); 173 NodeList l = element.getChildNodes(); 174 Element e1 = (Element )l.item(1); 175 l = e1.getChildNodes(); 176 element = (Element )l.item(1); 177 Node n = e1.getFirstChild(); 178 n = n.getNextSibling(); 179 element = (Element ) n; 180 182 inMsg.setObjectPart("buff", element); 183 184 boolean ok = 185 operation.executeRequestResponseOperation( 186 inMsg, 187 outMsg, 188 faultMsg); 189 assertTrue("operation returned false!!", ok); 190 191 Object o = outMsg.getObjectPart("return"); 192 assertTrue( 193 "return part not a DataHandler!!", 194 (o instanceof DataHandler )); 195 196 DataHandler dh = (DataHandler ) o; 197 assertTrue("response Datahandler content wrong!!", compareFiles(dh, content)); 198 199 } catch (Exception ex) { 200 ex.printStackTrace(); 201 assertTrue( 202 "AddressBookTest(" 203 + portName 204 + ") caught exception " 205 + ex.getLocalizedMessage(), 206 false); 207 } 208 } 209 210 private void doitMsgSendAtt(String portName, String protocol) { 211 if (portName.toUpperCase().indexOf("JMS") != -1 212 && !TestUtilities.areWeTesting("jms")) 213 return; 214 215 TestUtilities.setProviderForProtocol(protocol); 216 217 try { 218 WSIFServiceFactory factory = WSIFServiceFactory.newInstance(); 219 WSIFService service = 220 factory.getService( 221 wsdlLocation, 222 null, 223 null, 224 "http://mime/", 225 "Mime"); 226 227 WSIFPort port = service.getPort(portName); 228 WSIFOperation operation = port.createOperation("bounceImage4"); 229 WSIFMessage inMsg = operation.createInputMessage(); 230 WSIFMessage outMsg = operation.createOutputMessage(); 231 WSIFMessage faultMsg = operation.createFaultMessage(); 232 233 String inputDoc = 234 "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + 235 "<soapenv:Envelope xmlns:soapenv=\"http://schemas.xmlsoap.org/soap/envelope/\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\" xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">" + 236 " <soapenv:Body>" + 237 " <ns1:bounceImage4 soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://mime/\">" + 238 " <ns1:shouldBounce xsi:type=\"xsd:boolean\">true</ns1:shouldBounce>" + 239 " <ns1:file HREF=\"cid:413B07CE410E48EB9D89DC0A4DDD715D\"/>" + 240 " </ns1:bounceImage4>" + 241 " </soapenv:Body>" + 242 "</soapenv:Envelope>"; 243 244 DOMParser parser = new DOMParser(); 245 246 parser.parse(new InputSource (new StringReader (inputDoc))); 247 Element element = parser.getDocument().getDocumentElement(); 248 NodeList l = element.getChildNodes(); 249 Element e1 = (Element )l.item(1); 250 l = e1.getChildNodes(); 251 element = (Element )l.item(1); 252 Node n = e1.getFirstChild(); 253 n = n.getNextSibling(); 254 element = (Element ) n; 255 257 inMsg.setObjectPart("shouldBounce", element); 258 259 DataHandler dh1 = new DataHandler (new FileDataSource (IMAGE_LOCATION)); 260 inMsg.setObjectPart("file", dh1); 261 262 boolean ok = 263 operation.executeRequestResponseOperation( 264 inMsg, 265 outMsg, 266 faultMsg); 267 assertTrue("operation returned false!!", ok); 268 269 Object o = outMsg.getObjectPart("return"); 270 assertTrue( 271 "return part not a DataHandler!!", 272 (o instanceof DataHandler )); 273 274 DataHandler dhResponse = (DataHandler ) o; 275 assertTrue("response Datahandler content wrong!!", compareFiles(dhResponse, dh1)); 276 277 } catch (Exception ex) { 278 ex.printStackTrace(); 279 assertTrue( 280 "AddressBookTest(" 281 + portName 282 + ") caught exception " 283 + ex.getLocalizedMessage(), 284 false); 285 } 286 } 287 288 private void printElement(Element e) throws Exception { 289 OutputFormat of = new OutputFormat(e.getOwnerDocument(), "UTF-8", true); 290 XMLSerializer xmls = new XMLSerializer(of); 291 StringWriter sw = new StringWriter (); 292 xmls.setOutputCharStream(sw); 293 xmls.serialize(e); 294 System.err.println("element=" + sw.toString()); 295 } 296 297 private boolean compareFiles(DataHandler dh1, DataHandler dh2) 298 throws FileNotFoundException , IOException { 299 InputStream is1 = dh1.getInputStream(); 300 InputStream is2 = dh2.getInputStream(); 301 boolean success = false; 302 try { 303 success = compareFiles(is1, is2); 304 } finally { 305 if (null != is1) 306 is1.close(); 307 if (null != is2) 308 is2.close(); 309 } 310 return success; 311 } 312 313 private boolean compareFiles(DataHandler dh, String buff) 314 throws FileNotFoundException , IOException { 315 InputStream is = dh.getInputStream(); 316 boolean success = false; 317 try { 318 success = compareFiles(is, buff); 319 } finally { 320 if (null != is) 321 is.close(); 322 } 323 return success; 324 } 325 326 private boolean compareFiles(String one, String buff) 327 throws FileNotFoundException , IOException { 328 BufferedInputStream oneStream = null; 329 File f1 = new File (one); 330 331 boolean success = false; 332 try { 333 oneStream = 334 new BufferedInputStream ( 335 new FileInputStream (one), 336 buff.length()); 337 success = compareFiles(oneStream, buff); 338 } finally { 339 if (null != oneStream) 340 oneStream.close(); 341 } 342 return success; 343 } 344 345 private boolean compareFiles(InputStream is, String buff) 346 throws FileNotFoundException , IOException { 347 return compareFiles(is, new ByteArrayInputStream (buff.getBytes())); 348 } 349 350 private boolean compareFiles(InputStream is1, InputStream is2) 351 throws FileNotFoundException , IOException { 352 353 int avail1 = is1.available(); 354 int avail2 = is2.available(); 355 if (avail1 != avail2) return false; 356 if (avail1==0) return true; 357 358 byte[] buff1 = new byte[avail1]; 359 byte[] buff2 = new byte[avail2]; 360 361 Arrays.fill(buff1, (byte) 0); 362 Arrays.fill(buff2, (byte) 0); 363 364 int bread1 = -1; 365 int bread2 = -1; 366 bread1 = is1.read(buff1, 0, avail1); 367 bread2 = is2.read(buff2, 0, avail2); 368 String s1 = new String (buff1); 369 String s2 = new String (buff2); 370 if (!s1.equals(s2)) 371 return false; 372 return true; 373 } 374 375 private String concat(String one, String two) 376 throws FileNotFoundException , IOException { 377 InputStream is1 = null; 378 InputStream is2 = null; 379 try { 380 is1 = (new DataHandler (new FileDataSource (one))).getInputStream(); 381 is2 = (new DataHandler (new FileDataSource (two))).getInputStream(); 382 int avail1 = is1.available(); 383 int avail2 = is2.available(); 384 385 byte[] buff1 = new byte[avail1]; 386 byte[] buff2 = new byte[avail2]; 387 388 Arrays.fill(buff1, (byte) 0); 389 Arrays.fill(buff2, (byte) 0); 390 391 int bread1 = -1; 392 int bread2 = -1; 393 bread1 = is1.read(buff1, 0, avail1); 394 bread2 = is2.read(buff2, 0, avail2); 395 String s1 = new String (buff1); 396 String s2 = new String (buff2); 397 return s1 + s2; 398 399 } finally { 400 if (null != is1) 401 is1.close(); 402 if (null != is2) 403 is2.close(); 404 } 405 } 406 407 } 408 | Popular Tags |