From b31d3be8289a346956b8fdd1e8b08b275b4b0d37 Mon Sep 17 00:00:00 2001 From: Andrei Solntsev Date: Sun, 3 Jul 2016 01:03:38 +0300 Subject: [PATCH] #2032 Simplify some Binders to throws ParseException without unneeded wrapping + add missing unit-tests --- .../data/binding/types/CalendarBinder.java | 22 +++----- .../play/data/binding/types/DateBinder.java | 13 ++--- .../data/binding/types/DateTimeBinder.java | 3 +- .../binding/types/CalendarBinderTest.java | 46 ++++++++++++++++ .../data/binding/types/DateBinderTest.java | 55 +++++++++++++++++++ 5 files changed, 116 insertions(+), 23 deletions(-) create mode 100644 framework/test-src/play/data/binding/types/CalendarBinderTest.java create mode 100644 framework/test-src/play/data/binding/types/DateBinderTest.java diff --git a/framework/src/play/data/binding/types/CalendarBinder.java b/framework/src/play/data/binding/types/CalendarBinder.java index 6ad0fd32ac..cfa2abcb68 100644 --- a/framework/src/play/data/binding/types/CalendarBinder.java +++ b/framework/src/play/data/binding/types/CalendarBinder.java @@ -18,24 +18,20 @@ public class CalendarBinder implements TypeBinder { @Override - public Calendar bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws Exception { + public Calendar bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws ParseException { if (value == null || value.trim().length() == 0) { return null; } Calendar cal = Calendar.getInstance(Lang.getLocale()); - try { - Date date = AnnotationHelper.getDateAs(annotations, value); - if (date != null) { - cal.setTime(date); - } else { - SimpleDateFormat sdf = new SimpleDateFormat(I18N.getDateFormat()); - sdf.setLenient(false); - cal.setTime(sdf.parse(value)); - } - } catch (ParseException e) { - throw new IllegalArgumentException("Cannot convert [" + value + "] to a Calendar: " + e.toString()); - } + Date date = AnnotationHelper.getDateAs(annotations, value); + if (date != null) { + cal.setTime(date); + } else { + SimpleDateFormat sdf = new SimpleDateFormat(I18N.getDateFormat()); + sdf.setLenient(false); + cal.setTime(sdf.parse(value)); + } return cal; } } diff --git a/framework/src/play/data/binding/types/DateBinder.java b/framework/src/play/data/binding/types/DateBinder.java index 5fd10f6083..9595c56dfb 100644 --- a/framework/src/play/data/binding/types/DateBinder.java +++ b/framework/src/play/data/binding/types/DateBinder.java @@ -17,7 +17,7 @@ public class DateBinder implements TypeBinder { public static final String ISO8601 = "'ISO8601:'yyyy-MM-dd'T'HH:mm:ssZ"; @Override - public Date bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws Exception { + public Date bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws ParseException { if (value == null || value.trim().length() == 0) { return null; } @@ -35,13 +35,8 @@ public Date bind(String name, Annotation[] annotations, String value, Class actu // Ignore } - try { - SimpleDateFormat sdf = new SimpleDateFormat(ISO8601); - sdf.setLenient(false); - return sdf.parse(value); - } catch (Exception e) { - throw new IllegalArgumentException("Cannot convert [" + value + "] to a Date: " + e.toString()); - } - + SimpleDateFormat sdf = new SimpleDateFormat(ISO8601); + sdf.setLenient(false); + return sdf.parse(value); } } diff --git a/framework/src/play/data/binding/types/DateTimeBinder.java b/framework/src/play/data/binding/types/DateTimeBinder.java index 3fb6d1ecea..ea8ab4da80 100644 --- a/framework/src/play/data/binding/types/DateTimeBinder.java +++ b/framework/src/play/data/binding/types/DateTimeBinder.java @@ -2,6 +2,7 @@ import java.lang.annotation.Annotation; import java.lang.reflect.Type; +import java.text.ParseException; import org.joda.time.DateTime; @@ -15,7 +16,7 @@ public class DateTimeBinder implements TypeBinder { private static DateBinder dateBinder = new DateBinder(); @Override - public DateTime bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws Exception { + public DateTime bind(String name, Annotation[] annotations, String value, Class actualClass, Type genericType) throws ParseException { if (value == null || value.trim().length() == 0) { return null; } diff --git a/framework/test-src/play/data/binding/types/CalendarBinderTest.java b/framework/test-src/play/data/binding/types/CalendarBinderTest.java new file mode 100644 index 0000000000..4d60054c6b --- /dev/null +++ b/framework/test-src/play/data/binding/types/CalendarBinderTest.java @@ -0,0 +1,46 @@ +package play.data.binding.types; + +import org.junit.Before; +import org.junit.Test; +import play.Play; +import play.PlayBuilder; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Calendar; +import java.util.Date; + +import static org.junit.Assert.*; + +public class CalendarBinderTest { + + private CalendarBinder binder = new CalendarBinder(); + + @Before + public void setup() { + new PlayBuilder().build(); + } + + @Test + public void parses_date_to_calendar() throws ParseException { + Play.configuration.setProperty("date.format", "dd.MM.yyyy"); + Date expected = new SimpleDateFormat("dd.MM.yyyy").parse("31.12.1986"); + Calendar actual = binder.bind("client.birthday", null, "31.12.1986", Calendar.class, null); + assertEquals(expected, actual.getTime()); + } + + @Test + public void parses_null_to_null() throws ParseException { + assertNull(binder.bind("client.birthday", null, null, Calendar.class, null)); + } + + @Test + public void parses_empty_string_to_null() throws ParseException { + assertNull(binder.bind("client.birthday", null, "", Calendar.class, null)); + } + + @Test(expected = ParseException.class) + public void throws_ParseException_for_invalid_value() throws ParseException { + binder.bind("client.birthday", null, "12/31/1986", Calendar.class, null); + } +} \ No newline at end of file diff --git a/framework/test-src/play/data/binding/types/DateBinderTest.java b/framework/test-src/play/data/binding/types/DateBinderTest.java new file mode 100644 index 0000000000..da76ca3746 --- /dev/null +++ b/framework/test-src/play/data/binding/types/DateBinderTest.java @@ -0,0 +1,55 @@ +package play.data.binding.types; + +import org.junit.Before; +import org.junit.Test; +import play.Play; +import play.PlayBuilder; + +import java.text.ParseException; +import java.text.SimpleDateFormat; +import java.util.Date; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNull; + +public class DateBinderTest { + private DateBinder binder = new DateBinder(); + + @Before + public void setup() { + new PlayBuilder().build(); + } + + @Test + public void parses_date_in_play_format() throws ParseException { + Play.configuration.setProperty("date.format", "dd.MM.yyyy"); + + Date actual = binder.bind("client.birthday", null, "31.12.1986", Date.class, null); + assertEquals(date("12/31/1986+0500"), actual); + } + + @Test + public void parses_date_in_iso_format() throws ParseException { + Date actual = binder.bind("client.birthday", null, "ISO8601:1986-04-12T00:00:00+0500", Date.class, null); + assertEquals(date("12.04.1986+0500"), actual); + } + + @Test + public void parses_null_to_null() throws ParseException { + assertNull(binder.bind("client.birthday", null, null, Date.class, null)); + } + + @Test + public void parses_empty_string_to_null() throws ParseException { + assertNull(binder.bind("client.birthday", null, "", Date.class, null)); + } + + @Test(expected = ParseException.class) + public void throws_ParseException_for_invalid_value() throws ParseException { + binder.bind("client.birthday", null, "12/31/1986", Date.class, null); + } + + private Date date(String date) throws ParseException { + return new SimpleDateFormat("MM/dd/yyyyZ").parse(date); + } +} \ No newline at end of file