KickJava   Java API By Example, From Geeks To Geeks.

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


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

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

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

51     protected void tearDown() throws Exception JavaDoc {
52         super.tearDown();
53     }
54
55     /**
56      * Constructor for ActiveMQStreamMessageTest.
57      *
58      * @param arg0
59      */

60     public ActiveMQStreamMessageTest(String JavaDoc arg0) {
61         super(arg0);
62     }
63
64     public void testGetDataStructureType() {
65         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
66         assertEquals(msg.getDataStructureType(), CommandTypes.ACTIVEMQ_STREAM_MESSAGE);
67     }
68
69     public void testReadBoolean() {
70         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
71         try {
72             msg.writeBoolean(true);
73             msg.reset();
74             assertTrue(msg.readBoolean());
75             msg.reset();
76             assertTrue(msg.readString().equals("true"));
77             msg.reset();
78             try {
79                 msg.readByte();
80                 fail("Should have thrown exception");
81             } catch (MessageFormatException JavaDoc mfe) {
82             }
83             msg.reset();
84             try {
85                 msg.readShort();
86                 fail("Should have thrown exception");
87             } catch (MessageFormatException JavaDoc mfe) {
88             }
89             msg.reset();
90             try {
91                 msg.readInt();
92                 fail("Should have thrown exception");
93             } catch (MessageFormatException JavaDoc mfe) {
94             }
95             msg.reset();
96             try {
97                 msg.readLong();
98                 fail("Should have thrown exception");
99             } catch (MessageFormatException JavaDoc mfe) {
100             }
101             msg.reset();
102             try {
103                 msg.readFloat();
104                 fail("Should have thrown exception");
105             } catch (MessageFormatException JavaDoc mfe) {
106             }
107             msg.reset();
108             try {
109                 msg.readDouble();
110                 fail("Should have thrown exception");
111             } catch (MessageFormatException JavaDoc mfe) {
112             }
113             msg.reset();
114             try {
115                 msg.readChar();
116                 fail("Should have thrown exception");
117             } catch (MessageFormatException JavaDoc mfe) {
118             }
119             msg.reset();
120             try {
121                 msg.readBytes(new byte[1]);
122                 fail("Should have thrown exception");
123             } catch (MessageFormatException JavaDoc mfe) {
124             }
125         } catch (JMSException JavaDoc jmsEx) {
126             jmsEx.printStackTrace();
127             assertTrue(false);
128         }
129     }
130
131     public void testreadByte() {
132         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
133         try {
134             byte test = (byte) 4;
135             msg.writeByte(test);
136             msg.reset();
137             assertTrue(msg.readByte() == test);
138             msg.reset();
139             assertTrue(msg.readShort() == test);
140             msg.reset();
141             assertTrue(msg.readInt() == test);
142             msg.reset();
143             assertTrue(msg.readLong() == test);
144             msg.reset();
145             assertTrue(msg.readString().equals(new Byte JavaDoc(test).toString()));
146             msg.reset();
147             try {
148                 msg.readBoolean();
149                 fail("Should have thrown exception");
150             } catch (MessageFormatException JavaDoc mfe) {
151             }
152             msg.reset();
153             try {
154                 msg.readFloat();
155                 fail("Should have thrown exception");
156             } catch (MessageFormatException JavaDoc mfe) {
157             }
158             msg.reset();
159             try {
160                 msg.readDouble();
161                 fail("Should have thrown exception");
162             } catch (MessageFormatException JavaDoc mfe) {
163             }
164             msg.reset();
165             try {
166                 msg.readChar();
167                 fail("Should have thrown exception");
168             } catch (MessageFormatException JavaDoc mfe) {
169             }
170             msg.reset();
171             try {
172                 msg.readBytes(new byte[1]);
173                 fail("Should have thrown exception");
174             } catch (MessageFormatException JavaDoc mfe) {
175             }
176         } catch (JMSException JavaDoc jmsEx) {
177             jmsEx.printStackTrace();
178             assertTrue(false);
179         }
180     }
181
182     public void testReadShort() {
183         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
184         try {
185             short test = (short) 4;
186             msg.writeShort(test);
187             msg.reset();
188             assertTrue(msg.readShort() == test);
189             msg.reset();
190             assertTrue(msg.readInt() == test);
191             msg.reset();
192             assertTrue(msg.readLong() == test);
193             msg.reset();
194             assertTrue(msg.readString().equals(new Short JavaDoc(test).toString()));
195             msg.reset();
196             try {
197                 msg.readBoolean();
198                 fail("Should have thrown exception");
199             } catch (MessageFormatException JavaDoc mfe) {
200             }
201             msg.reset();
202             try {
203                 msg.readByte();
204                 fail("Should have thrown exception");
205             } catch (MessageFormatException JavaDoc mfe) {
206             }
207             msg.reset();
208             try {
209                 msg.readFloat();
210                 fail("Should have thrown exception");
211             } catch (MessageFormatException JavaDoc mfe) {
212             }
213             msg.reset();
214             try {
215                 msg.readDouble();
216                 fail("Should have thrown exception");
217             } catch (MessageFormatException JavaDoc mfe) {
218             }
219             msg.reset();
220             try {
221                 msg.readChar();
222                 fail("Should have thrown exception");
223             } catch (MessageFormatException JavaDoc mfe) {
224             }
225             msg.reset();
226             try {
227                 msg.readBytes(new byte[1]);
228                 fail("Should have thrown exception");
229             } catch (MessageFormatException JavaDoc mfe) {
230             }
231         } catch (JMSException JavaDoc jmsEx) {
232             jmsEx.printStackTrace();
233             assertTrue(false);
234         }
235     }
236
237     public void testReadChar() {
238         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
239         try {
240             char test = 'z';
241             msg.writeChar(test);
242             msg.reset();
243             assertTrue(msg.readChar() == test);
244             msg.reset();
245             assertTrue(msg.readString().equals(new Character JavaDoc(test).toString()));
246             msg.reset();
247             try {
248                 msg.readBoolean();
249                 fail("Should have thrown exception");
250             } catch (MessageFormatException JavaDoc mfe) {
251             }
252             msg.reset();
253             try {
254                 msg.readByte();
255                 fail("Should have thrown exception");
256             } catch (MessageFormatException JavaDoc mfe) {
257             }
258             msg.reset();
259             try {
260                 msg.readShort();
261                 fail("Should have thrown exception");
262             } catch (MessageFormatException JavaDoc mfe) {
263             }
264             msg.reset();
265             try {
266                 msg.readInt();
267                 fail("Should have thrown exception");
268             } catch (MessageFormatException JavaDoc mfe) {
269             }
270             msg.reset();
271             try {
272                 msg.readLong();
273                 fail("Should have thrown exception");
274             } catch (MessageFormatException JavaDoc mfe) {
275             }
276             msg.reset();
277             try {
278                 msg.readFloat();
279                 fail("Should have thrown exception");
280             } catch (MessageFormatException JavaDoc mfe) {
281             }
282             msg.reset();
283             try {
284                 msg.readDouble();
285                 fail("Should have thrown exception");
286             } catch (MessageFormatException JavaDoc mfe) {
287             }
288             msg.reset();
289             try {
290                 msg.readBytes(new byte[1]);
291                 fail("Should have thrown exception");
292             } catch (MessageFormatException JavaDoc mfe) {
293             }
294         } catch (JMSException JavaDoc jmsEx) {
295             jmsEx.printStackTrace();
296             assertTrue(false);
297         }
298     }
299
300     public void testReadInt() {
301         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
302         try {
303             int test = 4;
304             msg.writeInt(test);
305             msg.reset();
306             assertTrue(msg.readInt() == test);
307             msg.reset();
308             assertTrue(msg.readLong() == test);
309             msg.reset();
310             assertTrue(msg.readString().equals(new Integer JavaDoc(test).toString()));
311             msg.reset();
312             try {
313                 msg.readBoolean();
314                 fail("Should have thrown exception");
315             } catch (MessageFormatException JavaDoc mfe) {
316             }
317             msg.reset();
318             try {
319                 msg.readByte();
320                 fail("Should have thrown exception");
321             } catch (MessageFormatException JavaDoc mfe) {
322             }
323             msg.reset();
324             try {
325                 msg.readShort();
326                 fail("Should have thrown exception");
327             } catch (MessageFormatException JavaDoc mfe) {
328             }
329             msg.reset();
330             try {
331                 msg.readFloat();
332                 fail("Should have thrown exception");
333             } catch (MessageFormatException JavaDoc mfe) {
334             }
335             msg.reset();
336             try {
337                 msg.readDouble();
338                 fail("Should have thrown exception");
339             } catch (MessageFormatException JavaDoc mfe) {
340             }
341             msg.reset();
342             try {
343                 msg.readChar();
344                 fail("Should have thrown exception");
345             } catch (MessageFormatException JavaDoc mfe) {
346             }
347             msg.reset();
348             try {
349                 msg.readBytes(new byte[1]);
350                 fail("Should have thrown exception");
351             } catch (MessageFormatException JavaDoc mfe) {
352             }
353         } catch (JMSException JavaDoc jmsEx) {
354             jmsEx.printStackTrace();
355             assertTrue(false);
356         }
357     }
358
359     public void testReadLong() {
360         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
361         try {
362             long test = 4l;
363             msg.writeLong(test);
364             msg.reset();
365             assertTrue(msg.readLong() == test);
366             msg.reset();
367             assertTrue(msg.readString().equals(new Long JavaDoc(test).toString()));
368             msg.reset();
369             try {
370                 msg.readBoolean();
371                 fail("Should have thrown exception");
372             } catch (MessageFormatException JavaDoc mfe) {
373             }
374             msg.reset();
375             try {
376                 msg.readByte();
377                 fail("Should have thrown exception");
378             } catch (MessageFormatException JavaDoc mfe) {
379             }
380             msg.reset();
381             try {
382                 msg.readShort();
383                 fail("Should have thrown exception");
384             } catch (MessageFormatException JavaDoc mfe) {
385             }
386             msg.reset();
387             try {
388                 msg.readInt();
389                 fail("Should have thrown exception");
390             } catch (MessageFormatException JavaDoc mfe) {
391             }
392             msg.reset();
393             try {
394                 msg.readFloat();
395                 fail("Should have thrown exception");
396             } catch (MessageFormatException JavaDoc mfe) {
397             }
398             msg.reset();
399             try {
400                 msg.readDouble();
401                 fail("Should have thrown exception");
402             } catch (MessageFormatException JavaDoc mfe) {
403             }
404             msg.reset();
405             try {
406                 msg.readChar();
407                 fail("Should have thrown exception");
408             } catch (MessageFormatException JavaDoc mfe) {
409             }
410             msg.reset();
411             try {
412                 msg.readBytes(new byte[1]);
413                 fail("Should have thrown exception");
414             } catch (MessageFormatException JavaDoc mfe) {
415             }
416         } catch (JMSException JavaDoc jmsEx) {
417             jmsEx.printStackTrace();
418             assertTrue(false);
419         }
420     }
421
422     public void testReadFloat() {
423         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
424         try {
425             float test = 4.4f;
426             msg.writeFloat(test);
427             msg.reset();
428             assertTrue(msg.readFloat() == test);
429             msg.reset();
430             assertTrue(msg.readDouble() == test);
431             msg.reset();
432             assertTrue(msg.readString().equals(new Float JavaDoc(test).toString()));
433             msg.reset();
434             try {
435                 msg.readBoolean();
436                 fail("Should have thrown exception");
437             } catch (MessageFormatException JavaDoc mfe) {
438             }
439             msg.reset();
440             try {
441                 msg.readByte();
442                 fail("Should have thrown exception");
443             } catch (MessageFormatException JavaDoc mfe) {
444             }
445             msg.reset();
446             try {
447                 msg.readShort();
448                 fail("Should have thrown exception");
449             } catch (MessageFormatException JavaDoc mfe) {
450             }
451             msg.reset();
452             try {
453                 msg.readInt();
454                 fail("Should have thrown exception");
455             } catch (MessageFormatException JavaDoc mfe) {
456             }
457             msg.reset();
458             try {
459                 msg.readLong();
460                 fail("Should have thrown exception");
461             } catch (MessageFormatException JavaDoc mfe) {
462             }
463             msg.reset();
464             try {
465                 msg.readChar();
466                 fail("Should have thrown exception");
467             } catch (MessageFormatException JavaDoc mfe) {
468             }
469             msg.reset();
470             try {
471                 msg.readBytes(new byte[1]);
472                 fail("Should have thrown exception");
473             } catch (MessageFormatException JavaDoc mfe) {
474             }
475         } catch (JMSException JavaDoc jmsEx) {
476             jmsEx.printStackTrace();
477             assertTrue(false);
478         }
479     }
480
481     public void testReadDouble() {
482         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
483         try {
484             double test = 4.4d;
485             msg.writeDouble(test);
486             msg.reset();
487             assertTrue(msg.readDouble() == test);
488             msg.reset();
489             assertTrue(msg.readString().equals(new Double JavaDoc(test).toString()));
490             msg.reset();
491             try {
492                 msg.readBoolean();
493                 fail("Should have thrown exception");
494             } catch (MessageFormatException JavaDoc mfe) {
495             }
496             msg.reset();
497             try {
498                 msg.readByte();
499                 fail("Should have thrown exception");
500             } catch (MessageFormatException JavaDoc mfe) {
501             }
502             msg.reset();
503             try {
504                 msg.readShort();
505                 fail("Should have thrown exception");
506             } catch (MessageFormatException JavaDoc mfe) {
507             }
508             msg.reset();
509             try {
510                 msg.readInt();
511                 fail("Should have thrown exception");
512             } catch (MessageFormatException JavaDoc mfe) {
513             }
514             msg.reset();
515             try {
516                 msg.readLong();
517                 fail("Should have thrown exception");
518             } catch (MessageFormatException JavaDoc mfe) {
519             }
520             msg.reset();
521             try {
522                 msg.readFloat();
523                 fail("Should have thrown exception");
524             } catch (MessageFormatException JavaDoc mfe) {
525             }
526             msg.reset();
527             try {
528                 msg.readChar();
529                 fail("Should have thrown exception");
530             } catch (MessageFormatException JavaDoc mfe) {
531             }
532             msg.reset();
533             try {
534                 msg.readBytes(new byte[1]);
535                 fail("Should have thrown exception");
536             } catch (MessageFormatException JavaDoc mfe) {
537             }
538         } catch (JMSException JavaDoc jmsEx) {
539             jmsEx.printStackTrace();
540             assertTrue(false);
541         }
542     }
543
544     public void testReadString() {
545         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
546         try {
547             byte testByte = (byte) 2;
548             msg.writeString(new Byte JavaDoc(testByte).toString());
549             msg.reset();
550             assertTrue(msg.readByte() == testByte);
551             msg.clearBody();
552             short testShort = 3;
553             msg.writeString(new Short JavaDoc(testShort).toString());
554             msg.reset();
555             assertTrue(msg.readShort() == testShort);
556             msg.clearBody();
557             int testInt = 4;
558             msg.writeString(new Integer JavaDoc(testInt).toString());
559             msg.reset();
560             assertTrue(msg.readInt() == testInt);
561             msg.clearBody();
562             long testLong = 6l;
563             msg.writeString(new Long JavaDoc(testLong).toString());
564             msg.reset();
565             assertTrue(msg.readLong() == testLong);
566             msg.clearBody();
567             float testFloat = 6.6f;
568             msg.writeString(new Float JavaDoc(testFloat).toString());
569             msg.reset();
570             assertTrue(msg.readFloat() == testFloat);
571             msg.clearBody();
572             double testDouble = 7.7d;
573             msg.writeString(new Double JavaDoc(testDouble).toString());
574             msg.reset();
575             assertTrue(msg.readDouble() == testDouble);
576             msg.clearBody();
577             msg.writeString("true");
578             msg.reset();
579             assertTrue(msg.readBoolean());
580             msg.clearBody();
581             msg.writeString("a");
582             msg.reset();
583             try {
584                 msg.readChar();
585                 fail("Should have thrown exception");
586             } catch (MessageFormatException JavaDoc e) {
587             }
588             msg.clearBody();
589             msg.writeString("777");
590             msg.reset();
591             try {
592                 msg.readBytes(new byte[3]);
593                 fail("Should have thrown exception");
594             } catch (MessageFormatException JavaDoc e) {
595             }
596
597
598         } catch (JMSException JavaDoc jmsEx) {
599             jmsEx.printStackTrace();
600             assertTrue(false);
601         }
602     }
603
604     public void testReadBigString() {
605         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
606         try {
607             // Test with a 1Meg String
608
StringBuffer JavaDoc bigSB = new StringBuffer JavaDoc(1024*1024);
609             for( int i=0; i < 1024*1024; i++ ) {
610                 bigSB.append((char)'a'+i%26);
611             }
612             String JavaDoc bigString = bigSB.toString();
613
614             msg.writeString(bigString);
615             msg.reset();
616             assertEquals(bigString, msg.readString());
617             
618         } catch (JMSException JavaDoc jmsEx) {
619             jmsEx.printStackTrace();
620             assertTrue(false);
621         }
622     }
623
624     public void testReadBytes() {
625         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
626         try {
627             byte[] test = new byte[50];
628             for (int i = 0; i < test.length; i++) {
629                 test[i] = (byte) i;
630             }
631             msg.writeBytes(test);
632             msg.reset();
633             byte[] valid = new byte[test.length];
634             msg.readBytes(valid);
635             for (int i = 0; i < valid.length; i++) {
636                 assertTrue(valid[i] == test[i]);
637             }
638             msg.reset();
639             try {
640                 msg.readByte();
641                 fail("Should have thrown exception");
642             } catch (MessageFormatException JavaDoc mfe) {
643             }
644             msg.reset();
645             try {
646                 msg.readShort();
647                 fail("Should have thrown exception");
648             } catch (MessageFormatException JavaDoc mfe) {
649             }
650             msg.reset();
651             try {
652                 msg.readInt();
653                 fail("Should have thrown exception");
654             } catch (MessageFormatException JavaDoc mfe) {
655             }
656             msg.reset();
657             try {
658                 msg.readLong();
659                 fail("Should have thrown exception");
660             } catch (MessageFormatException JavaDoc mfe) {
661             }
662             msg.reset();
663             try {
664                 msg.readFloat();
665                 fail("Should have thrown exception");
666             } catch (MessageFormatException JavaDoc mfe) {
667             }
668             msg.reset();
669             try {
670                 msg.readChar();
671                 fail("Should have thrown exception");
672             } catch (MessageFormatException JavaDoc mfe) {
673             }
674             msg.reset();
675             try {
676                 msg.readString();
677                 fail("Should have thrown exception");
678             } catch (MessageFormatException JavaDoc mfe) {
679             }
680         } catch (JMSException JavaDoc jmsEx) {
681             jmsEx.printStackTrace();
682             assertTrue(false);
683         }
684     }
685
686     public void testReadObject() {
687         ActiveMQStreamMessage msg = new ActiveMQStreamMessage();
688         try {
689             byte testByte = (byte) 2;
690             msg.writeByte(testByte);
691             msg.reset();
692             assertTrue(((Byte JavaDoc) msg.readObject()).byteValue() == testByte);
693             msg.clearBody();
694
695             short testShort = 3;
696             msg.writeShort(testShort);
697             msg.reset();
698             assertTrue(((Short JavaDoc) msg.readObject()).shortValue() == testShort);
699             msg.clearBody();
700
701             int testInt = 4;
702             msg.writeInt(testInt);
703             msg.reset();
704             assertTrue(((Integer JavaDoc) msg.readObject()).intValue() == testInt);
705             msg.clearBody();
706
707             long testLong = 6l;
708             msg.writeLong(testLong);
709             msg.reset();
710             assertTrue(((Long JavaDoc) msg.readObject()).longValue() == testLong);
711             msg.clearBody();
712
713             float testFloat = 6.6f;
714             msg.writeFloat(testFloat);
715             msg.reset();
716             assertTrue(((Float JavaDoc) msg.readObject()).floatValue() == testFloat);
717             msg.clearBody();
718
719             double testDouble = 7.7d;
720             msg.writeDouble(testDouble);
721             msg.reset();
722             assertTrue(((Double JavaDoc) msg.readObject()).doubleValue() == testDouble);
723             msg.clearBody();
724
725             char testChar = 'z';
726             msg.writeChar(testChar);
727             msg.reset();
728             assertTrue(((Character JavaDoc) msg.readObject()).charValue() == testChar);
729             msg.clearBody();
730
731             byte[] data = new byte[50];
732             for (int i = 0; i < data.length; i++) {
733                 data[i] = (byte) i;
734             }
735             msg.writeBytes(data);
736             msg.reset();
737             byte[] valid = (byte[]) msg.readObject();
738             assertTrue(valid.length == data.length);
739             for (int i = 0; i < valid.length; i++) {
740                 assertTrue(valid[i] == data[i]);
741             }
742             msg.clearBody();
743             msg.writeBoolean(true);
744             msg.reset();
745             assertTrue(((Boolean JavaDoc) msg.readObject()).booleanValue());
746
747
748         } catch (JMSException JavaDoc jmsEx) {
749             jmsEx.printStackTrace();
750             assertTrue(false);
751         }
752     }
753
754     public void testClearBody() throws JMSException JavaDoc {
755         ActiveMQStreamMessage streamMessage = new ActiveMQStreamMessage();
756         try {
757             streamMessage.writeObject(new Serializable JavaDoc() {
758                 private static final long serialVersionUID = -5181896809607968727L;
759             });
760             streamMessage.clearBody();
761             assertFalse(streamMessage.isReadOnlyBody());
762             streamMessage.writeObject(new Serializable JavaDoc() {
763                 private static final long serialVersionUID = 5074177640797561141L;
764             });
765             streamMessage.readObject();
766             fail("should throw exception");
767         } catch (MessageNotReadableException JavaDoc mnwe) {
768         } catch (MessageNotWriteableException JavaDoc mnwe) {
769             fail("should be writeable");
770         }
771     }
772
773     public void testReset() throws JMSException JavaDoc {
774         ActiveMQStreamMessage streamMessage = new ActiveMQStreamMessage();
775         try {
776             streamMessage.writeDouble(24.5);
777             streamMessage.writeLong(311);
778         } catch (MessageNotWriteableException JavaDoc mnwe) {
779             fail("should be writeable");
780         }
781         streamMessage.reset();
782         try {
783             assertTrue(streamMessage.isReadOnlyBody());
784             assertEquals(streamMessage.readDouble(), 24.5, 0);
785             assertEquals(streamMessage.readLong(), 311);
786         } catch (MessageNotReadableException JavaDoc mnre) {
787             fail("should be readable");
788         }
789         try {
790             streamMessage.writeInt(33);
791             fail("should throw exception");
792         } catch (MessageNotWriteableException JavaDoc mnwe) {
793         }
794     }
795
796     public void testReadOnlyBody() throws JMSException JavaDoc {
797         ActiveMQStreamMessage message = new ActiveMQStreamMessage();
798         try {
799             message.writeBoolean(true);
800             message.writeByte((byte) 1);
801             message.writeBytes(new byte[1]);
802             message.writeBytes(new byte[3], 0, 2);
803             message.writeChar('a');
804             message.writeDouble(1.5);
805             message.writeFloat((float) 1.5);
806             message.writeInt(1);
807             message.writeLong(1);
808             message.writeObject("stringobj");
809             message.writeShort((short) 1);
810             message.writeString("string");
811         } catch (MessageNotWriteableException JavaDoc mnwe) {
812             fail("Should be writeable");
813         }
814         message.reset();
815         try {
816             message.readBoolean();
817             message.readByte();
818             assertEquals(1, message.readBytes(new byte[10]));
819             assertEquals(-1, message.readBytes(new byte[10]));
820             assertEquals(2, message.readBytes(new byte[10]));
821             assertEquals(-1, message.readBytes(new byte[10]));
822             message.readChar();
823             message.readDouble();
824             message.readFloat();
825             message.readInt();
826             message.readLong();
827             message.readString();
828             message.readShort();
829             message.readString();
830         } catch (MessageNotReadableException JavaDoc mnwe) {
831             fail("Should be readable");
832         }
833         try {
834             message.writeBoolean(true);
835             fail("Should have thrown exception");
836         } catch (MessageNotWriteableException JavaDoc mnwe) {
837         }
838         try {
839             message.writeByte((byte) 1);
840             fail("Should have thrown exception");
841         } catch (MessageNotWriteableException JavaDoc mnwe) {
842         }
843         try {
844             message.writeBytes(new byte[1]);
845             fail("Should have thrown exception");
846         } catch (MessageNotWriteableException JavaDoc mnwe) {
847         }
848         try {
849             message.writeBytes(new byte[3], 0, 2);
850             fail("Should have thrown exception");
851         } catch (MessageNotWriteableException JavaDoc mnwe) {
852         }
853         try {
854             message.writeChar('a');
855             fail("Should have thrown exception");
856         } catch (MessageNotWriteableException JavaDoc mnwe) {
857         }
858         try {
859             message.writeDouble(1.5);
860             fail("Should have thrown exception");
861         } catch (MessageNotWriteableException JavaDoc mnwe) {
862         }
863         try {
864             message.writeFloat((float) 1.5);
865             fail("Should have thrown exception");
866         } catch (MessageNotWriteableException JavaDoc mnwe) {
867         }
868         try {
869             message.writeInt(1);
870             fail("Should have thrown exception");
871         } catch (MessageNotWriteableException JavaDoc mnwe) {
872         }
873         try {
874             message.writeLong(1);
875             fail("Should have thrown exception");
876         } catch (MessageNotWriteableException JavaDoc mnwe) {
877         }
878         try {
879             message.writeObject("stringobj");
880             fail("Should have thrown exception");
881         } catch (MessageNotWriteableException JavaDoc mnwe) {
882         }
883         try {
884             message.writeShort((short) 1);
885             fail("Should have thrown exception");
886         } catch (MessageNotWriteableException JavaDoc mnwe) {
887         }
888         try {
889             message.writeString("string");
890             fail("Should have thrown exception");
891         } catch (MessageNotWriteableException JavaDoc mnwe) {
892         }
893     }
894
895     public void testWriteOnlyBody() throws JMSException JavaDoc {
896         ActiveMQStreamMessage message = new ActiveMQStreamMessage();
897         message.clearBody();
898         try {
899             message.writeBoolean(true);
900             message.writeByte((byte) 1);
901             message.writeBytes(new byte[1]);
902             message.writeBytes(new byte[3], 0, 2);
903             message.writeChar('a');
904             message.writeDouble(1.5);
905             message.writeFloat((float) 1.5);
906             message.writeInt(1);
907             message.writeLong(1);
908             message.writeObject("stringobj");
909             message.writeShort((short) 1);
910             message.writeString("string");
911         } catch (MessageNotWriteableException JavaDoc mnwe) {
912             fail("Should be writeable");
913         }
914         try {
915             message.readBoolean();
916             fail("Should have thrown exception");
917         } catch (MessageNotReadableException JavaDoc mnwe) {
918         }
919         try {
920             message.readByte();
921             fail("Should have thrown exception");
922         } catch (MessageNotReadableException JavaDoc e) {
923         }
924         try {
925             message.readBytes(new byte[1]);
926             fail("Should have thrown exception");
927         } catch (MessageNotReadableException JavaDoc e) {
928         }
929         try {
930             message.readBytes(new byte[2]);
931             fail("Should have thrown exception");
932         } catch (MessageNotReadableException JavaDoc e) {
933         }
934         try {
935             message.readChar();
936             fail("Should have thrown exception");
937         } catch (MessageNotReadableException JavaDoc e) {
938         }
939         try {
940             message.readDouble();
941             fail("Should have thrown exception");
942         } catch (MessageNotReadableException JavaDoc e) {
943         }
944         try {
945             message.readFloat();
946             fail("Should have thrown exception");
947         } catch (MessageNotReadableException JavaDoc e) {
948         }
949         try {
950             message.readInt();
951             fail("Should have thrown exception");
952         } catch (MessageNotReadableException JavaDoc e) {
953         }
954         try {
955             message.readLong();
956             fail("Should have thrown exception");
957         } catch (MessageNotReadableException JavaDoc e) {
958         }
959         try {
960             message.readString();
961             fail("Should have thrown exception");
962         } catch (MessageNotReadableException JavaDoc e) {
963         }
964         try {
965             message.readShort();
966             fail("Should have thrown exception");
967         } catch (MessageNotReadableException JavaDoc e) {
968         }
969         try {
970             message.readString();
971             fail("Should have thrown exception");
972         } catch (MessageNotReadableException JavaDoc e) {
973         }
974     }
975
976
977 }
978
Popular Tags