|
1 package hirondelle.web4j.model; |
|
2 |
|
3 import java.text.ParseException; |
|
4 import java.text.SimpleDateFormat; |
|
5 import java.util.Arrays; |
|
6 import java.util.Date; |
|
7 import java.util.List; |
|
8 import java.util.Locale; |
|
9 |
|
10 import junit.framework.TestCase; |
|
11 |
|
12 /** JUnit tests. */ |
|
13 public final class TESTDateTimeFormatter extends TestCase { |
|
14 |
|
15 /** Run the test cases. */ |
|
16 public static void main(String args[]) { |
|
17 String[] testCaseName = { TESTDateTimeFormatter.class.getName() }; |
|
18 junit.textui.TestRunner.main(testCaseName); |
|
19 } |
|
20 |
|
21 public TESTDateTimeFormatter(String aName) { |
|
22 super(aName); |
|
23 } |
|
24 |
|
25 // TEST CASES // |
|
26 |
|
27 public void testSpeed(){ |
|
28 //.046 |
|
29 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYY-MM-DD h12:mm:ss", "2009-10-28 1:59:01"); |
|
30 } |
|
31 |
|
32 public void testSpeedJDK() throws ParseException { |
|
33 String dateTime = "2009-10-28 01:59:01"; |
|
34 //.063 - slower than my classes - at least in this simple test |
|
35 SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss"); |
|
36 Date result = format.parse(dateTime); |
|
37 String dateResurrected = format.format(result); |
|
38 } |
|
39 |
|
40 public void testDate(){ |
|
41 testDate(SUCCESS, "2009-10-28", "YYYY-MM-DD", "2009-10-28"); |
|
42 testDate(SUCCESS, "2009-01-28", "YYYY-MM-DD", "2009-01-28"); |
|
43 testDate(SUCCESS, "2009-12-01", "YYYY-MM-DD", "2009-12-01"); |
|
44 testDate(SUCCESS, "2009-01-01", "YYYY-MM-DD", "2009-01-01"); |
|
45 |
|
46 testDate(SUCCESS, "2009-10-28", "YYYY-M-D", "2009-10-28"); |
|
47 testDate(SUCCESS, "2009-10-03", "YYYY-M-D", "2009-10-3"); |
|
48 testDate(SUCCESS, "2009-05-28", "YYYY-M-D", "2009-5-28"); |
|
49 |
|
50 testDate(SUCCESS, "2009-10-28", "YYYY-MM-D", "2009-10-28"); |
|
51 testDate(SUCCESS, "2009-10-03", "YYYY-MM-D", "2009-10-3"); |
|
52 testDate(SUCCESS, "2009-05-28", "YYYY-MM-D", "2009-05-28"); |
|
53 |
|
54 testDate(SUCCESS, "2009-10-28", "YYYY-M-DD", "2009-10-28"); |
|
55 testDate(SUCCESS, "2009-10-03", "YYYY-M-DD", "2009-10-03"); |
|
56 testDate(SUCCESS, "2009-05-28", "YYYY-M-DD", "2009-5-28"); |
|
57 |
|
58 testDate(SUCCESS, "2009-10-28", "YY-M-DD", "09-10-28"); |
|
59 testDate(SUCCESS, "2009-10-03", "YY-M-DD", "09-10-03"); |
|
60 testDate(SUCCESS, "2099-05-28", "YY-M-DD", "99-5-28"); |
|
61 |
|
62 testDate(SUCCESS, "2099-05-28", "YYYYMMDD", "20990528"); |
|
63 testDate(SUCCESS, "2099-11-28", "WWW MMM DD, YYYY", Locale.CANADA, "Sat Nov 28, 2099"); |
|
64 testDate(SUCCESS, "2099-11-28", "WWW-DD|th|", Locale.CANADA, "Sat-28th"); |
|
65 |
|
66 testDate(SUCCESS, "2009-12-31", "MM-DD-YYYY", "12-31-2009"); |
|
67 |
|
68 testDate(SUCCESS, "2009-12-31", "***YYYY-MM-DD", "***2009-12-31"); |
|
69 testDate(SUCCESS, "2009-12-31", "***YYYY-MM-DD*", "***2009-12-31*"); |
|
70 testDate(SUCCESS, "2009-12-31", "* YYYY-MM-DD *", "* 2009-12-31 *"); |
|
71 |
|
72 testDate(SUCCESS, "2009-12-31", "WWWW, MMM D, YYYY", Locale.CANADA, "Thursday, Dec 31, 2009"); |
|
73 testDate(SUCCESS, "2009-12-31", "WWW, MMM D, YYYY", Locale.CANADA, "Thu, Dec 31, 2009"); |
|
74 |
|
75 testDate(SUCCESS, "01:59:59", "hh:mm:ss", "01:59:59"); |
|
76 testDate(SUCCESS, "01:59:59", "h:mm:ss", "1:59:59"); |
|
77 testDate(SUCCESS, "01:59:59", "hh:m:ss", "01:59:59"); |
|
78 testDate(SUCCESS, "01:01:59", "hh:m:ss", "01:1:59"); |
|
79 testDate(SUCCESS, "01:59:59", "hh:mm:s", "01:59:59"); |
|
80 testDate(SUCCESS, "01:59:01", "hh:mm:s", "01:59:1"); |
|
81 testDate(SUCCESS, "01:59:01.123456789", "hh:mm:ss", "01:59:01"); |
|
82 testDate(SUCCESS, "01:59:01.123456789", "hh:mm:ss.f", "01:59:01.1"); |
|
83 testDate(SUCCESS, "01:59:01.123456789", "hh:mm:ss.ff", "01:59:01.12"); |
|
84 testDate(SUCCESS, "01:59:01.123456789", "hh:mm:ss.fff", "01:59:01.123"); |
|
85 testDate(SUCCESS, "01:59:01.123456789", "hh:mm:ss.ffff", "01:59:01.1234"); |
|
86 testDate(SUCCESS, "01:59:01.123456789", "hh:mm:ss.fffff", "01:59:01.12345"); |
|
87 testDate(SUCCESS, "01:59:01.123456789", "hh:mm:ss.ffffff", "01:59:01.123456"); |
|
88 testDate(SUCCESS, "01:59:01.123456789", "hh:mm:ss.fffffff", "01:59:01.1234567"); |
|
89 testDate(SUCCESS, "01:59:01.123456789", "hh:mm:ss.ffffffff", "01:59:01.12345678"); |
|
90 testDate(SUCCESS, "01:59:01.123456789", "hh:mm:ss.fffffffff", "01:59:01.123456789"); |
|
91 |
|
92 testDate(SUCCESS, "01:59:01", "hh", "01"); |
|
93 testDate(SUCCESS, "01:59:01", "mm", "59"); |
|
94 testDate(SUCCESS, "01:59:01", "ss", "01"); |
|
95 testDate(SUCCESS, "01:59:01", "hh ", "01 "); |
|
96 testDate(SUCCESS, "01:59:01", "hh:mm", "01:59"); |
|
97 testDate(SUCCESS, "01:59:01", "h:mm", "1:59"); |
|
98 testDate(SUCCESS, "01:59:01", "mm:ss", "59:01"); |
|
99 testDate(SUCCESS, "01:59:01", "mm:s", "59:1"); |
|
100 |
|
101 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYY-MM-DD hh:mm:ss", "2009-10-28 01:59:01"); |
|
102 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYYMMDDThh:mm:ss", "20091028T01:59:01"); |
|
103 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYY-MMM-DD hh:mm:ss", Locale.CANADA, "2009-Oct-28 01:59:01"); |
|
104 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYY-MMM-DD", Locale.CANADA, "2009-Oct-28"); |
|
105 testDate(SUCCESS, "2009-10-28 01:59:01", ":hh:mm:ss:", ":01:59:01:"); |
|
106 testDate(SUCCESS, "2009-04-28 13:59:01", "DD MMM, YYYY hh:mm:ss", Locale.CANADA_FRENCH, "28 avr, 2009 13:59:01"); |
|
107 |
|
108 |
|
109 testDate(SUCCESS, "01:59:01.01", "hh:mm:ss.ff", "01:59:01.01"); |
|
110 testDate(SUCCESS, "01:59:01.01", "hh:mm:ss.fff", "01:59:01.010"); |
|
111 testDate(SUCCESS, "01:59:01.01", "hh:mm:ss.ffff", "01:59:01.0100"); |
|
112 testDate(SUCCESS, "01:59:01.01", "hh:mm:ss.fffff", "01:59:01.01000"); |
|
113 testDate(SUCCESS, "01:59:01.01", "hh:mm:ss.ffffff", "01:59:01.010000"); |
|
114 testDate(SUCCESS, "01:59:01.01", "hh:mm:ss.fffffff", "01:59:01.0100000"); |
|
115 testDate(SUCCESS, "01:59:01.01", "hh:mm:ss.ffffffff", "01:59:01.01000000"); |
|
116 testDate(SUCCESS, "01:59:01.01", "hh:mm:ss.fffffffff", "01:59:01.010000000"); |
|
117 |
|
118 testDate(SUCCESS, "01:59:01.000000001", "hh:mm:ss", "01:59:01"); |
|
119 testDate(SUCCESS, "01:59:01.000000001", "hh:mm:ss.f", "01:59:01.0"); |
|
120 testDate(SUCCESS, "01:59:01.000000001", "hh:mm:ss.ff", "01:59:01.00"); |
|
121 testDate(SUCCESS, "01:59:01.000000001", "hh:mm:ss.fff", "01:59:01.000"); |
|
122 testDate(SUCCESS, "01:59:01.000000001", "hh:mm:ss.ffff", "01:59:01.0000"); |
|
123 testDate(SUCCESS, "01:59:01.000000001", "hh:mm:ss.fffff", "01:59:01.00000"); |
|
124 testDate(SUCCESS, "01:59:01.000000001", "hh:mm:ss.ffffff", "01:59:01.000000"); |
|
125 testDate(SUCCESS, "01:59:01.000000001", "hh:mm:ss.fffffff", "01:59:01.0000000"); |
|
126 testDate(SUCCESS, "01:59:01.000000001", "hh:mm:ss.ffffffff", "01:59:01.00000000"); |
|
127 testDate(SUCCESS, "01:59:01.000000001", "hh:mm:ss.fffffffff", "01:59:01.000000001"); |
|
128 |
|
129 testDate(SUCCESS, "01:59:01.000000000", "hh:mm:ss.fffffffff", "01:59:01.000000000"); |
|
130 |
|
131 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYY-MM-DD h12:mm:ss", "2009-10-28 1:59:01"); |
|
132 testDate(SUCCESS, "2009-10-28 13:59:01", "YYYY-MM-DD h12:mm:ss", "2009-10-28 1:59:01"); |
|
133 |
|
134 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYY-MM-DD hh12:mm:ss", "2009-10-28 01:59:01"); |
|
135 testDate(SUCCESS, "2009-10-28 12:59:01", "YYYY-MM-DD hh12:mm:ss", "2009-10-28 12:59:01"); |
|
136 testDate(SUCCESS, "2009-10-28 13:59:01", "YYYY-MM-DD hh12:mm:ss", "2009-10-28 01:59:01"); |
|
137 testDate(SUCCESS, "2009-10-28 23:59:01", "YYYY-MM-DD hh12:mm:ss", "2009-10-28 11:59:01"); |
|
138 |
|
139 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYY-MM-DD h12:mm:ss a", Locale.CANADA, "2009-10-28 1:59:01 AM"); |
|
140 testDate(SUCCESS, "2009-10-28 13:59:01", "YYYY-MM-DD h12:mm:ss a", Locale.CANADA, "2009-10-28 1:59:01 PM"); |
|
141 |
|
142 //WRONG : |
|
143 //this fails - tokens cannot appear next to each other: |
|
144 //testDate(SUCCESS, "2009-10-28 13:59:01", "YYYY-MM-DD h12:mm:ssa", Locale.CANADA, "2009-10-28 1:59:01PM"); |
|
145 //the workaround for the above is to use the escape character, with nothing in between: |
|
146 testDate(SUCCESS, "2009-10-28 13:59:01", "YYYY-MM-DD h12:mm:ss||a", Locale.CANADA, "2009-10-28 1:59:01PM"); |
|
147 } |
|
148 |
|
149 public void testEscapeChar(){ |
|
150 testDate(SUCCESS, "2009-10-28 01:59:01", "|Date:|YYYY-MM-DD |Time:|hh12:mm:ss", "Date:2009-10-28 Time:01:59:01"); |
|
151 testDate(SUCCESS, "15:59:59", "h12| o'clock| a", Locale.CANADA, "3 o'clock PM"); |
|
152 //inside escaped regions, the tokens are uninterpreted : |
|
153 testDate(SUCCESS, "2009-10-28 01:59:01", "|Date(YYYY-MM-DD):|YYYY-MM-DD |Timehh12:mm:ss:|hh12:mm:ss", "Date(YYYY-MM-DD):2009-10-28 Timehh12:mm:ss:01:59:01"); |
|
154 } |
|
155 |
|
156 public void testCustomFormats() { |
|
157 List<String> months = Arrays.asList("J", "F", "M", "A", "M", "J", "J", "A", "S", "O", "N", "D"); |
|
158 List<String> weekdays = Arrays.asList("sunday", "monday", "tuesday", "humpday", "thursday", "friday", "saturday"); |
|
159 List<String> amPm = Arrays.asList("am", "pm"); |
|
160 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYY-MM-DD hh:mm:ss a", months, weekdays, amPm, "2009-10-28 01:59:01 am"); |
|
161 testDate(SUCCESS, "2009-10-28 16:59:01", "YYYY-MM-DD h12:mm:ss a", months, weekdays, amPm, "2009-10-28 4:59:01 pm"); |
|
162 |
|
163 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYY-MMMM-DD hh:mm:ss a", months, weekdays, amPm, "2009-O-28 01:59:01 am"); |
|
164 testDate(SUCCESS, "2009-10-28 01:59:01", "YYYY-MM-DD WWWW hh:mm:ss a", months, weekdays, amPm, "2009-10-28 humpday 01:59:01 am"); |
|
165 } |
|
166 |
|
167 // PRIVATE |
|
168 |
|
169 private static final boolean SUCCESS = true; |
|
170 |
|
171 private void testDate(boolean aSuccess, String aDate , String aFormat, String aExpectedResult){ |
|
172 DateTimeParser parser = new DateTimeParser(); |
|
173 DateTime dateTime = parser.parse(aDate); |
|
174 DateTimeFormatter formatter = new DateTimeFormatter(aFormat); |
|
175 String result = formatter.format(dateTime); |
|
176 if(aSuccess){ |
|
177 if(! result.equals(aExpectedResult)){ |
|
178 throw new AssertionError("Expected:" + aExpectedResult + ", but result was:" + result); |
|
179 } |
|
180 } |
|
181 else { |
|
182 assertFalse(result.equals(aExpectedResult)); |
|
183 } |
|
184 } |
|
185 |
|
186 private void testDate(boolean aSuccess, String aDate , String aFormat, Locale aLocale, String aExpectedResult){ |
|
187 DateTimeParser parser = new DateTimeParser(); |
|
188 DateTime dateTime = parser.parse(aDate); |
|
189 DateTimeFormatter formatter = new DateTimeFormatter(aFormat, aLocale); |
|
190 String result = formatter.format(dateTime); |
|
191 if(aSuccess){ |
|
192 if(! result.equals(aExpectedResult)){ |
|
193 throw new AssertionError("Expected:" + aExpectedResult + ", but result was:" + result); |
|
194 } |
|
195 } |
|
196 else { |
|
197 assertFalse(result.equals(aExpectedResult)); |
|
198 } |
|
199 } |
|
200 |
|
201 private void testDate(boolean aSuccess, String aDate , String aFormat, List<String> aMonths, List<String> aWeekdays, List<String> aAmPm, String aExpectedResult){ |
|
202 DateTimeParser parser = new DateTimeParser(); |
|
203 DateTime dateTime = parser.parse(aDate); |
|
204 DateTimeFormatter formatter = new DateTimeFormatter(aFormat, aMonths, aWeekdays, aAmPm); |
|
205 String result = formatter.format(dateTime); |
|
206 if(aSuccess){ |
|
207 if(! result.equals(aExpectedResult)){ |
|
208 throw new AssertionError("Expected:" + aExpectedResult + ", but result was:" + result); |
|
209 } |
|
210 } |
|
211 else { |
|
212 assertFalse(result.equals(aExpectedResult)); |
|
213 } |
|
214 } |
|
215 } |