You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@wicket.apache.org by sv...@apache.org on 2017/10/07 08:00:35 UTC
wicket git commit: WICKET-6200 handle DateTimeParseException;
support ZonedDateTime
Repository: wicket
Updated Branches:
refs/heads/WICKET-6105-java.time 06655a6b0 -> 559796c40
WICKET-6200 handle DateTimeParseException; support ZonedDateTime
Project: http://git-wip-us.apache.org/repos/asf/wicket/repo
Commit: http://git-wip-us.apache.org/repos/asf/wicket/commit/559796c4
Tree: http://git-wip-us.apache.org/repos/asf/wicket/tree/559796c4
Diff: http://git-wip-us.apache.org/repos/asf/wicket/diff/559796c4
Branch: refs/heads/WICKET-6105-java.time
Commit: 559796c40ee10560a25226ef5647164ef5714d8b
Parents: 06655a6
Author: Sven Meier <sv...@apache.org>
Authored: Sat Oct 7 09:57:21 2017 +0200
Committer: Sven Meier <sv...@apache.org>
Committed: Sat Oct 7 09:59:47 2017 +0200
----------------------------------------------------------------------
.../converter/AbstractJavaTimeConverter.java | 10 +++-
.../converter/ZonedDateTimeConverter.java | 50 ++++++++++++++++
.../converter/LocalDateConverterTest.java | 11 ++++
.../converter/LocalDateTimeConverterTest.java | 11 ++++
.../converter/LocalTimeConverterTest.java | 11 ++++
.../converter/ZonedDateTimeConverterTest.java | 61 ++++++++++++++++++++
6 files changed, 153 insertions(+), 1 deletion(-)
----------------------------------------------------------------------
http://git-wip-us.apache.org/repos/asf/wicket/blob/559796c4/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
index d870b54..23e5e77 100644
--- 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
@@ -17,6 +17,7 @@
package org.apache.wicket.util.convert.converter;
import java.time.format.DateTimeFormatter;
+import java.time.format.DateTimeParseException;
import java.time.temporal.Temporal;
import java.time.temporal.TemporalAccessor;
import java.util.Locale;
@@ -50,7 +51,14 @@ public abstract class AbstractJavaTimeConverter<T extends Temporal> extends Abst
}
DateTimeFormatter dateTimeFormatter = getDateTimeFormatter(locale);
- TemporalAccessor temporalAccessor = dateTimeFormatter.parse(value);
+
+ TemporalAccessor temporalAccessor;
+ try {
+ temporalAccessor = dateTimeFormatter.parse(value);
+ } catch (DateTimeParseException ex) {
+ throw newConversionException("Cannot parse '" + value, value, locale);
+ }
+
return createTemporal(temporalAccessor);
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/559796c4/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..2972b20
--- /dev/null
+++ b/wicket-util/src/main/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverter.java
@@ -0,0 +1,50 @@
+/*
+ * 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.ZoneId;
+import java.time.ZonedDateTime;
+import java.time.format.DateTimeFormatter;
+import java.time.format.FormatStyle;
+import java.time.temporal.TemporalAccessor;
+
+/**
+ * Converts to {@link java.time.ZonedDateTime}.
+ */
+public class ZonedDateTimeConverter extends AbstractJavaTimeConverter<ZonedDateTime>
+{
+ private static final long serialVersionUID = 1L;
+
+ private static final DateTimeFormatter DATE_TIME_FORMATTER = DateTimeFormatter.ofLocalizedDateTime(FormatStyle.MEDIUM);
+
+ @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(ZoneId.systemDefault());
+ }
+}
http://git-wip-us.apache.org/repos/asf/wicket/blob/559796c4/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
index 5fceaf6..4058e32 100644
--- 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
@@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.is;
import java.time.LocalDate;
import java.util.Locale;
+import org.apache.wicket.util.convert.ConversionException;
import org.junit.Assert;
import org.junit.Test;
@@ -43,4 +44,14 @@ public class LocalDateConverterTest extends Assert
LocalDate date = converter.convertToObject("7/11/16", Locale.ENGLISH);
assertThat(date, is(equalTo(LocalDate.of(2016, 7, 11))));
}
+
+ @Test
+ public void convertFails() {
+ LocalDateConverter converter = new LocalDateConverter();
+
+ try {
+ converter.convertToObject("aaa", Locale.ENGLISH);
+ } catch (ConversionException expected) {
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/559796c4/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
index 05a037e..f9dc8fc 100644
--- 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
@@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.is;
import java.time.LocalDateTime;
import java.util.Locale;
+import org.apache.wicket.util.convert.ConversionException;
import org.junit.Assert;
import org.junit.Test;
@@ -43,4 +44,14 @@ public class LocalDateTimeConverterTest extends Assert
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))));
}
+
+ @Test
+ public void convertFails() {
+ LocalDateTimeConverter converter = new LocalDateTimeConverter();
+
+ try {
+ converter.convertToObject("aaa", Locale.ENGLISH);
+ } catch (ConversionException expected) {
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/559796c4/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalTimeConverterTest.java
----------------------------------------------------------------------
diff --git a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalTimeConverterTest.java b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalTimeConverterTest.java
index fcfdaf4..7c36b20 100644
--- a/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalTimeConverterTest.java
+++ b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/LocalTimeConverterTest.java
@@ -22,6 +22,7 @@ import static org.hamcrest.Matchers.is;
import java.time.LocalTime;
import java.util.Locale;
+import org.apache.wicket.util.convert.ConversionException;
import org.junit.Assert;
import org.junit.Test;
@@ -43,4 +44,14 @@ public class LocalTimeConverterTest extends Assert
LocalTime time = converter.convertToObject("01:02:03", Locale.ENGLISH);
assertThat(time, is(equalTo(LocalTime.of(1, 2, 3))));
}
+
+ @Test
+ public void convertFails() {
+ LocalTimeConverter converter = new LocalTimeConverter();
+
+ try {
+ converter.convertToObject("aaa", Locale.ENGLISH);
+ } catch (ConversionException expected) {
+ }
+ }
}
http://git-wip-us.apache.org/repos/asf/wicket/blob/559796c4/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..8d946c5
--- /dev/null
+++ b/wicket-util/src/test/java/org/apache/wicket/util/convert/converter/ZonedDateTimeConverterTest.java
@@ -0,0 +1,61 @@
+/*
+ * 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.ZoneId;
+import java.time.ZonedDateTime;
+import java.util.Locale;
+
+import org.apache.wicket.util.convert.ConversionException;
+import org.junit.Assert;
+import org.junit.Test;
+
+/**
+ * Tests for {@link ZonedDateTimeConverter}
+ */
+public class ZonedDateTimeConverterTest extends Assert
+{
+ private ZoneId zone = ZoneId.systemDefault();
+
+ @Test
+ public void convertToString() {
+ ZonedDateTimeConverter converter = new ZonedDateTimeConverter();
+ String date = converter.convertToString(ZonedDateTime.of(2016, 7, 11, 1, 2, 3, 0, zone), Locale.ENGLISH);
+ assertThat(date, is(equalTo("Jul 11, 2016 1:02:03 AM")));
+ }
+
+ @Test
+ public void convertToObject() {
+ ZonedDateTimeConverter converter = new ZonedDateTimeConverter();
+ ZoneId zone = ZoneId.systemDefault();
+ ZonedDateTime date = converter.convertToObject("Jul 11, 2016 1:02:03 AM", Locale.ENGLISH);
+ assertThat(date, is(equalTo(ZonedDateTime.of(2016, 7, 11, 1, 2, 3, 0, zone))));
+ }
+
+ @Test
+ public void convertFails() {
+ ZonedDateTimeConverter converter = new ZonedDateTimeConverter();
+
+ try {
+ converter.convertToObject("aaa", Locale.ENGLISH);
+ } catch (ConversionException expected) {
+ }
+ }
+}