From 9857baf37ba73299adaad328f509ad8c2a681d98 Mon Sep 17 00:00:00 2001 From: joerai Date: Wed, 20 May 2015 21:34:23 +1000 Subject: [PATCH] #440 - Fix Serialization Of Dates Ensure that dates are serialized to RFC2616 according to http://msdn.microsoft.com/en-us/library/azure/hh780742.aspx. --- .../implementation/BrokerPropertiesMapper.java | 14 +++++++++++--- .../servicebus/BrokerPropertiesMapperTest.java | 17 +++++++++++++++++ 2 files changed, 28 insertions(+), 3 deletions(-) diff --git a/serviceBus/src/main/java/com/microsoft/windowsazure/services/servicebus/implementation/BrokerPropertiesMapper.java b/serviceBus/src/main/java/com/microsoft/windowsazure/services/servicebus/implementation/BrokerPropertiesMapper.java index 6a404f72cd9c..b4c796905966 100644 --- a/serviceBus/src/main/java/com/microsoft/windowsazure/services/servicebus/implementation/BrokerPropertiesMapper.java +++ b/serviceBus/src/main/java/com/microsoft/windowsazure/services/servicebus/implementation/BrokerPropertiesMapper.java @@ -17,8 +17,10 @@ import java.io.IOException; import java.io.StringWriter; import java.io.Writer; +import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Locale; +import java.util.TimeZone; import org.codehaus.jackson.JsonGenerationException; import org.codehaus.jackson.JsonParseException; @@ -30,9 +32,7 @@ public class BrokerPropertiesMapper { public BrokerProperties fromString(String value) { ObjectMapper mapper = new ObjectMapper(); - SimpleDateFormat simpleDateFormat = new SimpleDateFormat( - "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); - mapper.setDateFormat(simpleDateFormat); + mapper.setDateFormat(getRFC2616DateFormatter()); try { return mapper.readValue(value.getBytes("UTF-8"), BrokerProperties.class); @@ -47,6 +47,7 @@ public BrokerProperties fromString(String value) { public String toString(BrokerProperties value) { ObjectMapper mapper = new ObjectMapper(); + mapper.setDateFormat(getRFC2616DateFormatter()); Writer writer = new StringWriter(); try { mapper.writeValue(writer, value); @@ -59,5 +60,12 @@ public String toString(BrokerProperties value) { } return writer.toString(); } + + private DateFormat getRFC2616DateFormatter() { + SimpleDateFormat simpleDateFormat = new SimpleDateFormat( + "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US); + simpleDateFormat.setTimeZone(TimeZone.getTimeZone("GMT")); + return simpleDateFormat; + } } diff --git a/serviceBus/src/test/java/com/microsoft/windowsazure/services/servicebus/BrokerPropertiesMapperTest.java b/serviceBus/src/test/java/com/microsoft/windowsazure/services/servicebus/BrokerPropertiesMapperTest.java index 738551198f27..33bebd6407f1 100644 --- a/serviceBus/src/test/java/com/microsoft/windowsazure/services/servicebus/BrokerPropertiesMapperTest.java +++ b/serviceBus/src/test/java/com/microsoft/windowsazure/services/servicebus/BrokerPropertiesMapperTest.java @@ -192,4 +192,21 @@ public void deserializeDateInZhCnLocaleCorrectly() { - lockedUntilUtc.getTime(); assertTrue(Math.abs(lockedUntilDelta) < 2000); } + + @Test + public void dateSerializeCorrectlyToRFC2616() { + // Arrange + BrokerPropertiesMapper mapper = new BrokerPropertiesMapper(); + Date date = new Date(1432120118000L); + + // Act + BrokerProperties properties = new BrokerProperties(); + properties.setScheduledEnqueueTimeUtc(date); + String json = mapper.toString(properties); + + // Assert + assertNotNull(json); + assertEquals("{\"ScheduledEnqueueTimeUtc\":\"Wed, 20 May 2015 11:08:38 GMT\"}", json); + + } }