1 22 package org.jboss.ejb3.test.standalone.jms.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.naming.InitialContext ; 32 import org.apache.log4j.BasicConfigurator; 33 import org.apache.log4j.ConsoleAppender; 34 import org.apache.log4j.FileAppender; 35 import org.apache.log4j.PatternLayout; 36 import org.jboss.ejb3.embedded.EJB3StandaloneBootstrap; 37 import org.jboss.ejb3.test.standalone.jms.TestStatus; 38 import org.jboss.test.JBossTestCase; 39 40 46 public class MDBUnitTestCase extends JBossTestCase 47 { 48 private static boolean booted = false; 49 50 public MDBUnitTestCase(String name) 51 { 52 super(name); 53 } 54 55 protected void setUp() throws Exception 56 { 57 61 super.setUp(); 62 BasicConfigurator.resetConfiguration(); 63 PatternLayout layout = new PatternLayout("%r %-5p [%c{1}] %m%n"); 64 ConsoleAppender appender = new ConsoleAppender(layout); 65 BasicConfigurator.configure(appender); 66 String file = System.getProperty("org.jboss.test.logfile"); 67 if (file != null) 68 { 69 FileAppender fileAppender = new FileAppender(layout, file); 70 BasicConfigurator.configure(fileAppender); 71 } 72 long start = System.currentTimeMillis(); 73 log.info("Starting"); 74 try 75 { 76 if (!booted) 77 { 78 booted = true; 79 EJB3StandaloneBootstrap.boot(""); 80 EJB3StandaloneBootstrap.scanClasspath("jms.jar"); 81 } 82 } 83 catch (Exception e) 84 { 85 throw e; 86 } 87 catch (Throwable t) 88 { 89 throw new RuntimeException (t); 90 } 91 log.info("setup took: " + (System.currentTimeMillis() - start)); 92 } 93 94 @Override 95 protected void tearDown() throws Exception 96 { 97 super.tearDown(); 98 EJB3StandaloneBootstrap.shutdown(); 99 } 100 101 102 protected InitialContext getLocalInitialContext() throws Exception 103 { 104 return new InitialContext (getLocalInitialContextProperties()); 105 } 106 107 protected Hashtable getLocalInitialContextProperties() 108 { 109 return EJB3StandaloneBootstrap.getInitialContextProperties(); 110 } 111 112 public void testQueue() throws Throwable 113 { 114 InitialContext ctx = getLocalInitialContext(); 115 116 executeEJBs(ctx); 117 } 118 119 private void executeEJBs(InitialContext ctx) 120 throws Exception 121 { 122 TestStatus status = (TestStatus) getLocalInitialContext().lookup("TestStatusBean/remote"); 123 status.clear(); 124 QueueConnection cnn = null; 125 QueueSender sender = null; 126 QueueSession session = null; 127 128 Queue queue = (Queue ) getInitialContext().lookup("queue/mdbtest"); 129 QueueConnectionFactory factory = (QueueConnectionFactory ) getInitialContext().lookup("ConnectionFactory"); 130 cnn = factory.createQueueConnection(); 131 session = cnn.createQueueSession(false, QueueSession.AUTO_ACKNOWLEDGE); 132 133 TextMessage msg = session.createTextMessage("Hello World"); 134 135 sender = session.createSender(queue); 136 sender.send(msg); 137 138 Thread.sleep(1000); 139 assertTrue(status.queueFired()); 140 } 141 142 protected void configureLoggingAfterBootstrap() 143 { 144 } 146 152 } | Popular Tags |