1 22 package org.jboss.test.jmsra.test; 23 import javax.jms.Connection ; 24 import javax.jms.Message ; 25 26 import javax.jms.QueueConnection ; 27 import javax.jms.QueueConnectionFactory ; 28 import javax.jms.QueueSender ; 29 import javax.jms.QueueSession ; 30 import javax.jms.Session ; 31 import javax.jms.TextMessage ; 32 import javax.jms.Queue ; 33 import javax.naming.Context ; 34 35 import javax.management.ObjectName ; 36 37 import javax.naming.InitialContext ; 38 import junit.framework.Assert; 39 40 import junit.framework.Test; 41 42 import org.jboss.test.JBossTestCase; 43 import org.jboss.test.JBossTestSetup; 44 45 import org.jboss.test.jmsra.bean.*; 46 47 56 57 public class RaSyncRecUnitTestCase extends JBossTestCase { 58 private final static String BEAN_JNDI = "QueueRec"; 59 private final static String QUEUE_FACTORY = "ConnectionFactory"; 60 private final static String QUEUE = "queue/A"; 61 private final static int MESSAGE_NR = 10; 62 63 66 protected QueueConnection connection; 67 70 protected QueueSession session; 71 74 protected QueueSender sender; 75 76 79 protected QueueRec rec; 80 81 88 public RaSyncRecUnitTestCase(String name) { 89 super(name); 90 } 91 92 97 protected void setUp() throws Exception 98 { 99 Context context = getInitialContext(); 101 try 102 { 103 QueueRecHome home = (QueueRecHome)context.lookup(BEAN_JNDI); 104 rec = home.create(); 105 106 init(context); 107 } 108 finally 109 { 110 context.close(); 111 } 112 113 connection.start(); 115 116 } 117 118 124 protected void init(final Context context) throws Exception 125 { 126 QueueConnectionFactory factory = 127 (QueueConnectionFactory )context.lookup(QUEUE_FACTORY); 128 129 connection = factory.createQueueConnection(); 130 131 session = ((QueueConnection )connection).createQueueSession(false, Session.AUTO_ACKNOWLEDGE); 132 133 Queue queue = (Queue )context.lookup(QUEUE); 134 135 sender = ((QueueSession )session).createSender(queue); 136 } 137 138 143 protected void tearDown() throws Exception 144 { 145 if (sender != null) 146 { 147 sender.close(); 148 } 149 if (session != null) { 150 session.close(); 151 } 152 if (connection != null) 153 { 154 connection.close(); 155 } 156 } 157 158 161 public void testSyncRec() throws Exception { 162 TextMessage message = session.createTextMessage(); 164 message.setText(String.valueOf(MESSAGE_NR)); 165 message.setIntProperty(Publisher.JMS_MESSAGE_NR, MESSAGE_NR); 166 sender.send(message); 167 getLog().debug("sent message with nr = " + MESSAGE_NR); 168 169 int res = rec.getMessage(); 171 Assert.assertEquals(MESSAGE_NR, res); 172 getLog().debug("testSyncRec() OK"); 173 } 174 175 public static Test suite() throws Exception 176 { 177 return new JBossTestSetup(getDeploySetup(RaSyncRecUnitTestCase.class, "jmsra.jar")) 178 { 179 protected void setUp() throws Exception 180 { 181 super.setUp(); 182 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 183 deploy (loader.getResource("messaging/test-destinations-full-service.xml").toString()); 184 } 185 186 protected void tearDown() throws Exception 187 { 188 super.tearDown(); 189 190 getServer().invoke 192 ( 193 new ObjectName ("jboss.mq.destination:service=Queue,name=testQueue"), 194 "removeAllMessages", 195 new Object [0], 196 new String [0] 197 ); 198 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 199 undeploy (loader.getResource("messaging/test-destinations-full-service.xml").toString()); 200 } 201 }; 202 } 203 } 205 206 207 208 209 | Popular Tags |