KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > jmsbridge > test > TopicTest


1 package net.walend.jmsbridge.test;
2
3 import java.util.List JavaDoc;
4 import java.util.ArrayList JavaDoc;
5
6 import javax.jms.TopicConnection JavaDoc;
7 import javax.jms.Topic JavaDoc;
8 import javax.jms.TopicSession JavaDoc;
9 import javax.jms.TopicPublisher JavaDoc;
10 import javax.jms.Message JavaDoc;
11 import javax.jms.ObjectMessage JavaDoc;
12 import javax.jms.TopicSubscriber JavaDoc;
13 import javax.jms.Session JavaDoc;
14 import javax.jms.JMSException JavaDoc;
15 import javax.jms.MessageListener JavaDoc;
16
17 import junit.framework.TestSuite;
18 import junit.framework.Test;
19
20 import net.walend.toolkit.junit.TestCase;
21
22 import net.walend.somnifugi.SomniJNDIBypass;
23 import net.walend.somnifugi.SomniDefaultExceptionListener;
24
25 import net.walend.jmsbridge.TopicBridge;
26
27 /**
28 Tests using Topics in some simple situations. Thread.sleep() is used to simulate work.
29
30  @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
31 */

32
33 public class TopicTest extends TestCase
34 {
35     public TopicTest(String JavaDoc testName)
36     {
37         super(testName);
38     }
39
40     protected class TenObjectPublisher
41         implements Runnable JavaDoc
42     {
43         private TopicConnection JavaDoc connection;
44         private Topic JavaDoc topic;
45
46         public TenObjectPublisher(TopicConnection JavaDoc connection,Topic JavaDoc topic)
47         {
48             this.connection = connection;
49             this.topic = topic;
50         }
51
52         public void run()
53         {
54             try
55                 {
56                     Thread.sleep(10);
57                     TopicSession JavaDoc session = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
58                     TopicPublisher JavaDoc publisher = session.createPublisher(topic);
59                     for(int i=0;i<10;i++)
60                         {
61                             Message JavaDoc message = session.createObjectMessage(new Integer JavaDoc(i));
62                             publisher.publish(message);
63                         }
64                 }
65             catch(InterruptedException JavaDoc ie)
66                 {
67                     fail(ie);
68                 }
69             catch(JMSException JavaDoc jmse)
70                 {
71                     fail(jmse);
72                 }
73         }
74     }
75
76    protected class TenObjectSubscriber
77         implements Runnable JavaDoc
78     {
79         private TopicConnection JavaDoc connection;
80         private Topic JavaDoc topic;
81         private List JavaDoc<Object JavaDoc> expected = new ArrayList JavaDoc<Object JavaDoc>();
82         private List JavaDoc<Object JavaDoc> results = new ArrayList JavaDoc<Object JavaDoc>();
83         private final Object JavaDoc guard = new Object JavaDoc();
84
85
86         public TenObjectSubscriber(TopicConnection JavaDoc connection,Topic JavaDoc topic)
87         {
88             this.connection = connection;
89             this.topic = topic;
90             for(int i=0;i<10;i++)
91                 {
92                     expected.add(new Integer JavaDoc(i));
93                 }
94         }
95
96         public void test()
97         {
98             synchronized(guard)
99                 {
100                     assertTrue("Expected results to be "+expected+", but got "+results,results.equals(expected));
101                 }
102         }
103
104
105         public void run()
106         {
107             synchronized(guard)
108                 {
109                     try
110                         {
111                             TopicSession JavaDoc session = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
112                             TopicSubscriber JavaDoc subscriber = session.createSubscriber(topic);
113                             
114                             for(int i=0;i<10;i++)
115                                 {
116                                     ObjectMessage JavaDoc message = (ObjectMessage JavaDoc)subscriber.receive(200);
117                                     assertTrue("Message not received after 200 ms.",message!=null);
118                                     
119                                     results.add(message.getObject());
120                         }
121                         }
122                     catch(JMSException JavaDoc jmse)
123                         {
124                             fail(jmse);
125                         }
126                 }
127         }
128     }
129
130     
131
132     public void testTwoThreads()
133     {
134         try
135             {
136                 TopicConnection JavaDoc connection = SomniJNDIBypass.IT.getTopicConnectionFactory().createTopicConnection();
137                 connection.start();
138                 
139                 Topic JavaDoc bridgeIn = SomniJNDIBypass.IT.getTopic("twoThreadBridgeIn");
140                 Topic JavaDoc bridgeOut = SomniJNDIBypass.IT.getTopic("twoThreadBridgeOut");
141
142                 TopicSession JavaDoc session = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
143                 TopicSubscriber JavaDoc source = session.createSubscriber(bridgeIn);
144                 TopicPublisher JavaDoc sink = session.createPublisher(bridgeOut);
145                 
146                 //todo use a junit-specific ExceptionListener.
147
TopicBridge bridge = new TopicBridge(source,sink,session,new SomniDefaultExceptionListener());
148
149                 TenObjectSubscriber tos = new TenObjectSubscriber(connection,bridgeOut);
150
151                 Thread JavaDoc subscribeThread = new Thread JavaDoc(tos);
152                 subscribeThread.start();
153
154                 Thread.sleep(10);
155                 
156                 Thread JavaDoc publishThread = new Thread JavaDoc(new TenObjectPublisher(connection,bridgeIn));
157                 publishThread.start();
158
159                 publishThread.join(10000);
160                 subscribeThread.join(10000);
161
162                 tos.test();
163
164             }
165         catch(JMSException JavaDoc jmse)
166             {
167                 fail(jmse);
168             }
169         catch(InterruptedException JavaDoc ie)
170             {
171                 fail(ie);
172             }
173     }
174
175     public void testTwoSubscribers()
176     {
177         try
178             {
179                 TopicConnection JavaDoc connection = SomniJNDIBypass.IT.getTopicConnectionFactory().createTopicConnection();
180                 connection.start();
181
182                 Topic JavaDoc bridgeIn = SomniJNDIBypass.IT.getTopic("twoSubsBridgeIn");
183                 Topic JavaDoc bridgeOut = SomniJNDIBypass.IT.getTopic("twoSubsBridgeOut");
184
185                 TopicSession JavaDoc session = connection.createTopicSession(false,Session.AUTO_ACKNOWLEDGE);
186                 TopicSubscriber JavaDoc source = session.createSubscriber(bridgeIn);
187                 TopicPublisher JavaDoc sink = session.createPublisher(bridgeOut);
188                 
189                 //todo use a junit-specific ExceptionListener.
190
TopicBridge bridge = new TopicBridge(source,sink,session,new SomniDefaultExceptionListener());
191
192
193                 TenObjectSubscriber tos1 = new TenObjectSubscriber(connection,bridgeOut);
194                 TenObjectSubscriber tos2 = new TenObjectSubscriber(connection,bridgeOut);
195
196                 Thread JavaDoc subscribeThread1 = new Thread JavaDoc(tos1);
197                 subscribeThread1.start();
198
199                 Thread.sleep(10);
200
201                 Thread JavaDoc subscribeThread2 = new Thread JavaDoc(tos2);
202                 subscribeThread2.start();
203
204                 Thread.sleep(10);
205                 
206                 Thread JavaDoc publishThread = new Thread JavaDoc(new TenObjectPublisher(connection,bridgeIn));
207                 publishThread.start();
208
209                 publishThread.join(10000);
210                 subscribeThread1.join(10000);
211                 subscribeThread2.join(10000);
212
213                 tos1.test();
214                 tos2.test();
215             }
216         catch(JMSException JavaDoc jmse)
217             {
218                 fail(jmse);
219             }
220         catch(InterruptedException JavaDoc ie)
221             {
222                 fail(ie);
223             }
224     }
225
226     public static Test suite()
227     {
228         TestSuite suite = new TestSuite() ;
229
230         suite.addTest(new TopicTest("testTwoThreads"));
231         suite.addTest(new TopicTest("testTwoSubscribers"));
232
233         return suite;
234     }
235 }
236
237
238 /*
239 Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
240 All rights reserved.
241
242 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
243
244 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
245
246 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
247
248 Neither the name of the SomnifugiJMS Project, walend.net, nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission from David Walend.
249
250 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
251
252 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
253 The net.walend.somnifugi.sql92 package is modified code from the openmq project, https://mq.dev.java.net/ , Copyright (c) of Sun, and carries the CDDL license, repeated here: You can obtain a copy of the license at https://glassfish.dev.java.net/public/CDDLv1.0.html. See the License for the specific language governing permissions and limitations under the License.
254
255 =================================================================================
256
257 For more information and the latest version of this software, please see http://somnifugi.sourceforge.net and http://walend.net or email <a HREF="mailto:david@walend.net">david@walend.net</a>.
258
259 */

260
261
262
Popular Tags