1 22 package org.jboss.test.jbossmessaging.ra; 23 24 import javax.jms.Connection ; 25 import javax.jms.Message ; 26 27 import javax.jms.MessageConsumer ; 28 import javax.jms.Session ; 29 import javax.naming.Context ; 30 31 import javax.naming.InitialContext ; 32 import junit.framework.Assert; 33 34 import junit.framework.TestCase; 35 36 import org.jboss.test.jbossmessaging.JMSTestCase; 37 38 import org.jboss.test.jmsra.bean.*; 39 40 50 public abstract class RaTest extends JMSTestCase 51 { 52 55 public final static long DEFAULT_TIMEOUT = 500L; 56 59 public final static long FLUSH_TIMEOUT = 500L; 60 61 64 protected String beanJNDI; 65 68 protected MessageConsumer consumer; 69 72 protected Publisher publisher; 73 76 protected Connection connection; 77 80 protected Session session; 81 82 89 protected RaTest(final String name, final String beanJNDI) 90 throws Exception 91 { 92 super(name); 93 this.beanJNDI = beanJNDI; 94 } 95 96 101 public void testSimple() throws Exception 102 { 103 printHeader(); 104 getLog().debug("Verify simple send of message"); 105 publisher.simple(1); 106 107 Assert.assertEquals(1, getJmsMessage()); 108 printOK(); 109 } 110 111 116 public void testSimpleFail() throws Exception 117 { 118 printHeader(); 119 getLog().debug("Verify simple failed transaction"); 120 publisher.simpleFail(2); 121 122 Assert.assertEquals(-1, getJmsMessage()); 123 printOK(); 124 } 125 126 131 public void testBeanOk() throws Exception 132 { 133 printHeader(); 134 getLog().debug("Verify bean ok"); 135 publisher.beanOk(3); 136 137 Assert.assertEquals(3, getJmsMessage()); 138 printOK(); 139 } 140 141 146 public void testBeanError() throws Exception 147 { 148 printHeader(); 149 getLog().debug("Verify bean eroor failed transaction"); 150 151 try 152 { 153 publisher.beanError(4); 154 } 155 catch (Exception ignore) 156 { 157 } 158 159 Assert.assertEquals(-1, getJmsMessage()); 160 printOK(); 161 } 162 163 168 protected void setUp() throws Exception 169 { 170 super.setUp() ; 172 173 Context context = getInitialContext(); 175 try 176 { 177 PublisherHome home = (PublisherHome)context.lookup(beanJNDI); 178 publisher = home.create(); 179 180 init(context); 181 } 182 finally 183 { 184 context.close(); 185 } 186 187 connection.start(); 189 190 flush(); 192 } 193 194 201 protected int getJmsMessage() throws Exception 202 { 203 return getJmsMessage(DEFAULT_TIMEOUT); 204 } 205 206 214 protected int getJmsMessage(long timeout) throws Exception 215 { 216 Message msg = consumer.receive(timeout); 217 if (msg != null) 218 { 219 getLog().debug("Recived message: " + msg); 220 int nr = msg.getIntProperty(Publisher.JMS_MESSAGE_NR); 221 getLog().debug("nr: " + nr); 222 return nr; 223 } 224 else 225 { 226 getLog().debug("NO message recived"); 227 return -1; 228 } 229 } 230 231 237 protected abstract void init(final Context context) throws Exception ; 238 239 244 protected void tearDown() throws Exception 245 { 246 if (consumer != null) 247 { 248 consumer.close(); 249 } 250 if (connection != null) 251 { 252 connection.close(); 253 } 254 255 super.tearDown() ; 257 } 258 259 262 protected void printHeader() 263 { 264 getLog().debug("\n---- Testing method " + getName() + 265 " for bean " + beanJNDI); 266 } 267 268 271 protected void printOK() 272 { 273 getLog().debug("---- Test OK\n"); 274 } 275 276 282 protected void flush() throws Exception 283 { 284 286 int nr = 0; 287 do 288 { 289 try 290 { 291 nr = getJmsMessage(FLUSH_TIMEOUT); 292 } 293 catch (Exception ignore) 294 { 295 } 296 } while (nr != -1); 297 } 299 300 public static junit.framework.Test suite() throws Exception 301 { 302 ClassLoader loader = Thread.currentThread().getContextClassLoader(); 303 String resourceName = getJMSResourceRelativePathname("test-destinations-service.xml") ; 304 return getDeploySetup(RaTest.class, loader.getResource(resourceName).toString()); 305 } 306 307 308 } 309 | Popular Tags |