1 18 package org.apache.activemq.command; 19 20 import java.io.IOException ; 21 import java.util.ArrayList ; 22 import java.util.Arrays ; 23 import java.util.Collections ; 24 import java.util.Enumeration ; 25 26 import javax.jms.JMSException ; 27 import javax.jms.MessageFormatException ; 28 import javax.jms.MessageNotReadableException ; 29 import javax.jms.MessageNotWriteableException ; 30 31 import org.apache.activemq.command.ActiveMQMapMessage; 32 33 import junit.framework.TestCase; 34 35 38 public class ActiveMQMapMessageTest extends TestCase { 39 private String name = "testName"; 40 41 public static void main(String [] args) { 42 junit.textui.TestRunner.run(ActiveMQMapMessageTest.class); 43 } 44 45 48 protected void setUp() throws Exception { 49 super.setUp(); 50 } 51 52 55 protected void tearDown() throws Exception { 56 super.tearDown(); 57 } 58 59 64 public ActiveMQMapMessageTest(String arg0) { 65 super(arg0); 66 } 67 68 public void testBytesConversion() throws JMSException , IOException { 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 StringBuffer bigSB = new StringBuffer (1024*1024); 84 for( int i=0; i < 1024*1024; i++ ) { 85 bigSB.append((char)'a'+i%26); 86 } 87 String 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 { 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 { 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 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 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 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 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 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 jmsEx) { 194 jmsEx.printStackTrace(); 195 assertTrue(false); 196 } 197 } 198 199 public void testGetString() { 200 ActiveMQMapMessage msg = new ActiveMQMapMessage(); 201 try { 202 String str = "test"; 203 msg.setString(this.name, str); 204 msg = (ActiveMQMapMessage) msg.copy(); 205 assertEquals(msg.getString(this.name), str); 206 } catch (JMSException 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 jmsEx) { 224 jmsEx.printStackTrace(); 225 assertTrue(false); 226 } 227 } 228 229 public void testGetObject() throws JMSException { 230 ActiveMQMapMessage msg = new ActiveMQMapMessage(); 231 Boolean booleanValue = Boolean.TRUE; 232 Byte byteValue = Byte.valueOf("1"); 233 byte[] bytesValue = new byte[3]; 234 Character charValue = new Character ('a'); 235 Double doubleValue = Double.valueOf("1.5"); 236 Float floatValue = Float.valueOf("1.5"); 237 Integer intValue = Integer.valueOf("1"); 238 Long longValue = Long.valueOf("1"); 239 Short shortValue = Short.valueOf("1"); 240 String 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 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 ); 262 assertEquals(msg.getObject("boolean"), booleanValue); 263 assertEquals(msg.getBoolean("boolean"), booleanValue.booleanValue()); 264 assertTrue(msg.getObject("byte") instanceof Byte ); 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 ); 271 assertEquals(msg.getObject("char"), charValue); 272 assertEquals(msg.getChar("char"), charValue.charValue()); 273 assertTrue(msg.getObject("double") instanceof Double ); 274 assertEquals(msg.getObject("double"), doubleValue); 275 assertEquals(msg.getDouble("double"), doubleValue.doubleValue(), 0); 276 assertTrue(msg.getObject("float") instanceof Float ); 277 assertEquals(msg.getObject("float"), floatValue); 278 assertEquals(msg.getFloat("float"), floatValue.floatValue(), 0); 279 assertTrue(msg.getObject("int") instanceof Integer ); 280 assertEquals(msg.getObject("int"), intValue); 281 assertEquals(msg.getInt("int"), intValue.intValue()); 282 assertTrue(msg.getObject("long") instanceof Long ); 283 assertEquals(msg.getObject("long"), longValue); 284 assertEquals(msg.getLong("long"), longValue.longValue()); 285 assertTrue(msg.getObject("short") instanceof Short ); 286 assertEquals(msg.getObject("short"), shortValue); 287 assertEquals(msg.getShort("short"), shortValue.shortValue()); 288 assertTrue(msg.getObject("string") instanceof String ); 289 assertEquals(msg.getObject("string"), stringValue); 290 assertEquals(msg.getString("string"), stringValue); 291 292 msg.clearBody(); 293 try { 294 msg.setObject("object", new Object ()); 295 fail("should have thrown exception"); 296 } catch (MessageFormatException e) { 297 } 298 299 } 300 301 public void testGetMapNames() throws JMSException { 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 mapNamesEnum = msg.getMapNames(); 319 ArrayList 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 { 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 { 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 { 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 mnre) { 395 fail("should be readable"); 396 } 397 try { 398 msg.setBoolean("boolean", true); 399 fail("should throw exception"); 400 } catch (MessageNotWriteableException mnwe) { 401 } 402 try { 403 msg.setByte("byte", (byte) 1); 404 fail("should throw exception"); 405 } catch (MessageNotWriteableException mnwe) { 406 } 407 try { 408 msg.setBytes("bytes", new byte[1]); 409 fail("should throw exception"); 410 } catch (MessageNotWriteableException mnwe) { 411 } 412 try { 413 msg.setBytes("bytes2", new byte[3], 0, 2); 414 fail("should throw exception"); 415 } catch (MessageNotWriteableException mnwe) { 416 } 417 try { 418 msg.setChar("char", 'a'); 419 fail("should throw exception"); 420 } catch (MessageNotWriteableException mnwe) { 421 } 422 try { 423 msg.setDouble("double", 1.5); 424 fail("should throw exception"); 425 } catch (MessageNotWriteableException mnwe) { 426 } 427 try { 428 msg.setFloat("float", 1.5f); 429 fail("should throw exception"); 430 } catch (MessageNotWriteableException mnwe) { 431 } 432 try { 433 msg.setInt("int", 1); 434 fail("should throw exception"); 435 } catch (MessageNotWriteableException mnwe) { 436 } 437 try { 438 msg.setLong("long", 1); 439 fail("should throw exception"); 440 } catch (MessageNotWriteableException mnwe) { 441 } 442 try { 443 msg.setObject("object", "stringObj"); 444 fail("should throw exception"); 445 } catch (MessageNotWriteableException mnwe) { 446 } 447 try { 448 msg.setShort("short", (short) 1); 449 fail("should throw exception"); 450 } catch (MessageNotWriteableException mnwe) { 451 } 452 try { 453 msg.setString("string", "string"); 454 fail("should throw exception"); 455 } catch (MessageNotWriteableException mnwe) { 456 } 457 } 458 459 public void testWriteOnlyBody() throws JMSException { 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 |