Repository: incubator-tamaya Updated Branches: refs/heads/master c46ce3652 -> aa5bff158
TAMAYA-91 Implemented converter for periods. Project: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/commit/aa5bff15 Tree: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/tree/aa5bff15 Diff: http://git-wip-us.apache.org/repos/asf/incubator-tamaya/diff/aa5bff15 Branch: refs/heads/master Commit: aa5bff158481fb1225fbeefd7dd31262c0d17544 Parents: c46ce36 Author: Oliver B. Fischer <ple...@apache.org> Authored: Thu Mar 10 22:11:31 2016 +0100 Committer: Oliver B. Fischer <ple...@apache.org> Committed: Thu Mar 10 22:11:44 2016 +0100 ---------------------------------------------------------------------- .../apache/tamaya/jodatime/PeriodConverter.java | 15 +++--- .../org.apache.tamaya.spi.PropertyConverter | 3 +- .../tamaya/jodatime/PeriodConverterIT.java | 51 ++++++++++++++++++++ .../tamaya/jodatime/PeriodConverterTest.java | 40 ++++++++++++--- 4 files changed, 94 insertions(+), 15 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/aa5bff15/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/PeriodConverter.java ---------------------------------------------------------------------- diff --git a/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/PeriodConverter.java b/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/PeriodConverter.java index 6aca863..a350d58 100644 --- a/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/PeriodConverter.java +++ b/sandbox/jodatime/src/main/java/org/apache/tamaya/jodatime/PeriodConverter.java @@ -37,11 +37,9 @@ import java.util.regex.Pattern; * period:</p> * * <ol> - * <li>Alternatice format ({@code Pyyyy-mm-ddThh:mm:ss})</li> + * <li>Alternative format ({@code Pyyyy-mm-ddThh:mm:ss})</li> * <li>ISO format ({@code PyYmMwWdDThHmMsS})</li> * </ol> - * - * */ public class PeriodConverter implements PropertyConverter<org.joda.time.Period> { @@ -60,11 +58,11 @@ public class PeriodConverter implements PropertyConverter<org.joda.time.Period> @Override public Period convert(String value, ConversionContext context) { - if (true == true) throw new RuntimeException("Method must catch up with the current API!"); - String trimmed = Objects.requireNonNull(value).trim(); - MutablePeriod result = null; + addSupportedFormats(context); + + MutablePeriod result = null; PeriodParser format = null; if (isISOFormat(trimmed)) { @@ -85,6 +83,11 @@ public class PeriodConverter implements PropertyConverter<org.joda.time.Period> return result != null ? result.toPeriod() : null; } + private void addSupportedFormats(ConversionContext context) { + context.addSupportedFormats(PeriodConverter.class, "PyYmMwWdDThHmMsS"); + context.addSupportedFormats(PeriodConverter.class, "Pyyyy-mm-ddThh:mm:ss"); + } + private boolean isISOFormat(String value) { return ISO_PATTERN.matcher(value).matches(); } http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/aa5bff15/sandbox/jodatime/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter ---------------------------------------------------------------------- diff --git a/sandbox/jodatime/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter b/sandbox/jodatime/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter index e182a4c..f312e9d 100644 --- a/sandbox/jodatime/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter +++ b/sandbox/jodatime/src/main/resources/META-INF/services/org.apache.tamaya.spi.PropertyConverter @@ -17,4 +17,5 @@ # under the License. # org.apache.tamaya.jodatime.DateTimeConverter -org.apache.tamaya.jodatime.DateTimeZoneConverter \ No newline at end of file +org.apache.tamaya.jodatime.DateTimeZoneConverter +org.apache.tamaya.jodatime.PeriodConverter http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/aa5bff15/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterIT.java ---------------------------------------------------------------------- diff --git a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterIT.java b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterIT.java new file mode 100644 index 0000000..f110754 --- /dev/null +++ b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterIT.java @@ -0,0 +1,51 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.tamaya.jodatime; + + +import org.apache.tamaya.spi.PropertyConverter; +import org.apache.tamaya.spi.ServiceContextManager; +import org.junit.Test; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.instanceOf; +import static org.hamcrest.Matchers.notNullValue; + +public class PeriodConverterIT { + @Test + public void periodConverterCanBeFoundAsServiceProvider() { + List<PropertyConverter> formats = ServiceContextManager.getServiceContext() + .getServices(PropertyConverter.class); + + PropertyConverter<?> converter = null; + + for (PropertyConverter format : formats) { + if (format instanceof PeriodConverter) { + converter = format; + break; + } + } + + assertThat("Converter not found via service context.", converter, notNullValue()); + assertThat(converter, instanceOf(PeriodConverter.class)); + } + +} http://git-wip-us.apache.org/repos/asf/incubator-tamaya/blob/aa5bff15/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterTest.java ---------------------------------------------------------------------- diff --git a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterTest.java b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterTest.java index 23689cd..0affb2c 100644 --- a/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterTest.java +++ b/sandbox/jodatime/src/test/java/org/apache/tamaya/jodatime/PeriodConverterTest.java @@ -18,7 +18,9 @@ */ package org.apache.tamaya.jodatime; +import org.apache.tamaya.TypeLiteral; import org.apache.tamaya.spi.ConversionContext; +import org.joda.time.DateTime; import org.joda.time.Period; import org.joda.time.format.ISOPeriodFormat; import org.joda.time.format.PeriodFormatter; @@ -28,7 +30,10 @@ import org.mockito.Mockito; 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.hasItem; +import static org.hamcrest.Matchers.hasSize; public class PeriodConverterTest { /* @@ -40,7 +45,6 @@ public class PeriodConverterTest { private static PeriodFormatter FORMATTER = ISOPeriodFormat.standard(); - @Ignore @Test public void canConvertPropertiesInAllSupportedFormats() { Object[][] inputResultPairs = { @@ -57,11 +61,7 @@ public class PeriodConverterTest { {"P1YT1S", FORMATTER.parsePeriod("P1Y0M0W0DT0H0M1S")}, // Alternative format - {"P0002-03-00T00:00:05", FORMATTER.parsePeriod("P2Y3M0W0DT0H0M5S")}, - {"P0002-03T00:00:05", FORMATTER.parsePeriod("P2Y3M0W0DT0H0M5S")}, - {"P0002T00:00:05", FORMATTER.parsePeriod("P2Y3M0W0DT0H0M5S")}, - {"P0002T00:05", FORMATTER.parsePeriod("P2Y3M0W0DT0H0M5S")} - + {"P0002-03-00T00:00:05", FORMATTER.parsePeriod("P2Y3M0W0DT0H0M5S")} }; ConversionContext context = Mockito.mock(ConversionContext.class); @@ -74,9 +74,33 @@ public class PeriodConverterTest { } } - @Ignore + @Test + public void invalidInputValuesResultInReturningNull() { + String[] inputValues = { + "P0002-03T00:00:05", + "P0002T00:00:05", + "P0002T00:05" + }; + + ConversionContext context = Mockito.mock(ConversionContext.class); + + for (String input : inputValues) { + Period period = converter.convert(input, context); + + assertThat(period, nullValue()); + } + } + @Test public void allSupportedFormatsAreAddedToTheConversionContext() { - if (true == true) throw new RuntimeException("Method must catch up with the current API!"); + String name = PeriodConverter.class.getSimpleName(); + + ConversionContext context = new ConversionContext.Builder(TypeLiteral.of(Period.class)).build(); + + converter.convert("P7Y0M0W0DT0H0M0S", context); + + assertThat(context.getSupportedFormats(), hasSize(2)); + assertThat(context.getSupportedFormats(), hasItem("PyYmMwWdDThHmMsS (" + name + ")")); + assertThat(context.getSupportedFormats(), hasItem("Pyyyy-mm-ddThh:mm:ss (" + name + ")")); } }