1 7 package org.jboss.test.jms.p2p; 8 9 import java.util.ArrayList ; 10 11 import javax.jms.Connection ; 12 import javax.jms.ConnectionFactory ; 13 import javax.jms.Message ; 14 import javax.jms.MessageConsumer ; 15 import javax.jms.MessageListener ; 16 import javax.jms.MessageProducer ; 17 import javax.jms.Session ; 18 import javax.jms.TextMessage ; 19 import javax.jms.Topic ; 20 21 import org.jboss.jms.client.JBossConnectionFactory; 22 import org.jboss.jms.client.p2p.P2PImplementation; 23 import org.jboss.jms.destination.JBossTopic; 24 import org.jboss.test.jms.BaseJMSTest; 25 26 32 public class BasicTestCase extends BaseJMSTest 33 implements MessageListener 34 { 35 37 39 private ArrayList messages = new ArrayList (); 40 41 43 45 public BasicTestCase(String name) 46 { 47 super(name); 48 } 49 50 52 public void testSimpleSendReceive() 53 throws Exception 54 { 55 Topic topic = new JBossTopic("testTopic"); 56 ConnectionFactory cf = new JBossConnectionFactory(new P2PImplementation()); 57 Connection c = cf.createConnection(); 58 try 59 { 60 Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); 61 MessageProducer p = s.createProducer(topic); 62 Message m = s.createTextMessage("Hello"); 63 MessageConsumer r = s.createConsumer(topic); 64 c.start(); 65 p.send(m); 66 TextMessage tm = (TextMessage ) r.receive(1000); 67 assertTrue("Should have a message", tm != null); 68 assertTrue("Message should say Hello", tm.getText().equals("Hello")); 69 } 70 finally 71 { 72 c.close(); 73 } 74 } 75 76 public void testMessageListener() 77 throws Exception 78 { 79 messages.clear(); 80 Topic topic = new JBossTopic("testTopic"); 81 ConnectionFactory cf = new JBossConnectionFactory(new P2PImplementation()); 82 Connection c = cf.createConnection(); 83 try 84 { 85 Session s = c.createSession(false, Session.AUTO_ACKNOWLEDGE); 86 MessageProducer p = s.createProducer(topic); 87 Message m = s.createTextMessage("Listen to this"); 88 MessageConsumer r = s.createConsumer(topic); 89 r.setMessageListener(this); 90 c.start(); 91 p.send(m); 92 Thread.sleep(1000); 93 assertTrue("Should have one message", messages.size() == 1); 94 TextMessage tm = (TextMessage ) messages.remove(0); 95 assertTrue("Message should say Listen to this", tm.getText().equals("Listen to this")); 96 } 97 finally 98 { 99 c.close(); 100 } 101 } 102 103 105 public void onMessage(Message message) 106 { 107 messages.add(message); 108 } 109 110 112 114 116 } 118 | Popular Tags |