KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jms > JmsFaultTest


1 /*
2  * The Apache Software License, Version 1.1
3  *
4  *
5  * Copyright (c) 2002 The Apache Software Foundation. All rights
6  * reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  * notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  * notice, this list of conditions and the following disclaimer in
17  * the documentation and/or other materials provided with the
18  * distribution.
19  *
20  * 3. The end-user documentation included with the redistribution,
21  * if any, must include the following acknowledgment:
22  * "This product includes software developed by the
23  * Apache Software Foundation (http://www.apache.org/)."
24  * Alternately, this acknowledgment may appear in the software itself,
25  * if and wherever such third-party acknowledgments normally appear.
26  *
27  * 4. The names "WSIF" and "Apache Software Foundation" must
28  * not be used to endorse or promote products derived from this
29  * software without prior written permission. For written
30  * permission, please contact apache@apache.org.
31  *
32  * 5. Products derived from this software may not be called "Apache",
33  * nor may "Apache" appear in their name, without prior written
34  * permission of the Apache Software Foundation.
35  *
36  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
37  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
38  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
39  * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
40  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
42  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
43  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
44  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
45  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
46  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  * ====================================================================
49  *
50  * This software consists of voluntary contributions made by many
51  * individuals on behalf of the Apache Software Foundation and was
52  * originally based on software copyright (c) 2001, 2002, International
53  * Business Machines, Inc., http://www.apache.org. For more
54  * information on the Apache Software Foundation, please see
55  * <http://www.apache.org/>.
56  */

57
58 package jms;
59
60 import java.util.Iterator JavaDoc;
61
62 import junit.framework.Test;
63 import junit.framework.TestCase;
64 import junit.framework.TestSuite;
65
66 import org.apache.wsif.WSIFConstants;
67 import org.apache.wsif.WSIFCorrelationId;
68 import org.apache.wsif.WSIFException;
69 import org.apache.wsif.WSIFMessage;
70 import org.apache.wsif.WSIFOperation;
71 import org.apache.wsif.WSIFPort;
72 import org.apache.wsif.WSIFService;
73 import org.apache.wsif.WSIFServiceFactory;
74 import util.TestUtilities;
75
76 /**
77  * Junit test to do various JMS tests
78  * @author Mark Whitlock
79  */

80 public class JmsFaultTest extends TestCase {
81     String JavaDoc wsdlLocation = TestUtilities.getWsdlPath("java\\test\\jms") + "jmsfault.wsdl";
82
83     private static final boolean SYNC = true;
84     private static final boolean ASYNC = false;
85
86     public JmsFaultTest(String JavaDoc name) {
87         super(name);
88     }
89
90     public static void main(String JavaDoc[] args) {
91        TestUtilities.startListeners(TestUtilities.NATIVEJMS_LISTENER);
92        junit.textui.TestRunner.run (suite());
93        TestUtilities.stopListeners();
94     }
95
96     public static Test suite() {
97         return new TestSuite(JmsFaultTest.class);
98     }
99
100     public void setUp() {
101         TestUtilities.setUpExtensionsAndProviders();
102     }
103
104     public void testNoFaultSync() {
105         doit("throwSimple", 0, false, false, null, null, SYNC);
106     }
107
108     public void testSimpleSync() {
109         doit(
110             "throwSimple",
111             1,
112             true,
113             false,
114             new String JavaDoc[] { "faultText" },
115             new Object JavaDoc[] { "A Simple Fault" }, SYNC);
116     }
117
118     public void testIntsSync() {
119         doit(
120             "throwSimple",
121             2,
122             true,
123             false,
124             new String JavaDoc[] { "faultInt1", "faultInt2", "faultInt3" },
125             new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3)}, SYNC);
126     }
127
128     public void testTwoIntsSync() {
129         doit(
130             "throwSimple",
131             3,
132             true,
133             false,
134             new String JavaDoc[] { "faultInt1", "faultInt2" },
135             new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2) }, SYNC);
136     }
137
138     public void testEmptyJmsFaultSync() {
139         doit(
140             "throwSimple",
141             2,
142             true,
143             false,
144             new String JavaDoc[] { "faultInt1", "faultInt2", "faultInt3" },
145             new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3)}, SYNC);
146     }
147
148     public void testIndicIntSync() {
149         doit(
150             "throwIndicInt",
151             4,
152             true,
153             false,
154             new String JavaDoc[] { "faultText" },
155             new Object JavaDoc[] { "A Simple Fault" }, SYNC);
156     }
157
158     public void testNoFaultAltSync() {
159         doit("throwSimple", 5, false, false, null, null, SYNC);
160     }
161
162     public void testFaultIndicPropSync() {
163         doit(
164             "throwProperties",
165             6,
166             true,
167             false,
168             new String JavaDoc[] { "faultText", "faultIndic" },
169             new Object JavaDoc[] { "A Fault Indicator", new Byte JavaDoc((byte) - 1)}, SYNC);
170     }
171
172     public void testJmsPropFaultSync() {
173         doit(
174             "throwProperties",
175             7,
176             true,
177             false,
178             new String JavaDoc[] { "faultText", "faultIndic", "faultProp" },
179             new Object JavaDoc[] {
180                 "Another Property Fault",
181                 new Byte JavaDoc((byte) - 2),
182                 "Another JMS Property" }, SYNC);
183     }
184
185     public void testIndicOnlySync() {
186         doit(
187             "throwProperties",
188             8,
189             true,
190             false,
191             new String JavaDoc[] { "faultIndic" },
192             new Object JavaDoc[] { new Byte JavaDoc((byte) - 3)},
193             SYNC);
194     }
195
196     public void testPropOnlySync() {
197         doit(
198             "throwProperties",
199             9,
200             true,
201             false,
202             new String JavaDoc[] { "faultProp" },
203             new Object JavaDoc[] { "Another JMS Property" },
204             SYNC);
205     }
206
207     public void testNullFaultSync() {
208         doit("throwProperties", 10, true, false, null, null, SYNC);
209     }
210
211     /* ***************** ASYNC ************************** */
212     
213     public void testNoFaultAsync() {
214         doit("throwSimple", 0, false, false, null, null, ASYNC);
215     }
216
217     public void testSimpleAsync() {
218         doit(
219             "throwSimple",
220             1,
221             true,
222             false,
223             new String JavaDoc[] { "faultText" },
224             new Object JavaDoc[] { "A Simple Fault" }, ASYNC);
225     }
226
227     public void testIntsAsync() {
228         doit(
229             "throwSimple",
230             2,
231             true,
232             false,
233             new String JavaDoc[] { "faultInt1", "faultInt2", "faultInt3" },
234             new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3)}, ASYNC);
235     }
236
237     public void testTwoIntsAsync() {
238         doit(
239             "throwSimple",
240             3,
241             true,
242             false,
243             new String JavaDoc[] { "faultInt1", "faultInt2" },
244             new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2) }, ASYNC);
245     }
246
247     public void testEmptyJmsFaultAsync() {
248         doit(
249             "throwSimple",
250             2,
251             true,
252             false,
253             new String JavaDoc[] { "faultInt1", "faultInt2", "faultInt3" },
254             new Object JavaDoc[] { new Integer JavaDoc(1), new Integer JavaDoc(2), new Integer JavaDoc(3)}, ASYNC);
255     }
256
257     public void testIndicIntAsync() {
258         doit(
259             "throwIndicInt",
260             4,
261             true,
262             false,
263             new String JavaDoc[] { "faultText" },
264             new Object JavaDoc[] { "A Simple Fault" }, ASYNC);
265     }
266
267     public void testNoFaultAltAsync() {
268         doit("throwSimple", 5, false, false, null, null, ASYNC);
269     }
270
271     public void testFaultIndicPropAsync() {
272         doit(
273             "throwProperties",
274             6,
275             true,
276             false,
277             new String JavaDoc[] { "faultText", "faultIndic" },
278             new Object JavaDoc[] { "A Fault Indicator", new Byte JavaDoc((byte) - 1)}, ASYNC);
279     }
280
281     public void testJmsPropFaultAsync() {
282         doit(
283             "throwProperties",
284             7,
285             true,
286             false,
287             new String JavaDoc[] { "faultText", "faultIndic", "faultProp" },
288             new Object JavaDoc[] {
289                 "Another Property Fault",
290                 new Byte JavaDoc((byte) - 2),
291                 "Another JMS Property" }, ASYNC);
292     }
293
294     public void testIndicOnlyAsync() {
295         doit(
296             "throwProperties",
297             8,
298             true,
299             false,
300             new String JavaDoc[] { "faultIndic" },
301             new Object JavaDoc[] { new Byte JavaDoc((byte) - 3)},
302             ASYNC);
303     }
304
305     public void testPropOnlyAsync() {
306         doit(
307             "throwProperties",
308             9,
309             true,
310             false,
311             new String JavaDoc[] { "faultProp" },
312             new Object JavaDoc[] { "Another JMS Property" },
313             ASYNC);
314     }
315
316     public void testNullFaultAsync() {
317         doit("throwProperties", 10, true, false, null, null, ASYNC);
318     }
319
320     private void doit(
321         String JavaDoc method,
322         int choice,
323         boolean faultExpected,
324         boolean exceptionExpected,
325         String JavaDoc[] names,
326         Object JavaDoc[] parts,
327         boolean blocks) {
328             
329         if (!TestUtilities.areWeTesting("jms"))
330             return;
331
332         try {
333             WSIFServiceFactory factory = WSIFServiceFactory.newInstance();
334             WSIFService service =
335                 factory.getService(
336                     wsdlLocation,
337                     null,
338                     null,
339                     "http://jms/",
340                     "JmsFault");
341
342             WSIFPort port = service.getPort("default");
343             WSIFOperation operation = port.createOperation(method);
344
345             WSIFMessage inputMessage = operation.createInputMessage();
346             inputMessage.setIntPart("choice",choice);
347             WSIFMessage outputMessage = operation.createOutputMessage();
348             WSIFMessage faultMessage = operation.createFaultMessage();
349
350             boolean operationSucceeded = false;
351             if (blocks == SYNC) {
352                 operationSucceeded =
353                     operation.executeRequestResponseOperation(
354                         inputMessage,
355                         outputMessage,
356                         faultMessage);
357
358             } else if (blocks == ASYNC) {
359                 WSIFMessage context = operation.getContext();
360                 context.setObjectPart(
361                     WSIFConstants.CONTEXT_JMS_PREFIX + "JMSReplyTo",
362                     TestUtilities.getWsifProperty("wsif.async.replytoq2"));
363                 operation.setContext(context);
364
365                 WSIFCorrelationId id =
366                     operation.executeRequestResponseAsync(inputMessage);
367                 System.out.println(
368                     "async operation done, correlation id="
369                         + id.getCorrelationId());
370
371                 Object JavaDoc jmsResponse =
372                     TestUtilities.getJMSAsyncResponse(
373                         id.getCorrelationId(),
374                         TestUtilities.getWsifProperty("wsif.async.replytoq2"));
375
376                 operationSucceeded =
377                     operation.processAsyncResponse(
378                         jmsResponse,
379                         outputMessage,
380                         faultMessage);
381
382             } else
383                 assertTrue(false);
384
385             System.out.println(
386                 "JmsFaultTest "
387                     + method
388                     + " "
389                     + operationSucceeded
390                     + " "
391                     + (names == null));
392
393             assertTrue(
394                 "Bad boolean success value from executeRequestReponseOperation",
395                 (operationSucceeded && !faultExpected)
396                     || (!operationSucceeded && faultExpected));
397                     
398             if (!operationSucceeded) {
399                 Iterator JavaDoc it = faultMessage.getPartNames();
400                 int i = 0;
401                 while (it.hasNext()) {
402                     it.next();
403                     i++;
404                 }
405                 
406                 if (names == null)
407                     names = new String JavaDoc[] {};
408                 if (parts == null)
409                     parts = new Object JavaDoc[] {};
410
411                 assertTrue(
412                     "Bad number of parts in the fault message expected="
413                         + names.length
414                         + " got="
415                         + i,
416                     i == names.length);
417
418                 for (i = 0; i < names.length; i++) {
419                     Object JavaDoc o = faultMessage.getObjectPart(names[i]);
420
421                     assertTrue(
422                         "Bad value for partName=" + names[i] + " " + o,
423                         parts[i].equals(o));
424                 }
425             }
426             
427             assertTrue(!exceptionExpected);
428         } catch (Exception JavaDoc e) {
429             System.err.println("JmsFaultTest(" + method + ") caught exception " + e);
430             if (!exceptionExpected || !(e instanceof WSIFException))
431                 e.printStackTrace();
432             assertTrue(exceptionExpected && e instanceof WSIFException);
433         }
434     }
435 }
Popular Tags