Skip to content

Commit 9ab52da

Browse files
author
bwynn
committed
Double isn't large enough to hold a SQS 'Number' so we need to leave it as a string, and potentially interpret on the outgoing side.
1 parent 124a334 commit 9ab52da

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

src/main/java/com/amazon/sqs/javamessaging/message/SQSMessage.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@
1515
package com.amazon.sqs.javamessaging.message;
1616

1717
import java.io.UnsupportedEncodingException;
18-
import java.nio.ByteBuffer;
1918
import java.nio.charset.Charset;
2019
import java.util.Date;
2120
import java.util.Enumeration;
@@ -1217,7 +1216,7 @@ private static Object getObjectValue(String value, String type) throws JMSExcept
12171216
return Short.valueOf(value);
12181217
} else if (NUMBER.equals(type)) {
12191218
// This may not be a JMS Type but it is a SQS Type, so we had better support it.
1220-
return Double.valueOf(value);
1219+
return value;
12211220
} else {
12221221
throw new JMSException(type + " is not a supported JMS property type");
12231222
}

src/test/java/com/amazon/sqs/javamessaging/message/SQSMessageTest.java

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
public class SQSMessageTest {
4747
private SQSSession mockSQSSession;
4848
final String myNumber = "myNumber";
49+
final String myNumberOverflow = "myNumberOverflow";
4950
final String myBinary = "myBinary";
5051
final String myTrueBoolean = "myTrueBoolean";
5152
final String myFalseBoolean = "myFalseBoolean";
@@ -271,6 +272,8 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
271272

272273
Acknowledger ack = mock(Acknowledger.class);
273274

275+
// 52 digits of value.
276+
final String numberOverflowValue = "1234567890123456789012345678901234567890123456789012";
274277
final ByteBuffer binaryValue = ByteBuffer.wrap(new byte[] {(byte)0xDE, (byte)0xAD, (byte)0xBE, (byte)0xEF});
275278

276279
Map<String,String> systemAttributes = new HashMap<String, String>();
@@ -282,6 +285,11 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
282285
messageAttributes.put(myNumber, new MessageAttributeValue()
283286
.withDataType(SQSMessagingClientConstants.NUMBER)
284287
.withStringValue("4"));
288+
289+
messageAttributes.put(myNumberOverflow, new MessageAttributeValue()
290+
.withDataType(SQSMessagingClientConstants.NUMBER)
291+
.withStringValue(numberOverflowValue));
292+
285293
messageAttributes.put(myBinary, new MessageAttributeValue()
286294
.withDataType(SQSMessagingClientConstants.BINARY)
287295
.withBinaryValue(binaryValue));
@@ -334,9 +342,25 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
334342

335343
// SQS "Number" data type must use StringValue, may be a floating point number
336344
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));
338348
Assert.assertEquals(4.0, message.getDoubleProperty(myNumber), 0);
339349

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+
340364
Assert.assertTrue(message.propertyExists(myBinary));
341365
Assert.assertEquals(binaryValue, message.getObjectProperty(myBinary));
342366

@@ -381,6 +405,7 @@ public void testSQSMessageAttributeToProperty() throws JMSException {
381405
Set<String> propertyNamesSet = new HashSet<String>(Arrays.asList(
382406
myBinary,
383407
myNumber,
408+
myNumberOverflow,
384409
myTrueBoolean,
385410
myFalseBoolean,
386411
myInteger,

0 commit comments

Comments
 (0)