diff --git a/core/src/main/java/org/jclouds/date/internal/DateServiceDateCodecFactory.java b/core/src/main/java/org/jclouds/date/internal/DateServiceDateCodecFactory.java index 62f54f6175..fdeed623dd 100644 --- a/core/src/main/java/org/jclouds/date/internal/DateServiceDateCodecFactory.java +++ b/core/src/main/java/org/jclouds/date/internal/DateServiceDateCodecFactory.java @@ -34,16 +34,18 @@ import com.google.inject.Inject; public class DateServiceDateCodecFactory implements DateCodecFactory { private final DateCodec rfc1123Codec; + private final DateServiceIso8601Codec iso8601Codec; @Inject - public DateServiceDateCodecFactory(DateServiceRfc1123Codec rfc1123Codec) { + public DateServiceDateCodecFactory(DateServiceRfc1123Codec rfc1123Codec, DateServiceIso8601Codec iso8601Codec) { this.rfc1123Codec = checkNotNull(rfc1123Codec, "rfc1123Codec"); + this.iso8601Codec = checkNotNull(iso8601Codec, "iso8601Codec"); } @Singleton public static class DateServiceRfc1123Codec implements DateCodec { - private final DateService dateService; + protected final DateService dateService; @Inject public DateServiceRfc1123Codec(final DateService dateService) { @@ -62,7 +64,34 @@ public class DateServiceDateCodecFactory implements DateCodecFactory { @Override public String toString() { - return "rfc1123Codec [dateService=" + dateService + "]"; + return "rfc1123()"; + } + + } + + @Singleton + public static class DateServiceIso8601Codec implements DateCodec { + + protected final DateService dateService; + + @Inject + public DateServiceIso8601Codec(DateService dateService) { + this.dateService = checkNotNull(dateService, "dateService"); + } + + @Override + public Date toDate(String date) throws IllegalArgumentException { + return dateService.iso8601DateParse(date); + } + + @Override + public String toString(Date date) { + return dateService.iso8601DateFormat(date); + } + + @Override + public String toString() { + return "iso8601()"; } } @@ -70,5 +99,9 @@ public class DateServiceDateCodecFactory implements DateCodecFactory { public DateCodec rfc1123() { return rfc1123Codec; } - + + public DateCodec iso8601() { + return iso8601Codec; + } + } diff --git a/core/src/test/java/org/jclouds/date/internal/DateServiceDateCodecFactoryTest.java b/core/src/test/java/org/jclouds/date/internal/DateServiceDateCodecFactoryTest.java index 6c6f12a005..02f8416011 100644 --- a/core/src/test/java/org/jclouds/date/internal/DateServiceDateCodecFactoryTest.java +++ b/core/src/test/java/org/jclouds/date/internal/DateServiceDateCodecFactoryTest.java @@ -24,8 +24,9 @@ import static org.testng.Assert.fail; import java.util.Date; import org.jclouds.date.DateCodec; +import org.jclouds.date.internal.DateServiceDateCodecFactory.DateServiceIso8601Codec; import org.jclouds.date.internal.DateServiceDateCodecFactory.DateServiceRfc1123Codec; -import org.testng.annotations.BeforeMethod; +import org.testng.annotations.BeforeTest; import org.testng.annotations.Test; /** @@ -38,12 +39,14 @@ public class DateServiceDateCodecFactoryTest { private DateServiceDateCodecFactory simpleDateCodecFactory; private DateCodec rfc1123Codec; + private DateCodec iso8601Codec; - @BeforeMethod + @BeforeTest public void setUp() { simpleDateCodecFactory = new DateServiceDateCodecFactory(new DateServiceRfc1123Codec( - new SimpleDateFormatDateService())); + new SimpleDateFormatDateService()), new DateServiceIso8601Codec(new SimpleDateFormatDateService())); rfc1123Codec = simpleDateCodecFactory.rfc1123(); + iso8601Codec = simpleDateCodecFactory.iso8601(); } @Test @@ -62,4 +65,21 @@ public class DateServiceDateCodecFactoryTest { } catch (IllegalArgumentException e) { } } + + @Test + public void testCodecForIso8601() { + Date date = new Date(1000); + assertEquals(iso8601Codec.toDate(iso8601Codec.toString(date)), date); + + assertEquals(iso8601Codec.toDate("1994-12-01T16:00:00.000Z"), new Date(786297600000L)); + } + + @Test + public void testCodecForIso8601ThrowsParseExceptionWhenMalformed() { + try { + iso8601Codec.toDate("-"); + fail(); + } catch (IllegalArgumentException e) { + } + } } diff --git a/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java b/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java index 6861f9d894..663ecaa2a5 100644 --- a/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java +++ b/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java @@ -35,6 +35,7 @@ import javax.net.ssl.SSLSession; import javax.ws.rs.core.UriBuilder; import org.jclouds.date.internal.DateServiceDateCodecFactory; +import org.jclouds.date.internal.DateServiceDateCodecFactory.DateServiceIso8601Codec; import org.jclouds.date.internal.DateServiceDateCodecFactory.DateServiceRfc1123Codec; import org.jclouds.date.internal.SimpleDateFormatDateService; import org.jclouds.http.BaseJettyTest; @@ -116,8 +117,9 @@ public class BackoffLimitedRetryHandlerTest { ExecutorService execService = Executors.newCachedThreadPool(); BackoffLimitedRetryHandler backoff = new BackoffLimitedRetryHandler(); HttpUtils utils = new HttpUtils(0, 500, 1, 1); - ContentMetadataCodec contentMetadataCodec = new DefaultContentMetadataCodec( - new DateServiceDateCodecFactory(new DateServiceRfc1123Codec(new SimpleDateFormatDateService()))); + ContentMetadataCodec contentMetadataCodec = new DefaultContentMetadataCodec(new DateServiceDateCodecFactory( + new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), new DateServiceIso8601Codec( + new SimpleDateFormatDateService()))); RedirectionRetryHandler retry = new RedirectionRetryHandler(uriBuilderProvider, backoff); JavaUrlHttpCommandExecutorService httpService = new JavaUrlHttpCommandExecutorService(utils, contentMetadataCodec, execService, diff --git a/core/src/test/java/org/jclouds/rest/internal/BaseRestClientExpectTest.java b/core/src/test/java/org/jclouds/rest/internal/BaseRestClientExpectTest.java index 38630f5dbe..c8cd7041c4 100644 --- a/core/src/test/java/org/jclouds/rest/internal/BaseRestClientExpectTest.java +++ b/core/src/test/java/org/jclouds/rest/internal/BaseRestClientExpectTest.java @@ -50,6 +50,7 @@ import org.jclouds.concurrent.MoreExecutors; import org.jclouds.concurrent.SingleThreaded; import org.jclouds.concurrent.config.ConfiguresExecutorService; import org.jclouds.date.internal.DateServiceDateCodecFactory; +import org.jclouds.date.internal.DateServiceDateCodecFactory.DateServiceIso8601Codec; import org.jclouds.date.internal.DateServiceDateCodecFactory.DateServiceRfc1123Codec; import org.jclouds.date.internal.SimpleDateFormatDateService; import org.jclouds.http.HttpCommandExecutorService; @@ -124,7 +125,8 @@ public abstract class BaseRestClientExpectTest { protected String provider = "mock"; protected ContentMetadataCodec contentMetadataCodec = new DefaultContentMetadataCodec( - new DateServiceDateCodecFactory(new DateServiceRfc1123Codec(new SimpleDateFormatDateService()))); + new DateServiceDateCodecFactory(new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), + new DateServiceIso8601Codec(new SimpleDateFormatDateService()))); /** * Override this to supply alternative bindings for use in the test. This is commonly used to diff --git a/drivers/gae/src/test/java/org/jclouds/gae/ConvertToGaeRequestTest.java b/drivers/gae/src/test/java/org/jclouds/gae/ConvertToGaeRequestTest.java index 83c6c7ef9e..d3dfd09498 100644 --- a/drivers/gae/src/test/java/org/jclouds/gae/ConvertToGaeRequestTest.java +++ b/drivers/gae/src/test/java/org/jclouds/gae/ConvertToGaeRequestTest.java @@ -32,6 +32,7 @@ import javax.ws.rs.core.HttpHeaders; import org.jclouds.crypto.Crypto; import org.jclouds.date.internal.DateServiceDateCodecFactory; +import org.jclouds.date.internal.DateServiceDateCodecFactory.DateServiceIso8601Codec; import org.jclouds.date.internal.DateServiceDateCodecFactory.DateServiceRfc1123Codec; import org.jclouds.date.internal.SimpleDateFormatDateService; import org.jclouds.encryption.internal.JCECrypto; @@ -75,7 +76,8 @@ public class ConvertToGaeRequestTest { void setupClient() { endPoint = URI.create("http://localhost:80/foo"); req = new ConvertToGaeRequest(new HttpUtils(0, 0, 0, 0), new DefaultContentMetadataCodec( - new DateServiceDateCodecFactory(new DateServiceRfc1123Codec(new SimpleDateFormatDateService())))); + new DateServiceDateCodecFactory(new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), + new DateServiceIso8601Codec(new SimpleDateFormatDateService())))); } diff --git a/drivers/gae/src/test/java/org/jclouds/gae/ConvertToJcloudsResponseTest.java b/drivers/gae/src/test/java/org/jclouds/gae/ConvertToJcloudsResponseTest.java index 51ec9932e3..7cab1c3d13 100644 --- a/drivers/gae/src/test/java/org/jclouds/gae/ConvertToJcloudsResponseTest.java +++ b/drivers/gae/src/test/java/org/jclouds/gae/ConvertToJcloudsResponseTest.java @@ -34,6 +34,7 @@ import javax.ws.rs.core.HttpHeaders; import org.jclouds.crypto.Crypto; import org.jclouds.date.internal.DateServiceDateCodecFactory; +import org.jclouds.date.internal.DateServiceDateCodecFactory.DateServiceIso8601Codec; import org.jclouds.date.internal.DateServiceDateCodecFactory.DateServiceRfc1123Codec; import org.jclouds.date.internal.SimpleDateFormatDateService; import org.jclouds.encryption.internal.JCECrypto; @@ -71,7 +72,8 @@ public class ConvertToJcloudsResponseTest { void setupClient() { endPoint = URI.create("http://localhost:80/foo"); req = new ConvertToJcloudsResponse(new DefaultContentMetadataCodec(new DateServiceDateCodecFactory( - new DateServiceRfc1123Codec(new SimpleDateFormatDateService())))); + new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), new DateServiceIso8601Codec( + new SimpleDateFormatDateService())))); } @Test