KickJava   Java API By Example, From Geeks To Geeks.

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


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.IOException JavaDoc;
21 import java.util.ArrayList JavaDoc;
22 import java.util.Arrays JavaDoc;
23 import java.util.Collections JavaDoc;
24 import java.util.Enumeration JavaDoc;
25
26 import javax.jms.JMSException JavaDoc;
27 import javax.jms.MessageFormatException JavaDoc;
28 import javax.jms.MessageNotReadableException JavaDoc;
29 import javax.jms.MessageNotWriteableException JavaDoc;
30
31 import org.apache.activemq.command.ActiveMQMapMessage;
32
33 import junit.framework.TestCase;
34
35 /**
36  * @version $Revision$
37  */

38 public class ActiveMQMapMessageTest extends TestCase {
39     private String JavaDoc name = "testName";
40
41     public static void main(String JavaDoc[] args) {
42         junit.textui.TestRunner.run(ActiveMQMapMessageTest.class);
43     }
44
45     /*
46      * @see TestCase#setUp()
47      */

48     protected void setUp() throws Exception JavaDoc {
49         super.setUp();
50     }
51
52     /*
53      * @see TestCase#tearDown()
54      */

55     protected void tearDown() throws Exception JavaDoc {
56         super.tearDown();
57     }
58
59     /**
60      * Constructor for ActiveMQMapMessageTest.
61      *
62      * @param arg0
63      */

64     public ActiveMQMapMessageTest(String JavaDoc arg0) {
65         super(arg0);
66     }
67
68     public void testBytesConversion() throws JMSException JavaDoc, IOException JavaDoc {
69         ActiveMQMapMessage msg = new ActiveMQMapMessage();
70         msg.setBoolean("boolean", true);
71         msg.setByte("byte", (byte) 1);
72         msg.setBytes("bytes", new byte[1]);
73         msg.setChar("char", 'a');
74         msg.setDouble("double", 1.5);
75         msg.setFloat("float", 1.5f);
76         msg.setInt("int", 1);
77         msg.setLong("long", 1);
78         msg.setObject("object", "stringObj");
79         msg.setShort("short", (short) 1);
80         msg.setString("string", "string");
81
82         // Test with a 1Meg String
83
StringBuffer JavaDoc bigSB = new StringBuffer JavaDoc(1024*1024);
84         for( int i=0; i < 1024*1024; i++ ) {
85             bigSB.append((char)'a'+i%26);
86         }
87         String JavaDoc bigString = bigSB.toString();
88         
89         msg.setString("bigString", bigString);
90         
91         msg = (ActiveMQMapMessage) msg.copy();
92         
93         assertEquals(msg.getBoolean("boolean"), true);
94         assertEquals(msg.getByte("byte"), (byte) 1);
95         assertEquals(msg.getBytes("bytes").length, 1);
96         assertEquals(msg.getChar("char"), 'a');
97         assertEquals(msg.getDouble("double"), 1.5, 0);
98         assertEquals(msg.getFloat("float"), 1.5f, 0);
99         assertEquals(msg.getInt("int"), 1);
100         assertEquals(msg.getLong("long"), 1);
101         assertEquals(msg.getObject("object"), "stringObj");
102         assertEquals(msg.getShort("short"), (short) 1);
103         assertEquals(msg.getString("string"), "string");
104         assertEquals(msg.getString("bigString"), bigString);
105     }
106
107     public void testGetBoolean() throws JMSException JavaDoc {
108         ActiveMQMapMessage msg = new ActiveMQMapMessage();
109         msg.setBoolean(name, true);
110         msg.setReadOnlyBody(true);
111         assertTrue(msg.getBoolean(name));
112         msg.clearBody();
113         msg.setString(name, "true");
114         
115         msg = (ActiveMQMapMessage) msg.copy();
116         
117         assertTrue(msg.getBoolean(name));
118     }
119
120     public void testGetByte() throws JMSException JavaDoc {
121         ActiveMQMapMessage msg = new ActiveMQMapMessage();
122         msg.setByte(this.name, (byte) 1);
123         msg = (ActiveMQMapMessage) msg.copy();
124         assertTrue(msg.getByte(this.name) == (byte) 1);
125     }
126
127     public void testGetShort() {
128         ActiveMQMapMessage msg = new ActiveMQMapMessage();
129         try {
130             msg.setShort(this.name, (short) 1);
131             msg = (ActiveMQMapMessage) msg.copy();
132             assertTrue(msg.getShort(this.name) == (short) 1);
133         } catch (JMSException JavaDoc jmsEx) {
134             jmsEx.printStackTrace();
135             assertTrue(false);
136         }
137     }
138
139     public void testGetChar() {
140         ActiveMQMapMessage msg = new ActiveMQMapMessage();
141         try {
142             msg.setChar(this.name, 'a');
143             msg = (ActiveMQMapMessage) msg.copy();
144             assertTrue(msg.getChar(this.name) == 'a');
145         } catch (JMSException JavaDoc jmsEx) {
146             jmsEx.printStackTrace();
147             assertTrue(false);
148         }
149     }
150
151     public void testGetInt() {
152         ActiveMQMapMessage msg = new ActiveMQMapMessage();
153         try {
154             msg.setInt(this.name, 1);
155             msg = (ActiveMQMapMessage) msg.copy();
156             assertTrue(msg.getInt(this.name) == 1);
157         } catch (JMSException JavaDoc jmsEx) {
158             jmsEx.printStackTrace();
159             assertTrue(false);
160         }
161     }
162
163     public void testGetLong() {
164         ActiveMQMapMessage msg = new ActiveMQMapMessage();
165         try {
166             msg.setLong(this.name, 1);
167             msg = (ActiveMQMapMessage) msg.copy();
168             assertTrue(msg.getLong(this.name) == 1);
169         } catch (JMSException JavaDoc jmsEx) {
170             jmsEx.printStackTrace();
171             assertTrue(false);
172         }
173     }
174
175     public void testGetFloat() {
176         ActiveMQMapMessage msg = new ActiveMQMapMessage();
177         try {
178             msg.setFloat(this.name, 1.5f);
179             msg = (ActiveMQMapMessage) msg.copy();
180             assertTrue(msg.getFloat(this.name) == 1.5f);
181         } catch (JMSException JavaDoc jmsEx) {
182             jmsEx.printStackTrace();
183             assertTrue(false);
184         }
185     }
186
187     public void testGetDouble() {
188         ActiveMQMapMessage msg = new ActiveMQMapMessage();
189         try {
190             msg.setDouble(this.name, 1.5);
191             msg = (ActiveMQMapMessage) msg.copy();
192             assertTrue(msg.getDouble(this.name) == 1.5);
193         } catch (JMSException JavaDoc jmsEx) {
194             jmsEx.printStackTrace();
195             assertTrue(false);
196         }
197     }
198
199     public void testGetString() {
200         ActiveMQMapMessage msg = new ActiveMQMapMessage();
201         try {
202             String JavaDoc str = "test";
203             msg.setString(this.name, str);
204             msg = (ActiveMQMapMessage) msg.copy();
205             assertEquals(msg.getString(this.name), str);
206         } catch (JMSException JavaDoc jmsEx) {
207             jmsEx.printStackTrace();
208             assertTrue(false);
209         }
210     }
211
212     public void testGetBytes() {
213         ActiveMQMapMessage msg = new ActiveMQMapMessage();
214         try {
215             byte[] bytes1 = new byte[3];
216             byte[] bytes2 = new byte[2];
217             System.arraycopy(bytes1, 0, bytes2, 0, 2);
218             msg.setBytes(this.name, bytes1);
219             msg.setBytes(this.name + "2", bytes1, 0, 2);
220             msg = (ActiveMQMapMessage) msg.copy();
221             assertTrue( Arrays.equals(msg.getBytes(this.name), bytes1));
222             assertEquals(msg.getBytes(this.name + "2").length, bytes2.length);
223         } catch (JMSException JavaDoc jmsEx) {
224             jmsEx.printStackTrace();
225             assertTrue(false);
226         }
227     }
228
229     public void testGetObject() throws JMSException JavaDoc {
230         ActiveMQMapMessage msg = new ActiveMQMapMessage();
231         Boolean JavaDoc booleanValue = Boolean.TRUE;
232         Byte JavaDoc byteValue = Byte.valueOf("1");
233         byte[] bytesValue = new byte[3];
234         Character JavaDoc charValue = new Character JavaDoc('a');
235         Double JavaDoc doubleValue = Double.valueOf("1.5");
236         Float JavaDoc floatValue = Float.valueOf("1.5");
237         Integer JavaDoc intValue = Integer.valueOf("1");
238         Long JavaDoc longValue = Long.valueOf("1");
239         Short JavaDoc shortValue = Short.valueOf("1");
240         String JavaDoc stringValue = "string";
241
242         try {
243             msg.setObject("boolean", booleanValue);
244             msg.setObject("byte", byteValue);
245             msg.setObject("bytes", bytesValue);
246             msg.setObject("char", charValue);
247             msg.setObject("double", doubleValue);
248             msg.setObject("float", floatValue);
249             msg.setObject("int", intValue);
250             msg.setObject("long", longValue);
251             msg.setObject("short", shortValue);
252             msg.setObject("string", stringValue);
253         } catch (MessageFormatException JavaDoc mfe) {
254             System.out.println("Caught: " + mfe);
255             mfe.printStackTrace();
256             fail("object formats should be correct");
257         }
258         
259         msg = (ActiveMQMapMessage) msg.copy();
260
261         assertTrue(msg.getObject("boolean") instanceof Boolean JavaDoc);
262         assertEquals(msg.getObject("boolean"), booleanValue);
263         assertEquals(msg.getBoolean("boolean"), booleanValue.booleanValue());
264         assertTrue(msg.getObject("byte") instanceof Byte JavaDoc);
265         assertEquals(msg.getObject("byte"), byteValue);
266         assertEquals(msg.getByte("byte"), byteValue.byteValue());
267         assertTrue(msg.getObject("bytes") instanceof byte[]);
268         assertEquals(((byte[]) msg.getObject("bytes")).length, bytesValue.length);
269         assertEquals(msg.getBytes("bytes").length, bytesValue.length);
270         assertTrue(msg.getObject("char") instanceof Character JavaDoc);
271         assertEquals(msg.getObject("char"), charValue);
272         assertEquals(msg.getChar("char"), charValue.charValue());
273         assertTrue(msg.getObject("double") instanceof Double JavaDoc);
274         assertEquals(msg.getObject("double"), doubleValue);
275         assertEquals(msg.getDouble("double"), doubleValue.doubleValue(), 0);
276         assertTrue(msg.getObject("float") instanceof Float JavaDoc);
277         assertEquals(msg.getObject("float"), floatValue);
278         assertEquals(msg.getFloat("float"), floatValue.floatValue(), 0);
279         assertTrue(msg.getObject("int") instanceof Integer JavaDoc);
280         assertEquals(msg.getObject("int"), intValue);
281         assertEquals(msg.getInt("int"), intValue.intValue());
282         assertTrue(msg.getObject("long") instanceof Long JavaDoc);
283         assertEquals(msg.getObject("long"), longValue);
284         assertEquals(msg.getLong("long"), longValue.longValue());
285         assertTrue(msg.getObject("short") instanceof Short JavaDoc);
286         assertEquals(msg.getObject("short"), shortValue);
287         assertEquals(msg.getShort("short"), shortValue.shortValue());
288         assertTrue(msg.getObject("string") instanceof String JavaDoc);
289         assertEquals(msg.getObject("string"), stringValue);
290         assertEquals(msg.getString("string"), stringValue);
291
292         msg.clearBody();
293         try {
294             msg.setObject("object", new Object JavaDoc());
295             fail("should have thrown exception");
296         } catch (MessageFormatException JavaDoc e) {
297         }
298
299     }
300
301     public void testGetMapNames() throws JMSException JavaDoc {
302         ActiveMQMapMessage msg = new ActiveMQMapMessage();
303         msg.setBoolean("boolean", true);
304         msg.setByte("byte", (byte) 1);
305         msg.setBytes("bytes1", new byte[1]);
306         msg.setBytes("bytes2", new byte[3], 0, 2);
307         msg.setChar("char", 'a');
308         msg.setDouble("double", 1.5);
309         msg.setFloat("float", 1.5f);
310         msg.setInt("int", 1);
311         msg.setLong("long", 1);
312         msg.setObject("object", "stringObj");
313         msg.setShort("short", (short) 1);
314         msg.setString("string", "string");
315
316         msg = (ActiveMQMapMessage) msg.copy();
317
318         Enumeration JavaDoc mapNamesEnum = msg.getMapNames();
319         ArrayList JavaDoc mapNamesList = Collections.list(mapNamesEnum);
320
321         assertEquals(mapNamesList.size(), 12);
322         assertTrue(mapNamesList.contains("boolean"));
323         assertTrue(mapNamesList.contains("byte"));
324         assertTrue(mapNamesList.contains("bytes1"));
325         assertTrue(mapNamesList.contains("bytes2"));
326         assertTrue(mapNamesList.contains("char"));
327         assertTrue(mapNamesList.contains("double"));
328         assertTrue(mapNamesList.contains("float"));
329         assertTrue(mapNamesList.contains("int"));
330         assertTrue(mapNamesList.contains("long"));
331         assertTrue(mapNamesList.contains("object"));
332         assertTrue(mapNamesList.contains("short"));
333         assertTrue(mapNamesList.contains("string"));
334     }
335
336     public void testItemExists() throws JMSException JavaDoc {
337         ActiveMQMapMessage mapMessage = new ActiveMQMapMessage();
338
339         mapMessage.setString("exists", "test");
340         
341         mapMessage = (ActiveMQMapMessage) mapMessage.copy();
342
343         assertTrue(mapMessage.itemExists("exists"));
344         assertFalse(mapMessage.itemExists("doesntExist"));
345     }
346
347     public void testClearBody() throws JMSException JavaDoc {
348         ActiveMQMapMessage mapMessage = new ActiveMQMapMessage();
349         mapMessage.setString("String", "String");
350         mapMessage.clearBody();
351         assertFalse(mapMessage.isReadOnlyBody());
352         
353         mapMessage.onSend();
354         mapMessage.setContent(mapMessage.getContent());
355         
356         assertNull(mapMessage.getString("String"));
357         mapMessage.clearBody();
358         mapMessage.setString("String", "String");
359         
360         mapMessage = (ActiveMQMapMessage) mapMessage.copy();
361
362         mapMessage.getString("String");
363     }
364
365     public void testReadOnlyBody() throws JMSException JavaDoc {
366         ActiveMQMapMessage msg = new ActiveMQMapMessage();
367         msg.setBoolean("boolean", true);
368         msg.setByte("byte", (byte) 1);
369         msg.setBytes("bytes", new byte[1]);
370         msg.setBytes("bytes2", new byte[3], 0, 2);
371         msg.setChar("char", 'a');
372         msg.setDouble("double", 1.5);
373         msg.setFloat("float", 1.5f);
374         msg.setInt("int", 1);
375         msg.setLong("long", 1);
376         msg.setObject("object", "stringObj");
377         msg.setShort("short", (short) 1);
378         msg.setString("string", "string");
379
380         msg.setReadOnlyBody(true);
381
382         try {
383             msg.getBoolean("boolean");
384             msg.getByte("byte");
385             msg.getBytes("bytes");
386             msg.getChar("char");
387             msg.getDouble("double");
388             msg.getFloat("float");
389             msg.getInt("int");
390             msg.getLong("long");
391             msg.getObject("object");
392             msg.getShort("short");
393             msg.getString("string");
394         } catch (MessageNotReadableException JavaDoc mnre) {
395             fail("should be readable");
396         }
397         try {
398             msg.setBoolean("boolean", true);
399             fail("should throw exception");
400         } catch (MessageNotWriteableException JavaDoc mnwe) {
401         }
402         try {
403             msg.setByte("byte", (byte) 1);
404             fail("should throw exception");
405         } catch (MessageNotWriteableException JavaDoc mnwe) {
406         }
407         try {
408             msg.setBytes("bytes", new byte[1]);
409             fail("should throw exception");
410         } catch (MessageNotWriteableException JavaDoc mnwe) {
411         }
412         try {
413             msg.setBytes("bytes2", new byte[3], 0, 2);
414             fail("should throw exception");
415         } catch (MessageNotWriteableException JavaDoc mnwe) {
416         }
417         try {
418             msg.setChar("char", 'a');
419             fail("should throw exception");
420         } catch (MessageNotWriteableException JavaDoc mnwe) {
421         }
422         try {
423             msg.setDouble("double", 1.5);
424             fail("should throw exception");
425         } catch (MessageNotWriteableException JavaDoc mnwe) {
426         }
427         try {
428             msg.setFloat("float", 1.5f);
429             fail("should throw exception");
430         } catch (MessageNotWriteableException JavaDoc mnwe) {
431         }
432         try {
433             msg.setInt("int", 1);
434             fail("should throw exception");
435         } catch (MessageNotWriteableException JavaDoc mnwe) {
436         }
437         try {
438             msg.setLong("long", 1);
439             fail("should throw exception");
440         } catch (MessageNotWriteableException JavaDoc mnwe) {
441         }
442         try {
443             msg.setObject("object", "stringObj");
444             fail("should throw exception");
445         } catch (MessageNotWriteableException JavaDoc mnwe) {
446         }
447         try {
448             msg.setShort("short", (short) 1);
449             fail("should throw exception");
450         } catch (MessageNotWriteableException JavaDoc mnwe) {
451         }
452         try {
453             msg.setString("string", "string");
454             fail("should throw exception");
455         } catch (MessageNotWriteableException JavaDoc mnwe) {
456         }
457     }
458
459     public void testWriteOnlyBody() throws JMSException JavaDoc {
460         ActiveMQMapMessage msg = new ActiveMQMapMessage();
461         msg.setReadOnlyBody(false);
462
463         msg.setBoolean("boolean", true);
464         msg.setByte("byte", (byte) 1);
465         msg.setBytes("bytes", new byte[1]);
466         msg.setBytes("bytes2", new byte[3], 0, 2);
467         msg.setChar("char", 'a');
468         msg.setDouble("double", 1.5);
469         msg.setFloat("float", 1.5f);
470         msg.setInt("int", 1);
471         msg.setLong("long", 1);
472         msg.setObject("object", "stringObj");
473         msg.setShort("short", (short) 1);
474         msg.setString("string", "string");
475
476         msg.setReadOnlyBody(true);
477         
478         msg.getBoolean("boolean");
479         msg.getByte("byte");
480         msg.getBytes("bytes");
481         msg.getChar("char");
482         msg.getDouble("double");
483         msg.getFloat("float");
484         msg.getInt("int");
485         msg.getLong("long");
486         msg.getObject("object");
487         msg.getShort("short");
488         msg.getString("string");
489     }
490
491 }
492
Popular Tags