1 | package net.sourceforge.calendardate; |
2 | |
3 | import java.text.ParseException; |
4 | |
5 | import junit.framework.TestCase; |
6 | import net.sourceforge.calendardate.CalendarDate; |
7 | import net.sourceforge.calendardate.CalendarDateFormat; |
8 | |
9 | public class CalendarDateFormatTestCase extends TestCase { |
10 | |
11 | public void testFormat() { |
12 | CalendarDateFormat shortFmt = new CalendarDateFormat("dd-MMM-yyyy"); |
13 | CalendarDate june12 = new CalendarDate(2004, 6, 12); |
14 | assertEquals("12-Jun-2004", shortFmt.format(june12)); |
15 | |
16 | CalendarDateFormat longFmt = new CalendarDateFormat("EEEEE, MMMM dd yyyy"); |
17 | assertEquals("Saturday, June 12 2004", longFmt.format(june12)); |
18 | } |
19 | |
20 | public void testParse() throws Exception { |
21 | CalendarDateFormat shortFmt = new CalendarDateFormat("dd-MMM-yyyy"); |
22 | CalendarDate june12 = new CalendarDate(2004, 6, 12); |
23 | assertEquals(june12, shortFmt.parse("12-Jun-2004")); |
24 | |
25 | CalendarDateFormat longFmt = new CalendarDateFormat("EEEEE, MMMM dd yyyy"); |
26 | assertEquals(june12, longFmt.parse("Saturday, June 12 2004")); |
27 | } |
28 | |
29 | public void testParsingDateOutOfRangeThrowsParseException() throws Exception { |
30 | checkCannotParse("29-Dec-1599"); |
31 | checkCannotParse("01-Jan-3000"); |
32 | } |
33 | |
34 | private void checkCannotParse(String dateStr) { |
35 | CalendarDateFormat shortFmt = new CalendarDateFormat("dd-MMM-yyyy"); |
36 | try { |
37 | shortFmt.parse(dateStr); |
38 | fail("Should not be able to parse date: " + dateStr); |
39 | } |
40 | catch (ParseException e) { |
41 | // Expected |
42 | } |
43 | } |
44 | |
45 | public void testParseIsNotLenient() throws Exception { |
46 | checkCannotParse("32-Jan-2004"); |
47 | } |
48 | |
49 | /** TODO: Look into why does this format is accepted by SimpleDateFormat */ |
50 | public void xxxtestCannotParseBadFormat() throws Exception { |
51 | checkCannotParse("31-Jan-2004f"); |
52 | } |
53 | |
54 | } |