001: /*
002: * The Apache Software License, Version 1.1
003: *
004: *
005: * Copyright (c) 2002 The Apache Software Foundation. All rights
006: * reserved.
007: *
008: * Redistribution and use in source and binary forms, with or without
009: * modification, are permitted provided that the following conditions
010: * are met:
011: *
012: * 1. Redistributions of source code must retain the above copyright
013: * notice, this list of conditions and the following disclaimer.
014: *
015: * 2. Redistributions in binary form must reproduce the above copyright
016: * notice, this list of conditions and the following disclaimer in
017: * the documentation and/or other materials provided with the
018: * distribution.
019: *
020: * 3. The end-user documentation included with the redistribution,
021: * if any, must include the following acknowledgment:
022: * "This product includes software developed by the
023: * Apache Software Foundation (http://www.apache.org/)."
024: * Alternately, this acknowledgment may appear in the software itself,
025: * if and wherever such third-party acknowledgments normally appear.
026: *
027: * 4. The names "WSIF" and "Apache Software Foundation" must
028: * not be used to endorse or promote products derived from this
029: * software without prior written permission. For written
030: * permission, please contact apache@apache.org.
031: *
032: * 5. Products derived from this software may not be called "Apache",
033: * nor may "Apache" appear in their name, without prior written
034: * permission of the Apache Software Foundation.
035: *
036: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
037: * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
038: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
039: * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
040: * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
041: * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
042: * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
043: * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
044: * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
045: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
046: * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
047: * SUCH DAMAGE.
048: * ====================================================================
049: *
050: * This software consists of voluntary contributions made by many
051: * individuals on behalf of the Apache Software Foundation and was
052: * originally based on software copyright (c) 2001, 2002, International
053: * Business Machines, Inc., http://www.apache.org. For more
054: * information on the Apache Software Foundation, please see
055: * <http://www.apache.org/>.
056: */
057:
058: package docStyle;
059:
060: import java.io.BufferedInputStream;
061: import java.io.ByteArrayInputStream;
062: import java.io.File;
063: import java.io.FileInputStream;
064: import java.io.FileNotFoundException;
065: import java.io.IOException;
066: import java.io.InputStream;
067: import java.io.StringReader;
068: import java.io.StringWriter;
069: import java.util.Arrays;
070:
071: import javax.activation.DataHandler;
072: import javax.activation.FileDataSource;
073: import junit.framework.Test;
074: import junit.framework.TestCase;
075: import junit.framework.TestSuite;
076:
077: import org.apache.wsif.WSIFMessage;
078: import org.apache.wsif.WSIFOperation;
079: import org.apache.wsif.WSIFPort;
080: import org.apache.wsif.WSIFService;
081: import org.apache.wsif.WSIFServiceFactory;
082: import org.apache.xerces.parsers.DOMParser;
083: import org.apache.xml.serialize.OutputFormat;
084: import org.apache.xml.serialize.XMLSerializer;
085: import org.w3c.dom.Element;
086: import org.w3c.dom.Node;
087: import org.w3c.dom.NodeList;
088: import org.xml.sax.InputSource;
089: import util.TestUtilities;
090:
091: /**
092: * Junit test to test out the AXIS provider docstyle support
093: */
094: public class MessagingAttachmentsTest extends TestCase {
095: String wsdlLocation = TestUtilities
096: .getWsdlPath("java\\test\\docStyle\\wsifservice")
097: + "mime.wsdl";
098: static String server = TestUtilities.getSoapServer().toUpperCase();
099:
100: private final static String IMAGE_LOCATION = TestUtilities
101: .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: // TestUtilities.startListeners(
110: // TestUtilities.ADDRESSBOOK_LISTENER
111: // | TestUtilities.ASYNC_LISTENER
112: // | TestUtilities.NATIVEJMS_LISTENER);
113: junit.textui.TestRunner.run(suite());
114: // TestUtilities.stopListeners();
115: }
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:
129: public void testMsgAxisSendAtt() {
130: doitMsgSendAtt(server + "Port", "axis");
131: }
132:
133: private void doitMsgRcvAtt(String portName, String protocol) {
134: if (portName.toUpperCase().indexOf("JMS") != -1
135: && !TestUtilities.areWeTesting("jms"))
136: return;
137:
138: TestUtilities.setProviderForProtocol(protocol);
139:
140: try {
141: WSIFServiceFactory factory = WSIFServiceFactory
142: .newInstance();
143: WSIFService service = factory.getService(wsdlLocation,
144: null, null, "http://mime/", "Mime");
145:
146: WSIFPort port = service.getPort(portName);
147: WSIFOperation operation = port
148: .createOperation("stringToDataHandler");
149: WSIFMessage inMsg = operation.createInputMessage();
150: WSIFMessage outMsg = operation.createOutputMessage();
151: WSIFMessage faultMsg = operation.createFaultMessage();
152:
153: String content = "The owl and the pussy cat went to sea in a beautiful pea-green boat,";
154:
155: String inputDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
156: + "<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\">"
157: + " <soapenv:Body>"
158: + " <ns1:stringToDataHandler soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://mime/\">"
159: + " <ns1:buff xsi:type=\"xsd:string\">"
160: + content
161: + "</ns1:buff>"
162: + " </ns1:stringToDataHandler>"
163: + " </soapenv:Body>" + "</soapenv:Envelope>";
164:
165: DOMParser parser = new DOMParser();
166:
167: parser.parse(new InputSource(new StringReader(inputDoc)));
168: Element element = parser.getDocument().getDocumentElement();
169: NodeList l = element.getChildNodes();
170: Element e1 = (Element) l.item(1);
171: l = e1.getChildNodes();
172: element = (Element) l.item(1);
173: Node n = e1.getFirstChild();
174: n = n.getNextSibling();
175: element = (Element) n;
176: // printElement(element);
177:
178: inMsg.setObjectPart("buff", element);
179:
180: boolean ok = operation.executeRequestResponseOperation(
181: inMsg, outMsg, faultMsg);
182: assertTrue("operation returned false!!", ok);
183:
184: Object o = outMsg.getObjectPart("return");
185: assertTrue("return part not a DataHandler!!",
186: (o instanceof DataHandler));
187:
188: DataHandler dh = (DataHandler) o;
189: assertTrue("response Datahandler content wrong!!",
190: compareFiles(dh, content));
191:
192: } catch (Exception ex) {
193: ex.printStackTrace();
194: assertTrue("AddressBookTest(" + portName
195: + ") caught exception " + ex.getLocalizedMessage(),
196: false);
197: }
198: }
199:
200: private void doitMsgSendAtt(String portName, String protocol) {
201: if (portName.toUpperCase().indexOf("JMS") != -1
202: && !TestUtilities.areWeTesting("jms"))
203: return;
204:
205: TestUtilities.setProviderForProtocol(protocol);
206:
207: try {
208: WSIFServiceFactory factory = WSIFServiceFactory
209: .newInstance();
210: WSIFService service = factory.getService(wsdlLocation,
211: null, null, "http://mime/", "Mime");
212:
213: WSIFPort port = service.getPort(portName);
214: WSIFOperation operation = port
215: .createOperation("bounceImage4");
216: WSIFMessage inMsg = operation.createInputMessage();
217: WSIFMessage outMsg = operation.createOutputMessage();
218: WSIFMessage faultMsg = operation.createFaultMessage();
219:
220: String inputDoc = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
221: + "<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\">"
222: + " <soapenv:Body>"
223: + " <ns1:bounceImage4 soapenv:encodingStyle=\"http://schemas.xmlsoap.org/soap/encoding/\" xmlns:ns1=\"http://mime/\">"
224: + " <ns1:shouldBounce xsi:type=\"xsd:boolean\">true</ns1:shouldBounce>"
225: + " <ns1:file href=\"cid:413B07CE410E48EB9D89DC0A4DDD715D\"/>"
226: + " </ns1:bounceImage4>"
227: + " </soapenv:Body>"
228: + "</soapenv:Envelope>";
229:
230: DOMParser parser = new DOMParser();
231:
232: parser.parse(new InputSource(new StringReader(inputDoc)));
233: Element element = parser.getDocument().getDocumentElement();
234: NodeList l = element.getChildNodes();
235: Element e1 = (Element) l.item(1);
236: l = e1.getChildNodes();
237: element = (Element) l.item(1);
238: Node n = e1.getFirstChild();
239: n = n.getNextSibling();
240: element = (Element) n;
241: // printElement(element);
242:
243: inMsg.setObjectPart("shouldBounce", element);
244:
245: DataHandler dh1 = new DataHandler(new FileDataSource(
246: IMAGE_LOCATION));
247: inMsg.setObjectPart("file", dh1);
248:
249: boolean ok = operation.executeRequestResponseOperation(
250: inMsg, outMsg, faultMsg);
251: assertTrue("operation returned false!!", ok);
252:
253: Object o = outMsg.getObjectPart("return");
254: assertTrue("return part not a DataHandler!!",
255: (o instanceof DataHandler));
256:
257: DataHandler dhResponse = (DataHandler) o;
258: assertTrue("response Datahandler content wrong!!",
259: compareFiles(dhResponse, dh1));
260:
261: } catch (Exception ex) {
262: ex.printStackTrace();
263: assertTrue("AddressBookTest(" + portName
264: + ") caught exception " + ex.getLocalizedMessage(),
265: false);
266: }
267: }
268:
269: private void printElement(Element e) throws Exception {
270: OutputFormat of = new OutputFormat(e.getOwnerDocument(),
271: "UTF-8", true);
272: XMLSerializer xmls = new XMLSerializer(of);
273: StringWriter sw = new StringWriter();
274: xmls.setOutputCharStream(sw);
275: xmls.serialize(e);
276: System.err.println("element=" + sw.toString());
277: }
278:
279: private boolean compareFiles(DataHandler dh1, DataHandler dh2)
280: throws FileNotFoundException, IOException {
281: InputStream is1 = dh1.getInputStream();
282: InputStream is2 = dh2.getInputStream();
283: boolean success = false;
284: try {
285: success = compareFiles(is1, is2);
286: } finally {
287: if (null != is1)
288: is1.close();
289: if (null != is2)
290: is2.close();
291: }
292: return success;
293: }
294:
295: private boolean compareFiles(DataHandler dh, String buff)
296: throws FileNotFoundException, IOException {
297: InputStream is = dh.getInputStream();
298: boolean success = false;
299: try {
300: success = compareFiles(is, buff);
301: } finally {
302: if (null != is)
303: is.close();
304: }
305: return success;
306: }
307:
308: private boolean compareFiles(String one, String buff)
309: throws FileNotFoundException, IOException {
310: BufferedInputStream oneStream = null;
311: File f1 = new File(one);
312:
313: boolean success = false;
314: try {
315: oneStream = new BufferedInputStream(
316: new FileInputStream(one), buff.length());
317: success = compareFiles(oneStream, buff);
318: } finally {
319: if (null != oneStream)
320: oneStream.close();
321: }
322: return success;
323: }
324:
325: private boolean compareFiles(InputStream is, String buff)
326: throws FileNotFoundException, IOException {
327: return compareFiles(is, new ByteArrayInputStream(buff
328: .getBytes()));
329: }
330:
331: private boolean compareFiles(InputStream is1, InputStream is2)
332: throws FileNotFoundException, IOException {
333:
334: int avail1 = is1.available();
335: int avail2 = is2.available();
336: if (avail1 != avail2)
337: return false;
338: if (avail1 == 0)
339: return true;
340:
341: byte[] buff1 = new byte[avail1];
342: byte[] buff2 = new byte[avail2];
343:
344: Arrays.fill(buff1, (byte) 0);
345: Arrays.fill(buff2, (byte) 0);
346:
347: int bread1 = -1;
348: int bread2 = -1;
349: bread1 = is1.read(buff1, 0, avail1);
350: bread2 = is2.read(buff2, 0, avail2);
351: String s1 = new String(buff1);
352: String s2 = new String(buff2);
353: if (!s1.equals(s2))
354: return false;
355: return true;
356: }
357:
358: private String concat(String one, String two)
359: throws FileNotFoundException, IOException {
360: InputStream is1 = null;
361: InputStream is2 = null;
362: try {
363: is1 = (new DataHandler(new FileDataSource(one)))
364: .getInputStream();
365: is2 = (new DataHandler(new FileDataSource(two)))
366: .getInputStream();
367: int avail1 = is1.available();
368: int avail2 = is2.available();
369:
370: byte[] buff1 = new byte[avail1];
371: byte[] buff2 = new byte[avail2];
372:
373: Arrays.fill(buff1, (byte) 0);
374: Arrays.fill(buff2, (byte) 0);
375:
376: int bread1 = -1;
377: int bread2 = -1;
378: bread1 = is1.read(buff1, 0, avail1);
379: bread2 = is2.read(buff2, 0, avail2);
380: String s1 = new String(buff1);
381: String s2 = new String(buff2);
382: return s1 + s2;
383:
384: } finally {
385: if (null != is1)
386: is1.close();
387: if (null != is2)
388: is2.close();
389: }
390: }
391:
392: }
|