Skip to content

Commit

Permalink
#2032 Simplify some Binders to throws ParseException without unneeded…
Browse files Browse the repository at this point in the history
… wrapping

+ add missing unit-tests
  • Loading branch information
asolntsev committed Jul 2, 2016
1 parent 28b39a9 commit b31d3be
Show file tree
Hide file tree
Showing 5 changed files with 116 additions and 23 deletions.
22 changes: 9 additions & 13 deletions framework/src/play/data/binding/types/CalendarBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,24 +18,20 @@
public class CalendarBinder implements TypeBinder<Calendar> {

@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;
}
}
13 changes: 4 additions & 9 deletions framework/src/play/data/binding/types/DateBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ public class DateBinder implements TypeBinder<Date> {
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;
}
Expand All @@ -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);
}
}
3 changes: 2 additions & 1 deletion framework/src/play/data/binding/types/DateTimeBinder.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import java.lang.annotation.Annotation;
import java.lang.reflect.Type;
import java.text.ParseException;

import org.joda.time.DateTime;

Expand All @@ -15,7 +16,7 @@ public class DateTimeBinder implements TypeBinder<DateTime> {
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;
}
Expand Down
46 changes: 46 additions & 0 deletions framework/test-src/play/data/binding/types/CalendarBinderTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
55 changes: 55 additions & 0 deletions framework/test-src/play/data/binding/types/DateBinderTest.java
Original file line number Diff line number Diff line change
@@ -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);
}
}

0 comments on commit b31d3be

Please sign in to comment.