1 22 package org.jboss.ejb3.test.standalone.embeddedjms.unit; 23 24 import java.util.Hashtable ; 25 import javax.jms.Queue ; 26 import javax.jms.QueueConnection ; 27 import javax.jms.QueueConnectionFactory ; 28 import javax.jms.QueueSender ; 29 import javax.jms.QueueSession ; 30 import javax.jms.TextMessage ; 31 import javax.jms.Topic ; 32 import javax.jms.TopicConnection ; 33 import javax.jms.TopicConnectionFactory ; 34 import javax.jms.TopicPublisher ; 35 import javax.jms.TopicSession ; 36 import javax.naming.InitialContext ; 37 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap; 38 import org.jboss.ejb3.test.standalone.embeddedjms.TestStatus; 39 import junit.extensions.TestSetup; 40 import junit.framework.Test; 41 import junit.framework.TestCase; 42 import junit.framework.TestSuite; 43 44 50 public class EmbeddedJmsTestCase extends TestCase 51 { 52 private static boolean booted = false; 53 54 public EmbeddedJmsTestCase(String name) 55 { 56 super(name); 57 } 58 59 public static Test suite() throws Exception 60 { 61 TestSuite suite = new TestSuite(); 62 suite.addTestSuite(EmbeddedJmsTestCase.class); 63 64 TestSetup wrapper = new TestSetup(suite) 66 { 67 protected void setUp() 68 { 69 startupEmbeddedJboss(); 70 } 71 72 protected void tearDown() 73 { 74 shutdownEmbeddedJboss(); 75 } 76 }; 77 78 return wrapper; 79 } 80 81 public static void startupEmbeddedJboss() 82 { 83 EJB3StandaloneBootstrap.boot(null); 84 EJB3StandaloneBootstrap.deployXmlResource("jboss-jms-beans.xml"); 85 EJB3StandaloneBootstrap.deployXmlResource("embeddedjms/testjms.xml"); 86 EJB3StandaloneBootstrap.scanClasspath("mdb.jar"); 87 } 88 89 public static void shutdownEmbeddedJboss() 90 { 91 EJB3StandaloneBootstrap.shutdown(); 92 } 93 94 protected InitialContext getInitialContext() throws Exception 95 { 96 return new InitialContext (getInitialContextProperties()); 97 } 98 99 protected Hashtable getInitialContextProperties() 100 { 101 return EJB3StandaloneBootstrap.getInitialContextProperties(); 102 } 103 104 105 public void testArchivesByResource() throws Throwable 106 { 107 InitialContext ctx = getInitialContext(); 108 109 executeQueue(ctx); 110 executeTopic(ctx); 111 executeTemporaryQueue(ctx); 112 } 113 114 115 private void executeQueue(InitialContext ctx) 116 throws Exception 117 { 118 119 TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote"); 120 status.clear(); 121 QueueConnection cnn = null; 122 QueueSender sender = null; 123 QueueSession session = null; 124 125 Queue queue = (Queue ) getInitialContext().lookup("queue/mdbtest"); 126 QueueConnectionFactory factory = (QueueConnectionFactory ) getInitialContext().lookup("java:/ConnectionFactory"); 127 cnn = factory.createQueueConnection(); 128 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 129 130 TextMessage msg = session.createTextMessage("Hello World"); 131 132 sender = session.createSender(queue); 133 sender.send(msg); 134 135 Thread.sleep(1000); 136 assertTrue(status.queueFired()); 137 } 138 139 private void executeTemporaryQueue(InitialContext ctx) 140 throws Exception 141 { 142 TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote"); 143 status.clear(); 144 QueueConnection cnn = null; 145 QueueSender sender = null; 146 QueueSession session = null; 147 148 Queue queue = (Queue ) getInitialContext().lookup("queue/tempmdbtest"); 149 QueueConnectionFactory factory = (QueueConnectionFactory ) getInitialContext().lookup("java:/ConnectionFactory"); 150 cnn = factory.createQueueConnection(); 151 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 152 153 TextMessage msg = session.createTextMessage("Hello World"); 154 155 sender = session.createSender(queue); 156 sender.send(msg); 157 158 Thread.sleep(1000); 159 assertTrue(status.temporaryQueueFired()); 160 } 161 162 private void executeTopic(InitialContext ctx) 163 throws Exception 164 { 165 166 TestStatus status = (TestStatus) getInitialContext().lookup("TestStatusBean/remote"); 167 status.clear(); 168 TopicConnection cnn = null; 169 TopicPublisher sender = null; 170 TopicSession session = null; 171 172 Topic topic = (Topic ) getInitialContext().lookup("topic/topictest"); 173 TopicConnectionFactory factory = (TopicConnectionFactory ) getInitialContext().lookup("java:/ConnectionFactory"); 174 cnn = factory.createTopicConnection(); 175 session = cnn.createTopicSession(false, QueueSession.AUTO_ACKNOWLEDGE); 176 177 TextMessage msg = session.createTextMessage("Hello World"); 178 179 sender = session.createPublisher(topic); 180 sender.send(msg); 181 182 Thread.sleep(1000); 183 assertTrue(status.topicFired()); 184 } 185 186 protected void configureLoggingAfterBootstrap() 187 { 188 } 190 } | Popular Tags |