/**
*
*/
package net.fp.rp.jms;
//import java.util.List;
import java.util.List;
import javax.jms.Message;
import javax.jms.TextMessage;
import org.apache.log4j.Logger;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;
import junit.framework.TestCase;
/**
* Test the sending and recieving of a JMS message with Spring.<br/>
* While the code needs a JBoss or similar server (to provide Access to JMS Queues) both
* the JMS message Send and JMS Message Recieve are plain old Java code , run in the unit test.
* <br/>
* The spring config files (supplied) ensures that messages send in testMessageSend() are heard in
* testMessageRecieve().
* <br/>
* Watch out for 'race' conditions between testMessageSend() and testMessageRecieve() as there is
* nothing to guarantee that a sent message will arrive before the 2nd test runs (probably worth
* resolving this later if it proves to be a problem.
* <br/>
* Code in this Test is based on the OnJava Article @link http://www.onjava.com/lpt/a/6490
* <br/>
* Items that this JUnit Test Depends Upon<br/>
* - Spring.jar (in common/lib/spring)<br/>
* - JBossall-client.jar (found in <jboss-install>/client/)<br/>
* - Spring App Context (core/test/net/fp/rp/jms/JmsMessageTest-AppContext.xml)<br/>
* - rp-core.jar , or the original source code.
* <br/>
* Server Configuration Required for this test to work.<br/>
* - JMS Queue Setup on JBoss / other app server : modify jbossmq-destinations-service.xml file
* (in the server/default/deploy/jms directory)
* <br/>
* Sample of this queue is: (ignore the <pre> tags)
* <pre>
* <!-- A common Queue for unit testing purposes-->
<mbean code="org.jboss.mq.server.jmx.Queue"
name="jboss.mq.destination:service=Queue,name=CommonQueue">
<depends optional-attribute-name="DestinationManager">
jboss.mq:service=DestinationManager
</depends>
</mbean>
*
* </pre>
* Other resources
* @link http://wiki.jboss.org/wiki/Wiki.jsp?page=JBossMQ
* @link http://wiki.jboss.org/wiki/Wiki.jsp?page=QueueExample
* @link http://wiki.jboss.org/wiki/Wiki.jsp?page=JmsReceiverBean
* @link http://www.hermesjms.com/demos/jboss_config.html
* @author paul browne
* Copyright @link www.firstpartners.net/red
*/
public class JmsMessageTest extends TestCase {
/**
* Where to find the Spring Context that we use for testing
*/
public static final String TEST_APP_CONTEXT="core/test/net/fp/rp/jms/JmsMessageTest-AppContext.xml";
/**
* The Name of the Spring Bean under test
*/
public static final String TEST_BEAN="jmsSender";
/** Logger for this class and subclasses */
protected final Logger log = Logger.getLogger(getClass());
//The application context used by this test
ApplicationContext context = null;
/**
* Get the (Test) Context from the file
*/
public void setUp() throws Exception{
context = new FileSystemXmlApplicationContext(TEST_APP_CONTEXT);
}
/**
* Test sending of messages
* @throws Throwable
*/
public void testMessageSend() throws Throwable{
ITestMessageSender jmsSender = (ITestMessageSender)context.getBean(TEST_BEAN);
//Send 5 messages
jmsSender.sendMessage("Sample message text 0");
jmsSender.sendMessage("Sample message text 1");
jmsSender.sendMessage("Sample message text 2");
jmsSender.sendMessage("Sample message text 3");
jmsSender.sendMessage("Sample message text 4");
//No asserts on this - other than an exception thrown
}
/**
* Test the Reciept of the messages that we sent in testMessageSend()
* @throws Throwable
*/
public void testMessageRecieve() throws Throwable{
//Get the messages
List<Message> messagesHeard = JmsTestListener.getMessages();
//Run Checks on them
assertEquals("Number of Messages should be 5",messagesHeard.size(),5);
//Ensure the messages are all of the type we expect
//Ensure the text is what we expect (and check the order)
TextMessage tmpTextMessage;
for (int a=0;a<messagesHeard.size();a++){
tmpTextMessage= (TextMessage)messagesHeard.get(a);
assertEquals(tmpTextMessage.getText(),"Sample message text "+a);
}
}
}