1 22 package org.jboss.ejb3.test.dd.mdb.unit; 23 24 import java.util.ArrayList ; 25 import java.util.HashMap ; 26 import java.util.Iterator ; 27 import javax.jms.ObjectMessage ; 28 import javax.jms.Queue ; 29 import javax.jms.QueueConnection ; 30 import javax.jms.QueueConnectionFactory ; 31 import javax.jms.QueueSender ; 32 import javax.jms.QueueSession ; 33 import javax.jms.Session ; 34 import javax.jms.TextMessage ; 35 import javax.jms.Topic ; 36 import javax.jms.TopicConnection ; 37 import javax.jms.TopicConnectionFactory ; 38 import javax.jms.TopicPublisher ; 39 import javax.jms.TopicSession ; 40 import javax.naming.NamingException ; 41 import org.jboss.ejb3.test.dd.mdb.CustomMessage; 42 import org.jboss.ejb3.test.dd.mdb.TestStatus; 43 import org.jboss.logging.Logger; 44 import org.jboss.test.JBossTestCase; 45 import org.jboss.test.JBossTestSetup; 46 import junit.framework.Test; 47 import junit.framework.TestSuite; 48 49 72 public class MDBUnitTestCase 73 extends JBossTestCase 74 { 75 private static final Logger log = Logger.getLogger(MDBUnitTestCase.class); 76 77 static String TOPIC_FACTORY = "ConnectionFactory"; 79 static String QUEUE_FACTORY = "ConnectionFactory"; 80 81 QueueConnection queueConnection; 82 TopicConnection topicConnection; 83 84 static HashMap jndiToDestName = new HashMap (); 85 static 86 { 87 jndiToDestName.put("queue/testQueue", "QUEUE.testQueue"); 88 jndiToDestName.put("topic/testTopic", "TOPIC.testTopic"); 89 jndiToDestName.put("topic/testDurableTopic", "TOPIC.testDurableTopic"); 90 jndiToDestName.put("queue/ex", "QUEUE.ex"); 91 jndiToDestName.put("queue/A", "QUEUE.A"); 92 jndiToDestName.put("queue/B", "QUEUE.B"); 93 } 94 95 96 98 String dest; 99 100 public MDBUnitTestCase(String name, String dest) { 101 super(name); 102 this.dest = dest; 103 } 109 110 111 protected void tearDown() throws Exception { 112 if (topicConnection != null) { 113 topicConnection.close(); 114 } 115 if (queueConnection != null) { 116 queueConnection.close(); 117 } 118 } 119 120 protected void printHeader() { 121 log.info("\n---- Testing method " + getName() + 122 " for destination " +dest); 123 } 124 125 private QueueSession getQueueSession() throws Exception { 126 if (queueConnection == null) { 127 QueueConnectionFactory queueFactory = 128 (QueueConnectionFactory )getInitialContext().lookup(QUEUE_FACTORY); 129 130 queueConnection = queueFactory.createQueueConnection(); 131 } 132 return queueConnection.createQueueSession(false, 133 Session.AUTO_ACKNOWLEDGE); 134 } 135 136 private TopicSession getTopicSession() throws Exception { 137 if (topicConnection == null) { 138 TopicConnectionFactory topicFactory = 139 (TopicConnectionFactory )getInitialContext().lookup(TOPIC_FACTORY); 140 topicConnection = topicFactory.createTopicConnection(); 141 } 142 143 return topicConnection.createTopicSession(false, 145 Session.AUTO_ACKNOWLEDGE); 146 } 147 148 151 public void testQueue() throws Exception { 152 printHeader(); 153 TestStatus status = (TestStatus)getInitialContext().lookup("TestStatusBean/remote"); 154 status.clear(); 155 156 QueueSession session = getQueueSession(); 157 Queue queue = (Queue )getInitialContext().lookup(dest); 158 QueueSender sender = session.createSender(queue); 159 160 log.debug("TestQueue: " + dest + " Sending 10 messages 1-10"); 161 for (int i = 1; i < 11; i++) { 162 TextMessage message = session.createTextMessage(); 163 message.setText("Queue Message " + dest + " nr " + i); 164 sender.send(queue, message); 165 } 166 167 sender.close(); 168 169 Thread.currentThread().sleep(10000); 170 171 ArrayList destinations = status.getDestinations(); 172 int size = (dest.equals("queue/ex")) ? 110 : 10; 173 174 assertEquals(size, destinations.size()); 175 String destinationName = (String )jndiToDestName.get(dest); 176 for (Iterator it = destinations.iterator() ; it.hasNext() ;) 177 { 178 assertEquals(destinationName, it.next()); 179 } 180 } 181 182 185 public void testTopic() throws Exception { 186 printHeader(); 187 TestStatus status = (TestStatus)getInitialContext().lookup("TestStatusBean/remote"); 188 status.clear(); 189 190 TopicSession session = getTopicSession(); 191 Topic topic = (Topic )getInitialContext().lookup(dest); 192 TopicPublisher pub = session.createPublisher(topic); 193 194 log.debug("TestTopic: " + dest + 195 ": Sending 10st messages 1-10"); 196 197 for (int i = 1; i < 11; i++) { 198 TextMessage message = session.createTextMessage(); 199 message.setText("Topic Message " + dest + " nr " + i); 200 pub.publish(topic, message); 201 } 202 203 pub.close(); 204 205 Thread.currentThread().sleep(10000); 206 207 ArrayList destinations = status.getDestinations(); 208 209 assertEquals(10, destinations.size()); 210 String destinationName = (String )jndiToDestName.get(dest); 211 for (Iterator it = destinations.iterator() ; it.hasNext() ;) 212 { 213 String name = (String )it.next(); 214 assertTrue("Destination name should start with '" + destinationName + "' it was '" + name + "'", name.startsWith(destinationName)); 215 } 216 217 } 218 219 222 public void testObjectMessage() throws Exception { 223 printHeader(); 224 QueueSession session = getQueueSession(); 225 Queue queue = (Queue )getInitialContext().lookup("queue/testObjectMessage"); 227 QueueSender sender = session.createSender(queue); 228 229 log.debug("TestQueue: Sending 10 messages 1-10"); 230 for (int i = 1; i < 11; i++) { 231 ObjectMessage message = session.createObjectMessage(); 232 message.setObject(new CustomMessage(i)); 233 sender.send(queue, message); 234 } 235 236 sender.close(); 237 session.close(); 238 } 239 240 241 public void testWaitForCompletion() throws Exception { 242 try { Thread.currentThread().sleep(1000*20); 243 } catch ( InterruptedException e ) {} 244 } 245 246 public void testNoQueueConstructionForAlreadyExists() 247 throws Exception 248 { 249 try 250 { 251 getInitialContext().lookup("queue/QueueInADifferentContext"); 252 } 253 catch (NamingException expected) 254 { 255 return; 256 } 257 fail("It should not create queue/QueueInADifferentContext"); 258 } 259 260 public void testNoTopicConstructionForAlreadyExists() 261 throws Exception 262 { 263 try 264 { 265 getInitialContext().lookup("topic/TopicInADifferentContext"); 266 } 267 catch (NamingException expected) 268 { 269 return; 270 } 271 fail("It should not create topic/TopicInADifferentContext"); 272 } 273 274 277 public static Test suite() throws Exception 278 { 279 TestSuite suite = new TestSuite(); 280 suite.addTest(new MDBUnitTestCase("testQueue","queue/testQueue")); 281 suite.addTest(new MDBUnitTestCase("testTopic","topic/testTopic")); 282 suite.addTest(new MDBUnitTestCase("testTopic","topic/testDurableTopic")); 283 suite.addTest(new MDBUnitTestCase("testQueue","queue/ex")); 284 suite.addTest(new MDBUnitTestCase("testQueue","queue/A")); 285 suite.addTest(new MDBUnitTestCase("testWaitForCompletion","")); 286 suite.addTest(new MDBUnitTestCase("testQueue","queue/B")); 287 288 return new JBossTestSetup(getDeploySetup(suite, "dd-mdb-jbossmq-destinations-service.xml, dd-mdb-service.xml, dd-mdb.jar")); 289 290 } 291 } 292 293 294 295 | Popular Tags |