1 /***
2 * Redistribution and use of this software and associated documentation
3 * ("Software"), with or without modification, are permitted provided
4 * that the following conditions are met:
5 *
6 * 1. Redistributions of source code must retain copyright
7 * statements and notices. Redistributions must also contain a
8 * copy of this document.
9 *
10 * 2. Redistributions in binary form must reproduce the
11 * above copyright notice, this list of conditions and the
12 * following disclaimer in the documentation and/or other
13 * materials provided with the distribution.
14 *
15 * 3. The name "Exolab" must not be used to endorse or promote
16 * products derived from this Software without prior written
17 * permission of Exoffice Technologies. For written permission,
18 * please contact tma@netspace.net.au.
19 *
20 * 4. Products derived from this Software may not be called "Exolab"
21 * nor may "Exolab" appear in their names without prior written
22 * permission of Exoffice Technologies. Exolab is a registered
23 * trademark of Exoffice Technologies.
24 *
25 * 5. Due credit should be given to the Exolab Project
26 * (http://www.exolab.org/).
27 *
28 * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
32 * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39 * OF THE POSSIBILITY OF SUCH DAMAGE.
40 *
41 * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42 *
43 * $Id: MapMessageTest.java,v 1.6 2004/02/03 07:31:03 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.message.map;
46
47 import java.util.Arrays;
48 import java.util.Enumeration;
49 import java.util.HashMap;
50 import java.util.Iterator;
51 import java.util.LinkedList;
52 import java.util.Map;
53
54 import javax.jms.MapMessage;
55 import javax.jms.MessageFormatException;
56
57 import junit.framework.Test;
58
59 import org.exolab.jmscts.core.AbstractMessageTestCase;
60 import org.exolab.jmscts.core.ClassHelper;
61 import org.exolab.jmscts.core.MessagePopulator;
62 import org.exolab.jmscts.core.TestContext;
63 import org.exolab.jmscts.core.TestCreator;
64 import org.exolab.jmscts.test.message.util.Conversions;
65 import org.exolab.jmscts.test.message.util.MessageValues;
66
67
68 /***
69 * This class tests the <code>MapMessage</code> message type.
70 *
71 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
72 * @version $Revision: 1.6 $
73 * @see javax.jms.MapMessage
74 * @see AbstractMessageTestCase
75 * @see org.exolab.jmscts.core.MessageTestRunner
76 * @jmscts.message MapMessage
77 */
78 public class MapMessageTest extends AbstractMessageTestCase
79 implements MessageValues {
80
81 /***
82 * Values to test conversions against. String conversions are handled
83 * separately, by the {@link #STRING_CONVERSION_VALUES} attribute
84 */
85 private static final Object[][] CONVERSION_VALUES = {
86 BOOLEANS, BYTES, SHORTS, CHARS, INTS, LONGS, FLOATS, DOUBLES,
87 BYTE_ARRAYS};
88
89 /***
90 * Float values to test string conversions against. String conversions
91 * don't have to support NaN, or +-Infinity, hence the reason they are not
92 * included here
93 */
94 private static final Float[] FLOAT_CONVERSION_VALUES = {
95 new Float(Float.MIN_VALUE), new Float(Float.MAX_VALUE)};
96
97 /***
98 * Double values to test string conversions against. String conversions
99 * don't have to support NaN, or +-Infinity, hence the reason they are not
100 * included here
101 */
102 private static final Double[] DOUBLE_CONVERSION_VALUES = {
103 new Double(Double.MIN_VALUE), new Double(Double.MAX_VALUE)};
104
105 /***
106 * Values to test string conversions against
107 */
108 private static final Object[][] STRING_CONVERSION_VALUES = {
109 BOOLEANS, BYTES, SHORTS, INTS, LONGS, FLOAT_CONVERSION_VALUES,
110 DOUBLE_CONVERSION_VALUES, STRINGS};
111
112
113 /***
114 * Construct a new <code>MapMessageTest</code>
115 *
116 * @param name the name of test case
117 */
118 public MapMessageTest(String name) {
119 super(name);
120 }
121
122 /***
123 * Sets up the test suite
124 *
125 * @return an instance of this class that may be run by
126 * {@link org.exolab.jmscts.core.JMSTestRunner}
127 */
128 public static Test suite() {
129 return TestCreator.createMessageTest(MapMessageTest.class);
130 }
131
132 /***
133 * Get the message populator. This implementation always returns null
134 *
135 * @return null
136 */
137 public MessagePopulator getMessagePopulator() {
138 return null;
139 }
140
141 /***
142 * Verifies valid conversions for all types except String
143 * (this is handled by testStringConversion()).
144 *
145 * @jmscts.requirement message.map.conversion
146 * @throws Exception for any error
147 */
148 public void testConversion() throws Exception {
149 final String prefix = "testConversion";
150 TestContext context = getContext();
151 MapMessage message = (MapMessage) context.getMessage();
152
153 int id = 0;
154 for (int i = 0; i < CONVERSION_VALUES.length; ++i) {
155 for (int j = 0; j < CONVERSION_VALUES[i].length; ++j, ++id) {
156 Object value = CONVERSION_VALUES[i][j];
157 set(message, value, prefix + id);
158 Class type = value.getClass();
159 Class[] valid = Conversions.getValidConversions(type);
160 for (int k = 0; k < valid.length; ++k) {
161 Object result = get(message, valid[k], prefix + id);
162 Object converted = Conversions.convert(value, valid[k]);
163 if (converted instanceof byte[]) {
164 // byte arrays cannot be converted
165 if (!Arrays.equals((byte[]) result,
166 (byte[]) converted)) {
167 fail("Result byte array different to that set");
168 }
169 } else if (!converted.equals(result)) {
170 fail("Conversion of type="
171 + ClassHelper.getPrimitiveName(type) + " to type="
172 + ClassHelper.getPrimitiveName(valid[k])
173 + " failed. Expected value=" + converted
174 + ", but got value=" + result);
175 }
176 }
177 }
178 }
179 }
180
181 /***
182 * Verifies valid string conversions
183 *
184 * @jmscts.requirement message.map.conversion
185 * @throws Exception for any error
186 */
187 public void testStringConversion() throws Exception {
188 final String prefix = "testStringConversion";
189 TestContext context = getContext();
190 MapMessage message = (MapMessage) context.getMessage();
191
192 int id = 0;
193 for (int i = 0; i < STRING_CONVERSION_VALUES.length; ++i) {
194 for (int j = 0; j < STRING_CONVERSION_VALUES[i].length; ++j) {
195 Object value = STRING_CONVERSION_VALUES[i][j];
196 set(message, value.toString(), prefix + id);
197 Class type = value.getClass();
198 Object result = get(message, type, prefix + id);
199 if (!value.equals(result)) {
200 fail("Conversion of type=String to type="
201 + ClassHelper.getPrimitiveName(type)
202 + " failed. Expected value=" + value
203 + ", but got value=" + result);
204 }
205 ++id;
206 }
207 }
208 }
209
210 /***
211 * Verifies that attempting to perform invalid invalid conversions
212 * throws MessageFormatException.
213 *
214 * @jmscts.requirement message.map.conversion
215 * @throws Exception for any error
216 */
217 public void testInvalidConversion() throws Exception {
218 final String prefix = "testInvalidConversion";
219 TestContext context = getContext();
220 MapMessage message = (MapMessage) context.getMessage();
221
222 int id = 0;
223 for (int i = 0; i < ALL_VALUES.length; ++i) {
224 for (int j = 0; j < ALL_VALUES[i].length; ++j) {
225 Object value = ALL_VALUES[i][j];
226 set(message, value, prefix + id);
227 Class type = value.getClass();
228 Class[] invalid = Conversions.getInvalidConversions(type);
229 for (int k = 0; k < invalid.length; ++k) {
230 try {
231 get(message, invalid[k], prefix + id);
232 fail("Expected MessageFormatException to be thrown "
233 + "when getting type="
234 + ClassHelper.getPrimitiveName(invalid[k])
235 + " for type="
236 + ClassHelper.getPrimitiveName(type));
237 } catch (MessageFormatException expected) {
238 // the expected behaviour
239 } catch (Exception exception) {
240 fail("Expected MessageFormatException to be thrown "
241 + "when getting type="
242 + ClassHelper.getPrimitiveName(invalid[k])
243 + " for type="
244 + ClassHelper.getPrimitiveName(type)
245 + ", but got exception="
246 + exception.getClass().getName() + ", message="
247 + exception.getMessage());
248 }
249 }
250 Object result = get(message, type, prefix + id);
251 compare(value, result);
252 ++id;
253 }
254 }
255 }
256
257 /***
258 * Verifies that attempting to perform invalid invalid string to numeric
259 * conversions throws NumberFormatException
260 *
261 * @jmscts.requirement message.map.conversion
262 * @throws Exception for any error
263 */
264 public void testInvalidNumericConversion() throws Exception {
265 final String prefix = "testInvalidNumericConversion";
266 TestContext context = getContext();
267 MapMessage message = (MapMessage) context.getMessage();
268 Class[] numerics = {Byte.class, Short.class, Integer.class, Long.class,
269 Float.class, Double.class};
270 String[] invalidNos = {"a", "0x00", "NaN", "-Infinity", "+Infinity"};
271 int id = 0;
272 for (int i = 0; i < invalidNos.length; ++i) {
273 String value = invalidNos[i];
274 set(message, value, prefix + id);
275 for (int j = 0; j < numerics.length; ++j) {
276 try {
277 get(message, numerics[j], prefix + id);
278 fail("Expected NumberFormatException to be thrown when "
279 + "getting value=" + value + " as type="
280 + ClassHelper.getPrimitiveName(numerics[j]));
281 } catch (NumberFormatException expected) {
282 // the expected behaviour
283 } catch (Exception exception) {
284 fail("Expected NumberFormatException to be thrown when "
285 + "getting value=" + value + " as type="
286 + ClassHelper.getPrimitiveName(numerics[j])
287 + ", but got exception="
288 + exception.getClass().getName() + ", message="
289 + exception.getMessage());
290 }
291 }
292 ++id;
293 }
294 }
295
296 /***
297 * Verifies behaviour of conversions from null
298 *
299 * @jmscts.requirement message.map.null
300 * @throws Exception for any error
301 */
302 public void testNullConversion() throws Exception {
303 final String key = "testNullConversion";
304 TestContext context = getContext();
305 MapMessage message = (MapMessage) context.getMessage();
306
307 try {
308 set(message, null, key);
309 } catch (Exception exception) {
310 fail("Failed to set a null object value to MapMessage, "
311 + "exception=" + exception.getClass().getName()
312 + ", message=" + exception.getMessage());
313 }
314 Object result = getNull(message, Boolean.class, key, null);
315 if (!result.equals(Boolean.FALSE)) {
316 fail("Expected MapMessage.getBoolean() to return false for a "
317 + "null value");
318 }
319
320 result = getNull(message, String.class, key, null);
321 if (result != null) {
322 fail("Expected MapMessage.getString() to return null for a "
323 + "null value");
324 }
325
326 result = getNull(message, byte[].class, key, null);
327 if (result != null) {
328 fail("Expected MapMessage.getBytes() to return null for a "
329 + "null value");
330 }
331
332 getNull(message, Byte.class, key, NumberFormatException.class);
333 getNull(message, Short.class, key, NumberFormatException.class);
334 getNull(message, Character.class, key, NullPointerException.class);
335 getNull(message, Integer.class, key, NumberFormatException.class);
336 getNull(message, Long.class, key, NumberFormatException.class);
337 getNull(message, Float.class, key, NullPointerException.class);
338 getNull(message, Double.class, key, NullPointerException.class);
339 }
340
341 /***
342 * Verifies that setObject() can handle all supported types
343 *
344 * @jmscts.requirement message.map.method.setObject
345 * @throws Exception for any error
346 */
347 public void testSetObject() throws Exception {
348 final String prefix = "testSetObject";
349 TestContext context = getContext();
350 MapMessage message = (MapMessage) context.getMessage();
351
352 int id = 0;
353 for (int i = 0; i < ALL_VALUES.length; ++i) {
354 for (int j = 0; j < ALL_VALUES[i].length; ++j) {
355 message.setObject(prefix + id, ALL_VALUES[i][j]);
356 ++id;
357 }
358 }
359 id = 0;
360 for (int i = 0; i < ALL_VALUES.length; ++i) {
361 for (int j = 0; j < ALL_VALUES[i].length; ++j) {
362 Object value = message.getObject(prefix + id);
363 if (ALL_VALUES[i][j] instanceof byte[]) {
364 byte[] source = (byte[]) ALL_VALUES[i][j];
365 byte[] result = (byte[]) value;
366 assertTrue(Arrays.equals(source, result));
367 } else {
368 assertTrue(ALL_VALUES[i][j].equals(value));
369 }
370 ++id;
371 }
372 }
373 }
374
375 /***
376 * Verifies that attempting to set an invalid object using the setObject()
377 * method throws MessageFormatException
378 *
379 * @jmscts.requirement message.map.method.setObject
380 * @throws Exception for any error
381 */
382 public void testInvalidObject() throws Exception {
383 TestContext context = getContext();
384 MapMessage message = (MapMessage) context.getMessage();
385
386 try {
387 message.setObject("testInvalidObject",
388 new java.math.BigDecimal(0.0));
389 fail("MapMessage.setObject() should only support "
390 + "objectified primitives");
391 } catch (MessageFormatException expected) {
392 // the expected behaviour
393 }
394 }
395
396 /***
397 * Verifies that elements populated in a map are identical to those
398 * retrieved
399 *
400 * @jmscts.requirement message.map
401 * @throws Exception for any error
402 */
403 public void testMap() throws Exception {
404 final String prefix = "testMap";
405 TestContext context = getContext();
406 MapMessage message = (MapMessage) context.getMessage();
407
408 HashMap elements = populate(message);
409
410 Iterator iter = elements.entrySet().iterator();
411 while (iter.hasNext()) {
412 Map.Entry element = (Map.Entry) iter.next();
413 String key = (String) element.getKey();
414 Object value = element.getValue();
415
416 // get the value using the primitive methods
417 Object result = get(message, value.getClass(), key);
418 compare(value, result);
419
420 // get the value using the object method
421 result = message.getObject(key);
422 compare(value, result);
423 }
424 }
425
426 /***
427 * Verifies that getMapNames() returns a name for each element in the set
428 *
429 * @jmscts.requirement message.map.method.getMapNames
430 * @throws Exception for any error
431 */
432 public void testGetMapNames() throws Exception {
433 TestContext context = getContext();
434 MapMessage message = (MapMessage) context.getMessage();
435
436 checkEmpty(message); // ensure that no names exist prior to population
437 HashMap elements = populate(message);
438
439 LinkedList names = new LinkedList();
440 Enumeration enum = message.getMapNames();
441 while (enum.hasMoreElements()) {
442 names.add(enum.nextElement());
443 }
444
445 if (elements.size() != names.size()) {
446 fail("getMapNames() returned a different number of names to that"
447 + " expected. Expected count=" + elements.size()
448 + " but got count=" + names.size());
449 }
450 if (!names.containsAll(elements.keySet())) {
451 fail("getMapNames() returned a different set of names to that "
452 + "expected");
453 }
454
455 // clear the message, and verify that getMapNames() returns no names
456 message.clearBody();
457 checkEmpty(message);
458 }
459
460 /***
461 * Verifies the behaviour of the itemExists() method
462 *
463 * @jmscts.requirement message.map.method.itemExists
464 * @throws Exception for any error
465 */
466 public void testItemExists() throws Exception {
467 TestContext context = getContext();
468 MapMessage message = (MapMessage) context.getMessage();
469
470 HashMap elements = populate(message);
471
472 Iterator iter = elements.keySet().iterator();
473 while (iter.hasNext()) {
474 String name = (String) iter.next();
475 if (!message.itemExists(name)) {
476 fail("itemExists() returned false for an existing element");
477 }
478 }
479
480 if (message.itemExists("bad_item_name")) {
481 fail("itemExists() returned true for a non-existent element");
482 }
483 }
484
485 /***
486 * Verifies that getting a MapMessage field for a field name that has not
487 * been set is handled as if the field exists with a null value.
488 *
489 * @jmscts.requirement message.map.unset
490 * @throws Exception for any error
491 */
492 public void testUnset() throws Exception {
493 final String key = "testUnset";
494 TestContext context = getContext();
495 MapMessage message = (MapMessage) context.getMessage();
496
497 Object result = getNull(message, Boolean.class, key, null);
498 assertEquals(Boolean.FALSE, result);
499
500 result = getNull(message, String.class, key, null);
501 assertEquals(null, result);
502
503 result = getNull(message, byte[].class, key, null);
504 assertEquals(null, result);
505
506 getNull(message, Byte.class, key, NumberFormatException.class);
507 getNull(message, Short.class, key, NumberFormatException.class);
508 getNull(message, Character.class, key, NullPointerException.class);
509 getNull(message, Integer.class, key, NumberFormatException.class);
510 getNull(message, Long.class, key, NumberFormatException.class);
511 getNull(message, Float.class, key, NullPointerException.class);
512 getNull(message, Double.class, key, NullPointerException.class);
513 }
514
515 /***
516 * Verifies that field names are case sensitive
517 * NOTE: this is not explicitly mentioned in the specification, but is
518 * in keeping with property name requirements
519 *
520 * @jmscts.requirement message.map.case
521 * @throws Exception for any error
522 */
523 public void testCase() throws Exception {
524 final String upperKey = "ABC";
525 final String lowerKey = upperKey.toLowerCase();
526 TestContext context = getContext();
527 MapMessage message = (MapMessage) context.getMessage();
528
529 // check case sensitivity across like types
530 for (int i = 0; i < ALL_VALUES.length; ++i) {
531 if (ALL_VALUES[i].length < 2) {
532 throw new Exception("Each entry in ALL_VALUES must have at "
533 + "least 2 distinct values");
534 }
535 Object value1 = ALL_VALUES[i][0];
536 Object value2 = ALL_VALUES[i][1];
537 set(message, value1, upperKey);
538 set(message, value2, lowerKey);
539
540 compare(value1, get(message, value1.getClass(), upperKey));
541 compare(value2, get(message, value2.getClass(), lowerKey));
542 }
543
544 // check case sensitivity across different types
545 message.clearBody();
546 String value1 = STRINGS[0];
547 Integer value2 = INTS[0];
548 message.setString(upperKey, value1);
549 message.setInt(lowerKey, value2.intValue());
550 compare(value1, get(message, value1.getClass(), upperKey));
551 compare(value2, get(message, value2.getClass(), lowerKey));
552 }
553
554 /***
555 * Verifies that a getMapNames() returns an empty Enumeration for an
556 * empty message
557 *
558 * @param message the message to check
559 * @throws Exception for any error
560 */
561 private void checkEmpty(MapMessage message) throws Exception {
562 Enumeration enum = message.getMapNames();
563 int count = 0;
564 while (enum.hasMoreElements()) {
565 count++;
566 }
567 if (count != 0) {
568 fail("Expected getMapNames to return no elements for an empty "
569 + "MapMessage, but returned=" + count + " names");
570 }
571 }
572
573 /***
574 * Compares two objects, failing if they are not the same
575 *
576 * @param expected the expected value
577 * @param result the actual value
578 */
579 private void compare(Object expected, Object result) {
580 if (expected instanceof byte[]) {
581 if (!Arrays.equals((byte[]) expected, (byte[]) result)) {
582 fail("Byte array returned by MapMessage does not "
583 + "match that expected");
584 }
585 } else if (!expected.equals(result)) {
586 fail("Value returned by MapMessage does not match that "
587 + "expected. Expected value=" + expected + ", but got value="
588 + result);
589 }
590 }
591
592 /***
593 * Populates a message with values
594 *
595 * @param message the message to populate
596 * @return a map of the populated values, keyed on their names
597 * @throws Exception for any error
598 */
599 private HashMap populate(MapMessage message) throws Exception {
600 final String prefix = "element";
601 HashMap result = new HashMap();
602 int id = 0;
603 // populate using primitive methods
604 for (int i = 0; i < ALL_VALUES.length; ++i) {
605 for (int j = 0; j < ALL_VALUES[i].length; ++j) {
606 String name = prefix + id;
607 set(message, ALL_VALUES[i][j], name);
608 result.put(name, ALL_VALUES[i][j]);
609 id++;
610 }
611 }
612
613 // populate using setObject
614 for (int i = 0; i < ALL_VALUES.length; ++i) {
615 for (int j = 0; j < ALL_VALUES[i].length; ++j) {
616 String name = prefix + id;
617 message.setObject(name, ALL_VALUES[i][j]);
618 result.put(name, ALL_VALUES[i][j]);
619 id++;
620 }
621 }
622
623 // populate using alternative setBytes
624 for (int i = 0; i < BYTE_ARRAYS.length; ++i) {
625 String name = prefix + id;
626 message.setBytes(name, BYTE_ARRAYS[i], 0, BYTE_ARRAYS[i].length);
627 result.put(name, BYTE_ARRAYS[i]);
628 id++;
629 }
630 return result;
631 }
632
633 /***
634 * Helper to set a message property, invoking the appropriate set() method
635 * based on the type of the property
636 *
637 * @param message the message to populate
638 * @param value the property value to set
639 * @param key the name of the property
640 * @throws Exception for any error
641 */
642 private void set(MapMessage message, Object value, String key)
643 throws Exception {
644 if (value instanceof Boolean) {
645 message.setBoolean(key, ((Boolean) value).booleanValue());
646 } else if (value instanceof Byte) {
647 message.setByte(key, ((Byte) value).byteValue());
648 } else if (value instanceof Short) {
649 message.setShort(key, ((Short) value).shortValue());
650 } else if (value instanceof Character) {
651 message.setChar(key, ((Character) value).charValue());
652 } else if (value instanceof Integer) {
653 message.setInt(key, ((Integer) value).intValue());
654 } else if (value instanceof Long) {
655 message.setLong(key, ((Long) value).longValue());
656 } else if (value instanceof Float) {
657 message.setFloat(key, ((Float) value).floatValue());
658 } else if (value instanceof Double) {
659 message.setDouble(key, ((Double) value).doubleValue());
660 } else if (value instanceof String) {
661 message.setString(key, (String) value);
662 } else if (value instanceof byte[]) {
663 message.setBytes(key, (byte[]) value);
664 } else {
665 // let the message deal with the exception
666 message.setObject(key, value);
667 }
668 }
669
670 /***
671 * Helper to get a message property, invoking the appropriate get() method
672 * based on the type of the property
673 *
674 * @param message the message
675 * @param type the type of the property
676 * @param key the name of the property
677 * @return the value of the property
678 * @throws Exception for any error
679 */
680 private Object get(MapMessage message, Class type, String key)
681 throws Exception {
682 Object result = null;
683 if (type.equals(Boolean.class)) {
684 result = new Boolean(message.getBoolean(key));
685 } else if (type.equals(Byte.class)) {
686 result = new Byte(message.getByte(key));
687 } else if (type.equals(Short.class)) {
688 result = new Short(message.getShort(key));
689 } else if (type.equals(Character.class)) {
690 result = new Character(message.getChar(key));
691 } else if (type.equals(Integer.class)) {
692 result = new Integer(message.getInt(key));
693 } else if (type.equals(Long.class)) {
694 result = new Long(message.getLong(key));
695 } else if (type.equals(Float.class)) {
696 result = new Float(message.getFloat(key));
697 } else if (type.equals(Double.class)) {
698 result = new Double(message.getDouble(key));
699 } else if (type.equals(String.class)) {
700 result = message.getString(key);
701 } else if (type.equals(byte[].class)) {
702 result = message.getBytes(key);
703 }
704 return result;
705 }
706
707 /***
708 * Helper to attempt to convert a null property
709 *
710 * @param message the message
711 * @param type the type of the property
712 * @param key the name of the property
713 * @param exceptionType if non-null, indicates the expected exception to
714 * be thrown by the conversion
715 * @return the value of the property
716 * @throws Exception for any error
717 */
718 private Object getNull(MapMessage message, Class type, String key,
719 Class exceptionType) throws Exception {
720 Object result = null;
721 try {
722 result = get(message, type, key);
723 if (exceptionType != null) {
724 fail("Expected exception, type=" + exceptionType.getName()
725 + " to be thrown when getting null as type="
726 + ClassHelper.getPrimitiveName(type));
727 }
728 } catch (Exception exception) {
729 if (exceptionType == null) {
730 fail("Did not expect exception to be thrown when getting "
731 + "null as type=" + ClassHelper.getPrimitiveName(type)
732 + " but got exception=" + exception.getClass().getName()
733 + ", message=" + exception.getMessage());
734 } else if (!exceptionType.isAssignableFrom(exception.getClass())) {
735 fail("Expected exception, type=" + exceptionType.getName()
736 + " to be thrown when getting null as type="
737 + ClassHelper.getPrimitiveName(type) + ", but got "
738 + "exception=" + exception.getClass().getName()
739 + ", message=" + exception.getMessage());
740 }
741 }
742 return result;
743 }
744
745 }
This page was automatically generated by Maven