KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > test > jbossmq > test > MessageTypesUnitTest


1 /*
2  * JBoss, Home of Professional Open Source
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.test.jbossmq.test;
8
9 import java.util.Arrays JavaDoc;
10 import java.math.BigInteger JavaDoc;
11 import javax.jms.DeliveryMode JavaDoc;
12 import javax.jms.MapMessage JavaDoc;
13 import javax.jms.Message JavaDoc;
14 import javax.jms.Queue JavaDoc;
15 import javax.jms.QueueConnection JavaDoc;
16 import javax.jms.QueueConnectionFactory JavaDoc;
17 import javax.jms.QueueReceiver JavaDoc;
18 import javax.jms.QueueSender JavaDoc;
19 import javax.jms.QueueSession JavaDoc;
20 import javax.jms.Session JavaDoc;
21 import javax.jms.TextMessage JavaDoc;
22 import javax.jms.BytesMessage JavaDoc;
23 import javax.jms.ObjectMessage JavaDoc;
24 import javax.jms.StreamMessage JavaDoc;
25 import javax.naming.Context JavaDoc;
26 import javax.naming.InitialContext JavaDoc;
27
28 import org.jboss.test.JBossTestCase;
29
30 /**
31  * Tests of sending/receiving all jms message types to/from a queue
32  *
33  * @author Scott.Stark@jboss.org
34  * @version $Revision: 1.1 $
35  */

36 public class MessageTypesUnitTest extends JBossTestCase
37 {
38    static String JavaDoc QUEUE_FACTORY = "ConnectionFactory";
39    static String JavaDoc TEST_QUEUE = "queue/testQueue";
40
41    private Context JavaDoc context;
42    private QueueConnection JavaDoc queueConnection;
43    private QueueSession JavaDoc session;
44    private QueueSender JavaDoc sender;
45    private QueueReceiver JavaDoc receiver;
46
47    public MessageTypesUnitTest(String JavaDoc name) throws Exception JavaDoc
48    {
49       super(name);
50    }
51
52    public void testMapMessage() throws Exception JavaDoc
53    {
54       log.info("+++ testMapMessage");
55       MapMessage JavaDoc sent = session.createMapMessage();
56       sent.setBoolean("Boolean", true);
57       sent.setByte("Byte", (byte) 1);
58       sent.setBytes("Bytes", "Bytes".getBytes());
59       sent.setChar("Char", 'c');
60       sent.setShort("Short", (short) 31415);
61       sent.setInt("Int", 314159);
62       sent.setLong("Long", 3141592653589793238L);
63       sent.setDouble("Double", 3.1415926535897932384626433832795);
64       sent.setFloat("Float", 3.141f);
65       sent.setObject("Object", "31415926535897932384626433832795");
66       sent.setString("String", "31415926535897932384626433832795");
67
68       MapMessage JavaDoc recv = (MapMessage JavaDoc) sendRecMsg(sent);
69       log.debug("recv: "+recv);
70       assertTrue("Boolean == true", recv.getBoolean("Boolean") == true);
71       assertTrue("Byte == 1", recv.getByte("Byte") == 1);
72       assertTrue("Bytes == Bytes[]",
73          Arrays.equals(recv.getBytes("Bytes"), "Bytes".getBytes()));
74       assertTrue("Char == c", recv.getChar("Char") == 'c');
75       assertTrue("Short == 314159", recv.getShort("Short") == 31415);
76       assertTrue("Int == 314159", recv.getInt("Int") == 314159);
77       assertTrue("Long == 3141592653589793238L",
78          recv.getLong("Long") == 3141592653589793238L);
79       assertTrue("Double == 3.1415926535897932384626433832795",
80          recv.getDouble("Double") == 3.1415926535897932384626433832795);
81       assertTrue("Float == true", recv.getFloat("Float") == 3.141f);
82       assertTrue("Object == 31415926535897932384626433832795",
83          recv.getObject("Object").equals("31415926535897932384626433832795"));
84       assertTrue("String == 31415926535897932384626433832795",
85          recv.getString("String").equals("31415926535897932384626433832795"));
86    }
87    public void testTextMessage() throws Exception JavaDoc
88    {
89       log.info("+++ testTextMessage");
90       String JavaDoc text = "A multiline text msg.\nSecond line.\n";
91       TextMessage JavaDoc sent = session.createTextMessage(text);
92       TextMessage JavaDoc recv = (TextMessage JavaDoc) sendRecMsg(sent);
93       log.debug("recv: "+recv);
94       assertTrue(recv.getText().equals(text));
95    }
96    public void testMessage() throws Exception JavaDoc
97    {
98       log.info("+++ testMessage");
99       Message JavaDoc sent = session.createMessage();
100       sent.setBooleanProperty("Boolean", true);
101       sent.setByteProperty("Byte", (byte) 1);
102       sent.setShortProperty("Short", (short) 31415);
103       sent.setIntProperty("Int", 314159);
104       sent.setLongProperty("Long", 3141592653589793238L);
105       sent.setDoubleProperty("Double", 3.1415926535897932384626433832795);
106       sent.setFloatProperty("Float", 3.141f);
107       sent.setObjectProperty("Object", "31415926535897932384626433832795");
108       sent.setStringProperty("String", "31415926535897932384626433832795");
109
110       Message JavaDoc recv = sendRecMsg(sent);
111       log.debug("recv: "+recv);
112       assertTrue("Boolean == true", recv.getBooleanProperty("Boolean") == true);
113       assertTrue("Byte == 1", recv.getByteProperty("Byte") == 1);
114       assertTrue("Short == 314159", recv.getShortProperty("Short") == 31415);
115       assertTrue("Int == 314159", recv.getIntProperty("Int") == 314159);
116       assertTrue("Long == 3141592653589793238L",
117          recv.getLongProperty("Long") == 3141592653589793238L);
118       assertTrue("Double == 3.1415926535897932384626433832795",
119          recv.getDoubleProperty("Double") == 3.1415926535897932384626433832795);
120       assertTrue("Float == true", recv.getFloatProperty("Float") == 3.141f);
121       assertTrue("Object == 31415926535897932384626433832795",
122          recv.getObjectProperty("Object").equals("31415926535897932384626433832795"));
123       assertTrue("String == 31415926535897932384626433832795",
124          recv.getStringProperty("String").equals("31415926535897932384626433832795"));
125    }
126    public void testBytesMessage() throws Exception JavaDoc
127    {
128       log.info("+++ testBytesMessage");
129       BytesMessage JavaDoc sent = session.createBytesMessage();
130       sent.writeBoolean(true);
131       sent.writeByte((byte) 1);
132       byte[] testBytes = "Bytes".getBytes();
133       sent.writeBytes(testBytes);
134       sent.writeChar('c');
135       sent.writeShort((short) 31415);
136       sent.writeInt(314159);
137       sent.writeLong(3141592653589793238L);
138       sent.writeDouble(3.1415926535897932384626433832795);
139       sent.writeFloat(3.141f);
140       sent.writeObject("31415926535897932384626433832795");
141       sent.writeUTF("31415926535897932384626433832795");
142
143       BytesMessage JavaDoc recv = (BytesMessage JavaDoc) sendRecMsg(sent);
144       log.debug("recv: "+recv);
145       assertTrue("Boolean == true", recv.readBoolean() == true);
146       assertTrue("Byte == 1", recv.readByte() == 1);
147       byte[] bytes = new byte[testBytes.length];
148       recv.readBytes(bytes);
149       assertTrue("Bytes == Bytes[]",
150          Arrays.equals(bytes, testBytes));
151       assertTrue("Char == c", recv.readChar() == 'c');
152       assertTrue("Short == 314159", recv.readShort() == 31415);
153       assertTrue("Int == 314159", recv.readInt() == 314159);
154       assertTrue("Long == 3141592653589793238L",
155          recv.readLong() == 3141592653589793238L);
156       assertTrue("Double == 3.1415926535897932384626433832795",
157          recv.readDouble() == 3.1415926535897932384626433832795);
158       assertTrue("Float == true", recv.readFloat() == 3.141f);
159       assertTrue("Object == 31415926535897932384626433832795",
160          recv.readUTF().equals("31415926535897932384626433832795"));
161       assertTrue("String == 31415926535897932384626433832795",
162          recv.readUTF().equals("31415926535897932384626433832795"));
163    }
164    public void testObjectMessage() throws Exception JavaDoc
165    {
166       log.info("+++ testObjectMessage");
167       BigInteger JavaDoc data = new BigInteger JavaDoc("31415926535897932384626433832795", 10);
168       ObjectMessage JavaDoc sent = session.createObjectMessage(data);
169       ObjectMessage JavaDoc recv = (ObjectMessage JavaDoc) sendRecMsg(sent);
170       log.debug("recv: "+recv);
171       BigInteger JavaDoc data2 = (BigInteger JavaDoc) recv.getObject();
172       assertTrue("BigInteger == BigInteger2", data2.equals(data));
173    }
174    public void testStreamMessage() throws Exception JavaDoc
175    {
176       log.info("+++ testStreamMessage");
177       StreamMessage JavaDoc sent = session.createStreamMessage();
178       sent.writeBoolean(true);
179       sent.writeByte((byte) 1);
180       byte[] testBytes = "Bytes".getBytes();
181       sent.writeBytes(testBytes);
182       sent.writeChar('c');
183       sent.writeShort((short) 31415);
184       sent.writeInt(314159);
185       sent.writeLong(3141592653589793238L);
186       sent.writeDouble(3.1415926535897932384626433832795);
187       sent.writeFloat(3.141f);
188       sent.writeObject("31415926535897932384626433832795");
189       sent.writeString("31415926535897932384626433832795");
190
191       StreamMessage JavaDoc recv = (StreamMessage JavaDoc) sendRecMsg(sent);
192       log.debug("recv: "+recv);
193       assertTrue("Boolean == true", recv.readBoolean() == true);
194       assertTrue("Byte == 1", recv.readByte() == 1);
195       // Quirky spec behavior requires a read past the end of the byte[] field
196
byte[] bytes = new byte[testBytes.length];
197       recv.readBytes(bytes);
198       assertTrue(recv.readBytes(bytes) < 0);
199       assertTrue("Bytes == Bytes[]",
200          Arrays.equals(bytes, testBytes));
201       char c = recv.readChar();
202       assertTrue("Char == c", c == 'c');
203       assertTrue("Short == 314159", recv.readShort() == 31415);
204       assertTrue("Int == 314159", recv.readInt() == 314159);
205       assertTrue("Long == 3141592653589793238L",
206          recv.readLong() == 3141592653589793238L);
207       assertTrue("Double == 3.1415926535897932384626433832795",
208          recv.readDouble() == 3.1415926535897932384626433832795);
209       assertTrue("Float == true", recv.readFloat() == 3.141f);
210       assertTrue("Object == 31415926535897932384626433832795",
211          recv.readObject().equals("31415926535897932384626433832795"));
212       assertTrue("String == 31415926535897932384626433832795",
213          recv.readString().equals("31415926535897932384626433832795"));
214    }
215
216    protected void setUp() throws Exception JavaDoc
217    {
218       context = new InitialContext JavaDoc();
219       QueueConnectionFactory JavaDoc queueFactory = (QueueConnectionFactory JavaDoc) context.lookup(QUEUE_FACTORY);
220       queueConnection = queueFactory.createQueueConnection();
221       queueConnection.start();
222       session = queueConnection.createQueueSession(false, Session.AUTO_ACKNOWLEDGE);
223       Queue JavaDoc queue = (Queue JavaDoc) context.lookup(TEST_QUEUE);
224       sender = session.createSender(queue);
225       receiver = session.createReceiver(queue);
226
227       log.debug("Connection to jms established.");
228    }
229
230    protected void tearDown() throws Exception JavaDoc
231    {
232       sender.close();
233       receiver.close();
234       session.close();
235       queueConnection.close();
236    }
237
238    private Message JavaDoc sendRecMsg(Message JavaDoc in) throws Exception JavaDoc
239    {
240       sender.send(in, DeliveryMode.NON_PERSISTENT, 4, 0);
241       Message JavaDoc out = receiver.receive(5000);
242       return out;
243    }
244
245 }
246
Popular Tags