Repository: wicket Updated Branches: refs/heads/WICKET-6200-converters-for-java-time [created] 8eb01465a
WICKET-6200 Add converters for java.time classes TODO: debug why Offset and Zoned do not work properly Project: http://git-wip-us.apache.org/repos/asf/wicket/repo Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/8eb01465 Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/8eb01465 Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/8eb01465 Branch: refs/heads/WICKET-6200-converters-for-java-time Commit: 8eb01465a556833700cf8514701c53e12f297263 Parents: 0f2d2e6 Author: Martin Tzvetanov Grigorov <[email protected]> Authored: Tue Jul 12 00:18:42 2016 +0200 Committer: Martin Tzvetanov Grigorov <[email protected]> Committed: Tue Jul 12 00:18:42 2016 +0200 ---------------------------------------------------------------------- .../org/apache/wicket/ConverterLocator.java | 14 +++- .../converter/AbstractJavaTimeConverter.java | 88 ++++++++++++++++++++ .../convert/converter/CalendarConverter.java | 8 -- .../convert/converter/LocalDateConverter.java | 49 +++++++++++ .../converter/LocalDateTimeConverter.java | 49 +++++++++++ .../converter/OffsetDateTimeConverter.java | 49 +++++++++++ .../converter/ZonedDateTimeConverter.java | 49 +++++++++++ .../converter/LocalDateConverterTest.java | 46 ++++++++++ .../converter/LocalDateTimeConverterTest.java | 46 ++++++++++ .../converter/OffsetDateTimeConverterTest.java | 49 +++++++++++ .../converter/ZonedDateTimeConverterTest.java | 49 +++++++++++ 11 files changed, 487 insertions(+), 9 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-core/src/main/java/org/apache/wicket/ConverterLocator.java ---------------------------------------------------------------------- diff --git a/wicket-core/src/main/java/org/apache/wicket/ConverterLocator.java b/wicket-core/src/main/java/org/apache/wicket/ConverterLocator.java index ea03de9..9e6086f 100644 --- a/wicket-core/src/main/java/org/apache/wicket/ConverterLocator.java +++ b/wicket-core/src/main/java/org/apache/wicket/ConverterLocator.java @@ -19,6 +19,10 @@ package org.apache.wicket; import java.lang.ref.WeakReference; import java.math.BigDecimal; import java.math.BigInteger; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.OffsetDateTime; +import java.time.ZonedDateTime; import java.util.Calendar; import java.util.Date; import java.util.HashMap; @@ -37,11 +41,15 @@ import org.apache.wicket.util.convert.converter.DateConverter; import org.apache.wicket.util.convert.converter.DoubleConverter; import org.apache.wicket.util.convert.converter.FloatConverter; import org.apache.wicket.util.convert.converter.IntegerConverter; +import org.apache.wicket.util.convert.converter.LocalDateConverter; +import org.apache.wicket.util.convert.converter.LocalDateTimeConverter; import org.apache.wicket.util.convert.converter.LongConverter; +import org.apache.wicket.util.convert.converter.OffsetDateTimeConverter; import org.apache.wicket.util.convert.converter.ShortConverter; import org.apache.wicket.util.convert.converter.SqlDateConverter; import org.apache.wicket.util.convert.converter.SqlTimeConverter; import org.apache.wicket.util.convert.converter.SqlTimestampConverter; +import org.apache.wicket.util.convert.converter.ZonedDateTimeConverter; import org.apache.wicket.util.lang.Args; import org.apache.wicket.util.lang.Objects; @@ -145,7 +153,7 @@ public class ConverterLocator implements IConverterLocator private static final long serialVersionUID = 1L; /** Maps Classes to ITypeConverters. */ - private final Map<String, IConverter<?>> classToConverter = new HashMap<String, IConverter<?>>(); + private final Map<String, IConverter<?>> classToConverter = new HashMap<>(); /** * Constructor @@ -175,6 +183,10 @@ public class ConverterLocator implements IConverterLocator set(java.sql.Time.class, new SqlTimeConverter()); set(java.sql.Timestamp.class, new SqlTimestampConverter()); set(Calendar.class, new CalendarConverter()); + set(LocalDate.class, new LocalDateConverter()); + set(LocalDateTime.class, new LocalDateTimeConverter()); + set(OffsetDateTime.class, new OffsetDateTimeConverter()); + set(ZonedDateTime.class, new ZonedDateTimeConverter()); } /** http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/AbstractJavaTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/AbstractJavaTimeConverter.java b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/AbstractJavaTimeConverter.java new file mode 100644 index 0000000..d870b54 --- /dev/null +++ b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/AbstractJavaTimeConverter.java @@ -0,0 +1,88 @@ +/* + * 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.wicket.util.convert.converter; + +import java.time.format.DateTimeFormatter; +import java.time.temporal.Temporal; +import java.time.temporal.TemporalAccessor; +import java.util.Locale; + +import org.apache.wicket.util.string.Strings; + +/** + * A base class for all java.time.** related converters + * + * @param <T> + * the type of the Temporal that is supported by this converter + */ +public abstract class AbstractJavaTimeConverter<T extends Temporal> extends AbstractConverter<T> +{ + private static final long serialVersionUID = 1L; + + /** + * Creates a new instance of D out of the passed date(time) as long + * @param temporalAccessor + * the date(time) in millis since Epoch + * @return a new instance of the specific type D + */ + protected abstract T createTemporal(TemporalAccessor temporalAccessor); + + @Override + public T convertToObject(final String value, Locale locale) + { + if (Strings.isEmpty(value)) + { + return null; + } + + DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(locale); + TemporalAccessor temporalAccessor = dateTimeFormatter.parse(value); + return createTemporal(temporalAccessor); + } + + @Override + public String convertToString(final T value, final Locale locale) + { + if (value == null) + { + return null; + } + + final DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(locale); + if (dateTimeFormatter != null) + { + return dateTimeFormatter.format(value); + } + return value.toString(); + } + + /** + * @param locale + * @return Returns the date time format. + */ + public DateTimeFormatter getDateTimeFormatter(Locale locale) + { + if (locale == null) + { + locale = Locale.getDefault(); + } + + return getDateTimeFormatter().withLocale(locale); + } + + protected abstract DateTimeFormatter getDateTimeFormatter(); +} http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/CalendarConverter.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/CalendarConverter.java b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/CalendarConverter.java index 162acf7..4d371e6 100644 --- a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/CalendarConverter.java +++ b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/CalendarConverter.java @@ -53,10 +53,6 @@ public class CalendarConverter implements IConverter<Calendar> this.dateConverter = dateConverter; } - /** - * @see org.apache.wicket.util.convert.IConverter#convertToObject(java.lang.String, - * java.util.Locale) - */ @Override public Calendar convertToObject(final String value, final Locale locale) { @@ -71,10 +67,6 @@ public class CalendarConverter implements IConverter<Calendar> return calendar; } - /** - * @see org.apache.wicket.util.convert.IConverter#convertToString(java.lang.Object, - * java.util.Locale) - */ @Override public String convertToString(final Calendar value, final Locale locale) { http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalDateConverter.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalDateConverter.java b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalDateConverter.java new file mode 100644 index 0000000..18952f7 --- /dev/null +++ b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalDateConverter.java @@ -0,0 +1,49 @@ +/* + * 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.wicket.util.convert.converter; + +import java.time.LocalDate; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.time.temporal.TemporalAccessor; + +/** + * Converts from Object to {@link LocalDate}. + */ +public class LocalDateConverter extends AbstractJavaTimeConverter<LocalDate> +{ + private static final long serialVersionUID = 1L; + + private static final DateTimeFormatter SHORT_DATE_TIME_FORMATTER = DateTimeFormatter.ofLocalizedDate(FormatStyle.SHORT); + + @Override + protected Class<LocalDate> getTargetType() + { + return LocalDate.class; + } + + @Override + protected LocalDate createTemporal(TemporalAccessor temporalAccessor) + { + return LocalDate.from(temporalAccessor); + } + + @Override + protected DateTimeFormatter getDateTimeFormatter() { + return SHORT_DATE_TIME_FORMATTER; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalDateTimeConverter.java b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalDateTimeConverter.java new file mode 100644 index 0000000..e32211a --- /dev/null +++ b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/LocalDateTimeConverter.java @@ -0,0 +1,49 @@ +/* + * 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.wicket.util.convert.converter; + +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.FormatStyle; +import java.time.temporal.TemporalAccessor; + +/** + * Converts from Object to {@link java.time.LocalDateTime}. + */ +public class LocalDateTimeConverter extends AbstractJavaTimeConverter<LocalDateTime> +{ + private static final long serialVersionUID = 1L; + + private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM); + + @Override + protected Class<LocalDateTime> getTargetType() + { + return LocalDateTime.class; + } + + @Override + protected LocalDateTime createTemporal(TemporalAccessor temporalAccessor) + { + return LocalDateTime.from(temporalAccessor); + } + + @Override + protected DateTimeFormatter getDateTimeFormatter() { + return DATE_TIME_FORMATTER; + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverter.java b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverter.java new file mode 100644 index 0000000..88d8216 --- /dev/null +++ b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverter.java @@ -0,0 +1,49 @@ +/* + * 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.wicket.util.convert.converter; + +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAccessor; + +/** + * Converts from Object to {@link OffsetDateTime}. + */ +public class OffsetDateTimeConverter extends AbstractJavaTimeConverter<OffsetDateTime> +{ + private static final long serialVersionUID = 1L; + + private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ISO_OFFSET_DATE_TIME; + + @Override + protected Class<OffsetDateTime> getTargetType() + { + return OffsetDateTime.class; + } + + @Override + protected OffsetDateTime createTemporal(TemporalAccessor temporalAccessor) + { + return OffsetDateTime.from(temporalAccessor); + } + + @Override + protected DateTimeFormatter getDateTimeFormatter() { + return DATE_TIME_FORMATTER.withZone(ZoneOffset.UTC); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverter.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverter.java b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverter.java new file mode 100644 index 0000000..b9d372f --- /dev/null +++ b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverter.java @@ -0,0 +1,49 @@ +/* + * 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.wicket.util.convert.converter; + +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.time.format.DateTimeFormatter; +import java.time.temporal.TemporalAccessor; + +/** + * Converts from Object to {@link ZonedDateTime}. + */ +public class ZonedDateTimeConverter extends AbstractJavaTimeConverter<ZonedDateTime> +{ + private static final long serialVersionUID = 1L; + + private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ISO_ZONED_DATE_TIME; + + @Override + protected Class<ZonedDateTime> getTargetType() + { + return ZonedDateTime.class; + } + + @Override + protected ZonedDateTime createTemporal(TemporalAccessor temporalAccessor) + { + return ZonedDateTime.from(temporalAccessor); + } + + @Override + protected DateTimeFormatter getDateTimeFormatter() { + return DATE_TIME_FORMATTER.withZone(ZoneOffset.UTC); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalDateConverterTest.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalDateConverterTest.java b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalDateConverterTest.java new file mode 100644 index 0000000..5fceaf6 --- /dev/null +++ b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalDateConverterTest.java @@ -0,0 +1,46 @@ +/* + * 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.wicket.util.convert.converter; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +import java.time.LocalDate; +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link LocalDateConverter} + */ +public class LocalDateConverterTest extends Assert +{ + @Test + public void convertToString() { + LocalDateConverter converter = new LocalDateConverter(); + String date = converter.convertToString(LocalDate.of(2016, 7, 11), Locale.ENGLISH); + assertThat(date, is(equalTo("7/11/16"))); + } + + @Test + public void convertToObject() { + LocalDateConverter converter = new LocalDateConverter(); + LocalDate date = converter.convertToObject("7/11/16", Locale.ENGLISH); + assertThat(date, is(equalTo(LocalDate.of(2016, 7, 11)))); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalDateTimeConverterTest.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalDateTimeConverterTest.java b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalDateTimeConverterTest.java new file mode 100644 index 0000000..05a037e --- /dev/null +++ b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalDateTimeConverterTest.java @@ -0,0 +1,46 @@ +/* + * 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.wicket.util.convert.converter; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +import java.time.LocalDateTime; +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link LocalDateTimeConverter} + */ +public class LocalDateTimeConverterTest extends Assert +{ + @Test + public void convertToString() { + LocalDateTimeConverter converter = new LocalDateTimeConverter(); + String date = converter.convertToString(LocalDateTime.of(2016, 7, 11, 1, 2, 3), Locale.ENGLISH); + assertThat(date, is(equalTo("Jul 11, 2016 1:02:03 AM"))); + } + + @Test + public void convertToObject() { + LocalDateTimeConverter converter = new LocalDateTimeConverter(); + LocalDateTime date = converter.convertToObject("Jul 11, 2016 1:02:03 AM", Locale.ENGLISH); + assertThat(date, is(equalTo(LocalDateTime.of(2016, 7, 11, 1, 2, 3)))); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverterTest.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverterTest.java b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverterTest.java new file mode 100644 index 0000000..a332579 --- /dev/null +++ b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/OffsetDateTimeConverterTest.java @@ -0,0 +1,49 @@ +/* + * 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.wicket.util.convert.converter; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +import java.time.OffsetDateTime; +import java.time.ZoneOffset; +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link OffsetDateTimeConverter} + */ +public class OffsetDateTimeConverterTest extends Assert +{ + @Test + public void convertToString() { + OffsetDateTimeConverter converter = new OffsetDateTimeConverter(); + OffsetDateTime offsetDateTime = OffsetDateTime.of(2016, 7, 11, 1, 2, 3, 0, ZoneOffset.ofHours(2)); + String dateTime = converter.convertToString(offsetDateTime, Locale.ENGLISH); + assertThat(dateTime, is(equalTo("2016-07-10T23:02:03Z"))); + } + + @Test + public void convertToObject() { + OffsetDateTimeConverter converter = new OffsetDateTimeConverter(); + OffsetDateTime dateTime = converter.convertToObject("2016-07-10T23:02:03+02:00", Locale.ENGLISH); + OffsetDateTime offsetDateTime = OffsetDateTime.of(2016, 7, 11, 1, 2, 3, 0, ZoneOffset.ofHours(2)); + assertThat(dateTime, is(equalTo(offsetDateTime))); + } +} http://git-wip-us.apache.org/repos/asf/wicket/blob/8eb01465/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverterTest.java ---------------------------------------------------------------------- diff --git a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverterTest.java b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverterTest.java new file mode 100644 index 0000000..72ac395 --- /dev/null +++ b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverterTest.java @@ -0,0 +1,49 @@ +/* + * 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.wicket.util.convert.converter; + +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.is; + +import java.time.ZoneOffset; +import java.time.ZonedDateTime; +import java.util.Locale; + +import org.junit.Assert; +import org.junit.Test; + +/** + * Tests for {@link ZonedDateTimeConverter} + */ +public class ZonedDateTimeConverterTest extends Assert +{ + @Test + public void convertToString() { + ZonedDateTimeConverter converter = new ZonedDateTimeConverter(); + ZonedDateTime zonedDateTime = ZonedDateTime.of(2016, 7, 11, 1, 2, 3, 0, ZoneOffset.ofHours(2)); + String dateTime = converter.convertToString(zonedDateTime, Locale.ENGLISH); + assertThat(dateTime, is(equalTo("2016-07-10T23:02:03Z"))); + } + + @Test + public void convertToObject() { + ZonedDateTimeConverter converter = new ZonedDateTimeConverter(); + ZonedDateTime dateTime = converter.convertToObject("2016-07-10T23:02:03Z", Locale.ENGLISH); + ZonedDateTime offsetDateTime = ZonedDateTime.of(2016, 7, 11, 1, 2, 3, 0, ZoneOffset.ofHours(2)); + assertThat(dateTime, is(equalTo(offsetDateTime))); + } +}
