1 22 package org.jboss.test.jmsra.test; 23 import javax.jms.Connection ; 24 import javax.jms.Message ; 25 26 import javax.jms.MessageConsumer ; 27 import javax.jms.Session ; 28 import javax.naming.Context ; 29 30 import javax.naming.InitialContext ; 31 import junit.framework.Assert; 32 33 import junit.framework.TestCase; 34 35 import org.jboss.test.JBossTestCase; 36 37 import org.jboss.test.jmsra.bean.*; 38 39 48 public abstract class RaTest 49 extends JBossTestCase 50 { 51 54 public final static long DEFAULT_TIMEOUT = 500L; 55 58 public final static long FLUSH_TIMEOUT = 500L; 59 60 63 protected String beanJNDI; 64 67 protected MessageConsumer consumer; 68 71 protected Publisher publisher; 72 75 protected Connection connection; 76 79 protected Session session; 80 81 88 protected RaTest(final String name, final String beanJNDI) 89 throws Exception 90 { 91 super(name); 92 this.beanJNDI = beanJNDI; 93 } 94 95 100 public void testSimple() throws Exception 101 { 102 printHeader(); 103 getLog().debug("Verify simple send of message"); 104 publisher.simple(1); 105 106 Assert.assertEquals(1, getJmsMessage()); 107 printOK(); 108 } 109 110 115 public void testSimpleFail() throws Exception 116 { 117 printHeader(); 118 getLog().debug("Verify simple failed transaction"); 119 publisher.simpleFail(2); 120 121 Assert.assertEquals(-1, getJmsMessage()); 122 printOK(); 123 } 124 125 130 public void testBeanOk() throws Exception 131 { 132 printHeader(); 133 getLog().debug("Verify bean ok"); 134 publisher.beanOk(3); 135 136 Assert.assertEquals(3, getJmsMessage()); 137 printOK(); 138 } 139 140 145 public void testBeanError() throws Exception 146 { 147 printHeader(); 148 getLog().debug("Verify bean eroor failed transaction"); 149 150 try 151 { 152 publisher.beanError(4); 153 } 154 catch (Exception ignore) 155 { 156 } 157 158 Assert.assertEquals(-1, getJmsMessage()); 159 printOK(); 160 } 161 162 167 protected void setUp() throws Exception 168 { 169 Context context = getInitialContext(); 171 try 172 { 173 PublisherHome home = (PublisherHome)context.lookup(beanJNDI); 174 publisher = home.create(); 175 176 init(context); 177 } 178 finally 179 { 180 context.close(); 181 } 182 183 connection.start(); 185 186 flush(); 188 } 189 190 197 protected int getJmsMessage() throws Exception 198 { 199 return getJmsMessage(DEFAULT_TIMEOUT); 200 } 201 202 210 protected int getJmsMessage(long timeout) throws Exception 211 { 212 Message msg = consumer.receive(timeout); 213 if (msg != null) 214 { 215 getLog().debug("Recived message: " + msg); 216 int nr = msg.getIntProperty(Publisher.JMS_MESSAGE_NR); 217 getLog().debug("nr: " + nr); 218 return nr; 219 } 220 else 221 { 222 getLog().debug("NO message recived"); 223 return -1; 224 } 225 } 226 227 233 protected abstract void init(final Context context) throws Exception ; 234 235 240 protected void tearDown() throws Exception 241 { 242 if (consumer != null) 243 { 244 consumer.close(); 245 } 246 if (connection != null) 247 { 248 connection.close(); 249 } 250 } 251 252 255 protected void printHeader() 256 { 257 getLog().debug("\n---- Testing method " + getName() + 258 " for bean " + beanJNDI); 259 } 260 261 264 protected void printOK() 265 { 266 getLog().debug("---- Test OK\n"); 267 } 268 269 275 protected void flush() throws Exception 276 { 277 279 int nr = 0; 280 do 281 { 282 try 283 { 284 nr = getJmsMessage(FLUSH_TIMEOUT); 285 } 286 catch (Exception ignore) 287 { 288 } 289 } while (nr != -1); 290 } 292 293 public static junit.framework.Test suite() throws Exception 294 { 295 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 296 return getDeploySetup(RaTest.class, 297 loader.getResource("messaging/test-destinations-service.xml").toString()); 298 } 299 300 301 } 302 | Popular Tags |