46
46
public class SQSMessageTest {
47
47
private SQSSession mockSQSSession ;
48
48
final String myNumber = "myNumber" ;
49
+ final String myNumberOverflow = "myNumberOverflow" ;
49
50
final String myBinary = "myBinary" ;
50
51
final String myTrueBoolean = "myTrueBoolean" ;
51
52
final String myFalseBoolean = "myFalseBoolean" ;
@@ -271,6 +272,8 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
271
272
272
273
Acknowledger ack = mock (Acknowledger .class );
273
274
275
+ // 52 digits of value.
276
+ final String numberOverflowValue = "1234567890123456789012345678901234567890123456789012" ;
274
277
final ByteBuffer binaryValue = ByteBuffer .wrap (new byte [] {(byte )0xDE , (byte )0xAD , (byte )0xBE , (byte )0xEF });
275
278
276
279
Map <String ,String > systemAttributes = new HashMap <String , String >();
@@ -282,6 +285,11 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
282
285
messageAttributes .put (myNumber , new MessageAttributeValue ()
283
286
.withDataType (SQSMessagingClientConstants .NUMBER )
284
287
.withStringValue ("4" ));
288
+
289
+ messageAttributes .put (myNumberOverflow , new MessageAttributeValue ()
290
+ .withDataType (SQSMessagingClientConstants .NUMBER )
291
+ .withStringValue (numberOverflowValue ));
292
+
285
293
messageAttributes .put (myBinary , new MessageAttributeValue ()
286
294
.withDataType (SQSMessagingClientConstants .BINARY )
287
295
.withBinaryValue (binaryValue ));
@@ -334,9 +342,25 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
334
342
335
343
// SQS "Number" data type must use StringValue, may be a floating point number
336
344
Assert .assertTrue (message .propertyExists (myNumber ));
337
- Assert .assertEquals (4.0 , (double )message .getObjectProperty (myNumber ), 0 );
345
+ Assert .assertEquals ("4" , message .getObjectProperty (myNumber ));
346
+ Assert .assertEquals ("4" , message .getStringProperty (myNumber ));
347
+ Assert .assertEquals (4 , message .getIntProperty (myNumber ));
338
348
Assert .assertEquals (4.0 , message .getDoubleProperty (myNumber ), 0 );
339
349
350
+ // A Number can hold a value with quite a lot of significant digits, it must be represented as a StringValue,
351
+ // and can be interpreted as other things, but we should expect errors if the value overflows.
352
+ Assert .assertTrue (message .propertyExists (myNumberOverflow ));
353
+ Assert .assertEquals (numberOverflowValue , message .getObjectProperty (myNumberOverflow ));
354
+ Assert .assertEquals (numberOverflowValue , message .getStringProperty (myNumberOverflow ));
355
+ // Expecting a loss of precision here when interpreted as a double.
356
+ Assert .assertEquals (1.23456789012345678E51 , message .getDoubleProperty (myNumberOverflow ), 0 );
357
+ try {
358
+ message .getIntProperty (myNumberOverflow );
359
+ Assert .fail ("message.getIntProperty did not throw a NumberFormatException" );
360
+ } catch (NumberFormatException nfe ) {
361
+ Assert .assertEquals ("For input string: \" " +numberOverflowValue +"\" " , nfe .getMessage ());
362
+ }
363
+
340
364
Assert .assertTrue (message .propertyExists (myBinary ));
341
365
Assert .assertEquals (binaryValue , message .getObjectProperty (myBinary ));
342
366
@@ -381,6 +405,7 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
381
405
Set <String > propertyNamesSet = new HashSet <String >(Arrays .asList (
382
406
myBinary ,
383
407
myNumber ,
408
+ myNumberOverflow ,
384
409
myTrueBoolean ,
385
410
myFalseBoolean ,
386
411
myInteger ,
0 commit comments