KickJava   Java API By Example, From Geeks To Geeks.

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


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

35
36 public class QueueTest extends TestCase
37 {
38     public QueueTest(String JavaDoc testName)
39     {
40         super(testName);
41     }
42
43     protected void sendTenObjects(QueueConnection JavaDoc connection,Queue JavaDoc queue)
44     {
45         try
46             {
47                 QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
48                 QueueSender JavaDoc sender = session.createSender(queue);
49                 for(int i=0;i<10;i++)
50                     {
51                         Message JavaDoc message = session.createObjectMessage(new Integer JavaDoc(i));
52                         sender.send(message);
53                     }
54             }
55         catch(JMSException JavaDoc jmse)
56             {
57                 fail(jmse);
58             }
59     }
60
61     protected void receiveTenObjects(QueueConnection JavaDoc connection,Queue JavaDoc queue)
62     {
63         try
64             {
65                 QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
66                 QueueReceiver JavaDoc receiver = session.createReceiver(queue);
67                 List JavaDoc<Object JavaDoc> results = new ArrayList JavaDoc<Object JavaDoc>();
68                 List JavaDoc<Object JavaDoc> expected = new ArrayList JavaDoc<Object JavaDoc>();
69                 
70                 for(int i=0;i<10;i++)
71                     {
72                         expected.add(new Integer JavaDoc(i));
73                         ObjectMessage JavaDoc message = (ObjectMessage JavaDoc)receiver.receive(100);
74                         
75                         results.add(message.getObject());
76                     }
77                 assertTrue("Expected results to be "+expected+", but got "+results,results.equals(expected));
78             }
79         catch(JMSException JavaDoc jmse)
80             {
81                 fail(jmse);
82             }
83     }
84
85     public void testHotStart()
86     {
87         try
88             {
89                 QueueConnection JavaDoc connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection();
90                 connection.start();
91                 
92                 Queue JavaDoc bridgeIn = SomniJNDIBypass.IT.getQueue("brddgeInTest");
93
94                 Queue JavaDoc bridgeOut = SomniJNDIBypass.IT.getQueue("bridgeOutTest");
95                 
96
97                 QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
98
99                 QueueReceiver JavaDoc source = session.createReceiver(bridgeIn);
100                 QueueSender JavaDoc sink = session.createSender(bridgeOut);
101
102                 //todo use an ExceptionListener that can talk to JUnit.
103
QueueBridge bridge = new QueueBridge(source,sink,session,new SomniDefaultExceptionListener());
104
105                 sendTenObjects(connection,bridgeIn);
106                 
107                 receiveTenObjects(connection,bridgeOut);
108             }
109         catch(JMSException JavaDoc jmse)
110             {
111                 fail(jmse);
112             }
113     }
114
115     protected class TenObjectSender
116         implements Runnable JavaDoc
117     {
118         private QueueConnection JavaDoc connection;
119         private Queue JavaDoc queue;
120
121         public TenObjectSender(QueueConnection JavaDoc connection,Queue JavaDoc queue)
122         {
123             this.connection = connection;
124             this.queue = queue;
125         }
126
127         public void run()
128         {
129             try
130                 {
131                     Thread.sleep(10);
132                     QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
133                     QueueSender JavaDoc sender = session.createSender(queue);
134                     for(int i=0;i<10;i++)
135                         {
136                             Message JavaDoc message = session.createObjectMessage(new Integer JavaDoc(i));
137                             sender.send(message);
138                         }
139                 }
140             catch(InterruptedException JavaDoc ie)
141                 {
142                     fail(ie);
143                 }
144             catch(JMSException JavaDoc jmse)
145                 {
146                     fail(jmse);
147                 }
148         }
149     }
150
151    protected class TenObjectReceiver
152         implements Runnable JavaDoc
153     {
154         private QueueConnection JavaDoc connection;
155         private Queue JavaDoc queue;
156
157         public TenObjectReceiver(QueueConnection JavaDoc connection,Queue JavaDoc queue)
158         {
159             this.connection = connection;
160             this.queue = queue;
161         }
162
163         public void run()
164         {
165             try
166                 {
167                     QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
168                     QueueReceiver JavaDoc receiver = session.createReceiver(queue);
169                     List JavaDoc<Object JavaDoc> results = new ArrayList JavaDoc<Object JavaDoc>();
170                     List JavaDoc<Object JavaDoc> expected = new ArrayList JavaDoc<Object JavaDoc>();
171                     
172                     for(int i=0;i<10;i++)
173                         {
174                             expected.add(new Integer JavaDoc(i));
175                             ObjectMessage JavaDoc message = (ObjectMessage JavaDoc)receiver.receive(100);
176                             
177                             results.add(message.getObject());
178                         }
179                     assertTrue("Expected results to be "+expected+", but got "+results,results.equals(expected));
180                 }
181             catch(JMSException JavaDoc jmse)
182                 {
183                     fail(jmse);
184                 }
185         }
186     }
187
188     public void testTwoThreads()
189     {
190         try
191             {
192                 QueueConnection JavaDoc connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection();
193                 connection.start();
194                 
195                 Queue JavaDoc bridgeIn = SomniJNDIBypass.IT.getQueue("twoThreadsIn");
196
197                 Queue JavaDoc bridgeOut = SomniJNDIBypass.IT.getQueue("twoThreadsOut");
198
199                 QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
200
201                 QueueReceiver JavaDoc source = session.createReceiver(bridgeIn);
202                 QueueSender JavaDoc sink = session.createSender(bridgeOut);
203
204                 //todo use an ExceptionListener that can talk to JUnit.
205
QueueBridge bridge = new QueueBridge(source,sink,session,new SomniDefaultExceptionListener());
206
207                 Thread JavaDoc sendThread = new Thread JavaDoc(new TenObjectSender(connection,bridgeIn));
208                 sendThread.start();
209
210                 Thread JavaDoc receiveThread = new Thread JavaDoc(new TenObjectReceiver(connection,bridgeOut));
211                 receiveThread.start();
212
213                 sendThread.join(10000);
214                 receiveThread.join(10000);
215
216             }
217         catch(JMSException JavaDoc jmse)
218             {
219                 fail(jmse);
220             }
221         catch(InterruptedException JavaDoc ie)
222             {
223                 fail(ie);
224             }
225     }
226
227     public void testProperties()
228     {
229         try
230             {
231                 QueueConnection JavaDoc connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection();
232                 connection.start();
233                 
234                 Queue JavaDoc bridgeIn = SomniJNDIBypass.IT.getQueue("brddgeInTest");
235
236                 Queue JavaDoc bridgeOut = SomniJNDIBypass.IT.getQueue("bridgeOutTest");
237                 
238
239                 QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
240
241                 QueueReceiver JavaDoc source = session.createReceiver(bridgeIn);
242                 QueueSender JavaDoc sink = session.createSender(bridgeOut);
243
244                 //todo use an ExceptionListener that can talk to JUnit.
245
QueueBridge bridge = new QueueBridge(source,sink,session,new SomniDefaultExceptionListener());
246
247                 QueueSession JavaDoc session2 = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
248                 QueueSender JavaDoc sender = session2.createSender(bridgeIn);
249                 Message JavaDoc message = session.createObjectMessage(new Integer JavaDoc(0));
250
251                 message.setStringProperty("propName","test");
252
253                 sender.send(message);
254                 
255                 QueueSession JavaDoc session3 = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
256                 QueueReceiver JavaDoc receiver = session3.createReceiver(bridgeOut);
257                 
258                 ObjectMessage JavaDoc message2 = (ObjectMessage JavaDoc)receiver.receive(100);
259
260                 assertTrue("message2 should not be null.",null!=message2);
261                 assertTrue("message2 should contain a property named 'propName'",message2.propertyExists("propName"));
262
263                 assertTrue("message2's property 'propName' should be 'test' but is "+message2.getStringProperty("propName"),"test".equals(message2.getStringProperty("propName")));
264
265             }
266         catch(JMSException JavaDoc jmse)
267             {
268                 fail(jmse);
269             }
270     }
271
272     public static Test suite()
273     {
274         TestSuite suite = new TestSuite() ;
275
276         suite.addTest(new QueueTest("testHotStart"));
277         suite.addTest(new QueueTest("testTwoThreads"));
278         suite.addTest(new QueueTest("testProperties"));
279
280         return suite;
281     }
282 }
283
284
285 /*
286 Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
287 All rights reserved.
288
289 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
290
291 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
292
293 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.
294
295 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.
296
297 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
298
299 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.
300 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.
301
302 =================================================================================
303
304 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>.
305
306 */

307
308
309
Popular Tags