KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > activemq > command > ActiveMQBytesMessageTest


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.command;
19
20 import javax.jms.JMSException JavaDoc;
21 import javax.jms.MessageFormatException JavaDoc;
22 import javax.jms.MessageNotReadableException JavaDoc;
23 import javax.jms.MessageNotWriteableException JavaDoc;
24
25 import org.apache.activemq.command.ActiveMQBytesMessage;
26 import org.apache.activemq.command.CommandTypes;
27
28 import junit.framework.TestCase;
29
30 /**
31  * @version $Revision$
32  */

33 public class ActiveMQBytesMessageTest extends TestCase {
34     public static void main(String JavaDoc[] args) {
35         junit.textui.TestRunner.run(ActiveMQBytesMessageTest.class);
36     }
37
38     /*
39      * @see TestCase#setUp()
40      */

41     protected void setUp() throws Exception JavaDoc {
42         super.setUp();
43     }
44
45     /*
46      * @see TestCase#tearDown()
47      */

48     protected void tearDown() throws Exception JavaDoc {
49         super.tearDown();
50     }
51
52     /**
53      * Constructor for ActiveMQBytesMessageTest.
54      *
55      * @param arg0
56      */

57     public ActiveMQBytesMessageTest(String JavaDoc arg0) {
58         super(arg0);
59     }
60
61     public void testGetDataStructureType() {
62         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
63         assertEquals(msg.getDataStructureType(), CommandTypes.ACTIVEMQ_BYTES_MESSAGE);
64     }
65
66     public void testGetBodyLength() {
67         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
68         int len = 10;
69         try {
70             for (int i = 0; i < len; i++) {
71                 msg.writeLong(5l);
72             }
73         } catch (JMSException JavaDoc ex) {
74             ex.printStackTrace();
75         }
76         try {
77             msg.reset();
78             assertTrue(msg.getBodyLength() == (len * 8));
79         } catch (Throwable JavaDoc e) {
80             e.printStackTrace();
81             assertTrue(false);
82         }
83     }
84
85     public void testReadBoolean() {
86         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
87         try {
88             msg.writeBoolean(true);
89             msg.reset();
90             assertTrue(msg.readBoolean());
91         } catch (JMSException JavaDoc jmsEx) {
92             jmsEx.printStackTrace();
93             assertTrue(false);
94         }
95     }
96
97     public void testReadByte() {
98         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
99         try {
100             msg.writeByte((byte) 2);
101             msg.reset();
102             assertTrue(msg.readByte() == 2);
103         } catch (JMSException JavaDoc jmsEx) {
104             jmsEx.printStackTrace();
105             assertTrue(false);
106         }
107     }
108
109     public void testReadUnsignedByte() {
110         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
111         try {
112             msg.writeByte((byte) 2);
113             msg.reset();
114             assertTrue(msg.readUnsignedByte() == 2);
115         } catch (JMSException JavaDoc jmsEx) {
116             jmsEx.printStackTrace();
117             assertTrue(false);
118         }
119     }
120
121     public void testReadShort() {
122         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
123         try {
124             msg.writeShort((short) 3000);
125             msg.reset();
126             assertTrue(msg.readShort() == 3000);
127         } catch (JMSException JavaDoc jmsEx) {
128             jmsEx.printStackTrace();
129             assertTrue(false);
130         }
131     }
132
133     public void testReadUnsignedShort() {
134         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
135         try {
136             msg.writeShort((short) 3000);
137             msg.reset();
138             assertTrue(msg.readUnsignedShort() == 3000);
139         } catch (JMSException JavaDoc jmsEx) {
140             jmsEx.printStackTrace();
141             assertTrue(false);
142         }
143     }
144
145     public void testReadChar() {
146         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
147         try {
148             msg.writeChar('a');
149             msg.reset();
150             assertTrue(msg.readChar() == 'a');
151         } catch (JMSException JavaDoc jmsEx) {
152             jmsEx.printStackTrace();
153             assertTrue(false);
154         }
155     }
156
157     public void testReadInt() {
158         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
159         try {
160             msg.writeInt(3000);
161             msg.reset();
162             assertTrue(msg.readInt() == 3000);
163         } catch (JMSException JavaDoc jmsEx) {
164             jmsEx.printStackTrace();
165             assertTrue(false);
166         }
167     }
168
169     public void testReadLong() {
170         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
171         try {
172             msg.writeLong(3000);
173             msg.reset();
174             assertTrue(msg.readLong() == 3000);
175         } catch (JMSException JavaDoc jmsEx) {
176             jmsEx.printStackTrace();
177             assertTrue(false);
178         }
179     }
180
181     public void testReadFloat() {
182         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
183         try {
184             msg.writeFloat(3.3f);
185             msg.reset();
186             assertTrue(msg.readFloat() == 3.3f);
187         } catch (JMSException JavaDoc jmsEx) {
188             jmsEx.printStackTrace();
189             assertTrue(false);
190         }
191     }
192
193     public void testReadDouble() {
194         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
195         try {
196             msg.writeDouble(3.3d);
197             msg.reset();
198             assertTrue(msg.readDouble() == 3.3d);
199         } catch (JMSException JavaDoc jmsEx) {
200             jmsEx.printStackTrace();
201             assertTrue(false);
202         }
203     }
204
205     public void testReadUTF() {
206         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
207         try {
208             String JavaDoc str = "this is a test";
209             msg.writeUTF(str);
210             msg.reset();
211             assertTrue(msg.readUTF().equals(str));
212         } catch (JMSException JavaDoc jmsEx) {
213             jmsEx.printStackTrace();
214             assertTrue(false);
215         }
216     }
217
218     /*
219      * Class to test for int readBytes(byte[])
220      */

221     public void testReadBytesbyteArray() {
222         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
223         try {
224             byte[] data = new byte[50];
225             for (int i = 0; i < data.length; i++) {
226                 data[i] = (byte) i;
227             }
228             msg.writeBytes(data);
229             msg.reset();
230             byte[] test = new byte[data.length];
231             msg.readBytes(test);
232             for (int i = 0; i < test.length; i++) {
233                 assertTrue(test[i] == i);
234             }
235         } catch (JMSException JavaDoc jmsEx) {
236             jmsEx.printStackTrace();
237             assertTrue(false);
238         }
239     }
240
241     public void testWriteObject() throws JMSException JavaDoc {
242         ActiveMQBytesMessage msg = new ActiveMQBytesMessage();
243         try {
244             msg.writeObject("fred");
245             msg.writeObject(Boolean.TRUE);
246             msg.writeObject(new Character JavaDoc('q'));
247             msg.writeObject(new Byte JavaDoc((byte) 1));
248             msg.writeObject(new Short JavaDoc((short) 3));
249             msg.writeObject(new Integer JavaDoc(3));
250             msg.writeObject(new Long JavaDoc(300l));
251             msg.writeObject(new Float JavaDoc(3.3f));
252             msg.writeObject(new Double JavaDoc(3.3));
253             msg.writeObject(new byte[3]);
254         } catch (MessageFormatException JavaDoc mfe) {
255             fail("objectified primitives should be allowed");
256         }
257         try {
258             msg.writeObject(new Object JavaDoc());
259             fail("only objectified primitives are allowed");
260         } catch (MessageFormatException JavaDoc mfe) {
261         }
262     }
263
264
265     /* new */
266     public void testClearBody() throws JMSException JavaDoc {
267         ActiveMQBytesMessage bytesMessage = new ActiveMQBytesMessage();
268         try {
269             bytesMessage.writeInt(1);
270             bytesMessage.clearBody();
271             assertFalse(bytesMessage.isReadOnlyBody());
272             bytesMessage.writeInt(1);
273             bytesMessage.readInt();
274         } catch (MessageNotReadableException JavaDoc mnwe) {
275         } catch (MessageNotWriteableException JavaDoc mnwe) {
276             assertTrue(false);
277         }
278     }
279
280     public void testReset() throws JMSException JavaDoc {
281         ActiveMQBytesMessage message = new ActiveMQBytesMessage();
282         try {
283             message.writeDouble(24.5);
284             message.writeLong(311);
285         } catch (MessageNotWriteableException JavaDoc mnwe) {
286             fail("should be writeable");
287         }
288         message.reset();
289         try {
290             assertTrue(message.isReadOnlyBody());
291             assertEquals(message.readDouble(), 24.5, 0);
292             assertEquals(message.readLong(), 311);
293         } catch (MessageNotReadableException JavaDoc mnre) {
294             fail("should be readable");
295         }
296         try {
297             message.writeInt(33);
298             fail("should throw exception");
299         } catch (MessageNotWriteableException JavaDoc mnwe) {
300         }
301     }
302
303     public void testReadOnlyBody() throws JMSException JavaDoc {
304         ActiveMQBytesMessage message = new ActiveMQBytesMessage();
305         try {
306             message.writeBoolean(true);
307             message.writeByte((byte) 1);
308             message.writeByte((byte) 1);
309             message.writeBytes(new byte[1]);
310             message.writeBytes(new byte[3], 0, 2);
311             message.writeChar('a');
312             message.writeDouble(1.5);
313             message.writeFloat((float) 1.5);
314             message.writeInt(1);
315             message.writeLong(1);
316             message.writeObject("stringobj");
317             message.writeShort((short) 1);
318             message.writeShort((short) 1);
319             message.writeUTF("utfstring");
320         } catch (MessageNotWriteableException JavaDoc mnwe) {
321             fail("Should be writeable");
322         }
323         message.reset();
324         try {
325             message.readBoolean();
326             message.readByte();
327             message.readUnsignedByte();
328             message.readBytes(new byte[1]);
329             message.readBytes(new byte[2], 2);
330             message.readChar();
331             message.readDouble();
332             message.readFloat();
333             message.readInt();
334             message.readLong();
335             message.readUTF();
336             message.readShort();
337             message.readUnsignedShort();
338             message.readUTF();
339         } catch (MessageNotReadableException JavaDoc mnwe) {
340             fail("Should be readable");
341         }
342         try {
343             message.writeBoolean(true);
344             fail("Should have thrown exception");
345         } catch (MessageNotWriteableException JavaDoc mnwe) {
346         }
347         try {
348             message.writeByte((byte) 1);
349             fail("Should have thrown exception");
350         } catch (MessageNotWriteableException JavaDoc mnwe) {
351         }
352         try {
353             message.writeBytes(new byte[1]);
354             fail("Should have thrown exception");
355         } catch (MessageNotWriteableException JavaDoc mnwe) {
356         }
357         try {
358             message.writeBytes(new byte[3], 0, 2);
359             fail("Should have thrown exception");
360         } catch (MessageNotWriteableException JavaDoc mnwe) {
361         }
362         try {
363             message.writeChar('a');
364             fail("Should have thrown exception");
365         } catch (MessageNotWriteableException JavaDoc mnwe) {
366         }
367         try {
368             message.writeDouble(1.5);
369             fail("Should have thrown exception");
370         } catch (MessageNotWriteableException JavaDoc mnwe) {
371         }
372         try {
373             message.writeFloat((float) 1.5);
374             fail("Should have thrown exception");
375         } catch (MessageNotWriteableException JavaDoc mnwe) {
376         }
377         try {
378             message.writeInt(1);
379             fail("Should have thrown exception");
380         } catch (MessageNotWriteableException JavaDoc mnwe) {
381         }
382         try {
383             message.writeLong(1);
384             fail("Should have thrown exception");
385         } catch (MessageNotWriteableException JavaDoc mnwe) {
386         }
387         try {
388             message.writeObject("stringobj");
389             fail("Should have thrown exception");
390         } catch (MessageNotWriteableException JavaDoc mnwe) {
391         }
392         try {
393             message.writeShort((short) 1);
394             fail("Should have thrown exception");
395         } catch (MessageNotWriteableException JavaDoc mnwe) {
396         }
397         try {
398             message.writeUTF("utfstring");
399             fail("Should have thrown exception");
400         } catch (MessageNotWriteableException JavaDoc mnwe) {
401         }
402     }
403
404     public void testWriteOnlyBody() throws JMSException JavaDoc {
405         ActiveMQBytesMessage message = new ActiveMQBytesMessage();
406         message.clearBody();
407         try {
408             message.writeBoolean(true);
409             message.writeByte((byte) 1);
410             message.writeByte((byte) 1);
411             message.writeBytes(new byte[1]);
412             message.writeBytes(new byte[3], 0, 2);
413             message.writeChar('a');
414             message.writeDouble(1.5);
415             message.writeFloat((float) 1.5);
416             message.writeInt(1);
417             message.writeLong(1);
418             message.writeObject("stringobj");
419             message.writeShort((short) 1);
420             message.writeShort((short) 1);
421             message.writeUTF("utfstring");
422         } catch (MessageNotWriteableException JavaDoc mnwe) {
423             fail("Should be writeable");
424         }
425         try {
426             message.readBoolean();
427             fail("Should have thrown exception");
428         } catch (MessageNotReadableException JavaDoc mnwe) {
429         }
430         try {
431             message.readByte();
432             fail("Should have thrown exception");
433         } catch (MessageNotReadableException JavaDoc e) {
434         }
435         try {
436             message.readUnsignedByte();
437             fail("Should have thrown exception");
438         } catch (MessageNotReadableException JavaDoc e) {
439         }
440         try {
441             message.readBytes(new byte[1]);
442             fail("Should have thrown exception");
443         } catch (MessageNotReadableException JavaDoc e) {
444         }
445         try {
446             message.readBytes(new byte[2], 2);
447             fail("Should have thrown exception");
448         } catch (MessageNotReadableException JavaDoc e) {
449         }
450         try {
451             message.readChar();
452             fail("Should have thrown exception");
453         } catch (MessageNotReadableException JavaDoc e) {
454         }
455         try {
456             message.readDouble();
457             fail("Should have thrown exception");
458         } catch (MessageNotReadableException JavaDoc e) {
459         }
460         try {
461             message.readFloat();
462             fail("Should have thrown exception");
463         } catch (MessageNotReadableException JavaDoc e) {
464         }
465         try {
466             message.readInt();
467             fail("Should have thrown exception");
468         } catch (MessageNotReadableException JavaDoc e) {
469         }
470         try {
471             message.readLong();
472             fail("Should have thrown exception");
473         } catch (MessageNotReadableException JavaDoc e) {
474         }
475         try {
476             message.readUTF();
477             fail("Should have thrown exception");
478         } catch (MessageNotReadableException JavaDoc e) {
479         }
480         try {
481             message.readShort();
482             fail("Should have thrown exception");
483         } catch (MessageNotReadableException JavaDoc e) {
484         }
485         try {
486             message.readUnsignedShort();
487             fail("Should have thrown exception");
488         } catch (MessageNotReadableException JavaDoc e) {
489         }
490         try {
491             message.readUTF();
492             fail("Should have thrown exception");
493         } catch (MessageNotReadableException JavaDoc e) {
494         }
495     }
496 }
497
Popular Tags