1 22 package org.jboss.ejb3.test.mdbtransactions.unit; 23 24 import javax.jms.Message ; 25 import javax.jms.Queue ; 26 import javax.jms.QueueConnection ; 27 import javax.jms.QueueConnectionFactory ; 28 import javax.jms.QueueReceiver ; 29 import javax.jms.QueueSender ; 30 import javax.jms.QueueSession ; 31 import javax.jms.ObjectMessage ; 32 33 import javax.naming.InitialContext ; 34 import javax.naming.NamingException ; 35 36 import org.jboss.ejb3.InitialContextFactory; 37 import org.jboss.ejb3.test.mdbtransactions.Entity; 38 import org.jboss.ejb3.test.mdbtransactions.StatelessFacade; 39 import org.jboss.ejb3.test.mdbtransactions.TestStatus; 40 import org.jboss.logging.Logger; 41 import org.jboss.test.JBossTestCase; 42 43 import junit.framework.Test; 44 45 48 public class MDBUnitTestCase extends JBossTestCase 49 { 50 private static final Logger log = Logger.getLogger(MDBUnitTestCase.class); 51 52 static boolean deployed = false; 53 54 static int test = 0; 55 56 public MDBUnitTestCase(String name) 57 { 58 59 super(name); 60 61 } 62 63 public void testMdbTransactions() throws Exception 64 { 65 TestStatus status = (TestStatus)getInitialContext().lookup("TestStatusBean/remote"); 66 status.clear(); 67 68 sendMessages("queue/mdbtest", 2); 69 70 Thread.sleep(5000); 71 72 assertEquals(1, status.messageCount()); 73 assertTrue(status.caughtRollback()); 74 } 75 76 protected void sendMessages(String queueName, int numMessages) throws Exception 77 { 78 QueueConnection connection = null; 79 QueueSender sender = null; 80 QueueSession session = null; 81 82 Queue queue = (Queue ) getInitialContext().lookup( 83 queueName); 84 QueueConnectionFactory factory = getQueueConnectionFactory(); 85 connection = factory.createQueueConnection(); 86 connection.start(); 87 session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 88 89 QueueReceiver receiver = session.createReceiver(queue); 90 Message message = receiver.receiveNoWait(); 91 while (message != null) 92 message = receiver.receiveNoWait(); 93 94 Entity entity = new Entity(); 95 entity.setName("test-entity"); 96 entity.setId(1234L); 97 98 StatelessFacade stateless = (StatelessFacade)getInitialContext().lookup("StatelessFacade"); 99 stateless.clear(entity); 100 101 ObjectMessage msg = session.createObjectMessage(entity); 102 103 sender = session.createSender(queue); 104 105 for (int i = 0 ; i < numMessages ; ++i) 106 sender.send(msg); 107 108 session.close(); 109 connection.close(); 110 } 111 112 public void testRollback() throws Exception 113 { 114 TestStatus status = (TestStatus)getInitialContext().lookup("TestStatusBean/remote"); 115 status.clear(); 116 117 sendMessages("queue/rollbackmdbtest", 1); 118 119 Thread.sleep(5000); 120 121 Queue queue = (Queue ) getInitialContext().lookup("queue/DLQ"); 122 QueueConnectionFactory factory = getQueueConnectionFactory(); 123 QueueConnection connection = factory.createQueueConnection(); 124 connection.start(); 125 QueueSession session = connection.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 126 QueueReceiver receiver = session.createReceiver(queue); 127 Message message = receiver.receiveNoWait(); 128 assertNotNull(message); 129 130 session.close(); 131 connection.close(); 132 } 133 134 protected QueueConnectionFactory getQueueConnectionFactory() 135 throws Exception 136 { 137 try 138 { 139 return (QueueConnectionFactory ) getInitialContext().lookup( 140 "ConnectionFactory"); 141 } catch (NamingException e) 142 { 143 return (QueueConnectionFactory ) getInitialContext().lookup( 144 "java:/ConnectionFactory"); 145 } 146 } 147 148 protected InitialContext getInitialContext() throws Exception 149 { 150 return InitialContextFactory.getInitialContext(); 151 } 152 153 public static Test suite() throws Exception 154 { 155 return getDeploySetup(MDBUnitTestCase.class, 156 "mdbtransactionstest-service.xml, mdbtransactions-test.jar"); 157 } 158 159 } | Popular Tags |