added rfc822 + iso8601Seconds to DateCodecFactory and simplified ctor

This commit is contained in:
Adrian Cole 2012-12-09 00:29:19 -08:00
parent 04e43c75f1
commit 802b570068
8 changed files with 127 additions and 29 deletions

View File

@ -24,6 +24,7 @@ import java.util.Date;
* converting from Date->String and vice versa.
*
* @author aled
* @see DateCodecFactory
*/
public interface DateCodec {

View File

@ -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();
}

View File

@ -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
@ -96,6 +128,38 @@ 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;
@ -106,4 +170,9 @@ public class DateServiceDateCodecFactory implements DateCodecFactory {
return iso8601Codec;
}
@Override
public DateCodec iso8601Seconds() {
return iso8601SecondsCodec;
}
}

View File

@ -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) {
}
}
}

View File

@ -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,

View File

@ -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<S> {
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

View File

@ -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

View File

@ -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