1 22 package org.jboss.test.mdb.test; 23 24 import javax.management.ObjectName ; 25 import javax.jms.*; 26 import javax.naming.*; 27 28 import junit.framework.Test; 29 import junit.framework.TestCase; 30 import junit.framework.TestSuite; 31 32 import org.jboss.test.mdb.bean.CustomMessage; 33 34 import org.jboss.test.JBossTestCase; 35 import org.jboss.test.JBossTestSetup; 36 import org.jboss.test.jbossmq.test.SecurityUnitTestCase; 37 38 61 public class MDBUnitTestCase 62 extends JBossTestCase 63 { 64 66 static String TOPIC_FACTORY = "ConnectionFactory"; 68 static String QUEUE_FACTORY = "ConnectionFactory"; 69 70 QueueConnection queueConnection; 71 TopicConnection topicConnection; 72 73 75 String dest; 76 77 public MDBUnitTestCase(String name, String dest) { 78 super(name); 79 this.dest = dest; 80 } 86 87 88 protected void tearDown() throws Exception { 89 if (topicConnection != null) { 90 topicConnection.close(); 91 } 92 if (queueConnection != null) { 93 queueConnection.close(); 94 } 95 } 96 97 protected void printHeader() { 98 getLog().info("\n---- Testing method " + getName() + 99 " for destination " +dest); 100 } 101 102 private QueueSession getQueueSession() throws Exception { 103 if (queueConnection == null) { 104 QueueConnectionFactory queueFactory = 105 (QueueConnectionFactory)getInitialContext().lookup(QUEUE_FACTORY); 106 107 queueConnection = queueFactory.createQueueConnection(); 108 } 109 return queueConnection.createQueueSession(false, 110 Session.AUTO_ACKNOWLEDGE); 111 } 112 113 private TopicSession getTopicSession() throws Exception { 114 if (topicConnection == null) { 115 TopicConnectionFactory topicFactory = 116 (TopicConnectionFactory)getInitialContext().lookup(TOPIC_FACTORY); 117 topicConnection = topicFactory.createTopicConnection(); 118 } 119 120 return topicConnection.createTopicSession(false, 122 Session.AUTO_ACKNOWLEDGE); 123 } 124 125 128 public void testQueue() throws Exception { 129 printHeader(); 130 QueueSession session = getQueueSession(); 131 Queue queue = (Queue)getInitialContext().lookup(dest); 132 QueueSender sender = session.createSender(queue); 133 134 getLog().debug("TestQueue: " + dest + " Sending 10 messages 1-10"); 135 for (int i = 1; i < 11; i++) { 136 TextMessage message = session.createTextMessage(); 137 message.setText("Queue Message " + dest + " nr " + i); 138 sender.send(queue, message); 139 } 140 141 sender.close(); 142 } 143 144 147 public void testTopic() throws Exception { 148 printHeader(); 149 TopicSession session = getTopicSession(); 150 Topic topic = (Topic)getInitialContext().lookup(dest); 151 TopicPublisher pub = session.createPublisher(topic); 152 153 getLog().debug("TestTopic: " + dest + 154 ": Sending 10st messages 1-10"); 155 156 for (int i = 1; i < 11; i++) { 157 TextMessage message = session.createTextMessage(); 158 message.setText("Topic Message " + dest + " nr " + i); 159 pub.publish(topic, message); 160 } 161 162 pub.close(); 163 } 164 165 168 public void testObjectMessage() throws Exception { 169 printHeader(); 170 QueueSession session = getQueueSession(); 171 Queue queue = (Queue)getInitialContext().lookup("queue/testObjectMessage"); 173 QueueSender sender = session.createSender(queue); 174 175 getLog().debug("TestQueue: Sending 10 messages 1-10"); 176 for (int i = 1; i < 11; i++) { 177 ObjectMessage message = session.createObjectMessage(); 178 message.setObject(new CustomMessage(i)); 179 sender.send(queue, message); 180 } 181 182 sender.close(); 183 session.close(); 184 } 185 186 187 public void testWaitForCompleation() throws Exception { 188 try { Thread.currentThread().sleep(1000*20); 189 } catch ( InterruptedException e ) {} 190 } 191 192 public void testNoQueueConstructionForAlreadyExists() 193 throws Exception 194 { 195 try 196 { 197 getInitialContext().lookup("queue/QueueInADifferentContext"); 198 } 199 catch (NamingException expected) 200 { 201 return; 202 } 203 fail("It should not create queue/QueueInADifferentContext"); 204 } 205 206 public void testNoTopicConstructionForAlreadyExists() 207 throws Exception 208 { 209 try 210 { 211 getInitialContext().lookup("topic/TopicInADifferentContext"); 212 } 213 catch (NamingException expected) 214 { 215 return; 216 } 217 fail("It should not create topic/TopicInADifferentContext"); 218 } 219 220 223 public static Test suite() throws Exception 224 { 225 TestSuite suite = new TestSuite(); 226 227 suite.addTest(new MDBUnitTestCase("testNoQueueConstructionForAlreadyExists","")); 229 suite.addTest(new MDBUnitTestCase("testNoTopicConstructionForAlreadyExists","")); 230 suite.addTest(new MDBUnitTestCase("testObjectMessage","")); 231 suite.addTest(new MDBUnitTestCase("testQueue","queue/testQueue")); 232 suite.addTest(new MDBUnitTestCase("testTopic","topic/testTopic")); 233 suite.addTest(new MDBUnitTestCase("testTopic","topic/testDurableTopic")); 234 suite.addTest(new MDBUnitTestCase("testQueue","queue/ex")); 235 suite.addTest(new MDBUnitTestCase("testQueue","queue/A")); 236 suite.addTest(new MDBUnitTestCase("testWaitForCompleation","")); 237 suite.addTest(new MDBUnitTestCase("testQueue","queue/B")); 238 239 Test wrapper = new JBossTestSetup(suite) 240 { 241 protected void setUp() throws Exception 242 { 243 super.setUp(); 244 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 245 deploy (loader.getResource("messaging/test-destinations-full-service.xml").toString()); 246 deploy ("mdb.jar"); 247 } 248 protected void tearDown() throws Exception 249 { 250 super.tearDown(); 251 252 getServer().invoke 254 ( 255 new ObjectName ("jboss.mq.destination:service=Queue,name=DLQ"), 256 "removeAllMessages", 257 new Object [0], 258 new String [0] 259 ); 260 261 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 262 undeploy ("mdb.jar"); 263 264 TopicConnectionFactory topicFactory = (TopicConnectionFactory) getInitialContext().lookup(TOPIC_FACTORY); 266 TopicConnection topicConnection = topicFactory.createTopicConnection("john", "needle"); 267 TopicSession session = topicConnection.createTopicSession(false, Session.AUTO_ACKNOWLEDGE); 268 session.unsubscribe("DurableSubscriberExample"); 269 topicConnection.close(); 270 271 undeploy (loader.getResource("messaging/test-destinations-full-service.xml").toString()); 272 273 } 274 }; 275 276 return wrapper; 277 } 278 } 279 280 281 282 | Popular Tags |