TAMAYA-87 Implemeted support for org.joda.time.DateTimeZone.
Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/80ece983 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/80ece983 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/80ece983 Branch: refs/heads/master Commit: 80ece9835c210c23cb0c5dcec79bb3fb9af8bbab Parents: e4971a1 Author: Oliver B. Fischer <ple...@apache.org> Authored: Sun Mar 6 23:24:01 2016 +0100 Committer: Oliver B. Fischer <ple...@apache.org> Committed: Sun Mar 6 23:24:01 2016 +0100 ---------------------------------------------------------------------- .../tamaya/jodatime/DateTimeZoneConverter.java | 22 +++++++++++++++++--- .../jodatime/DateTimeZoneConverterTest.java | 20 ++++++++++++++---- 2 files changed, 35 insertions(+), 7 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/80ece983/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/DateTimeZoneConverter.java ---------------------------------------------------------------------- diff --git a/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/DateTimeZoneConverter.java b/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/DateTimeZoneConverter.java index bf18393..198a345 100644 --- a/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/DateTimeZoneConverter.java +++ b/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/DateTimeZoneConverter.java @@ -26,14 +26,25 @@ import java.util.regex.Pattern; import static java.util.Objects.requireNonNull; +/** + * Converter, converting from {@code String} to Joda-Time's + * {@code DateTimeZone}. + * + * This converter supports the conversion from a numerich time zone + * information in the format {@code [+-]hh:mm} as well as from + * all time zone ids supported by Joda Time. + * + * @see DateTimeZone + * @see DateTimeZone#getAvailableIDs() + */ public class DateTimeZoneConverter implements PropertyConverter<DateTimeZone> { - private static final Pattern IS_INTEGER_VALUE = Pattern.compile("(\\+|-)?\\d+"); + private static final String PATTERN_REGEX = "(\\+|-)?\\d+"; + private static final Pattern IS_INTEGER_VALUE = Pattern.compile(PATTERN_REGEX); @Override public DateTimeZone convert(String value, ConversionContext context) { - if (true == true) throw new RuntimeException("Method must catch up with the current API!"); - String trimmed = requireNonNull(value).trim(); + addSupportedFormats(context); DateTimeZone result = null; @@ -52,6 +63,11 @@ public class DateTimeZoneConverter implements PropertyConverter<DateTimeZone> { return result; } + private void addSupportedFormats(ConversionContext context) { + context.addSupportedFormats(DateTimeZoneConverter.class, "Time zone in the form [+-]hh:mm via the regex " + PATTERN_REGEX); + context.addSupportedFormats(DateTimeZoneConverter.class, "All time zone ids supported by Joda Time"); + } + private boolean isSingleIntegerValue(String value) { boolean match = IS_INTEGER_VALUE.matcher(value).matches(); http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/80ece983/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeZoneConverterTest.java ---------------------------------------------------------------------- diff --git a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeZoneConverterTest.java b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeZoneConverterTest.java index 6620044..babbe06 100644 --- a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeZoneConverterTest.java +++ b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/DateTimeZoneConverterTest.java @@ -18,7 +18,9 @@ */ package org.apache.tamaya.jodatime; +import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConversionContext; +import org.apache.tamaya.spi.ConversionContext.Builder; import org.joda.time.DateTimeZone; import org.junit.Ignore; import org.junit.Test; @@ -28,11 +30,13 @@ import static org.hamcrest.CoreMatchers.equalTo; import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.contains; +import static org.hamcrest.Matchers.hasItem; +import static org.hamcrest.Matchers.hasSize; public class DateTimeZoneConverterTest { private DateTimeZoneConverter converter = new DateTimeZoneConverter(); - @Ignore @Test public void canConvertDateTimeZoneInformation() { Object[][] inputResultPairs = { @@ -63,7 +67,6 @@ public class DateTimeZoneConverterTest { } } - @Ignore @Test public void invalidInputValuesResultInReturningNull() { String[] inputValues = { @@ -83,10 +86,19 @@ public class DateTimeZoneConverterTest { } } - @Ignore @Test public void allSupportedFormatsAreAddedToTheConversionContext() { - if (true == true) throw new RuntimeException("Method must catch up with the current API!"); + String firstFormat = "Time zone in the form [+-]hh:mm via the regex (\\+|-)?\\d+ (DateTimeZoneConverter)"; + String secondFormat = "All time zone ids supported by Joda Time (DateTimeZoneConverter)"; + + ConversionContext context = new Builder(TypeLiteral.of(DateTimeZone.class)).build(); + + DateTimeZone result = converter.convert("+01:00", context); + + assertThat(result, notNullValue()); + assertThat(context.getSupportedFormats(), hasSize(2)); + assertThat(context.getSupportedFormats(), hasItem(firstFormat)); + assertThat(context.getSupportedFormats(), hasItem(secondFormat)); } }