'iso8601DateParseWithOptionalTZ' -> 'iso8601DateOrSecondsDateParse' in DateService

Follow-up to d57bbebe
This commit is contained in:
Andrew Phillips 2014-07-28 09:42:56 -04:00
parent 8ce9f79fde
commit 229fdd70cb
9 changed files with 51 additions and 44 deletions

View File

@ -53,7 +53,7 @@ public class CopyObjectHandler extends ParseSax.HandlerWithResult<ObjectMetadata
this.currentETag = currentOrNull(currentText);
} else if (qName.equals("LastModified")) {
this.currentLastModified = dateParser
.iso8601DateParseWithOptionalTZ(currentOrNull(currentText));
.iso8601DateOrSecondsDateParse(currentOrNull(currentText));
} else if (qName.equals("CopyObjectResult")) {
metadata = new CopyObjectResult(currentLastModified, currentETag);
}

View File

@ -70,7 +70,7 @@ public class ListAllMyBucketsHandler extends ParseSax.HandlerWithResult<Set<Buck
currentName = currentOrNull(currentText);
} else if (qName.equals("CreationDate")) {
currentCreationDate = dateParser
.iso8601DateParseWithOptionalTZ(currentOrNull(currentText));
.iso8601DateOrSecondsDateParse(currentOrNull(currentText));
}
currentText = new StringBuilder();
}

View File

@ -98,7 +98,7 @@ public class ListBucketHandler extends ParseSax.HandlerWithResult<ListBucketResp
builder.uri(uriBuilder(getRequest().getEndpoint()).clearQuery().appendPath(currentKey).build());
} else if (qName.equals("LastModified")) {
builder.lastModified(dateParser
.iso8601DateParseWithOptionalTZ(currentOrNull(currentText)));
.iso8601DateOrSecondsDateParse(currentOrNull(currentText)));
} else if (qName.equals("ETag")) {
String currentETag = currentOrNull(currentText);
builder.eTag(currentETag);

View File

@ -40,7 +40,7 @@ public class CopyObjectHandlerTest extends BaseHandlerTest {
private DateService dateService;
private final String copyObjectXML = "<CopyObjectResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><LastModified>2014-07-23T20:53:17+0000</LastModified><ETag>\"92836a3ea45a6984d1b4d23a747d46bb\"</ETag></CopyObjectResult>";
private final String copyObjectResultWithSecondsDate = "<CopyObjectResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><LastModified>2014-07-23T20:53:17+0000</LastModified><ETag>\"92836a3ea45a6984d1b4d23a747d46bb\"</ETag></CopyObjectResult>";
@BeforeTest
@Override
@ -66,8 +66,8 @@ public class CopyObjectHandlerTest extends BaseHandlerTest {
* Verifies that the parser doesn't barf if the timestamp in the copy object
* xml has time zone designators.
*/
public void testTimeStampWithTZ() {
InputStream is = Strings2.toInputStream(copyObjectXML);
public void testApplyInputStreamWithSecondsDate() {
InputStream is = Strings2.toInputStream(copyObjectResultWithSecondsDate);
ObjectMetadata expected = new CopyObjectResult(
new SimpleDateFormatDateService()
.iso8601SecondsDateParse("2014-07-23T20:53:17+0000"),

View File

@ -49,7 +49,7 @@ import com.google.common.collect.ImmutableList;
@Test(groups = "unit", testName = "ListBucketHandlerTest")
public class ListBucketHandlerTest extends BaseHandlerTest {
public static final String listBucketWithPrefixAppsSlash = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>adriancole.org.jclouds.s3.amazons3testdelimiter</Name><Prefix>apps/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>apps/0</Key><LastModified>2009-05-07T18:27:08.000Z</LastModified><ETag>&quot;c82e6a0025c31c5de5947fda62ac51ab&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/1</Key><LastModified>2009-05-07T18:27:09.000Z</LastModified><ETag>&quot;944fab2c5a9a6bacf07db5e688310d7a&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/2</Key><LastModified>2009-05-07T18:27:09.000Z</LastModified><ETag>&quot;a227b8888045c8fd159fb495214000f0&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/3</Key><LastModified>2009-05-07T18:27:09.000Z</LastModified><ETag>&quot;c9caa76c3dec53e2a192608ce73eef03&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/4</Key><LastModified>2009-05-07T18:27:09.000Z</LastModified><ETag>&quot;1ce5d0dcc6154a647ea90c7bdf82a224&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/5</Key><LastModified>2009-05-07T18:27:09.000Z</LastModified><ETag>&quot;79433524d87462ee05708a8ef894ed55&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/6</Key><LastModified>2009-05-07T18:27:10.000Z</LastModified><ETag>&quot;dd00a060b28ddca8bc5a21a49e306f67&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/7</Key><LastModified>2009-05-07T18:27:10.000Z</LastModified><ETag>&quot;8cd06eca6e819a927b07a285d750b100&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/8</Key><LastModified>2009-05-07T18:27:10.000Z</LastModified><ETag>&quot;174495094d0633b92cbe46603eee6bad&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents><Contents><Key>apps/9</Key><LastModified>2009-05-07T18:27:10.000Z</LastModified><ETag>&quot;cd8a19b26fea8a827276df0ad11c580d&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>";
public static final String listBucketWithTSTimeZone = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>adriancole.org.jclouds.s3.amazons3testdelimiter</Name><Prefix>apps/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>apps/9</Key><LastModified>2014-07-23T20:53:17+0000</LastModified><ETag>&quot;cd8a19b26fea8a827276df0ad11c580d&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>";
public static final String listBucketWithSecondsDate = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>adriancole.org.jclouds.s3.amazons3testdelimiter</Name><Prefix>apps/</Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>apps/9</Key><LastModified>2014-07-23T20:53:17+0000</LastModified><ETag>&quot;cd8a19b26fea8a827276df0ad11c580d&quot;</ETag><Size>8</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListBucketResult>";
public static final String listBucketWithSlashDelimiterAndCommonPrefixApps = "<ListBucketResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"> <Delimiter>/</Delimiter> <CommonPrefixes><Prefix>apps/</Prefix></CommonPrefixes></ListBucketResult>";
private DateService dateService = new SimpleDateFormatDateService();
@ -141,9 +141,9 @@ public class ListBucketHandlerTest extends BaseHandlerTest {
* list bucket response has time zone designators in it.
*/
@Test
public void testListMyBucketsWithTZ() {
public void testListMyBucketsWithSecondsDate() {
ListBucketResponse bucket = createParser().parse(
Strings2.toInputStream(listBucketWithTSTimeZone));
Strings2.toInputStream(listBucketWithSecondsDate));
ObjectMetadata expected = new CopyObjectResult(
new SimpleDateFormatDateService()
.iso8601SecondsDateParse("2014-07-23T20:53:17+0000"),

View File

@ -88,7 +88,7 @@ public interface DateService {
* @return the Date object of the parsed string.
* @throws IllegalArgumentException
*/
Date iso8601DateParseWithOptionalTZ(String toParse)
Date iso8601DateOrSecondsDateParse(String toParse)
throws IllegalArgumentException;
String rfc1123DateFormat(Date date);

View File

@ -172,6 +172,20 @@ public class SimpleDateFormatDateService implements DateService {
}
}
@Override
public Date iso8601DateOrSecondsDateParse(String toParse)
throws IllegalArgumentException {
try {
return iso8601DateParse(toParse);
} catch (IllegalArgumentException orig) {
try {
return iso8601SecondsDateParse(toParse);
} catch (IllegalArgumentException ignored) {
throw orig;
}
}
}
@Override
public String iso8601SecondsDateFormat(Date date) {
synchronized (iso8601SecondsSimpleDateFormat) {
@ -206,18 +220,4 @@ public class SimpleDateFormatDateService implements DateService {
}
}
}
@Override
public Date iso8601DateParseWithOptionalTZ(String toParse)
throws IllegalArgumentException {
try {
return iso8601DateParse(toParse);
} catch (IllegalArgumentException orig) {
try {
return iso8601SecondsDateParse(toParse);
} catch (IllegalArgumentException ie) {
throw orig;
}
}
}
}

View File

@ -110,12 +110,19 @@ public class DateServiceTest extends PerformanceTest {
}
@Test
public void testIso8601OptionalTZDateParse() {
public void testIso8601DateOrSecondsDateParse() {
Date dsDate = dateService
.iso8601DateParseWithOptionalTZ(testData[0].iso8601SecondsDateString);
Date secondsDate = dateService
.iso8601SecondsDateParse(testData[0].iso8601SecondsDateString);
assertEquals(dsDate, secondsDate);
.iso8601DateOrSecondsDateParse(testData[0].iso8601DateString);
assertEquals(dsDate, testData[0].date);
Date dsSecondsDate = dateService
.iso8601DateOrSecondsDateParse(testData[0].iso8601SecondsDateString);
assertEquals(dsSecondsDate, testData[0].date);
}
@Test(expectedExceptions = IllegalArgumentException.class)
public void testIso8601DateOrSecondsDateParseIllegal() {
dateService.iso8601DateOrSecondsDateParse("-1");
}
@Test

View File

@ -131,7 +131,21 @@ public class JodaDateService implements DateService {
toParse = adjustTz(toParse);
return iso8601SecondsDateFormatter.parseDateTime(toParse).toDate();
}
@Override
public Date iso8601DateOrSecondsDateParse(String toParse)
throws IllegalArgumentException {
try {
return iso8601DateParse(toParse);
} catch (IllegalArgumentException orig) {
try {
return iso8601SecondsDateParse(toParse);
} catch (IllegalArgumentException ignored) {
throw orig;
}
}
}
@Override
public final String rfc1123DateFormat(Date dateTime) {
return rfc1123DateFormat.print(new DateTime(dateTime));
@ -146,18 +160,4 @@ public class JodaDateService implements DateService {
public final Date rfc1123DateParse(String toParse) {
return rfc1123DateFormat.parseDateTime(toParse).toDate();
}
@Override
public Date iso8601DateParseWithOptionalTZ(String toParse)
throws IllegalArgumentException {
try {
return iso8601DateParse(toParse);
} catch (IllegalArgumentException orig) {
try {
return iso8601SecondsDateParse(toParse);
} catch (IllegalArgumentException ie) {
throw orig;
}
}
}
}