KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jacorb > test > notification > typed > TypedProxyPushSupplierImplTest


1 package org.jacorb.test.notification.typed;
2
3 /*
4  * JacORB - a free Java ORB
5  *
6  * Copyright (C) 1997-2003 Gerald Brose.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Library General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16  * Library General Public License for more details.
17  *
18  * You should have received a copy of the GNU Library General Public
19  * License along with this library; if not, write to the Free
20  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21  */

22
23 import java.util.HashMap JavaDoc;
24 import java.util.Map JavaDoc;
25
26 import junit.framework.Assert;
27 import junit.framework.Test;
28
29 import org.easymock.MockControl;
30 import org.jacorb.notification.AnyMessage;
31 import org.jacorb.notification.OfferManager;
32 import org.jacorb.notification.StructuredEventMessage;
33 import org.jacorb.notification.SubscriptionManager;
34 import org.jacorb.notification.TypedEventMessage;
35 import org.jacorb.notification.engine.DefaultPushTaskExecutorFactory;
36 import org.jacorb.notification.servant.ITypedAdmin;
37 import org.jacorb.notification.servant.TypedProxyPushSupplierImpl;
38 import org.jacorb.test.notification.NotificationTestCase;
39 import org.jacorb.test.notification.NotificationTestCaseSetup;
40 import org.omg.CORBA.Any JavaDoc;
41 import org.omg.CosEventChannelAdmin.TypeError;
42 import org.omg.CosEventComm.Disconnected;
43 import org.omg.CosNotification.EventType;
44 import org.omg.CosNotification.Property;
45 import org.omg.CosNotification.PropertySeqHelper;
46 import org.omg.CosNotification.StructuredEvent;
47 import org.omg.CosNotifyChannelAdmin.ConsumerAdmin;
48 import org.omg.CosNotifyComm.InvalidEventType;
49 import org.omg.CosTypedNotifyChannelAdmin.TypedProxyPushSupplier;
50 import org.omg.CosTypedNotifyChannelAdmin.TypedProxyPushSupplierHelper;
51 import org.omg.CosTypedNotifyComm.TypedPushConsumer;
52 import org.omg.CosTypedNotifyComm.TypedPushConsumerPOA;
53
54 import EDU.oswego.cs.dl.util.concurrent.Latch;
55
56 /**
57  * @author Alphonse Bendt
58  * @version $Id: TypedProxyPushSupplierImplTest.java,v 1.8 2005/05/01 21:15:16 alphonse.bendt Exp $
59  */

60 public class TypedProxyPushSupplierImplTest extends NotificationTestCase
61 {
62     private TypedProxyPushSupplierImpl objectUnderTest_;
63
64     private TypedProxyPushSupplier proxyPushSupplier_;
65
66     private static final String JavaDoc DRINKING_COFFEE_ID = "::org::jacorb::test::notification::typed::Coffee::drinking_coffee";
67
68     private MockControl controlAdmin_;
69
70     private ITypedAdmin mockAdmin_;
71
72     private MockControl controlConsumerAdmin_;
73
74     private ConsumerAdmin mockConsumerAdmin_;
75
76     public TypedProxyPushSupplierImplTest(String JavaDoc name, NotificationTestCaseSetup setup)
77     {
78         super(name, setup);
79     }
80
81     public void setUpTest() throws Exception JavaDoc
82     {
83         controlAdmin_ = MockControl.createNiceControl(ITypedAdmin.class);
84         mockAdmin_ = (ITypedAdmin) controlAdmin_.getMock();
85         mockAdmin_.getProxyID();
86         controlAdmin_.setReturnValue(10);
87
88         mockAdmin_.isIDPublic();
89         controlAdmin_.setReturnValue(true);
90
91         mockAdmin_.getContainer();
92         controlAdmin_.setReturnValue(getPicoContainer());
93
94         mockAdmin_.getSupportedInterface();
95         controlAdmin_.setReturnValue(CoffeeHelper.id());
96
97         controlAdmin_.replay();
98
99         controlConsumerAdmin_ = MockControl.createControl(ConsumerAdmin.class);
100         mockConsumerAdmin_ = (ConsumerAdmin) controlConsumerAdmin_.getMock();
101
102         objectUnderTest_ = new TypedProxyPushSupplierImpl(mockAdmin_, mockConsumerAdmin_, getORB(),
103                 getPOA(), getConfiguration(), getTaskProcessor(), new DefaultPushTaskExecutorFactory(1), new OfferManager(), new SubscriptionManager());
104
105         proxyPushSupplier_ = TypedProxyPushSupplierHelper.narrow(objectUnderTest_.activate());
106     }
107
108     public void testID()
109     {
110         assertEquals(new Integer JavaDoc(10), objectUnderTest_.getID());
111         assertTrue(objectUnderTest_.isIDPublic());
112     }
113
114
115     public void testConnect() throws Exception JavaDoc
116     {
117         MockCoffee _mockCoffee = new MockCoffee();
118
119         final Coffee _coffee = _mockCoffee._this(getClientORB());
120
121         MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer()
122         {
123             public org.omg.CORBA.Object JavaDoc get_typed_consumer()
124             {
125                 return _coffee;
126             }
127         };
128
129         TypedPushConsumer _consumer = _mockConsumer._this(getClientORB());
130
131         proxyPushSupplier_.connect_typed_push_consumer(_consumer);
132     }
133
134     public void testConnectWrongTypeThrowsException() throws Exception JavaDoc
135     {
136         final Map JavaDoc _map = new HashMap JavaDoc();
137
138         MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer()
139         {
140             public org.omg.CORBA.Object JavaDoc get_typed_consumer()
141             {
142                 return (org.omg.CORBA.Object JavaDoc) _map.get("object");
143             }
144         };
145
146         final TypedPushConsumer _consumer = _mockConsumer._this(getClientORB());
147
148         _map.put("object", _consumer);
149
150         try
151         {
152             proxyPushSupplier_.connect_typed_push_consumer(_consumer);
153             fail();
154         } catch (TypeError e)
155         {
156             // expected
157
}
158     }
159
160     public void testPushPulledEvent() throws Exception JavaDoc
161     {
162         // setup test data
163
TypedEventMessage _event = new TypedEventMessage();
164
165         _event.setTypedEvent(PullCoffeeHelper.id(), DRINKING_COFFEE_ID, new Property[] {
166                 new Property("name", toAny("alphonse")), new Property("minutes", toAny(10)) });
167
168         // setup mock
169
MockCoffee _mockCoffee = new MockCoffee()
170         {
171             public void drinking_coffee(String JavaDoc name, int minutes)
172             {
173                 super.drinking_coffee(name, minutes);
174
175                 assertEquals("alphonse", name);
176                 assertEquals(10, minutes);
177             }
178         };
179
180         _mockCoffee.drinking_coffee_expect = 1;
181
182         // setup and connect consumer
183
final Coffee _coffee = _mockCoffee._this(getClientORB());
184
185         MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer()
186         {
187             public org.omg.CORBA.Object JavaDoc get_typed_consumer()
188             {
189                 return _coffee;
190             }
191         };
192
193         TypedPushConsumer _consumer = _mockConsumer._this(getClientORB());
194
195         proxyPushSupplier_.connect_typed_push_consumer(_consumer);
196
197         // run test
198
objectUnderTest_.getMessageConsumer().deliverMessage(_event.getHandle());
199
200         // verify results
201
_mockCoffee.verify();
202     }
203
204     public void testPushTyped() throws Exception JavaDoc
205     {
206         // setup test data
207
TypedEventMessage _event = new TypedEventMessage();
208
209         _event.setTypedEvent(CoffeeHelper.id(), DRINKING_COFFEE_ID, new Property[] {
210                 new Property("name", toAny("alphonse")), new Property("minutes", toAny(10)) });
211
212         // setup mock
213
MockCoffee _mockCoffee = new MockCoffee()
214         {
215             public void drinking_coffee(String JavaDoc name, int minutes)
216             {
217                 super.drinking_coffee(name, minutes);
218
219                 assertEquals("alphonse", name);
220                 assertEquals(10, minutes);
221             }
222         };
223
224         _mockCoffee.drinking_coffee_expect = 1;
225
226         // setup and connect consumer
227
final Coffee _coffee = _mockCoffee._this(getClientORB());
228
229         MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer()
230         {
231             public org.omg.CORBA.Object JavaDoc get_typed_consumer()
232             {
233                 return _coffee;
234             }
235         };
236
237         TypedPushConsumer _consumer = _mockConsumer._this(getClientORB());
238
239         proxyPushSupplier_.connect_typed_push_consumer(_consumer);
240
241         // run test
242
objectUnderTest_.getMessageConsumer().deliverMessage(_event.getHandle());
243
244         // verify results
245
_mockCoffee.verify();
246     }
247
248     public void testPushStructured() throws Exception JavaDoc
249     {
250         // setup test data
251
StructuredEventMessage _event = new StructuredEventMessage();
252
253         StructuredEvent _data = getTestUtils().getEmptyStructuredEvent();
254
255         _data.filterable_data = new Property[] {
256                 new Property("operation", toAny(DRINKING_COFFEE_ID)),
257                 new Property("name", toAny("alphonse")), new Property("minutes", toAny(10)) };
258
259         _event.setStructuredEvent(_data, false, false);
260
261         // setup mock
262
MockCoffee _mockCoffee = new MockCoffee()
263         {
264             public void drinking_coffee(String JavaDoc name, int minutes)
265             {
266                 super.drinking_coffee(name, minutes);
267
268                 assertEquals("alphonse", name);
269                 assertEquals(10, minutes);
270             }
271         };
272
273         _mockCoffee.drinking_coffee_expect = 1;
274
275         // setup and connect consumer
276
final Coffee _coffee = _mockCoffee._this(getClientORB());
277
278         MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer()
279         {
280             public org.omg.CORBA.Object JavaDoc get_typed_consumer()
281             {
282                 return _coffee;
283             }
284         };
285
286         TypedPushConsumer _consumer = _mockConsumer._this(getClientORB());
287
288         proxyPushSupplier_.connect_typed_push_consumer(_consumer);
289
290         // run test
291
objectUnderTest_.getMessageConsumer().deliverMessage(_event.getHandle());
292
293         // verify results
294
_mockCoffee.verify();
295     }
296
297     public void testPushAny() throws Exception JavaDoc
298     {
299         // setup test data
300
AnyMessage _event = new AnyMessage();
301
302         Any JavaDoc _any = getORB().create_any();
303
304         PropertySeqHelper.insert(_any, new Property[] {
305                 new Property("operation", toAny(DRINKING_COFFEE_ID)),
306                 new Property("name", toAny("alphonse")), new Property("minutes", toAny(10)) });
307
308         _event.setAny(_any);
309
310         final Latch _hasReceived = new Latch();
311         
312         // setup mock
313
MockCoffee _mockCoffee = new MockCoffee()
314         {
315             public void drinking_coffee(String JavaDoc name, int minutes)
316             {
317                 super.drinking_coffee(name, minutes);
318                
319                 assertEquals("alphonse", name);
320                 assertEquals(10, minutes);
321                 
322                 _hasReceived.release();
323             }
324         };
325
326         _mockCoffee.drinking_coffee_expect = 1;
327
328         // setup and connect consumer
329
final Coffee _coffee = _mockCoffee._this(getClientORB());
330
331         MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer()
332         {
333             public org.omg.CORBA.Object JavaDoc get_typed_consumer()
334             {
335                 return _coffee;
336             }
337         };
338
339         TypedPushConsumer _consumer = _mockConsumer._this(getClientORB());
340
341         proxyPushSupplier_.connect_typed_push_consumer(_consumer);
342
343         // run test
344
objectUnderTest_.getMessageConsumer().deliverMessage(_event.getHandle());
345
346         assertTrue(_hasReceived.attempt(5000));
347         
348         // verify results
349
_mockCoffee.verify();
350     }
351
352     public static Test suite() throws Exception JavaDoc
353     {
354         return NotificationTestCase.suite(TypedProxyPushSupplierImplTest.class);
355     }
356 }
357
358 class MockTypedPushConsumer extends TypedPushConsumerPOA
359 {
360     public org.omg.CORBA.Object JavaDoc get_typed_consumer()
361     {
362         return null;
363     }
364
365     public void push(Any JavaDoc any) throws Disconnected
366     {
367         // ignored
368
}
369
370     public void disconnect_push_consumer()
371     {
372         // ingored
373
}
374
375     public void offer_change(EventType[] eventTypeArray, EventType[] eventTypeArray1)
376             throws InvalidEventType
377     {
378
379         // ignored
380
}
381 }
382
383 class MockCoffee extends CoffeePOA
384 {
385     int drinking_coffee_expect;
386
387     int drinking_coffee_called;
388
389     int cancel_coffee_expect;
390
391     int cancel_coffee_called;
392
393     public void drinking_coffee(String JavaDoc string, int n)
394     {
395         drinking_coffee_called++;
396     }
397
398     public void cancel_coffee(String JavaDoc string)
399     {
400         cancel_coffee_called++;
401     }
402
403     public void verify()
404     {
405         Assert.assertEquals(drinking_coffee_expect, drinking_coffee_called);
406         Assert.assertEquals(cancel_coffee_expect, cancel_coffee_called);
407     }
408 }
Popular Tags