diff --git a/core/src/main/java/org/jclouds/date/DateCodec.java b/core/src/main/java/org/jclouds/date/DateCodec.java index 8570b58c7a..bd4068e913 100644 --- a/core/src/main/java/org/jclouds/date/DateCodec.java +++ b/core/src/main/java/org/jclouds/date/DateCodec.java @@ -24,6 +24,7 @@ import java.util.Date; * converting from Date->String and vice versa. * * @author aled + * @see DateCodecFactory */ public interface DateCodec { diff --git a/core/src/main/java/org/jclouds/date/DateCodecFactory.java b/core/src/main/java/org/jclouds/date/DateCodecFactory.java index 596030811a..a99cf32249 100644 --- a/core/src/main/java/org/jclouds/date/DateCodecFactory.java +++ b/core/src/main/java/org/jclouds/date/DateCodecFactory.java @@ -26,11 +26,17 @@ import com.google.inject.ImplementedBy; * Codecs for converting from Date->String and vice versa. * * @author aled + * @see DateCodec */ @ImplementedBy(DateServiceDateCodecFactory.class) public interface DateCodecFactory { + DateCodec rfc822(); + DateCodec rfc1123(); DateCodec iso8601(); + + DateCodec iso8601Seconds(); + } 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 b462186156..9e9552956e 100644 --- a/core/src/main/java/org/jclouds/date/internal/DateServiceDateCodecFactory.java +++ b/core/src/main/java/org/jclouds/date/internal/DateServiceDateCodecFactory.java @@ -33,13 +33,45 @@ import com.google.inject.Inject; @Singleton public class DateServiceDateCodecFactory implements DateCodecFactory { + private final DateCodec rfc822Codec; private final DateCodec rfc1123Codec; - private final DateServiceIso8601Codec iso8601Codec; + private final DateCodec iso8601Codec; + private final DateCodec iso8601SecondsCodec; @Inject - public DateServiceDateCodecFactory(DateServiceRfc1123Codec rfc1123Codec, DateServiceIso8601Codec iso8601Codec) { - this.rfc1123Codec = checkNotNull(rfc1123Codec, "rfc1123Codec"); - this.iso8601Codec = checkNotNull(iso8601Codec, "iso8601Codec"); + public DateServiceDateCodecFactory(DateService dateService) { + checkNotNull(dateService, "dateService"); + this.rfc822Codec = new DateServiceRfc822Codec(dateService); + this.rfc1123Codec = new DateServiceRfc1123Codec(dateService); + this.iso8601Codec = new DateServiceIso8601Codec(dateService); + this.iso8601SecondsCodec = new DateServiceIso8601SecondsCodec(dateService); + } + + @Singleton + public static class DateServiceRfc822Codec implements DateCodec { + + protected final DateService dateService; + + @Inject + public DateServiceRfc822Codec(final DateService dateService) { + this.dateService = checkNotNull(dateService, "dateService"); + } + + @Override + public Date toDate(String date) throws IllegalArgumentException { + return dateService.rfc822DateParse(date); + } + + @Override + public String toString(Date date) { + return dateService.rfc822DateFormat(date); + } + + @Override + public String toString() { + return "rfc822()"; + } + } @Singleton @@ -68,7 +100,7 @@ public class DateServiceDateCodecFactory implements DateCodecFactory { } } - + @Singleton public static class DateServiceIso8601Codec implements DateCodec { @@ -95,15 +127,52 @@ public class DateServiceDateCodecFactory implements DateCodecFactory { } } + + @Singleton + public static class DateServiceIso8601SecondsCodec implements DateCodec { + + protected final DateService dateService; + + @Inject + public DateServiceIso8601SecondsCodec(DateService dateService) { + this.dateService = checkNotNull(dateService, "dateService"); + } + + @Override + public Date toDate(String date) throws IllegalArgumentException { + return dateService.iso8601SecondsDateParse(date); + } + + @Override + public String toString(Date date) { + return dateService.iso8601SecondsDateFormat(date); + } + + @Override + public String toString() { + return "iso8601Seconds()"; + } + + } + + @Override + public DateCodec rfc822() { + return rfc822Codec; + } @Override public DateCodec rfc1123() { return rfc1123Codec; } - + @Override public DateCodec iso8601() { return iso8601Codec; } + @Override + public DateCodec iso8601Seconds() { + return iso8601SecondsCodec; + } + } 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 02f8416011..68f2a7fdd1 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,6 @@ 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.BeforeTest; import org.testng.annotations.Test; @@ -38,15 +36,35 @@ import org.testng.annotations.Test; public class DateServiceDateCodecFactoryTest { private DateServiceDateCodecFactory simpleDateCodecFactory; + private DateCodec rfc822Codec; private DateCodec rfc1123Codec; private DateCodec iso8601Codec; + private DateCodec iso8601SecondsCodec; @BeforeTest public void setUp() { - simpleDateCodecFactory = new DateServiceDateCodecFactory(new DateServiceRfc1123Codec( - new SimpleDateFormatDateService()), new DateServiceIso8601Codec(new SimpleDateFormatDateService())); + simpleDateCodecFactory = new DateServiceDateCodecFactory(new SimpleDateFormatDateService()); + rfc822Codec = simpleDateCodecFactory.rfc822(); rfc1123Codec = simpleDateCodecFactory.rfc1123(); iso8601Codec = simpleDateCodecFactory.iso8601(); + iso8601SecondsCodec = simpleDateCodecFactory.iso8601Seconds(); + } + + @Test + public void testCodecForRfc822() { + Date date = new Date(1000); + assertEquals(rfc822Codec.toDate(rfc822Codec.toString(date)), date); + + assertEquals(rfc822Codec.toDate("Thu, 01 Dec 1994 16:00:00 GMT"), new Date(786297600000L)); + } + + @Test + public void testCodecForRfc822ThrowsParseExceptionWhenMalformed() { + try { + rfc822Codec.toDate("wrong"); + fail(); + } catch (IllegalArgumentException e) { + } } @Test @@ -82,4 +100,21 @@ public class DateServiceDateCodecFactoryTest { } catch (IllegalArgumentException e) { } } + + @Test + public void testCodecForIso8601Seconds() { + Date date = new Date(1000); + assertEquals(iso8601SecondsCodec.toDate(iso8601SecondsCodec.toString(date)), date); + + assertEquals(iso8601SecondsCodec.toDate("2012-11-14T21:51:28UTC").getTime(), 1352929888000l); + } + + @Test + public void testCodecForIso8601SecondsThrowsParseExceptionWhenMalformed() { + try { + iso8601SecondsCodec.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 5d857bb8de..7779761a0e 100644 --- a/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java +++ b/core/src/test/java/org/jclouds/http/handlers/BackoffLimitedRetryHandlerTest.java @@ -35,8 +35,6 @@ 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; import org.jclouds.http.HttpCommand; @@ -118,8 +116,7 @@ public class BackoffLimitedRetryHandlerTest { BackoffLimitedRetryHandler backoff = new BackoffLimitedRetryHandler(); HttpUtils utils = new HttpUtils(0, 500, 1, 1); ContentMetadataCodec contentMetadataCodec = new DefaultContentMetadataCodec(new DateServiceDateCodecFactory( - new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), new DateServiceIso8601Codec( - new SimpleDateFormatDateService()))); + new SimpleDateFormatDateService())); RedirectionRetryHandler retry = new RedirectionRetryHandler(uriBuilderProvider, backoff); JavaUrlHttpCommandExecutorService httpService = new JavaUrlHttpCommandExecutorService(utils, contentMetadataCodec, execService, @@ -230,4 +227,4 @@ public class BackoffLimitedRetryHandlerTest { assertEquals(handler.shouldRetryRequest(command, response), false); // Failure 6 } -} \ No newline at end of file +} diff --git a/core/src/test/java/org/jclouds/rest/internal/BaseRestApiExpectTest.java b/core/src/test/java/org/jclouds/rest/internal/BaseRestApiExpectTest.java index b922cbec82..fd178c9a2d 100644 --- a/core/src/test/java/org/jclouds/rest/internal/BaseRestApiExpectTest.java +++ b/core/src/test/java/org/jclouds/rest/internal/BaseRestApiExpectTest.java @@ -50,8 +50,6 @@ 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; import org.jclouds.http.HttpRequest; @@ -122,8 +120,7 @@ public abstract class BaseRestApiExpectTest { protected String provider = "mock"; protected ContentMetadataCodec contentMetadataCodec = new DefaultContentMetadataCodec( - new DateServiceDateCodecFactory(new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), - new DateServiceIso8601Codec(new SimpleDateFormatDateService()))); + new DateServiceDateCodecFactory(new SimpleDateFormatDateService())); /** * Override this to supply alternative bindings for use in the test. This is commonly used to @@ -570,4 +567,4 @@ public abstract class BaseRestApiExpectTest { props.put(Constants.PROPERTY_MAX_RETRIES, 1); return props; } -} \ No newline at end of file +} 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 9dbaa0c9c0..de0c140cf5 100644 --- a/drivers/gae/src/test/java/org/jclouds/gae/ConvertToGaeRequestTest.java +++ b/drivers/gae/src/test/java/org/jclouds/gae/ConvertToGaeRequestTest.java @@ -32,8 +32,6 @@ 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; import org.jclouds.http.HttpRequest; @@ -75,9 +73,7 @@ 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 DateServiceIso8601Codec(new SimpleDateFormatDateService())))); - + new DateServiceDateCodecFactory(new SimpleDateFormatDateService()))); } @Test 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 4a43e8a3dd..ea8bb89895 100644 --- a/drivers/gae/src/test/java/org/jclouds/gae/ConvertToJcloudsResponseTest.java +++ b/drivers/gae/src/test/java/org/jclouds/gae/ConvertToJcloudsResponseTest.java @@ -34,8 +34,6 @@ 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; import org.jclouds.http.HttpResponse; @@ -72,8 +70,7 @@ public class ConvertToJcloudsResponseTest { void setupClient() { endPoint = URI.create("http://localhost:80/foo"); req = new ConvertToJcloudsResponse(new DefaultContentMetadataCodec(new DateServiceDateCodecFactory( - new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), new DateServiceIso8601Codec( - new SimpleDateFormatDateService())))); + new SimpleDateFormatDateService()))); } @Test