KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > JMSMessageTest


1 /**
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one or more
4  * contributor license agreements. See the NOTICE file distributed with
5  * this work for additional information regarding copyright ownership.
6  * The ASF licenses this file to You under the Apache License, Version 2.0
7  * (the "License"); you may not use this file except in compliance with
8  * the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */

18 package org.apache.activemq;
19
20 import java.net.URISyntaxException JavaDoc;
21 import java.util.Enumeration JavaDoc;
22 import java.util.HashMap JavaDoc;
23 import java.util.Vector JavaDoc;
24
25 import javax.jms.BytesMessage JavaDoc;
26 import javax.jms.ConnectionFactory JavaDoc;
27 import javax.jms.DeliveryMode JavaDoc;
28 import javax.jms.Destination JavaDoc;
29 import javax.jms.JMSException JavaDoc;
30 import javax.jms.MapMessage JavaDoc;
31 import javax.jms.MessageConsumer JavaDoc;
32 import javax.jms.MessageEOFException JavaDoc;
33 import javax.jms.MessageProducer JavaDoc;
34 import javax.jms.ObjectMessage JavaDoc;
35 import javax.jms.Session JavaDoc;
36 import javax.jms.StreamMessage JavaDoc;
37 import javax.jms.TextMessage JavaDoc;
38
39 import junit.framework.Test;
40
41 import org.apache.activemq.ActiveMQConnectionFactory;
42 import org.apache.activemq.command.ActiveMQDestination;
43
44 /**
45  * Test cases used to test the JMS message consumer.
46  *
47  * @version $Revision$
48  */

49 public class JMSMessageTest extends JmsTestSupport {
50
51     public ActiveMQDestination destination;
52     public int deliveryMode;
53     public int prefetch;
54     public int ackMode;
55     public byte destinationType;
56     public boolean durableConsumer;
57     public String JavaDoc connectURL;
58
59     /**
60      * Run all these tests in both marshaling and non-marshaling mode.
61      */

62     public void initCombos() {
63         addCombinationValues("connectURL", new Object JavaDoc[] {
64                 "vm://localhost?marshal=false",
65                 "vm://localhost?marshal=true"
66                 });
67         addCombinationValues("deliveryMode", new Object JavaDoc[] {
68                 new Integer JavaDoc(DeliveryMode.NON_PERSISTENT),
69                 new Integer JavaDoc(DeliveryMode.PERSISTENT) });
70         addCombinationValues("destinationType", new Object JavaDoc[] {
71                 new Byte JavaDoc(ActiveMQDestination.QUEUE_TYPE)});
72     }
73     
74     public void testTextMessage() throws Exception JavaDoc {
75
76         // Receive a message with the JMS API
77
connection.start();
78         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
79         destination = createDestination(session, destinationType);
80         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
81         MessageProducer JavaDoc producer = session.createProducer(destination);
82
83         // Send the message.
84
{
85             TextMessage JavaDoc message = session.createTextMessage();
86             message.setText("Hi");
87             producer.send(message);
88         }
89         
90         // Check the Message
91
{
92             TextMessage JavaDoc message = (TextMessage JavaDoc)consumer.receive(1000);
93             assertNotNull(message);
94             assertEquals( "Hi", message.getText() );
95         }
96         
97         assertNull(consumer.receiveNoWait());
98     }
99
100     public static Test suite() {
101         return suite(JMSMessageTest.class);
102     }
103
104     public static void main(String JavaDoc[] args) {
105         junit.textui.TestRunner.run(suite());
106     }
107
108     protected ConnectionFactory JavaDoc createConnectionFactory() throws URISyntaxException JavaDoc {
109         ActiveMQConnectionFactory factory = new ActiveMQConnectionFactory(connectURL);
110         return factory;
111     }
112     
113     public void testBytesMessageLength() throws Exception JavaDoc {
114
115         // Receive a message with the JMS API
116
connection.start();
117         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
118         destination = createDestination(session, destinationType);
119         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
120         MessageProducer JavaDoc producer = session.createProducer(destination);
121
122         // Send the message
123
{
124             BytesMessage JavaDoc message = session.createBytesMessage();
125             message.writeInt(1);
126             message.writeInt(2);
127             message.writeInt(3);
128             message.writeInt(4);
129             producer.send(message);
130         }
131         
132         // Check the message.
133
{
134             BytesMessage JavaDoc message = (BytesMessage JavaDoc) consumer.receive(1000);
135             assertNotNull(message);
136             assertEquals(16, message.getBodyLength() );
137         }
138         
139         assertNull(consumer.receiveNoWait());
140     }
141     
142     public void testObjectMessage() throws Exception JavaDoc {
143
144         // Receive a message with the JMS API
145
connection.start();
146         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
147         destination = createDestination(session, destinationType);
148         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
149         MessageProducer JavaDoc producer = session.createProducer(destination);
150         
151         // send the message.
152
{
153             ObjectMessage JavaDoc message = session.createObjectMessage();
154             message.setObject("Hi");
155             producer.send(message);
156         }
157         
158         // Check the message
159
{
160             ObjectMessage JavaDoc message = (ObjectMessage JavaDoc)consumer.receive(1000);
161             assertNotNull(message);
162             assertEquals( "Hi", message.getObject() );
163         }
164         assertNull(consumer.receiveNoWait());
165     }
166     
167     public void testBytesMessage() throws Exception JavaDoc {
168
169         // Receive a message with the JMS API
170
connection.start();
171         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
172         destination = createDestination(session, destinationType);
173         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
174         MessageProducer JavaDoc producer = session.createProducer(destination);
175         
176         // Send the message
177
{
178             BytesMessage JavaDoc message = session.createBytesMessage();
179             message.writeBoolean(true);
180             producer.send(message);
181         }
182         
183         // Check the message
184
{
185             BytesMessage JavaDoc message = (BytesMessage JavaDoc)consumer.receive(1000);
186             assertNotNull(message);
187             assertTrue( message.readBoolean() );
188     
189             try {
190                 message.readByte();
191                 fail("Expected exception not thrown.");
192             } catch (MessageEOFException JavaDoc e) {
193             }
194             
195         }
196         assertNull(consumer.receiveNoWait());
197     }
198
199     public void testStreamMessage() throws Exception JavaDoc {
200
201         // Receive a message with the JMS API
202
connection.start();
203         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
204         destination = createDestination(session, destinationType);
205         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
206         MessageProducer JavaDoc producer = session.createProducer(destination);
207
208         // Send the message.
209
{
210             StreamMessage JavaDoc message = session.createStreamMessage();
211             message.writeString("This is a test to see how it works.");
212             producer.send(message);
213         }
214         
215         // Check the message.
216
{
217             StreamMessage JavaDoc message = (StreamMessage JavaDoc) consumer.receive(1000);
218             assertNotNull(message);
219             
220             // Invalid conversion should throw exception and not move the stream position.
221
try {
222                 message.readByte();
223                 fail("Should have received NumberFormatException");
224             } catch (NumberFormatException JavaDoc e) {
225             }
226             
227             assertEquals("This is a test to see how it works.", message.readString() );
228     
229             // Invalid conversion should throw exception and not move the stream position.
230
try {
231                 message.readByte();
232                 fail("Should have received MessageEOFException");
233             } catch (MessageEOFException JavaDoc e) {
234             }
235         }
236         assertNull(consumer.receiveNoWait());
237     }
238
239     public void testMapMessage() throws Exception JavaDoc {
240
241         // Receive a message with the JMS API
242
connection.start();
243         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
244         destination = createDestination(session, destinationType);
245         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
246         MessageProducer JavaDoc producer = session.createProducer(destination);
247
248         // send the message.
249
{
250             MapMessage JavaDoc message = session.createMapMessage();
251             message.setBoolean("boolKey", true);
252             producer.send(message);
253         }
254         
255         // get the message.
256
{
257             MapMessage JavaDoc message = (MapMessage JavaDoc)consumer.receive(1000);
258             assertNotNull(message);
259             assertTrue( message.getBoolean("boolKey") );
260         }
261         assertNull(consumer.receiveNoWait());
262     }
263     
264     static class ForeignMessage implements TextMessage JavaDoc {
265
266         private String JavaDoc messageId;
267         private long timestamp;
268         private String JavaDoc correlationId;
269         private Destination JavaDoc replyTo;
270         private Destination JavaDoc destination;
271         public int deliveryMode;
272         private boolean redelivered;
273         private String JavaDoc type;
274         private long expiration;
275         private int priority;
276         private String JavaDoc text;
277         HashMap JavaDoc props = new HashMap JavaDoc();
278
279         public String JavaDoc getJMSMessageID() throws JMSException JavaDoc {
280             return messageId;
281         }
282
283         public void setJMSMessageID(String JavaDoc arg0) throws JMSException JavaDoc {
284             messageId = arg0;
285         }
286
287         public long getJMSTimestamp() throws JMSException JavaDoc {
288             return timestamp;
289         }
290
291         public void setJMSTimestamp(long arg0) throws JMSException JavaDoc {
292             timestamp = arg0;
293         }
294
295         public byte[] getJMSCorrelationIDAsBytes() throws JMSException JavaDoc {
296             return null;
297         }
298         public void setJMSCorrelationIDAsBytes(byte[] arg0) throws JMSException JavaDoc {
299         }
300
301         public void setJMSCorrelationID(String JavaDoc arg0) throws JMSException JavaDoc {
302             correlationId = arg0;
303         }
304         public String JavaDoc getJMSCorrelationID() throws JMSException JavaDoc {
305             return correlationId;
306         }
307
308         public Destination JavaDoc getJMSReplyTo() throws JMSException JavaDoc {
309             return replyTo;
310         }
311         public void setJMSReplyTo(Destination JavaDoc arg0) throws JMSException JavaDoc {
312             replyTo = arg0;
313         }
314
315         public Destination JavaDoc getJMSDestination() throws JMSException JavaDoc {
316             return destination;
317         }
318
319         public void setJMSDestination(Destination JavaDoc arg0) throws JMSException JavaDoc {
320             destination = arg0;
321         }
322
323         public int getJMSDeliveryMode() throws JMSException JavaDoc {
324             return deliveryMode;
325         }
326
327         public void setJMSDeliveryMode(int arg0) throws JMSException JavaDoc {
328             deliveryMode = arg0;
329         }
330
331         public boolean getJMSRedelivered() throws JMSException JavaDoc {
332             return redelivered;
333         }
334
335         public void setJMSRedelivered(boolean arg0) throws JMSException JavaDoc {
336             redelivered=arg0;
337         }
338
339         public String JavaDoc getJMSType() throws JMSException JavaDoc {
340             return type;
341         }
342
343         public void setJMSType(String JavaDoc arg0) throws JMSException JavaDoc {
344             type=arg0;
345         }
346
347         public long getJMSExpiration() throws JMSException JavaDoc {
348             return expiration;
349         }
350
351         public void setJMSExpiration(long arg0) throws JMSException JavaDoc {
352             expiration = arg0;
353         }
354
355         public int getJMSPriority() throws JMSException JavaDoc {
356             return priority;
357         }
358
359         public void setJMSPriority(int arg0) throws JMSException JavaDoc {
360             priority=arg0;
361         }
362
363         public void clearProperties() throws JMSException JavaDoc {
364         }
365         public boolean propertyExists(String JavaDoc arg0) throws JMSException JavaDoc {
366             return false;
367         }
368         public boolean getBooleanProperty(String JavaDoc arg0) throws JMSException JavaDoc {
369             return false;
370         }
371         public byte getByteProperty(String JavaDoc arg0) throws JMSException JavaDoc {
372             return 0;
373         }
374         public short getShortProperty(String JavaDoc arg0) throws JMSException JavaDoc {
375             return 0;
376         }
377         public int getIntProperty(String JavaDoc arg0) throws JMSException JavaDoc {
378             return 0;
379         }
380         public long getLongProperty(String JavaDoc arg0) throws JMSException JavaDoc {
381             return 0;
382         }
383         public float getFloatProperty(String JavaDoc arg0) throws JMSException JavaDoc {
384             return 0;
385         }
386         public double getDoubleProperty(String JavaDoc arg0) throws JMSException JavaDoc {
387             return 0;
388         }
389         public String JavaDoc getStringProperty(String JavaDoc arg0) throws JMSException JavaDoc {
390             return (String JavaDoc) props.get(arg0);
391         }
392         public Object JavaDoc getObjectProperty(String JavaDoc arg0) throws JMSException JavaDoc {
393             return props.get(arg0);
394         }
395         public Enumeration JavaDoc getPropertyNames() throws JMSException JavaDoc {
396             return new Vector JavaDoc(props.keySet()).elements();
397         }
398         public void setBooleanProperty(String JavaDoc arg0, boolean arg1) throws JMSException JavaDoc {
399         }
400         public void setByteProperty(String JavaDoc arg0, byte arg1) throws JMSException JavaDoc {
401         }
402         public void setShortProperty(String JavaDoc arg0, short arg1) throws JMSException JavaDoc {
403         }
404         public void setIntProperty(String JavaDoc arg0, int arg1) throws JMSException JavaDoc {
405         }
406         public void setLongProperty(String JavaDoc arg0, long arg1) throws JMSException JavaDoc {
407         }
408         public void setFloatProperty(String JavaDoc arg0, float arg1) throws JMSException JavaDoc {
409         }
410         public void setDoubleProperty(String JavaDoc arg0, double arg1) throws JMSException JavaDoc {
411         }
412         public void setStringProperty(String JavaDoc arg0, String JavaDoc arg1) throws JMSException JavaDoc {
413             props.put( arg0, arg1);
414         }
415         public void setObjectProperty(String JavaDoc arg0, Object JavaDoc arg1) throws JMSException JavaDoc {
416             props.put( arg0, arg1);
417         }
418
419         public void acknowledge() throws JMSException JavaDoc {
420         }
421         public void clearBody() throws JMSException JavaDoc {
422         }
423
424         public void setText(String JavaDoc arg0) throws JMSException JavaDoc {
425             text = arg0;
426         }
427         public String JavaDoc getText() throws JMSException JavaDoc {
428             return text;
429         }
430     }
431
432     public void testForeignMessage() throws Exception JavaDoc {
433
434         // Receive a message with the JMS API
435
connection.start();
436         Session JavaDoc session = connection.createSession(false, Session.AUTO_ACKNOWLEDGE);
437         destination = createDestination(session, destinationType);
438         MessageConsumer JavaDoc consumer = session.createConsumer(destination);
439         MessageProducer JavaDoc producer = session.createProducer(destination);
440         
441         // Send the message.
442
{
443             ForeignMessage message = new ForeignMessage();
444             message.text= "Hello";
445             message.setStringProperty("test", "value");
446             producer.send(message);
447         }
448         
449         // Validate message is OK.
450
{
451             TextMessage JavaDoc message = (TextMessage JavaDoc)consumer.receive(1000);
452             assertNotNull(message);
453             assertEquals( "Hello", message.getText() );
454             assertEquals( "value", message.getStringProperty("test") );
455         }
456         
457         assertNull(consumer.receiveNoWait());
458     }
459
460     
461 }
462
Popular Tags