KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > walend > somnifugi > test > MessageTest


1 package net.walend.somnifugi.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 import java.util.Hashtable JavaDoc;
9
10 import javax.naming.InitialContext JavaDoc;
11 import javax.naming.Context JavaDoc;
12 import javax.naming.NamingException JavaDoc;
13
14 import javax.jms.QueueConnectionFactory JavaDoc;
15 import javax.jms.QueueConnection JavaDoc;
16 import javax.jms.Queue JavaDoc;
17 import javax.jms.QueueSession JavaDoc;
18 import javax.jms.QueueSender JavaDoc;
19 import javax.jms.Message JavaDoc;
20 import javax.jms.ObjectMessage JavaDoc;
21 import javax.jms.TextMessage JavaDoc;
22 import javax.jms.QueueReceiver JavaDoc;
23 import javax.jms.Session JavaDoc;
24 import javax.jms.JMSException JavaDoc;
25 import javax.jms.MessageListener JavaDoc;
26 import javax.jms.DeliveryMode JavaDoc;
27 import javax.jms.QueueRequestor JavaDoc;
28 import javax.jms.MessageFormatException JavaDoc;
29
30 import junit.framework.TestSuite;
31 import junit.framework.Test;
32
33 import net.walend.toolkit.junit.TestCase;
34
35 import net.walend.somnifugi.SomniJNDIBypass;
36 import net.walend.somnifugi.SomniQueueConnectionFactory;
37
38 /**
39 Tests using Queue in some simple situations. Thread.sleep() is used to simulate work.
40
41  @author <a HREF="http://walend.net">David Walend</a> <a HREF="mailto:david@walend.net">david@walend.net</a>
42 */

43
44 public class MessageTest extends TestCase
45 {
46     public MessageTest(String JavaDoc testName)
47     {
48         super(testName);
49     }
50
51     protected void sendObjects(QueueConnection JavaDoc connection,Queue JavaDoc queue,int howMany)
52     {
53         try
54             {
55                 QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
56                 QueueSender JavaDoc sender = session.createSender(queue);
57                 for(int i=0;i<howMany;i++)
58                     {
59                         Message JavaDoc message = session.createObjectMessage(new Integer JavaDoc(i));
60
61                         sender.send(message);
62                     }
63             }
64         catch(JMSException JavaDoc jmse)
65             {
66                 fail(jmse);
67             }
68     }
69
70     protected ObjectMessage JavaDoc receiveMessage(QueueConnection JavaDoc connection,Queue JavaDoc queue)
71     {
72         try
73             {
74                 QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
75                 QueueReceiver JavaDoc receiver = session.createReceiver(queue);
76
77                 return (ObjectMessage JavaDoc)receiver.receive(100);
78             }
79         catch(JMSException JavaDoc jmse)
80             {
81                 fail(jmse);
82                 return null;
83             }
84     }
85
86     protected void receiveNoObjects(QueueConnection JavaDoc connection,Queue JavaDoc queue)
87     {
88         try
89             {
90                 QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
91                 QueueReceiver JavaDoc receiver = session.createReceiver(queue);
92                 
93                 for(int i=0;i<10;i++)
94                     {
95                         ObjectMessage JavaDoc message = (ObjectMessage JavaDoc)receiver.receive(10);
96
97                         assertTrue("Message should be null but is "+message,message==null);
98                     }
99             }
100         catch(JMSException JavaDoc jmse)
101             {
102                 fail(jmse);
103             }
104     }
105
106     public void testProperties()
107     {
108         try
109             {
110                 QueueConnection JavaDoc connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection();
111                 
112                 TestExceptionListener exceptionListener = new TestExceptionListener();
113                 connection.setExceptionListener(exceptionListener);
114
115                 connection.start();
116
117                 Queue JavaDoc queue = SomniJNDIBypass.IT.getQueue("testProperties");
118
119                 QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
120                 QueueSender JavaDoc sender = session.createSender(queue);
121                 Message JavaDoc message = session.createObjectMessage(new Integer JavaDoc(0));
122
123                 message.setBooleanProperty("true prop",true);
124                 message.setBooleanProperty("false prop",false);
125
126                 message.setByteProperty("byte prop",(byte)13);
127
128                 message.setObjectProperty("double prop",new Double JavaDoc(233.322));
129
130                 sender.send(message);
131
132                 Message JavaDoc message2 = receiveMessage(connection,queue);
133
134                 assertTrue(message2.getBooleanProperty("true prop"));
135                 assertTrue(!message2.getBooleanProperty("false prop"));
136                 assertTrue(!message2.getBooleanProperty("never set prop"));
137
138                 assertTrue(message2.getByteProperty("byte prop")==(byte)13);
139
140                 assertTrue(message2.getObjectProperty("double prop").equals(new Double JavaDoc(233.322)));
141                 assertTrue(message2.getDoubleProperty("double prop")==233.322);
142                 assertTrue(message2.getObjectProperty("byte prop").equals(new Byte JavaDoc((byte)13)));
143
144                 try
145                     {
146                         message2.getByteProperty("true prop");
147                         fail("true prop should not return a byte");
148                     }
149                 catch(MessageFormatException JavaDoc mfe)
150                     {
151                         //expected
152
}
153                 
154                 receiveNoObjects(connection,queue);
155
156                 connection.close();
157
158                 if(exceptionListener.caughtException())
159                     {
160                         fail(exceptionListener.getException());
161                     }
162             }
163         catch(JMSException JavaDoc jmse)
164             {
165                 fail(jmse);
166             }
167     }
168
169     public void testTextMessage()
170     {
171         try
172         {
173             QueueConnection JavaDoc connection = SomniJNDIBypass.IT.getQueueConnectionFactory().createQueueConnection();
174             
175             TestExceptionListener exceptionListener = new TestExceptionListener();
176             connection.setExceptionListener(exceptionListener);
177
178             connection.start();
179
180             Queue JavaDoc queue = SomniJNDIBypass.IT.getQueue("textMessageTest");
181
182             QueueSession JavaDoc session = connection.createQueueSession(false,Session.AUTO_ACKNOWLEDGE);
183             QueueSender JavaDoc sender = session.createSender(queue);
184             Message JavaDoc message = session.createTextMessage("Hi!");
185
186             sender.send(message);
187
188             QueueReceiver JavaDoc receiver = session.createReceiver(queue);
189
190             TextMessage JavaDoc message2 = (TextMessage JavaDoc)receiver.receive(100);
191             
192             assertTrue(message2.getText().equals("Hi!"));
193             
194             receiveNoObjects(connection,queue);
195
196             connection.close();
197
198             if(exceptionListener.caughtException())
199             {
200                 fail(exceptionListener.getException());
201             }
202         }
203         catch(JMSException JavaDoc jmse)
204         {
205             fail(jmse);
206         }
207     }
208
209     public static Test suite()
210     {
211         TestSuite suite = new TestSuite() ;
212
213         suite.addTest(new MessageTest("testProperties"));
214         suite.addTest(new MessageTest("testTextMessage"));
215
216         return suite;
217     }
218 }
219
220
221 /*
222 Copyright (c) 2001, 2002, 2003, 2004, 2005, 2006 David Walend
223 All rights reserved.
224
225 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
226
227 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
228
229 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.
230
231 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.
232
233 Credits in redistributions in source or binary forms must include a link to http://somnifugi.sourceforge.net .
234
235 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.
236 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.
237
238 =================================================================================
239
240 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>.
241
242 */

243
244
245
Popular Tags