1 package org.jacorb.test.notification.typed; 2 3 22 23 import java.util.HashMap ; 24 import java.util.Map ; 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 ; 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 60 public class TypedProxyPushSupplierImplTest extends NotificationTestCase 61 { 62 private TypedProxyPushSupplierImpl objectUnderTest_; 63 64 private TypedProxyPushSupplier proxyPushSupplier_; 65 66 private static final String 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 name, NotificationTestCaseSetup setup) 77 { 78 super(name, setup); 79 } 80 81 public void setUpTest() throws Exception 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 (10), objectUnderTest_.getID()); 111 assertTrue(objectUnderTest_.isIDPublic()); 112 } 113 114 115 public void testConnect() throws Exception 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 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 135 { 136 final Map _map = new HashMap (); 137 138 MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer() 139 { 140 public org.omg.CORBA.Object get_typed_consumer() 141 { 142 return (org.omg.CORBA.Object ) _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 } 158 } 159 160 public void testPushPulledEvent() throws Exception 161 { 162 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 MockCoffee _mockCoffee = new MockCoffee() 170 { 171 public void drinking_coffee(String 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 final Coffee _coffee = _mockCoffee._this(getClientORB()); 184 185 MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer() 186 { 187 public org.omg.CORBA.Object 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 objectUnderTest_.getMessageConsumer().deliverMessage(_event.getHandle()); 199 200 _mockCoffee.verify(); 202 } 203 204 public void testPushTyped() throws Exception 205 { 206 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 MockCoffee _mockCoffee = new MockCoffee() 214 { 215 public void drinking_coffee(String 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 final Coffee _coffee = _mockCoffee._this(getClientORB()); 228 229 MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer() 230 { 231 public org.omg.CORBA.Object 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 objectUnderTest_.getMessageConsumer().deliverMessage(_event.getHandle()); 243 244 _mockCoffee.verify(); 246 } 247 248 public void testPushStructured() throws Exception 249 { 250 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 MockCoffee _mockCoffee = new MockCoffee() 263 { 264 public void drinking_coffee(String 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 final Coffee _coffee = _mockCoffee._this(getClientORB()); 277 278 MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer() 279 { 280 public org.omg.CORBA.Object 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 objectUnderTest_.getMessageConsumer().deliverMessage(_event.getHandle()); 292 293 _mockCoffee.verify(); 295 } 296 297 public void testPushAny() throws Exception 298 { 299 AnyMessage _event = new AnyMessage(); 301 302 Any _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 MockCoffee _mockCoffee = new MockCoffee() 314 { 315 public void drinking_coffee(String 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 final Coffee _coffee = _mockCoffee._this(getClientORB()); 330 331 MockTypedPushConsumer _mockConsumer = new MockTypedPushConsumer() 332 { 333 public org.omg.CORBA.Object 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 objectUnderTest_.getMessageConsumer().deliverMessage(_event.getHandle()); 345 346 assertTrue(_hasReceived.attempt(5000)); 347 348 _mockCoffee.verify(); 350 } 351 352 public static Test suite() throws Exception 353 { 354 return NotificationTestCase.suite(TypedProxyPushSupplierImplTest.class); 355 } 356 } 357 358 class MockTypedPushConsumer extends TypedPushConsumerPOA 359 { 360 public org.omg.CORBA.Object get_typed_consumer() 361 { 362 return null; 363 } 364 365 public void push(Any any) throws Disconnected 366 { 367 } 369 370 public void disconnect_push_consumer() 371 { 372 } 374 375 public void offer_change(EventType[] eventTypeArray, EventType[] eventTypeArray1) 376 throws InvalidEventType 377 { 378 379 } 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 string, int n) 394 { 395 drinking_coffee_called++; 396 } 397 398 public void cancel_coffee(String 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 |