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: PropertyConversionTest.java,v 1.5 2004/02/03 07:31:03 tanderson Exp $
44 */
45 package org.exolab.jmscts.test.message.properties;
46
47 import javax.jms.Message;
48 import javax.jms.MessageFormatException;
49
50 import junit.framework.Test;
51
52 import org.exolab.jmscts.core.AbstractMessageTestCase;
53 import org.exolab.jmscts.core.ClassHelper;
54 import org.exolab.jmscts.core.TestCreator;
55 import org.exolab.jmscts.test.message.util.PropertyValues;
56
57
58 /***
59 * This class tests message property conversion
60 *
61 * @author <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
62 * @version $Revision: 1.5 $
63 * @see AbstractMessageTestCase
64 */
65 public class PropertyConversionTest extends AbstractMessageTestCase
66 implements PropertyValues {
67
68 /***
69 * Invalid boolean conversions
70 */
71 private static final Class[] INVALID_BOOLEANS = {
72 byte.class, short.class, int.class, long.class, float.class,
73 double.class};
74
75 /***
76 * Invalid byte conversions
77 */
78 private static final Class[] INVALID_BYTES = {
79 boolean.class, float.class, double.class};
80
81 /***
82 * Invalid short conversions
83 */
84 private static final Class[] INVALID_SHORTS = {
85 boolean.class, byte.class, float.class, double.class};
86
87 /***
88 * Invalid int conversions
89 */
90 private static final Class[] INVALID_INTS = {
91 boolean.class, byte.class, short.class, float.class, double.class};
92
93 /***
94 * Invalid long conversions
95 */
96 private static final Class[] INVALID_LONGS = {
97 boolean.class, byte.class, short.class, int.class, float.class,
98 double.class};
99
100 /***
101 * Invalid float conversions
102 */
103 private static final Class[] INVALID_FLOATS = {
104 boolean.class, byte.class, short.class, int.class, long.class};
105
106 /***
107 * Invalid double conversions
108 */
109 private static final Class[] INVALID_DOUBLES = {
110 boolean.class, byte.class, short.class, int.class, long.class,
111 float.class};
112
113 // Note: for the primitive type A -> string -> primitive type B tests
114 // to work correctly, the range of values for numeric primitives must
115 // not overlap those of smaller numeric primitives eg. the set of
116 // values defined by INTS must exceed the range of the values defined
117 // by SHORTS and so on.
118
119
120 /***
121 * Construct a new <code>PropertyConversionTest</code>
122 *
123 * @param name the name of test case
124 */
125 public PropertyConversionTest(String name) {
126 super(name);
127 }
128
129 /***
130 * Sets up the test suite
131 *
132 * @return an instance of this class that may be run by
133 * {@link org.exolab.jmscts.core.JMSTestRunner}
134 */
135 public static Test suite() {
136 return TestCreator.createMessageTest(PropertyConversionTest.class);
137 }
138
139 /***
140 * Verifies boolean property conversion
141 *
142 * @jmscts.requirement properties.conversion
143 * @throws Exception for any error
144 */
145 public void testBoolean() throws Exception {
146 final String name = "boolean";
147
148 Message message = getContext().getMessage();
149
150 for (int i = 0; i < BOOLEANS.length; ++i) {
151 boolean value = BOOLEANS[i].booleanValue();
152 message.setObjectProperty(name, BOOLEANS[i]);
153 Boolean property = (Boolean) message.getObjectProperty(name);
154 assertEquals(value, property.booleanValue());
155
156 message.setBooleanProperty(name, value);
157
158 // check valid conversions
159 assertEquals(value, message.getBooleanProperty(name));
160 assertTrue(message.getObjectProperty(name) instanceof Boolean);
161 property = (Boolean) message.getObjectProperty(name);
162 assertEquals(value, property.booleanValue());
163
164 String str = message.getStringProperty(name);
165 assertEquals(value, Boolean.valueOf(str).booleanValue());
166
167 // check invalid conversions
168 for (int j = 0; j < INVALID_BOOLEANS.length; ++j) {
169 expectException(name, boolean.class, INVALID_BOOLEANS[j],
170 MessageFormatException.class);
171 }
172 }
173 }
174
175 /***
176 * Verifies byte property conversion
177 *
178 * @jmscts.requirement properties.conversion
179 * @throws Exception for any error
180 */
181 public void testByte() throws Exception {
182 final String name = "byte";
183
184 Message message = getContext().getMessage();
185
186 for (int i = 0; i < BYTES.length; ++i) {
187 byte value = BYTES[i].byteValue();
188 message.setObjectProperty(name, BYTES[i]);
189 assertEquals(
190 value, ((Byte) message.getObjectProperty(name)).byteValue());
191
192 message.setByteProperty(name, value);
193
194 // check valid conversions
195 assertEquals(value, message.getByteProperty(name));
196 assertEquals(value, message.getShortProperty(name));
197 assertEquals(value, message.getIntProperty(name));
198 assertEquals(value, message.getLongProperty(name));
199 assertTrue(message.getObjectProperty(name) instanceof Byte);
200 assertEquals(
201 value, ((Byte) message.getObjectProperty(name)).byteValue());
202
203 String str = message.getStringProperty(name);
204 assertEquals(value, Byte.parseByte(str));
205
206 // check invalid conversions
207 for (int j = 0; j < INVALID_BYTES.length; ++j) {
208 expectException(name, byte.class, INVALID_BYTES[j],
209 MessageFormatException.class);
210 }
211 }
212 }
213
214 /***
215 * Verifies short property conversion
216 *
217 * @jmscts.requirement properties.conversion
218 * @throws Exception for any error
219 */
220 public void testShort() throws Exception {
221 final String name = "short";
222
223 Message message = getContext().getMessage();
224
225 for (int i = 0; i < SHORTS.length; ++i) {
226 short value = SHORTS[i].shortValue();
227 message.setObjectProperty(name, SHORTS[i]);
228 assertEquals(
229 value, ((Short) message.getObjectProperty(name)).shortValue());
230
231 message.setShortProperty(name, value);
232
233 // check valid conversions
234 assertEquals(value, message.getShortProperty(name));
235 assertEquals(value, message.getIntProperty(name));
236 assertEquals(value, message.getLongProperty(name));
237
238 String str = message.getStringProperty(name);
239 assertEquals(value, Short.parseShort(str));
240
241 // check invalid conversions
242 for (int j = 0; j < INVALID_SHORTS.length; ++j) {
243 expectException(name, short.class, INVALID_SHORTS[j],
244 MessageFormatException.class);
245 }
246 }
247 }
248
249 /***
250 * Verifies int property conversion
251 *
252 * @jmscts.requirement properties.conversion
253 * @throws Exception for any error
254 */
255 public void testInt() throws Exception {
256 final String name = "int";
257
258 Message message = getContext().getMessage();
259
260 for (int i = 0; i < INTS.length; ++i) {
261 int value = INTS[i].intValue();
262 message.setObjectProperty(name, INTS[i]);
263 assertTrue(((Integer) message.getObjectProperty(name)).intValue()
264 == value);
265
266 message.setIntProperty(name, value);
267
268 // check valid conversions
269 assertEquals(value, message.getIntProperty(name));
270 assertEquals(value, message.getLongProperty(name));
271 assertTrue(message.getObjectProperty(name) instanceof Integer);
272 assertEquals(
273 value, ((Integer) message.getObjectProperty(name)).intValue());
274
275 String str = message.getStringProperty(name);
276 assertEquals(value, Integer.parseInt(str));
277
278 // check invalid conversions
279 for (int j = 0; j < INVALID_INTS.length; ++j) {
280 expectException(name, int.class, INVALID_INTS[j],
281 MessageFormatException.class);
282 }
283 }
284 }
285
286 /***
287 * Verifies long property conversion
288 *
289 * @jmscts.requirement properties.conversion
290 * @throws Exception for any error
291 */
292 public void testLong() throws Exception {
293 final String name = "long";
294
295 Message message = getContext().getMessage();
296
297 for (int i = 0; i < LONGS.length; ++i) {
298 long value = LONGS[i].longValue();
299 message.setObjectProperty(name, LONGS[i]);
300 assertEquals(
301 value, ((Long) message.getObjectProperty(name)).longValue());
302
303 message.setLongProperty(name, value);
304
305 // check valid conversions
306 assertEquals(value, message.getLongProperty(name));
307 assertTrue(message.getObjectProperty(name) instanceof Long);
308 assertEquals(
309 value, ((Long) message.getObjectProperty(name)).longValue());
310
311 String str = message.getStringProperty(name);
312 assertEquals(value, Long.parseLong(str));
313
314 // check invalid conversions
315 for (int j = 0; j < INVALID_LONGS.length; ++j) {
316 expectException(name, long.class, INVALID_LONGS[j],
317 MessageFormatException.class);
318 }
319 }
320 }
321
322 /***
323 * Verifies float property conversion
324 *
325 * @jmscts.requirement properties.conversion
326 * @throws Exception for any error
327 */
328 public void testFloat() throws Exception {
329 final String name = "float";
330
331 Message message = getContext().getMessage();
332
333 for (int i = 0; i < FLOATS.length; ++i) {
334 // check valid conversions
335 float value = FLOATS[i].floatValue();
336 message.setObjectProperty(name, FLOATS[i]);
337 checkFloat(name, value);
338
339 message.setFloatProperty(name, value);
340
341 assertTrue(message.getObjectProperty(name) instanceof Float);
342 checkFloat(name, value);
343
344 String str = message.getStringProperty(name);
345 if (str.equals(Float.toString(Float.NaN))) {
346 assertTrue(Float.isNaN(value));
347 } else if (str.equals(Float.toString(Float.NEGATIVE_INFINITY))) {
348 assertTrue(value == Float.NEGATIVE_INFINITY);
349 } else if (str.equals(Float.toString(Float.POSITIVE_INFINITY))) {
350 assertTrue(value == Float.POSITIVE_INFINITY);
351 } else {
352 assertTrue(Float.parseFloat(str) == FLOATS[i].floatValue());
353 }
354
355 // check invalid conversions
356 for (int j = 0; j < INVALID_FLOATS.length; ++j) {
357 expectException(name, float.class, INVALID_FLOATS[j],
358 MessageFormatException.class);
359 }
360 }
361 }
362
363 /***
364 * Verifies double property conversion
365 *
366 * @jmscts.requirement properties.conversion
367 * @throws Exception for any error
368 */
369 public void testDouble() throws Exception {
370 final String name = "double";
371
372 Message message = getContext().getMessage();
373
374 for (int i = 0; i < DOUBLES.length; ++i) {
375 // check valid conversions
376 double value = DOUBLES[i].doubleValue();
377 message.setObjectProperty(name, DOUBLES[i]);
378 checkDouble(name, value);
379
380 message.setDoubleProperty(name, value);
381
382 assertTrue(message.getObjectProperty(name) instanceof Double);
383 checkDouble(name, value);
384
385 String str = message.getStringProperty(name);
386 if (str.equals(Double.toString(Double.NaN))) {
387 assertTrue(Double.isNaN(value));
388 } else if (str.equals(Double.toString(Double.NEGATIVE_INFINITY))) {
389 assertTrue(value == Double.NEGATIVE_INFINITY);
390 } else if (str.equals(Double.toString(Double.POSITIVE_INFINITY))) {
391 assertTrue(value == Double.POSITIVE_INFINITY);
392 } else {
393 assertTrue(Double.parseDouble(str) == value);
394 }
395
396 // check invalid conversions
397 for (int j = 0; j < INVALID_DOUBLES.length; ++j) {
398 expectException(name, double.class, INVALID_DOUBLES[j],
399 MessageFormatException.class);
400 }
401 }
402 }
403
404 /***
405 * Verifies string <-> object property conversions
406 *
407 * @jmscts.requirement properties.conversion
408 * @throws Exception for any error
409 */
410 public void testStringObject() throws Exception {
411 final String name = "stringObject";
412
413 Message message = getContext().getMessage();
414
415 for (int i = 0; i < STRINGS.length; ++i) {
416 message.setObjectProperty(name, STRINGS[i]);
417 assertTrue(((String) message.getObjectProperty(name)).equals(
418 STRINGS[i]));
419
420 message.setStringProperty(name, STRINGS[i]);
421
422 assertEquals(STRINGS[i], message.getStringProperty(name));
423 assertTrue(message.getObjectProperty(name) instanceof String);
424 assertEquals(STRINGS[i], message.getObjectProperty(name));
425 }
426
427 // check null
428 message.setObjectProperty(name, null);
429 assertNull(message.getStringProperty(name));
430 }
431
432 /***
433 * Verifies string <-> boolean property conversions
434 *
435 * @jmscts.requirement properties.conversion
436 * @throws Exception for any error
437 */
438 public void testStringBoolean() throws Exception {
439 final String name = "stringBoolean";
440
441 Message message = getContext().getMessage();
442
443 // check valid string <-> boolean conversions
444 for (int i = 0; i < BOOLEANS.length; ++i) {
445 String value = BOOLEANS[i].toString();
446 message.setStringProperty(name, value);
447 assertEquals(value, message.getStringProperty(name));
448 assertEquals(BOOLEANS[i].booleanValue(),
449 message.getBooleanProperty(name));
450 }
451
452 // check invalid string <-> boolean conversions. The Boolean.valueOf()
453 // operation treats anything not 'true' (case is ignored) as being
454 // false, so no exception is expected.
455 Object[][] invalidBooleans = {BYTES, SHORTS, INTS, LONGS, FLOATS,
456 DOUBLES};
457 for (int i = 0; i < invalidBooleans.length; ++i) {
458 for (int j = 0; j < invalidBooleans[i].length; ++j) {
459 String value = invalidBooleans[i][j].toString();
460 message.setStringProperty(name, value);
461 assertFalse(message.getBooleanProperty(name));
462 }
463 }
464 }
465
466 /***
467 * Verifies string <-> byte property conversions
468 *
469 * @jmscts.requirement properties.conversion
470 * @throws Exception for any error
471 */
472 public void testStringByte() throws Exception {
473 final String name = "stringByte";
474
475 Message message = getContext().getMessage();
476
477 // check valid string <-> byte conversions
478 for (int i = 0; i < BYTES.length; ++i) {
479 String value = BYTES[i].toString();
480 message.setStringProperty(name, value);
481 assertEquals(value, message.getStringProperty(name));
482 assertEquals(BYTES[i].byteValue(), message.getByteProperty(name));
483 }
484
485 // check invalid string -> byte conversions
486 final Class[] invalidBytes = {boolean.class, short.class, int.class,
487 long.class, float.class, double.class};
488 expectConversionException(byte.class, invalidBytes,
489 NumberFormatException.class);
490 }
491
492 /***
493 * Verifies string <-> short property conversions
494 *
495 * @jmscts.requirement properties.conversion
496 * @throws Exception for any error
497 */
498 public void testStringShort() throws Exception {
499 final String name = "stringShort";
500
501 Message message = getContext().getMessage();
502
503 // check valid string <-> short conversions
504 for (int i = 0; i < SHORTS.length; ++i) {
505 String value = SHORTS[i].toString();
506 message.setStringProperty(name, value);
507 assertEquals(value, message.getStringProperty(name));
508 assertEquals(SHORTS[i].shortValue(),
509 message.getShortProperty(name));
510 }
511
512 // check invalid string -> short conversions
513 final Class[] invalidShorts = {boolean.class, int.class, long.class,
514 float.class, double.class};
515 expectConversionException(short.class, invalidShorts,
516 NumberFormatException.class);
517
518 }
519
520 /***
521 * Verifies string <-> int property conversions
522 *
523 * @jmscts.requirement properties.conversion
524 * @throws Exception for any error
525 */
526 public void testStringInt() throws Exception {
527 final String name = "stringInt";
528
529 Message message = getContext().getMessage();
530
531 // check valid string <-> int conversions
532 for (int i = 0; i < INTS.length; ++i) {
533 String value = INTS[i].toString();
534 message.setStringProperty(name, value);
535 assertEquals(value, message.getStringProperty(name));
536 assertEquals(INTS[i].intValue(), message.getIntProperty(name));
537 }
538
539 // check invalid string -> int conversions
540 final Class[] invalidInts = {boolean.class, long.class, float.class,
541 double.class};
542 expectConversionException(int.class, invalidInts,
543 NumberFormatException.class);
544 }
545
546 /***
547 * Verifies string <-> long property conversions
548 *
549 * @jmscts.requirement properties.conversion
550 * @throws Exception for any error
551 */
552 public void testStringLong() throws Exception {
553 final String name = "stringLong";
554
555 Message message = getContext().getMessage();
556
557 // check valid string <-> long conversions
558 for (int i = 0; i < LONGS.length; ++i) {
559 String value = LONGS[i].toString();
560 message.setStringProperty(name, value);
561 assertEquals(value, message.getStringProperty(name));
562 assertEquals(LONGS[i].longValue(), message.getLongProperty(name));
563 }
564
565 // check invalid string -> long conversions
566 final Class[] invalidLongs = {boolean.class, float.class,
567 double.class};
568 expectConversionException(long.class, invalidLongs,
569 NumberFormatException.class);
570 }
571
572 /***
573 * Verifies string <-> float property conversions
574 *
575 * @jmscts.requirement properties.conversion
576 * @throws Exception for any error
577 */
578 public void testStringFloat() throws Exception {
579 final String name = "stringFloat";
580
581 Message message = getContext().getMessage();
582
583 // check valid string <-> float conversions
584 for (int i = 0; i < FLOATS.length; ++i) {
585 String value = FLOATS[i].toString();
586 message.setStringProperty(name, value);
587 assertTrue(message.getStringProperty(name).equals(value));
588
589 if (value.equals(Float.toString(Float.NaN))) {
590 assertTrue(FLOATS[i].isNaN());
591 } else if (value.equals(Float.toString(Float.NEGATIVE_INFINITY))) {
592 assertTrue(FLOATS[i].floatValue() == Float.NEGATIVE_INFINITY);
593 } else if (value.equals(Float.toString(Float.POSITIVE_INFINITY))) {
594 assertTrue(FLOATS[i].floatValue() == Float.POSITIVE_INFINITY);
595 } else {
596 assertTrue(message.getFloatProperty(name)
597 == FLOATS[i].floatValue());
598 }
599 }
600
601 // check invalid string -> float conversions
602 final Class[] invalidFloats = {boolean.class};
603 expectConversionException(float.class, invalidFloats,
604 NumberFormatException.class);
605 // NOTE: the JDK 1.3 implementation of floats treats numbers that
606 // exceed the range of a float as being infinity, hence the following:
607 for (int i = 0; i < DOUBLES.length; ++i) {
608 double value = DOUBLES[i].doubleValue();
609 if (value == Double.MAX_VALUE || value == Double.MIN_VALUE) {
610 message.setStringProperty(name, DOUBLES[i].toString());
611 if (value == Double.MIN_VALUE) {
612 if (message.getFloatProperty(name) != 0.0) {
613 fail("Expected string conversion from double to "
614 + "float to underflow for double=" + value
615 + ", but got " + message.getFloatProperty(name));
616 }
617 } else {
618 if (!Float.isInfinite(message.getFloatProperty(name))) {
619 fail("Expected string conversion from double to "
620 + "float to be infinite for double=" + value
621 + ", but got " + message.getFloatProperty(name));
622 }
623 }
624 } else {
625 expectConversionException(float.class,
626 new Double[]{DOUBLES[i]},
627 NumberFormatException.class);
628 }
629 }
630 }
631
632 /***
633 * Verifies string <-> double property conversions
634 *
635 * @jmscts.requirement properties.conversion
636 * @throws Exception for any error
637 */
638 public void testStringDouble() throws Exception {
639 final String name = "stringDouble";
640
641 Message message = getContext().getMessage();
642
643 // check valid string <-> double conversions
644 for (int i = 0; i < DOUBLES.length; ++i) {
645 String value = DOUBLES[i].toString();
646 message.setStringProperty(name, value);
647 assertTrue(message.getStringProperty(name).equals(value));
648
649 if (value.equals(Double.toString(Double.NaN))) {
650 assertTrue(DOUBLES[i].isNaN());
651 } else if (value.equals(Double.toString(
652 Double.NEGATIVE_INFINITY))) {
653 assertTrue(DOUBLES[i].doubleValue()
654 == Double.NEGATIVE_INFINITY);
655 } else if (value.equals(Double.toString(
656 Double.POSITIVE_INFINITY))) {
657 assertTrue(DOUBLES[i].doubleValue()
658 == Double.POSITIVE_INFINITY);
659 } else {
660 assertTrue(message.getDoubleProperty(name)
661 == DOUBLES[i].doubleValue());
662 }
663 }
664
665 // check invalid string -> double conversions
666 final Class[] invalidDoubles = {boolean.class};
667 expectConversionException(double.class, invalidDoubles,
668 NumberFormatException.class);
669 }
670
671 /***
672 * Verifies that attempting to read a null as a primitive type is
673 * equivalent to calling the primitive's corresponding valueOf(String)
674 * conversion method with a null value.
675 *
676 * @jmscts.requirement properties.conversion.null
677 * @throws Exception for any error
678 */
679 public void testNullToPrimitive() throws Exception {
680 final String name = "nulltest";
681
682 Message message = getContext().getMessage();
683 message.setObjectProperty(name, null);
684
685 // boolean - for booleans, anything other than "true"
686 // (case is ignored) is considered false
687 assertFalse(message.getBooleanProperty(name));
688
689 // byte
690 expectException(name, byte.class, byte.class,
691 NumberFormatException.class);
692
693 // short
694 expectException(name, short.class, short.class,
695 NumberFormatException.class);
696
697 // int
698 expectException(name, int.class, int.class,
699 NumberFormatException.class);
700
701 // long
702 expectException(name, long.class, long.class,
703 NumberFormatException.class);
704
705 // float
706 expectException(name, float.class, float.class,
707 NullPointerException.class);
708
709 // double
710 expectException(name, double.class, double.class,
711 NullPointerException.class);
712 }
713
714 /***
715 * Checks that a float property equals the expected value
716 *
717 * @param name the name of the property
718 * @param expected the expected value
719 * @throws Exception if the value doesn't match the expected value
720 */
721 private void checkFloat(String name, float expected) throws Exception {
722
723 Message message = getContext().getMessage();
724
725 if (Float.isNaN(expected)) {
726 assertTrue(Float.isNaN(message.getFloatProperty(name)));
727 assertTrue(Double.isNaN(message.getDoubleProperty(name)));
728 assertTrue(((Float) message.getObjectProperty(name)).isNaN());
729 } else {
730 assertTrue(message.getFloatProperty(name) == expected);
731 assertTrue(message.getDoubleProperty(name) == expected);
732 assertTrue(((Float) message.getObjectProperty(name)).floatValue()
733 == expected);
734 }
735 }
736
737 /***
738 * Checks that a double property equals the expected value
739 *
740 * @param name the name of the property
741 * @param expected the expected value
742 * @throws Exception if the value doesn't match the expected value
743 */
744 private void checkDouble(String name, double expected) throws Exception {
745
746 Message message = getContext().getMessage();
747
748 if (Double.isNaN(expected)) {
749 assertTrue(Double.isNaN(message.getDoubleProperty(name)));
750 assertTrue(((Double) message.getObjectProperty(name)).isNaN());
751 } else {
752 assertTrue(message.getDoubleProperty(name) == expected);
753 assertTrue(((Double) message.getObjectProperty(name)).doubleValue()
754 == expected);
755 }
756 }
757
758 /***
759 * For each class 'A' listed in classes, converts the range of values
760 * associated with 'A' to a String property, and then verifies that the
761 * conversion to an instance of the primitive fails
762 *
763 * @param primitive the class of the primitive type
764 * @param classes the list of primitive types to attempt conversion for
765 * @param exceptionType the class of the expected exception
766 * @throws Exception if the operation succeeds, or the wrong exception is
767 * thrown
768 */
769 private void expectConversionException(Class primitive, Class[] classes,
770 Class exceptionType)
771 throws Exception {
772
773 for (int i = 0; i < classes.length; ++i) {
774 if (classes[i].equals(boolean.class)) {
775 expectConversionException(primitive, BOOLEANS, exceptionType);
776 } else if (classes[i].equals(byte.class)) {
777 expectConversionException(primitive, BYTES, exceptionType);
778 } else if (classes[i].equals(short.class)) {
779 expectConversionException(primitive, SHORTS, exceptionType);
780 } else if (classes[i].equals(int.class)) {
781 expectConversionException(primitive, INTS, exceptionType);
782 } else if (classes[i].equals(long.class)) {
783 expectConversionException(primitive, LONGS, exceptionType);
784 } else if (classes[i].equals(float.class)) {
785 expectConversionException(primitive, FLOATS, exceptionType);
786 } else if (classes[i].equals(double.class)) {
787 expectConversionException(primitive, DOUBLES, exceptionType);
788 } else {
789 throw new Exception("Don't support " + classes[i].getName());
790 }
791 }
792 }
793
794 /***
795 * For each value in an array, convert them to a string, and attempt
796 * conversion using the supplied primitive's
797 * message accessor method. The operation is expected to fail.
798 *
799 * @param primitive the class of the primitive type
800 * @param values the array of values to convert
801 * @param exceptionType the class of the expected exception
802 * @throws Exception if the operation succeeds, or the wrong exception is
803 * thrown
804 */
805 private void expectConversionException(Class primitive, Object[] values,
806 Class exceptionType)
807 throws Exception {
808
809 Message message = getContext().getMessage();
810
811 for (int i = 0; i < values.length; ++i) {
812 Class type = values[i].getClass();
813 String name = ClassHelper.getPrimitiveType(type).getName();
814 message.setStringProperty(name, values[i].toString());
815 expectException(name, String.class, primitive, exceptionType);
816 }
817 }
818
819 /***
820 * Expect an exception when converting from one primitive type to another
821 *
822 * @param name the property name
823 * @param from the type to convert from
824 * @param to the type to convert to
825 * @param exceptionType the expected exception
826 * @throws Exception if the operation succeeds, or the wrong exception is
827 * thrown
828 */
829 private void expectException(String name, Class from, Class to,
830 Class exceptionType) throws Exception {
831
832 Message message = getContext().getMessage();
833 try {
834 if (to.equals(boolean.class)) {
835 message.getBooleanProperty(name);
836 } else if (to.equals(byte.class)) {
837 message.getByteProperty(name);
838 } else if (to.equals(byte.class)) {
839 message.getByteProperty(name);
840 } else if (to.equals(short.class)) {
841 message.getShortProperty(name);
842 } else if (to.equals(int.class)) {
843 message.getIntProperty(name);
844 } else if (to.equals(long.class)) {
845 message.getLongProperty(name);
846 } else if (to.equals(float.class)) {
847 message.getFloatProperty(name);
848 } else if (to.equals(double.class)) {
849 message.getDoubleProperty(name);
850 } else if (to.equals(String.class)) {
851 message.getStringProperty(name);
852 }
853
854 Object value = message.getObjectProperty(name);
855 String text = (value == null) ? "<null>" : value.toString();
856 fail("Conversion from " + from.getName() + "=" + text
857 + " to " + to.getName() + " should fail");
858 } catch (Exception exception) {
859 if (!exception.getClass().equals(exceptionType)) {
860 Object value = message.getObjectProperty(name);
861 String text = (value == null) ? "<null>" : value.toString();
862 fail("Conversion from " + from.getName() + "=" + text
863 + " to " + to.getName() + " threw "
864 + exception.getClass().getName() + " - it should throw "
865 + exceptionType.getName());
866 }
867 }
868 }
869
870 }
This page was automatically generated by Maven