KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > exolab > jms > net > invoke > CallbackTestCase


1 /**
2  * Redistribution and use of this software and associated documentation
3  * ("Software"), with or without modification, are permitted provided
4  * that the following conditions are met:
5  *
6  * 1. Redistributions of source code must retain copyright
7  * statements and notices. Redistributions must also contain a
8  * copy of this document.
9  *
10  * 2. Redistributions in binary form must reproduce the
11  * above copyright notice, this list of conditions and the
12  * following disclaimer in the documentation and/or other
13  * materials provided with the distribution.
14  *
15  * 3. The name "Exolab" must not be used to endorse or promote
16  * products derived from this Software without prior written
17  * permission of Exoffice Technologies. For written permission,
18  * please contact info@exolab.org.
19  *
20  * 4. Products derived from this Software may not be called "Exolab"
21  * nor may "Exolab" appear in their names without prior written
22  * permission of Exoffice Technologies. Exolab is a registered
23  * trademark of Exoffice Technologies.
24  *
25  * 5. Due credit should be given to the Exolab Project
26  * (http://www.exolab.org/).
27  *
28  * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29  * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30  * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32  * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33  * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39  * OF THE POSSIBILITY OF SUCH DAMAGE.
40  *
41  * Copyright 2004-2005 (C) Exoffice Technologies Inc. All Rights Reserved.
42  *
43  * $Id: CallbackTestCase.java,v 1.6 2005/05/24 05:48:30 tanderson Exp $
44  */

45 package org.exolab.jms.net.invoke;
46
47 import java.util.Map JavaDoc;
48
49 import org.exolab.jms.net.Callback;
50 import org.exolab.jms.net.CallbackService;
51 import org.exolab.jms.net.CallbackServiceImpl;
52 import org.exolab.jms.net.orb.ORB;
53 import org.exolab.jms.net.proxy.Proxy;
54 import org.exolab.jms.net.registry.Registry;
55
56
57 /**
58  * Tests callbacks.
59  *
60  * @author <a HREF="mailto:tma@netspace.net.au">Tim Anderson</a>
61  * @version $Revision: 1.6 $ $Date: 2005/05/24 05:48:30 $
62  */

63 public abstract class CallbackTestCase extends ORBTestCase {
64
65     /**
66      * The callback service.
67      */

68     private CallbackServiceImpl _service = new CallbackServiceImpl();
69
70     /**
71      * Callback service name.
72      */

73     private static final String JavaDoc CALLBACK_SERVICE = "callback";
74
75
76     /**
77      * Construct a new <code>CallbackTestCase</code>.
78      *
79      * @param name the name of test case
80      * @param uri the server export URI
81      */

82     public CallbackTestCase(String JavaDoc name, String JavaDoc uri) {
83         super(name, uri);
84     }
85
86     /**
87      * Construct a new <code>CallbackTestCase</code>.
88      *
89      * @param name the name of test case
90      * @param uri the server export URI
91      * @param properties connection properties. May be <code>null</code>
92      */

93     public CallbackTestCase(String JavaDoc name, String JavaDoc uri, Map JavaDoc properties) {
94         super(name, uri, properties);
95     }
96
97     /**
98      * Construct a new <code>CallbackTestCase</code>.
99      *
100      * @param name the name of test case
101      * @param uri the server export URI
102      * @param routeURI the route URI
103      */

104     public CallbackTestCase(String JavaDoc name, String JavaDoc uri, String JavaDoc routeURI) {
105         super(name, uri, routeURI);
106     }
107
108     /**
109      * Construct a new <code>CallbackTestCase</code>.
110      *
111      * @param name the name of test case
112      * @param uri the export URI
113      * @param routeURI the route URI
114      * @param properties connection properties. May be <code>null</code>
115      */

116     public CallbackTestCase(String JavaDoc name, String JavaDoc uri, String JavaDoc routeURI,
117                             Map JavaDoc properties) {
118         super(name, uri, routeURI, properties);
119     }
120
121     /**
122      * Construct a new <code>CallbackTestCase</code>.
123      *
124      * @param name the name of test case
125      * @param uri the export URI
126      * @param routeURI the route URI
127      * @param connectionProps connection properties. May be <code>null</code>
128      * @param acceptorProps acceptor properites. May be <code>null</code>
129      */

130     public CallbackTestCase(String JavaDoc name, String JavaDoc uri, String JavaDoc routeURI,
131                             Map JavaDoc connectionProps, Map JavaDoc acceptorProps) {
132         super(name, uri, routeURI, connectionProps, acceptorProps);
133     }
134
135     /**
136      * Tests a single callback.
137      *
138      * @throws Exception for any error
139      */

140     public void testCallback() throws Exception JavaDoc {
141         final int count = 10;
142         ORB client = getClientORB();
143         Registry registry = client.getRegistry(getConnectionProperties());
144         CallbackService service =
145                 (CallbackService) registry.lookup(CALLBACK_SERVICE);
146
147         LoggingCallback callback = new LoggingCallback();
148         Callback proxy = (Callback) client.exportObjectTo(callback,
149                                                           getServerURI());
150         service.addCallback(proxy);
151
152         for (int i = 0; i < count; ++i) {
153             _service.invoke(new Integer JavaDoc(i));
154         }
155
156         Integer JavaDoc[] objects = (Integer JavaDoc[]) callback.getObjects().toArray(
157                 new Integer JavaDoc[0]);
158         assertEquals(count, objects.length);
159         for (int i = 0; i < count; ++i) {
160             assertEquals(i, objects[i].intValue());
161         }
162     }
163
164     /**
165      * Tests callback recursion.
166      *
167      * @throws Exception for any error
168      */

169     public void testRecursion() throws Exception JavaDoc {
170         final int count = 10;
171         final int depth = 4; // recursion depth
172

173         ORB client = getClientORB();
174         Registry registry = client.getRegistry(getConnectionProperties());
175         CallbackService service =
176                 (CallbackService) registry.lookup(CALLBACK_SERVICE);
177
178         RecursiveCallback callback = new RecursiveCallback(service, depth);
179         Callback proxy = (Callback) client.exportObjectTo(callback,
180                                                           getServerURI());
181         service.addCallback(proxy);
182
183         for (int i = 0; i < count; ++i) {
184             _service.invoke(new Integer JavaDoc(i));
185         }
186
187         Integer JavaDoc[] objects = (Integer JavaDoc[]) callback.getObjects().toArray(
188                 new Integer JavaDoc[0]);
189         assertEquals(count * depth, objects.length);
190         for (int i = 0, index = 0; i < count; ++i) {
191             for (int j = 0; j < depth; ++j, ++index) {
192                 assertEquals(i, objects[index].intValue());
193             }
194         }
195     }
196
197     /**
198      * Sets up the test case.
199      *
200      * @throws Exception for any error
201      */

202     protected void setUp() throws Exception JavaDoc {
203         super.setUp();
204
205         ORB server = getORB();
206         Proxy proxy = server.exportObject(_service);
207         server.getRegistry().bind(CALLBACK_SERVICE, proxy);
208     }
209
210 }
211
Popular Tags