added rfc822 + iso8601Seconds to DateCodecFactory and simplified ctor

This commit is contained in:
Adrian Cole 2012-12-09 00:29:19 -08:00
parent 72deb93570
commit b8eb49f0a9
8 changed files with 125 additions and 27 deletions

View File

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

View File

@ -26,11 +26,17 @@ import com.google.inject.ImplementedBy;
* Codecs for converting from Date->String and vice versa. * Codecs for converting from Date->String and vice versa.
* *
* @author aled * @author aled
* @see DateCodec
*/ */
@ImplementedBy(DateServiceDateCodecFactory.class) @ImplementedBy(DateServiceDateCodecFactory.class)
public interface DateCodecFactory { public interface DateCodecFactory {
DateCodec rfc822();
DateCodec rfc1123(); DateCodec rfc1123();
DateCodec iso8601(); DateCodec iso8601();
DateCodec iso8601Seconds();
} }

View File

@ -33,13 +33,45 @@ import com.google.inject.Inject;
@Singleton @Singleton
public class DateServiceDateCodecFactory implements DateCodecFactory { public class DateServiceDateCodecFactory implements DateCodecFactory {
private final DateCodec rfc822Codec;
private final DateCodec rfc1123Codec; private final DateCodec rfc1123Codec;
private final DateServiceIso8601Codec iso8601Codec; private final DateCodec iso8601Codec;
private final DateCodec iso8601SecondsCodec;
@Inject @Inject
public DateServiceDateCodecFactory(DateServiceRfc1123Codec rfc1123Codec, DateServiceIso8601Codec iso8601Codec) { public DateServiceDateCodecFactory(DateService dateService) {
this.rfc1123Codec = checkNotNull(rfc1123Codec, "rfc1123Codec"); checkNotNull(dateService, "dateService");
this.iso8601Codec = checkNotNull(iso8601Codec, "iso8601Codec"); 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 @Singleton
@ -68,7 +100,7 @@ public class DateServiceDateCodecFactory implements DateCodecFactory {
} }
} }
@Singleton @Singleton
public static class DateServiceIso8601Codec implements DateCodec { 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 @Override
public DateCodec rfc1123() { public DateCodec rfc1123() {
return rfc1123Codec; return rfc1123Codec;
} }
@Override @Override
public DateCodec iso8601() { public DateCodec iso8601() {
return iso8601Codec; 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 java.util.Date;
import org.jclouds.date.DateCodec; 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.BeforeTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -38,15 +36,35 @@ import org.testng.annotations.Test;
public class DateServiceDateCodecFactoryTest { public class DateServiceDateCodecFactoryTest {
private DateServiceDateCodecFactory simpleDateCodecFactory; private DateServiceDateCodecFactory simpleDateCodecFactory;
private DateCodec rfc822Codec;
private DateCodec rfc1123Codec; private DateCodec rfc1123Codec;
private DateCodec iso8601Codec; private DateCodec iso8601Codec;
private DateCodec iso8601SecondsCodec;
@BeforeTest @BeforeTest
public void setUp() { public void setUp() {
simpleDateCodecFactory = new DateServiceDateCodecFactory(new DateServiceRfc1123Codec( simpleDateCodecFactory = new DateServiceDateCodecFactory(new SimpleDateFormatDateService());
new SimpleDateFormatDateService()), new DateServiceIso8601Codec(new SimpleDateFormatDateService())); rfc822Codec = simpleDateCodecFactory.rfc822();
rfc1123Codec = simpleDateCodecFactory.rfc1123(); rfc1123Codec = simpleDateCodecFactory.rfc1123();
iso8601Codec = simpleDateCodecFactory.iso8601(); 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 @Test
@ -82,4 +100,21 @@ public class DateServiceDateCodecFactoryTest {
} catch (IllegalArgumentException e) { } 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 javax.ws.rs.core.UriBuilder;
import org.jclouds.date.internal.DateServiceDateCodecFactory; 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.date.internal.SimpleDateFormatDateService;
import org.jclouds.http.BaseJettyTest; import org.jclouds.http.BaseJettyTest;
import org.jclouds.http.HttpCommand; import org.jclouds.http.HttpCommand;
@ -118,8 +116,7 @@ public class BackoffLimitedRetryHandlerTest {
BackoffLimitedRetryHandler backoff = new BackoffLimitedRetryHandler(); BackoffLimitedRetryHandler backoff = new BackoffLimitedRetryHandler();
HttpUtils utils = new HttpUtils(0, 500, 1, 1); HttpUtils utils = new HttpUtils(0, 500, 1, 1);
ContentMetadataCodec contentMetadataCodec = new DefaultContentMetadataCodec(new DateServiceDateCodecFactory( ContentMetadataCodec contentMetadataCodec = new DefaultContentMetadataCodec(new DateServiceDateCodecFactory(
new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), new DateServiceIso8601Codec( new SimpleDateFormatDateService()));
new SimpleDateFormatDateService())));
RedirectionRetryHandler retry = new RedirectionRetryHandler(uriBuilderProvider, backoff); RedirectionRetryHandler retry = new RedirectionRetryHandler(uriBuilderProvider, backoff);
JavaUrlHttpCommandExecutorService httpService = new JavaUrlHttpCommandExecutorService(utils, JavaUrlHttpCommandExecutorService httpService = new JavaUrlHttpCommandExecutorService(utils,
contentMetadataCodec, execService, contentMetadataCodec, execService,

View File

@ -50,8 +50,6 @@ import org.jclouds.concurrent.MoreExecutors;
import org.jclouds.concurrent.SingleThreaded; import org.jclouds.concurrent.SingleThreaded;
import org.jclouds.concurrent.config.ConfiguresExecutorService; import org.jclouds.concurrent.config.ConfiguresExecutorService;
import org.jclouds.date.internal.DateServiceDateCodecFactory; 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.date.internal.SimpleDateFormatDateService;
import org.jclouds.http.HttpCommandExecutorService; import org.jclouds.http.HttpCommandExecutorService;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
@ -122,8 +120,7 @@ public abstract class BaseRestApiExpectTest<S> {
protected String provider = "mock"; protected String provider = "mock";
protected ContentMetadataCodec contentMetadataCodec = new DefaultContentMetadataCodec( protected ContentMetadataCodec contentMetadataCodec = new DefaultContentMetadataCodec(
new DateServiceDateCodecFactory(new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), new DateServiceDateCodecFactory(new SimpleDateFormatDateService()));
new DateServiceIso8601Codec(new SimpleDateFormatDateService())));
/** /**
* Override this to supply alternative bindings for use in the test. This is commonly used to * 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.crypto.Crypto;
import org.jclouds.date.internal.DateServiceDateCodecFactory; 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.date.internal.SimpleDateFormatDateService;
import org.jclouds.encryption.internal.JCECrypto; import org.jclouds.encryption.internal.JCECrypto;
import org.jclouds.http.HttpRequest; import org.jclouds.http.HttpRequest;
@ -75,9 +73,7 @@ public class ConvertToGaeRequestTest {
void setupClient() { void setupClient() {
endPoint = URI.create("http://localhost:80/foo"); endPoint = URI.create("http://localhost:80/foo");
req = new ConvertToGaeRequest(new HttpUtils(0, 0, 0, 0), new DefaultContentMetadataCodec( req = new ConvertToGaeRequest(new HttpUtils(0, 0, 0, 0), new DefaultContentMetadataCodec(
new DateServiceDateCodecFactory(new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), new DateServiceDateCodecFactory(new SimpleDateFormatDateService())));
new DateServiceIso8601Codec(new SimpleDateFormatDateService()))));
} }
@Test @Test

View File

@ -33,8 +33,6 @@ import javax.ws.rs.core.HttpHeaders;
import org.jclouds.crypto.Crypto; import org.jclouds.crypto.Crypto;
import org.jclouds.date.internal.DateServiceDateCodecFactory; 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.date.internal.SimpleDateFormatDateService;
import org.jclouds.encryption.internal.JCECrypto; import org.jclouds.encryption.internal.JCECrypto;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
@ -72,8 +70,7 @@ public class ConvertToJcloudsResponseTest {
void setupClient() { void setupClient() {
endPoint = URI.create("http://localhost:80/foo"); endPoint = URI.create("http://localhost:80/foo");
req = new ConvertToJcloudsResponse(new DefaultContentMetadataCodec(new DateServiceDateCodecFactory( req = new ConvertToJcloudsResponse(new DefaultContentMetadataCodec(new DateServiceDateCodecFactory(
new DateServiceRfc1123Codec(new SimpleDateFormatDateService()), new DateServiceIso8601Codec( new SimpleDateFormatDateService())));
new SimpleDateFormatDateService()))));
} }
@Test @Test