-
Notifications
You must be signed in to change notification settings - Fork 680
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
#2032 Simplify some Binders to throws ParseException without unneeded…
… wrapping + add missing unit-tests
- Loading branch information
Showing
5 changed files
with
116 additions
and
23 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
46 changes: 46 additions & 0 deletions
46
framework/test-src/play/data/binding/types/CalendarBinderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
55
framework/test-src/play/data/binding/types/DateBinderTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |