1 22 package org.objectweb.petals.jbi.messaging; 23 24 import java.lang.reflect.Method ; 25 import java.util.HashMap ; 26 import java.util.concurrent.LinkedBlockingQueue ; 27 import java.util.concurrent.TimeUnit ; 28 29 import javax.jbi.JBIException; 30 import javax.jbi.messaging.ExchangeStatus; 31 import javax.jbi.messaging.MessageExchange; 32 import javax.jbi.messaging.MessageExchangeFactory; 33 import javax.jbi.messaging.MessagingException; 34 import javax.jbi.messaging.MessageExchange.Role; 35 import javax.jbi.servicedesc.ServiceEndpoint; 36 import javax.xml.namespace.QName ; 37 38 import junit.framework.TestCase; 39 40 import org.easymock.classextension.EasyMock; 41 42 import org.objectweb.petals.jbi.component.context.ComponentContextImpl; 43 import org.objectweb.petals.jbi.messaging.mock.MockLogger; 44 import org.objectweb.petals.jbi.registry.ConsumerEndpoint; 45 import org.objectweb.petals.jbi.routing.RouterImpl; 46 import org.objectweb.petals.jbi.routing.RoutingException; 47 import org.objectweb.petals.util.LoggingUtil; 48 49 54 public class DeliveryChannelImplTest extends TestCase { 55 56 public void testAccept() throws SecurityException , NoSuchMethodException , 57 MessagingException { 58 DeliveryChannelImpl deliveryChannelImpl = EasyMock.createMock( 59 DeliveryChannelImpl.class, new Method [] {DeliveryChannelImpl.class 60 .getDeclaredMethod("accept", new Class [] {long.class})}); 61 LoggingUtil loggingUtil = EasyMock.createMock(LoggingUtil.class); 62 MessageExchange messageExchange = EasyMock 63 .createMock(MessageExchange.class); 64 65 loggingUtil.call(); 66 EasyMock.expect(deliveryChannelImpl.accept(0)).andReturn( 67 messageExchange); 68 69 EasyMock.replay(deliveryChannelImpl); 70 EasyMock.replay(loggingUtil); 71 72 deliveryChannelImpl.log = loggingUtil; 73 74 assertEquals(deliveryChannelImpl.accept(), messageExchange); 75 } 76 77 @SuppressWarnings ("unchecked") 78 public void testAcceptLong() throws SecurityException , 79 NoSuchMethodException , MessagingException, IllegalArgumentException , 80 InterruptedException { 81 DeliveryChannelImpl deliveryChannelImpl = EasyMock.createMock( 82 DeliveryChannelImpl.class, new Method [] {DeliveryChannelImpl.class 83 .getDeclaredMethod("checkDeliveryChannelIsOpened", 84 new Class [] {})}); 85 LoggingUtil loggingUtil = EasyMock.createMock(LoggingUtil.class); 86 MessageExchangeImpl messageExchangeImpl = EasyMock 87 .createMock(MessageExchangeImpl.class); 88 LinkedBlockingQueue <MessageExchangeImpl> queue = EasyMock 89 .createMock(LinkedBlockingQueue .class); 90 MessageExchangeFactoryImpl messageExchangeFactoryImpl = EasyMock 91 .createMock(MessageExchangeFactoryImpl.class); 92 MessageExchange messageExchange = EasyMock 93 .createMock(MessageExchange.class); 94 95 loggingUtil.start(); 96 deliveryChannelImpl.checkDeliveryChannelIsOpened(); 97 loggingUtil.end(); 98 EasyMock.expect(messageExchangeImpl.getPattern()).andReturn( 99 MessageExchangeImpl.IN_ONLY_PATTERN); 100 EasyMock.expect(queue.poll(0,TimeUnit.MILLISECONDS)).andReturn(messageExchangeImpl); 101 EasyMock.expect( 102 messageExchangeFactoryImpl.createExchangeDecorator( 103 messageExchangeImpl, MessageExchangeImpl.IN_ONLY_PATTERN)) 104 .andReturn(messageExchange); 105 106 EasyMock.replay(messageExchangeImpl); 107 EasyMock.replay(deliveryChannelImpl); 108 EasyMock.replay(loggingUtil); 109 EasyMock.replay(queue); 110 EasyMock.replay(messageExchangeFactoryImpl); 111 112 deliveryChannelImpl.log = loggingUtil; 113 deliveryChannelImpl.queue = queue; 114 deliveryChannelImpl.messageExchangeFactory = messageExchangeFactoryImpl; 115 116 assertEquals(messageExchange, deliveryChannelImpl.accept(0)); 117 } 118 119 @SuppressWarnings ("unchecked") 120 public void testAcceptLongException() throws SecurityException , 121 NoSuchMethodException , MessagingException, IllegalArgumentException , 122 InterruptedException { 123 DeliveryChannelImpl deliveryChannelImpl = EasyMock.createMock( 124 DeliveryChannelImpl.class, new Method [] {DeliveryChannelImpl.class 125 .getDeclaredMethod("checkDeliveryChannelIsOpened", 126 new Class [] {})}); 127 LoggingUtil loggingUtil = EasyMock.createMock(LoggingUtil.class); 128 LinkedBlockingQueue <MessageExchangeImpl> queue = EasyMock 129 .createMock( LinkedBlockingQueue .class); 130 131 loggingUtil.start(); 132 deliveryChannelImpl.checkDeliveryChannelIsOpened(); 133 loggingUtil.end(); 134 EasyMock.expect(queue.poll(0,TimeUnit.MILLISECONDS)).andThrow( 135 new IllegalArgumentException ("test exception")); 136 137 EasyMock.replay(deliveryChannelImpl); 138 EasyMock.replay(loggingUtil); 139 EasyMock.replay(queue); 140 141 deliveryChannelImpl.log = loggingUtil; 142 deliveryChannelImpl.queue = queue; 143 try { 144 deliveryChannelImpl.accept(0); 145 fail(); 146 } catch (Exception e) { 147 } 149 } 150 151 public void testCheckDeliveryChannelIsOpenedException() { 152 DeliveryChannelImpl deliveryChannelImpl = new DeliveryChannelImpl( 153 EasyMock.createMock(ComponentContextImpl.class), EasyMock 154 .createMock(RouterImpl.class), null); 155 deliveryChannelImpl.opened = false; 156 157 try { 158 deliveryChannelImpl.checkDeliveryChannelIsOpened(); 159 fail(); 160 } catch (MessagingException e) { 161 } 163 } 164 165 public void testCheckMessageExchangeExceptionMessageNull() { 166 DeliveryChannelImpl deliveryChannelImpl = new DeliveryChannelImpl( 167 EasyMock.createMock(ComponentContextImpl.class), EasyMock 168 .createMock(RouterImpl.class), null); 169 try { 170 deliveryChannelImpl.checkMessageExchange(null); 171 fail(); 172 } catch (MessagingException e) { 173 } 175 } 176 177 public void testCheckMessageExchangeExceptionTerminated() { 178 DeliveryChannelImpl deliveryChannelImpl = new DeliveryChannelImpl( 179 EasyMock.createMock(ComponentContextImpl.class), EasyMock 180 .createMock(RouterImpl.class), null); 181 MessageExchangeImpl messageExchangeImpl = EasyMock 182 .createMock(MessageExchangeImpl.class); 183 MessageExchangeDecorator messageExchangeDecorator = EasyMock 184 .createMock(MessageExchangeDecorator.class); 185 186 EasyMock.expect(messageExchangeDecorator.getMessageExchange()) 187 .andReturn(messageExchangeImpl); 188 EasyMock.expect(messageExchangeImpl.isTerminated()).andReturn(true); 189 190 EasyMock.replay(messageExchangeDecorator); 191 EasyMock.replay(messageExchangeImpl); 192 193 try { 194 deliveryChannelImpl.checkMessageExchange(messageExchangeDecorator); 195 fail(); 196 } catch (MessagingException e) { 197 } 199 } 200 201 @SuppressWarnings ("unchecked") 202 public void testClose() throws SecurityException , NoSuchMethodException , 203 IllegalArgumentException , InterruptedException , JBIException { 204 DeliveryChannelImpl deliveryChannelImpl = EasyMock.createMock( 205 DeliveryChannelImpl.class, new Method [] {DeliveryChannelImpl.class 206 .getDeclaredMethod("checkDeliveryChannelIsOpened", 207 new Class [] {})}); 208 LoggingUtil loggingUtil = EasyMock.createMock(LoggingUtil.class); 209 LinkedBlockingQueue <MessageExchangeImpl> queue = EasyMock 210 .createMock( LinkedBlockingQueue .class); 211 ComponentContextImpl componentContextImpl = EasyMock 212 .createMock(ComponentContextImpl.class); 213 214 loggingUtil.call(); 215 deliveryChannelImpl.checkDeliveryChannelIsOpened(); 216 componentContextImpl.deregisterAllEndpoints(); 217 219 EasyMock.replay(deliveryChannelImpl); 220 EasyMock.replay(loggingUtil); 221 EasyMock.replay(componentContextImpl); 222 EasyMock.replay(queue); 223 224 deliveryChannelImpl.log = loggingUtil; 225 deliveryChannelImpl.queue = queue; 226 deliveryChannelImpl.opened = true; 227 deliveryChannelImpl.context = componentContextImpl; 228 229 deliveryChannelImpl.close(); 230 assertFalse(deliveryChannelImpl.opened); 231 } 232 233 public void testCreateExchangeFactory() { 234 ComponentContextImpl componentContextImpl = EasyMock 235 .createMock(ComponentContextImpl.class); 236 237 ConsumerEndpoint consumerEndpoint = new ConsumerEndpoint("compo", "0"); 238 EasyMock.expect(componentContextImpl.getAddress()).andReturn( 239 consumerEndpoint).anyTimes(); 240 241 EasyMock.replay(componentContextImpl); 242 243 DeliveryChannelImpl deliveryChannelImpl = new DeliveryChannelImpl( 244 componentContextImpl, EasyMock.createMock(RouterImpl.class), null); 245 246 deliveryChannelImpl.logger = new MockLogger(); 247 deliveryChannelImpl.context = componentContextImpl; 248 249 MessageExchangeFactory messageExchangeFactory = deliveryChannelImpl 250 .createExchangeFactory(); 251 assertEquals( 252 ((MessageExchangeFactoryImpl) messageExchangeFactory).consumerEndpoint 253 .getComponentName(), consumerEndpoint.getComponentName()); 254 } 255 256 public void testCreateExchangeFactoryWithInterfaceName() { 257 ComponentContextImpl componentContextImpl = EasyMock 258 .createMock(ComponentContextImpl.class); 259 260 ConsumerEndpoint consumerEndpoint = new ConsumerEndpoint("compo", "0"); 261 EasyMock.expect(componentContextImpl.getAddress()).andReturn( 262 consumerEndpoint).anyTimes(); 263 264 EasyMock.replay(componentContextImpl); 265 266 DeliveryChannelImpl deliveryChannelImpl = new DeliveryChannelImpl( 267 componentContextImpl, EasyMock.createMock(RouterImpl.class), null); 268 269 deliveryChannelImpl.logger = new MockLogger(); 270 deliveryChannelImpl.context = componentContextImpl; 271 272 MessageExchangeFactory messageExchangeFactory = deliveryChannelImpl 273 .createExchangeFactory(new QName ("inter")); 274 assertEquals( 275 ((MessageExchangeFactoryImpl) messageExchangeFactory).defaultInterfaceName, 276 new QName ("inter")); 277 } 278 279 public void testCreateExchangeFactoryWithEndpoint() { 280 ComponentContextImpl componentContextImpl = EasyMock 281 .createMock(ComponentContextImpl.class); 282 ServiceEndpoint serviceEndpoint = EasyMock 283 .createMock(ServiceEndpoint.class); 284 ConsumerEndpoint consumerEndpoint = new ConsumerEndpoint("compo", "0"); 285 EasyMock.expect(componentContextImpl.getAddress()).andReturn( 286 consumerEndpoint).anyTimes(); 287 288 EasyMock.replay(componentContextImpl); 289 290 DeliveryChannelImpl deliveryChannelImpl = new DeliveryChannelImpl( 291 componentContextImpl, EasyMock.createMock(RouterImpl.class), null); 292 293 deliveryChannelImpl.logger = new MockLogger(); 294 deliveryChannelImpl.context = componentContextImpl; 295 296 MessageExchangeFactory messageExchangeFactory = deliveryChannelImpl 297 .createExchangeFactory(serviceEndpoint); 298 assertEquals( 299 ((MessageExchangeFactoryImpl) messageExchangeFactory).consumerEndpoint 300 .getComponentName(), consumerEndpoint.getComponentName()); 301 assertEquals( 302 ((MessageExchangeFactoryImpl) messageExchangeFactory).defaultServiceEndpoint, 303 serviceEndpoint); 304 } 305 306 public void testCreateExchangeFactoryWithServiceName() { 307 ComponentContextImpl componentContextImpl = EasyMock 308 .createMock(ComponentContextImpl.class); 309 ConsumerEndpoint consumerEndpoint = new ConsumerEndpoint("compo", "0"); 310 EasyMock.expect(componentContextImpl.getAddress()).andReturn( 311 consumerEndpoint).anyTimes(); 312 313 EasyMock.replay(componentContextImpl); 314 315 DeliveryChannelImpl deliveryChannelImpl = new DeliveryChannelImpl( 316 componentContextImpl, EasyMock.createMock(RouterImpl.class), null); 317 318 deliveryChannelImpl.logger = new MockLogger(); 319 deliveryChannelImpl.context = componentContextImpl; 320 321 MessageExchangeFactory messageExchangeFactory = deliveryChannelImpl 322 .createExchangeFactoryForService(new QName ("serv")); 323 assertEquals( 324 ((MessageExchangeFactoryImpl) messageExchangeFactory).consumerEndpoint 325 .getComponentName(), consumerEndpoint.getComponentName()); 326 assertEquals( 327 ((MessageExchangeFactoryImpl) messageExchangeFactory).defaultServiceName, 328 new QName ("serv")); 329 } 330 331 public void testIsOpened() { 332 DeliveryChannelImpl deliveryChannelImpl = new DeliveryChannelImpl( 333 EasyMock.createMock(ComponentContextImpl.class), EasyMock 334 .createMock(RouterImpl.class), null); 335 deliveryChannelImpl.opened = true; 336 assertTrue(deliveryChannelImpl.isOpened()); 337 } 338 339 public void testPush() throws SecurityException , NoSuchMethodException , 340 IllegalArgumentException , InterruptedException , JBIException { 341 DeliveryChannelImpl deliveryChannelImpl = EasyMock.createMock( 342 DeliveryChannelImpl.class, new Method [] {DeliveryChannelImpl.class 343 .getDeclaredMethod("checkDeliveryChannelIsOpened", 344 new Class [] {})}); 345 MessageExchangeImpl messageExchangeImpl = EasyMock 346 .createMock(MessageExchangeImpl.class); 347 LoggingUtil loggingUtil = EasyMock.createMock(LoggingUtil.class); 348 MessageExchange.Role role = MessageExchange.Role.CONSUMER; 349 MessageExchangeDecorator messageExchangeDecorator = EasyMock 350 .createMock(MessageExchangeDecorator.class); 351 352 messageExchangeDecorator.setMessageExchange(messageExchangeImpl); 353 deliveryChannelImpl.checkDeliveryChannelIsOpened(); 354 messageExchangeDecorator.setWaitingOnSynchronousSend(false); 355 EasyMock.expect(messageExchangeDecorator.getRole()).andReturn(role).anyTimes(); 356 EasyMock.expect(messageExchangeDecorator.getExchangeId()).andReturn("1").anyTimes(); 357 EasyMock.expect(messageExchangeDecorator.getMessageExchange()).andReturn(messageExchangeImpl).anyTimes(); 358 359 EasyMock.expect(messageExchangeImpl.getRole()).andReturn(role).anyTimes(); 360 EasyMock.expect(messageExchangeImpl.getExchangeId()).andReturn("1").anyTimes(); 361 362 EasyMock.replay(deliveryChannelImpl); 363 EasyMock.replay(messageExchangeImpl); 364 EasyMock.replay(messageExchangeDecorator); 365 366 deliveryChannelImpl.log = loggingUtil; 367 deliveryChannelImpl.waitingExchanges = new HashMap <String , MessageExchangeDecorator>(); 368 deliveryChannelImpl.waitingExchanges.put(deliveryChannelImpl.waitingExchangeKey(messageExchangeDecorator), messageExchangeDecorator); 369 370 deliveryChannelImpl.push(messageExchangeImpl); 371 } 372 373 @SuppressWarnings ("unchecked") 374 public void testPush1() throws SecurityException , NoSuchMethodException , 375 IllegalArgumentException , InterruptedException , JBIException { 376 DeliveryChannelImpl deliveryChannelImpl = EasyMock.createMock( 377 DeliveryChannelImpl.class, new Method [] {DeliveryChannelImpl.class 378 .getDeclaredMethod("checkDeliveryChannelIsOpened", 379 new Class [] {})}); 380 MessageExchange.Role role = MessageExchange.Role.CONSUMER; 381 MessageExchangeImpl messageExchangeImpl = EasyMock 382 .createMock(MessageExchangeImpl.class); 383 LoggingUtil loggingUtil = EasyMock.createMock(LoggingUtil.class); 384 LinkedBlockingQueue <MessageExchangeImpl> queue = EasyMock 385 .createMock( LinkedBlockingQueue .class); 386 387 deliveryChannelImpl.checkDeliveryChannelIsOpened(); 388 EasyMock.expect(messageExchangeImpl.getRole()).andReturn(role).anyTimes(); 389 EasyMock.expect(messageExchangeImpl.getExchangeId()).andReturn("1").anyTimes(); 390 391 queue.put(messageExchangeImpl); 392 393 EasyMock.replay(deliveryChannelImpl); 394 EasyMock.replay(messageExchangeImpl); 395 EasyMock.replay(queue); 396 397 deliveryChannelImpl.log = loggingUtil; 398 deliveryChannelImpl.waitingExchanges = new HashMap <String , MessageExchangeDecorator>(); 399 deliveryChannelImpl.waitingExchanges.put("1", null); 400 deliveryChannelImpl.queue = queue; 401 402 deliveryChannelImpl.push(messageExchangeImpl); 403 } 404 405 public void testSend() throws SecurityException , NoSuchMethodException , 406 MessagingException, RoutingException { 407 DeliveryChannelImpl deliveryChannelImpl = EasyMock.createMock( 408 DeliveryChannelImpl.class, new Method [] { 409 DeliveryChannelImpl.class.getDeclaredMethod( 410 "checkDeliveryChannelIsOpened", new Class [] {}), 411 DeliveryChannelImpl.class 412 .getDeclaredMethod("checkMessageExchange", 413 new Class [] {MessageExchange.class})}); 414 MessageExchangeDecorator messageExchange = EasyMock 415 .createMock(MessageExchangeDecorator.class); 416 MessageExchangeImpl messageExchangeImpl = EasyMock 417 .createMock(MessageExchangeImpl.class); 418 RouterImpl routerImpl = EasyMock.createMock(RouterImpl.class); 419 420 EasyMock.expect(messageExchangeImpl.getStatus()).andReturn( 421 ExchangeStatus.DONE); 422 messageExchangeImpl.setTerminated(true); 423 messageExchange.setProperty(DeliveryChannelImpl.PROPERTY_SENDSYNC,null); 424 deliveryChannelImpl.checkDeliveryChannelIsOpened(); 425 deliveryChannelImpl.checkMessageExchange(messageExchange); 426 EasyMock.expect(messageExchange.getMessageExchange()).andReturn( 427 messageExchangeImpl); 428 routerImpl.send(null, messageExchangeImpl,0); 429 430 EasyMock.replay(messageExchange); 431 EasyMock.replay(routerImpl); 432 EasyMock.replay(deliveryChannelImpl); 433 EasyMock.replay(messageExchangeImpl); 434 435 deliveryChannelImpl.log = EasyMock.createMock(LoggingUtil.class); 436 deliveryChannelImpl.router = routerImpl; 437 438 deliveryChannelImpl.send(messageExchange); 439 } 440 441 public void testSendSync() throws SecurityException , NoSuchMethodException , 442 MessagingException, RoutingException { 443 DeliveryChannelImpl deliveryChannelImpl = EasyMock.createMock( 444 DeliveryChannelImpl.class, new Method [] {DeliveryChannelImpl.class 445 .getDeclaredMethod("sendSync", new Class [] { 446 MessageExchange.class, long.class})}); 447 LoggingUtil loggingUtil = EasyMock.createMock(LoggingUtil.class); 448 MessageExchange messageExchange = EasyMock 449 .createMock(MessageExchange.class); 450 messageExchange.setProperty(DeliveryChannelImpl.PROPERTY_SENDSYNC,"true"); 451 452 loggingUtil.call(); 453 EasyMock.expect(deliveryChannelImpl.sendSync(messageExchange, 0)) 454 .andReturn(Boolean.TRUE); 455 456 EasyMock.replay(deliveryChannelImpl); 457 EasyMock.replay(loggingUtil); 458 EasyMock.replay(messageExchange); 459 460 deliveryChannelImpl.log = loggingUtil; 461 462 deliveryChannelImpl.sendSync(messageExchange); 463 } 464 465 public void testSendSyncTime() throws SecurityException , 466 NoSuchMethodException , MessagingException, RoutingException { 467 DeliveryChannelImpl deliveryChannelImpl = EasyMock.createMock( 468 DeliveryChannelImpl.class, 469 new Method [] {DeliveryChannelImpl.class.getDeclaredMethod("sendExchange", 470 new Class [] {MessageExchange.class,Long.TYPE})}); 471 LoggingUtil loggingUtil = EasyMock.createNiceMock(LoggingUtil.class); 472 MessageExchange.Role role = MessageExchange.Role.CONSUMER; 473 MessageExchangeDecorator messageExchangeDecorator = EasyMock 474 .createMock(MessageExchangeDecorator.class); 475 MessageExchangeImpl messageExchangeImpl = EasyMock 476 .createMock(MessageExchangeImpl.class); 477 478 messageExchangeDecorator.setProperty(DeliveryChannelImpl.PROPERTY_SENDSYNC,"true"); 479 messageExchangeDecorator.setWaitingOnSynchronousSend(true); 480 481 EasyMock.expect(messageExchangeDecorator.getExchangeId()) 482 .andReturn("1").anyTimes(); 483 EasyMock.expect(messageExchangeDecorator.getRole()) 484 .andReturn(role).anyTimes(); 485 486 deliveryChannelImpl.sendExchange(messageExchangeDecorator, 5); 487 EasyMock.expect(messageExchangeDecorator.isWaitingOnSynchronousSend()) 488 .andReturn(true).anyTimes(); 489 EasyMock.expect(messageExchangeDecorator.getMessageExchange()) 490 .andReturn(messageExchangeImpl).anyTimes(); 491 EasyMock.expect(messageExchangeDecorator.getObserverRole()).andReturn( 492 Role.CONSUMER); 493 messageExchangeImpl.setRole(Role.CONSUMER); 494 EasyMock.expect(messageExchangeImpl.getRole()).andReturn( 495 Role.CONSUMER).anyTimes(); 496 EasyMock.expect(messageExchangeImpl.getExchangeId()).andReturn( 497 "").anyTimes(); 498 messageExchangeDecorator.setStatus(ExchangeStatus.ERROR); 499 messageExchangeImpl.setTerminated(true); 500 501 EasyMock.replay(messageExchangeImpl); 502 EasyMock.replay(deliveryChannelImpl); 503 EasyMock.replay(messageExchangeDecorator); 504 EasyMock.replay(loggingUtil); 505 506 deliveryChannelImpl.log = loggingUtil; 507 deliveryChannelImpl.waitingExchanges = new HashMap <String , MessageExchangeDecorator>(); 508 509 deliveryChannelImpl.sendSync(messageExchangeDecorator, 5); 510 } 511 } 512 | Popular Tags |