1 22 package org.jboss.ejb3.test.mdb.unit; 23 24 import java.util.Enumeration ; 25 import java.util.List ; 26 27 import javax.jms.DeliveryMode ; 28 import javax.jms.MessageProducer ; 29 import javax.jms.Queue ; 30 import javax.jms.QueueBrowser ; 31 import javax.jms.QueueConnection ; 32 import javax.jms.QueueConnectionFactory ; 33 import javax.jms.QueueReceiver ; 34 import javax.jms.QueueSender ; 35 import javax.jms.QueueSession ; 36 import javax.jms.TextMessage ; 37 import javax.jms.Topic ; 38 import javax.jms.TopicConnection ; 39 import javax.jms.TopicConnectionFactory ; 40 import javax.jms.TopicSession ; 41 import javax.management.ObjectName ; 42 import javax.naming.InitialContext ; 43 import javax.naming.NamingException ; 44 45 import junit.framework.Test; 46 47 import org.jboss.ejb3.ClientKernelAbstraction; 48 import org.jboss.ejb3.InitialContextFactory; 49 import org.jboss.ejb3.KernelAbstractionFactory; 50 import org.jboss.ejb3.test.mdb.Stateless; 51 import org.jboss.ejb3.test.mdb.TestStatus; 52 import org.jboss.logging.Logger; 53 import org.jboss.security.SecurityAssociation; 54 import org.jboss.security.SimplePrincipal; 55 import org.jboss.test.JBossTestCase; 56 import org.jboss.util.collection.CollectionsUtil; 57 58 64 public class MDBUnitTestCase extends JBossTestCase 65 { 66 @SuppressWarnings ("unused") 67 private static final Logger log = Logger.getLogger(MDBUnitTestCase.class); 68 69 static boolean deployed = false; 70 71 static int test = 0; 72 73 public MDBUnitTestCase(String name) 74 { 75 76 super(name); 77 78 } 79 80 private static void removeAllMessagesFromDLQ() throws Exception 81 { 82 ObjectName dlqName = new ObjectName ("jboss.messaging.destination:name=DLQ,service=Queue"); 83 ClientKernelAbstraction kernel = KernelAbstractionFactory.getClientInstance(); 84 kernel.invoke(dlqName, "removeAllMessages", new Object [0], new String [0]); 85 } 86 87 public void testOverrideQueue() throws Exception 88 { 89 SecurityAssociation.setPrincipal(new SimplePrincipal("anyone")); 90 SecurityAssociation.setCredential("password".toCharArray()); 91 92 TestStatus status = (TestStatus) getInitialContext().lookup( 93 "TestStatusBean/remote"); 94 clear(status); 95 QueueConnection cnn = null; 96 QueueSender sender = null; 97 QueueSession session = null; 98 99 Queue queue = (Queue ) getInitialContext().lookup( 100 "queue/overridequeuetest"); 101 QueueConnectionFactory factory = getQueueConnectionFactory(); 102 cnn = factory.createQueueConnection(); 103 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 104 105 TextMessage msg = session.createTextMessage("Hello World"); 106 107 sender = session.createSender(queue); 108 sender.send(msg); 109 session.close(); 110 cnn.close(); 111 112 Thread.sleep(5000); 113 assertEquals(1, status.overrideQueueFired()); 114 } 115 116 public void testNondurableQueue() throws Exception 117 { 118 SecurityAssociation.setPrincipal(new SimplePrincipal("anyone")); 119 SecurityAssociation.setCredential("password".toCharArray()); 120 121 TestStatus status = (TestStatus) getInitialContext().lookup( 122 "TestStatusBean/remote"); 123 clear(status); 124 QueueConnection cnn = null; 125 QueueSender sender = null; 126 QueueSession session = null; 127 128 Queue queue = (Queue ) getInitialContext().lookup( 129 "queue/nondurablemdbtest"); 130 QueueConnectionFactory factory = getQueueConnectionFactory(); 131 cnn = factory.createQueueConnection(); 132 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 133 134 TextMessage msg = session.createTextMessage("Hello World"); 135 msg.setJMSDeliveryMode(DeliveryMode.NON_PERSISTENT); 136 137 sender = session.createSender(queue); 138 sender.setDeliveryMode(DeliveryMode.NON_PERSISTENT); 139 sender.send(msg); 140 session.close(); 141 cnn.close(); 142 143 Thread.sleep(2000); 144 assertEquals(1, status.nondurableQueueFired()); 145 } 146 147 public void testDefaultedQueue() throws Exception 148 { 149 SecurityAssociation.setPrincipal(new SimplePrincipal("anyone")); 150 SecurityAssociation.setCredential("password".toCharArray()); 151 152 TestStatus status = (TestStatus) getInitialContext().lookup( 153 "TestStatusBean/remote"); 154 clear(status); 155 QueueConnection cnn = null; 156 QueueSender sender = null; 157 QueueSession session = null; 158 159 Queue queue = (Queue ) getInitialContext() 160 .lookup("queue/defaultedmdbtest"); 161 QueueConnectionFactory factory = getQueueConnectionFactory(); 162 cnn = factory.createQueueConnection(); 163 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 164 165 TextMessage msg = session.createTextMessage("Hello World"); 166 167 sender = session.createSender(queue); 168 sender.send(msg); 169 session.close(); 170 cnn.close(); 171 172 Thread.sleep(2000); 173 assertEquals(1, status.defaultedQueueFired()); 174 } 175 176 public void testOverrideDefaultedQueue() throws Exception 177 { 178 SecurityAssociation.setPrincipal(new SimplePrincipal("anyone")); 179 SecurityAssociation.setCredential("password".toCharArray()); 180 181 TestStatus status = (TestStatus) getInitialContext().lookup( 182 "TestStatusBean/remote"); 183 clear(status); 184 QueueConnection cnn = null; 185 QueueSender sender = null; 186 QueueSession session = null; 187 188 Queue queue = (Queue ) getInitialContext().lookup( 189 "queue/overridedefaultedmdbtest"); 190 QueueConnectionFactory factory = getQueueConnectionFactory(); 191 cnn = factory.createQueueConnection(); 192 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 193 194 TextMessage msg = session.createTextMessage("Hello World"); 195 196 sender = session.createSender(queue); 197 sender.send(msg); 198 session.close(); 199 cnn.close(); 200 201 Thread.sleep(2000); 202 assertEquals(1, status.overrideDefaultedQueueFired()); 203 } 204 205 public void testQueue() throws Exception 206 { 207 setSecurity("anyone", "password"); 208 209 TestStatus status = (TestStatus) getInitialContext().lookup( 210 "TestStatusBean/remote"); 211 clear(status); 212 QueueConnection cnn = null; 213 QueueSender sender = null; 214 QueueSession session = null; 215 216 Queue queue = (Queue ) getInitialContext().lookup("queue/mdbtest"); 217 QueueConnectionFactory factory = getQueueConnectionFactory(); 218 cnn = factory.createQueueConnection(); 219 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 220 221 TextMessage msg = session.createTextMessage("Hello World"); 222 223 sender = session.createSender(queue); 224 sender.send(msg); 225 sender.send(msg); 226 sender.send(msg); 227 sender.send(msg); 228 sender.send(msg); 229 session.close(); 230 cnn.close(); 231 232 Thread.sleep(2000); 233 assertEquals(5, status.queueFired()); 234 assertTrue(status.interceptedQueue()); 235 assertTrue(status.postConstruct()); 236 assertEquals(5, status.messageCount()); 237 238 241 Stateless stateless = (Stateless) getInitialContext().lookup("Stateless"); 242 assertNotNull(stateless); 243 String state = stateless.getState(); 244 assertEquals("Set", state); 245 } 246 247 public void testTopic() throws Exception 248 { 249 TestStatus status = (TestStatus) getInitialContext().lookup( 250 "TestStatusBean/remote"); 251 clear(status); 252 TopicConnection cnn = null; 253 MessageProducer sender = null; 254 TopicSession session = null; 255 256 Topic topic = (Topic ) getInitialContext().lookup("topic/mdbtest"); 257 TopicConnectionFactory factory = getTopicConnectionFactory(); 258 cnn = factory.createTopicConnection(); 259 session = cnn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE); 260 261 TextMessage msg = session.createTextMessage("Hello World"); 262 263 sender = session.createProducer(topic); 264 sender.send(msg); 265 session.close(); 266 cnn.close(); 267 268 Thread.sleep(2000); 269 assertEquals(1, status.topicFired()); 270 assertTrue(status.interceptedTopic()); 271 assertFalse(status.postConstruct()); 272 assertFalse(status.preDestroy()); 273 } 274 275 public void testRuntimeException() throws Exception 276 { 277 TestStatus status = (TestStatus) getInitialContext().lookup( 278 "TestStatusBean/remote"); 279 clear(status); 280 QueueConnection cnn = null; 281 QueueSender sender = null; 282 QueueSession session = null; 283 284 Queue queue = (Queue ) getInitialContext().lookup("queue/bmtmdbtest"); 285 QueueConnectionFactory factory = getQueueConnectionFactory(); 286 cnn = factory.createQueueConnection(); 287 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 288 289 TextMessage msg = session.createTextMessage("Hello World"); 290 msg.setIntProperty("JMS_JBOSS_REDELIVERY_LIMIT", 1); 291 292 sender = session.createSender(queue); 293 sender.send(msg); 294 session.close(); 295 cnn.close(); 296 297 Thread.sleep(2000); 298 assertEquals(1, status.bmtQueueRan()); 299 } 300 301 public void testRuntimeExceptionCMT() throws Exception 302 { 303 TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote"); 304 clear(status); 305 QueueConnection cnn = null; 306 QueueSender sender = null; 307 QueueSession session = null; 308 309 Queue queue = (Queue ) getInitialContext().lookup("queue/cmtmdbtest"); 310 QueueConnectionFactory factory = getQueueConnectionFactory(); 311 cnn = factory.createQueueConnection(); 312 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 313 314 TextMessage msg = session.createTextMessage("Hello World"); 315 msg.setIntProperty("JMS_JBOSS_REDELIVERY_LIMIT", 1); 316 317 sender = session.createSender(queue); 318 sender.send(msg); 319 session.close(); 320 cnn.close(); 321 322 Thread.sleep(2000); 323 assertEquals(1, status.cmtQueueRan()); 324 } 325 326 329 public void testExpiredMessagesMove() throws Exception 330 { 331 QueueConnection connection = null; 332 QueueSender sender = null; 333 QueueSession session = null; 334 335 QueueConnectionFactory factory = getQueueConnectionFactory(); 336 connection = factory.createQueueConnection(); 337 connection.start(); 338 session = connection.createQueueSession(false, 339 QueueSession.AUTO_ACKNOWLEDGE); 340 341 Queue queue = (Queue ) getInitialContext().lookup("queue/expirytest"); 342 sender = session.createSender(queue); 343 344 Queue dlq = (Queue ) getInitialContext().lookup("queue/DLQ"); 345 346 removeAllMessagesFromDLQ(); 347 348 long now = System.currentTimeMillis(); 349 350 TextMessage message = session.createTextMessage(); 351 message.setStringProperty("foo", "bar"); 352 message.setStringProperty("null", null); 353 String text = "expire on server"; 354 message.setText(text); 355 356 System.out.println("!!!! sending ..."); 357 sender.send(message, DeliveryMode.NON_PERSISTENT, 4, 1); 358 359 System.out.println("!!!! sleeping ..."); 360 Thread.sleep(2000); 361 362 assertSize(session, queue, 0); 363 assertSize(session, dlq, 1); 364 365 QueueReceiver receiver = session.createReceiver(dlq); 366 message = (TextMessage ) receiver.receiveNoWait(); 367 assertNotNull(message); 368 assertEquals("QUEUE.expirytest", message 369 .getStringProperty("JBOSS_ORIG_DESTINATION")); 370 assertTrue(message.getLongProperty("JBOSS_ORIG_EXPIRATION") > now); 371 assertEquals(0L, message.getJMSExpiration()); 372 assertEquals(text, message.getText()); 373 assertEquals("bar", message.getStringProperty("foo")); 374 assertNull(message.getStringProperty("null")); 375 376 sender.close(); 377 receiver.close(); 378 379 session.close(); 380 connection.close(); 381 } 382 383 public void testDlqMaxResent() throws Exception 384 { 385 QueueConnection connection = null; 386 QueueSender sender = null; 387 QueueSession session = null; 388 389 QueueConnectionFactory factory = getQueueConnectionFactory(); 390 connection = factory.createQueueConnection(); 391 connection.start(); 392 session = connection.createQueueSession(false, 393 QueueSession.AUTO_ACKNOWLEDGE); 394 395 Queue queue = (Queue ) getInitialContext().lookup("queue/dlqtest"); 396 sender = session.createSender(queue); 397 398 Queue dlq = (Queue ) getInitialContext().lookup("queue/DLQ"); 399 400 removeAllMessagesFromDLQ(); 401 402 TextMessage message = session.createTextMessage(); 403 message.setStringProperty("foo", "bar"); 404 message.setStringProperty("null", null); 405 String text = "expire on server"; 406 message.setText(text); 407 408 sender.send(message); 409 410 Thread.sleep(1000); 411 412 assertSize(session, queue, 0); 413 assertSize(session, dlq, 1); 414 415 QueueReceiver receiver = session.createReceiver(dlq); 416 message = (TextMessage ) receiver.receiveNoWait(); 417 assertNotNull(message); 418 assertEquals("JBossQueue[dlqtest]", message 423 .getStringProperty("JBOSS_ORIG_DESTINATION")); 424 assertEquals(0L, message.getJMSExpiration()); 425 assertEquals(text, message.getText()); 426 assertEquals("bar", message.getStringProperty("foo")); 427 assertNull(message.getStringProperty("null")); 428 429 sender.close(); 430 receiver.close(); 431 432 session.close(); 433 connection.close(); 434 } 435 436 public void testRollbackCMT() throws Exception 437 { 438 TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote"); 439 clear(status); 440 QueueConnection cnn = null; 441 QueueSender sender = null; 442 QueueSession session = null; 443 444 Queue queue = (Queue ) getInitialContext().lookup("queue/rollbackcmtmdbtest"); 445 QueueConnectionFactory factory = getQueueConnectionFactory(); 446 cnn = factory.createQueueConnection(); 447 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 448 449 TextMessage msg = session.createTextMessage("Hello World"); 450 msg.setIntProperty("JMS_JBOSS_REDELIVERY_LIMIT", 1); 451 452 sender = session.createSender(queue); 453 sender.send(msg); 454 session.close(); 455 cnn.close(); 456 457 Thread.sleep(2000); 458 assertEquals(1, status.rollbackCmtQueueRan()); 459 } 460 461 private List list(QueueSession session, Queue queue) throws Exception 462 { 463 QueueBrowser browser = session.createBrowser(queue); 464 Enumeration e = browser.getEnumeration(); 465 List messages = CollectionsUtil.list(e); 466 467 browser.close(); 468 469 return messages; 470 } 471 472 private int size(QueueSession session, Queue queue) throws Exception 473 { 474 List l = list(session, queue); 475 return l.size(); 476 } 477 478 private void assertSize(QueueSession session, Queue queue, int size) 479 throws Exception 480 { 481 assertEquals(size, size(session, queue)); 482 } 483 484 protected QueueConnectionFactory getQueueConnectionFactory() 485 throws Exception 486 { 487 try 488 { 489 return (QueueConnectionFactory ) getInitialContext().lookup( 490 "ConnectionFactory"); 491 } catch (NamingException e) 492 { 493 return (QueueConnectionFactory ) getInitialContext().lookup( 494 "java:/ConnectionFactory"); 495 } 496 } 497 498 protected TopicConnectionFactory getTopicConnectionFactory() 499 throws Exception 500 { 501 try 502 { 503 return (TopicConnectionFactory ) getInitialContext().lookup( 504 "ConnectionFactory"); 505 } catch (NamingException e) 506 { 507 return (TopicConnectionFactory ) getInitialContext().lookup( 508 "java:/ConnectionFactory"); 509 } 510 } 511 512 protected void clear(TestStatus status) 513 { 514 status.clear(); 515 assertEquals(0, status.bmtQueueRan()); 516 assertEquals(0, status.defaultedQueueFired()); 517 assertEquals(0, status.messageCount()); 518 assertEquals(0, status.nondurableQueueFired()); 519 assertEquals(0, status.overrideDefaultedQueueFired()); 520 assertEquals(0, status.overrideQueueFired()); 521 assertEquals(0, status.queueFired()); 522 assertEquals(0, status.topicFired()); 523 assertFalse(status.interceptedQueue()); 524 assertFalse(status.interceptedTopic()); 525 assertFalse(status.postConstruct()); 526 assertFalse(status.preDestroy()); 527 } 528 529 protected void setSecurity(String user, String password) 530 { 531 SecurityAssociation.setPrincipal(new SimplePrincipal(user)); 532 SecurityAssociation.setCredential(password.toCharArray()); 533 534 InitialContextFactory.setSecurity(user, password); 535 } 536 537 protected InitialContext getInitialContext() throws Exception 538 { 539 return InitialContextFactory.getInitialContext(); 540 } 541 542 public static Test suite() throws Exception 543 { 544 return getDeploySetup(MDBUnitTestCase.class, 545 "mdbtest-service.xml, mdb-test.jar"); 546 } 547 548 } | Popular Tags |