KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jms > p2p > BasicTestCase


1 /*
2  * JBoss, the OpenSource J2EE webOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.jms.p2p;
8
9 import java.util.ArrayList JavaDoc;
10
11 import javax.jms.Connection JavaDoc;
12 import javax.jms.ConnectionFactory JavaDoc;
13 import javax.jms.Message JavaDoc;
14 import javax.jms.MessageConsumer JavaDoc;
15 import javax.jms.MessageListener JavaDoc;
16 import javax.jms.MessageProducer JavaDoc;
17 import javax.jms.Session JavaDoc;
18 import javax.jms.TextMessage JavaDoc;
19 import javax.jms.Topic JavaDoc;
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 /**
27  * A basic test
28  *
29  * @author <a HREF="adrian@jboss.org>Adrian Brock</a>
30  * @version $Revision: 1.1 $
31  */

32 public class BasicTestCase extends BaseJMSTest
33    implements MessageListener JavaDoc
34 {
35    // Constants -----------------------------------------------------
36

37    // Attributes ----------------------------------------------------
38

39    private ArrayList JavaDoc messages = new ArrayList JavaDoc();
40
41    // Static --------------------------------------------------------
42

43    // Constructors --------------------------------------------------
44

45    public BasicTestCase(String JavaDoc name)
46    {
47       super(name);
48    }
49
50    // Public --------------------------------------------------------
51

52    public void testSimpleSendReceive()
53       throws Exception JavaDoc
54    {
55       Topic JavaDoc topic = new JBossTopic("testTopic");
56       ConnectionFactory JavaDoc cf = new JBossConnectionFactory(new P2PImplementation());
57       Connection JavaDoc c = cf.createConnection();
58       try
59       {
60          Session JavaDoc s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
61          MessageProducer JavaDoc p = s.createProducer(topic);
62          Message JavaDoc m = s.createTextMessage("Hello");
63          MessageConsumer JavaDoc r = s.createConsumer(topic);
64          c.start();
65          p.send(m);
66          TextMessage JavaDoc tm = (TextMessage JavaDoc) 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 JavaDoc
78    {
79       messages.clear();
80       Topic JavaDoc topic = new JBossTopic("testTopic");
81       ConnectionFactory JavaDoc cf = new JBossConnectionFactory(new P2PImplementation());
82       Connection JavaDoc c = cf.createConnection();
83       try
84       {
85          Session JavaDoc s = c.createSession(false, Session.AUTO_ACKNOWLEDGE);
86          MessageProducer JavaDoc p = s.createProducer(topic);
87          Message JavaDoc m = s.createTextMessage("Listen to this");
88          MessageConsumer JavaDoc 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 JavaDoc tm = (TextMessage JavaDoc) 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    // MessageListener implementation----------------------------------
104

105    public void onMessage(Message JavaDoc message)
106    {
107       messages.add(message);
108    }
109
110    // Protected ------------------------------------------------------
111

112    // Package Private ------------------------------------------------
113

114    // Private --------------------------------------------------------
115

116    // Inner Classes --------------------------------------------------
117
}
118
Popular Tags