Issue 1007: ios8601 codec

This commit is contained in:
Adrian Cole 2012-07-06 21:23:10 -07:00
parent 578c9e93f9
commit a73240f41b
6 changed files with 73 additions and 12 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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