package org.jboss.test.jmsra.test;
import javax.jms.Connection;
import javax.jms.Message;
import javax.jms.MessageConsumer;
import javax.jms.Session;
import javax.naming.Context;
import javax.naming.InitialContext;
import junit.framework.Assert;
import junit.framework.TestCase;
import org.jboss.test.JBossTestCase;
import org.jboss.test.jmsra.bean.*;
public abstract class RaTest
extends JBossTestCase
{
public final static long DEFAULT_TIMEOUT = 500L;
public final static long FLUSH_TIMEOUT = 500L;
protected String beanJNDI;
protected MessageConsumer consumer;
protected Publisher publisher;
protected Connection connection;
protected Session session;
protected RaTest(final String name, final String beanJNDI)
throws Exception
{
super(name);
this.beanJNDI = beanJNDI;
}
public void testSimple() throws Exception
{
printHeader();
getLog().debug("Verify simple send of message");
publisher.simple(1);
Assert.assertEquals(1, getJmsMessage());
printOK();
}
public void testSimpleFail() throws Exception
{
printHeader();
getLog().debug("Verify simple failed transaction");
publisher.simpleFail(2);
Assert.assertEquals(-1, getJmsMessage());
printOK();
}
public void testBeanOk() throws Exception
{
printHeader();
getLog().debug("Verify bean ok");
publisher.beanOk(3);
Assert.assertEquals(3, getJmsMessage());
printOK();
}
public void testBeanError() throws Exception
{
printHeader();
getLog().debug("Verify bean eroor failed transaction");
try
{
publisher.beanError(4);
}
catch (Exception ignore)
{
}
Assert.assertEquals(-1, getJmsMessage());
printOK();
}
protected void setUp() throws Exception
{
Context context = getInitialContext();
try
{
PublisherHome home = (PublisherHome)context.lookup(beanJNDI);
publisher = home.create();
init(context);
}
finally
{
context.close();
}
connection.start();
flush();
}
protected int getJmsMessage() throws Exception
{
return getJmsMessage(DEFAULT_TIMEOUT);
}
protected int getJmsMessage(long timeout) throws Exception
{
Message msg = consumer.receive(timeout);
if (msg != null)
{
getLog().debug("Recived message: " + msg);
int nr = msg.getIntProperty(Publisher.JMS_MESSAGE_NR);
getLog().debug("nr: " + nr);
return nr;
}
else
{
getLog().debug("NO message recived");
return -1;
}
}
protected abstract void init(final Context context) throws Exception;
protected void tearDown() throws Exception
{
if (consumer != null)
{
consumer.close();
}
if (connection != null)
{
connection.close();
}
}
protected void printHeader()
{
getLog().debug("\n---- Testing method " + getName() +
" for bean " + beanJNDI);
}
protected void printOK()
{
getLog().debug("---- Test OK\n");
}
protected void flush() throws Exception
{
int nr = 0;
do
{
try
{
nr = getJmsMessage(FLUSH_TIMEOUT);
}
catch (Exception ignore)
{
}
} while (nr != -1);
}
}