1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.tobago.renderkit.html.scarborough.standard;
21
22 import org.junit.Assert;
23 import org.junit.Test;
24
25 import java.io.IOException;
26 import java.text.DecimalFormat;
27 import java.text.ParseException;
28 import java.text.SimpleDateFormat;
29 import java.util.Arrays;
30 import java.util.Calendar;
31 import java.util.Date;
32 import java.util.List;
33 import java.util.Locale;
34
35 public class DateUnitTest extends AbstractJavaScriptTestBase {
36
37 private static final int[] YEAR_MONTH_DAY
38 = {Calendar.YEAR, Calendar.MONTH, Calendar.DAY_OF_MONTH};
39
40 private static final List<Date> DATES = Arrays.asList(
41 createDate(1970, 1, 1),
42 createDate(2005, 11, 21),
43 createDate(2005, 12, 24),
44 createDate(1972, 1, 29)
45 );
46
47 private static Date createDate(int year, int month, int day) {
48 Calendar calendar = Calendar.getInstance(Locale.ENGLISH);
49 calendar.set(Calendar.YEAR, year);
50 calendar.set(Calendar.MONTH, month - 1);
51 calendar.set(Calendar.DAY_OF_MONTH, day);
52 return calendar.getTime();
53 }
54
55 protected void setUp() throws Exception {
56 super.setUp();
57 loadScriptFile("tobago-converter.js");
58 }
59
60 @Test
61 public void testNumberOnlyDateFormats() throws IOException {
62 for (Date date : DATES) {
63 checkFormat("yyyyMMdd", date, YEAR_MONTH_DAY, Locale.ENGLISH);
64 checkFormat("ddMMyyyy", date, YEAR_MONTH_DAY, Locale.ENGLISH);
65 checkFormat("d.M.yyyy", date, YEAR_MONTH_DAY, Locale.ENGLISH);
66 checkFormat("dd.MM.yyyy", date, YEAR_MONTH_DAY, Locale.ENGLISH);
67 checkFormat("dd/MM/yyyy", date, YEAR_MONTH_DAY, Locale.ENGLISH);
68 checkFormat("dd/MM/yyy", date, YEAR_MONTH_DAY, Locale.ENGLISH);
69 checkFormat("dd/MM/yy", date, YEAR_MONTH_DAY, Locale.ENGLISH);
70 checkFormat("EddMMyyyy", date, YEAR_MONTH_DAY, Locale.ENGLISH);
71 }
72 }
73
74 @Test
75 public void testEnglishMonths() throws IOException {
76 for (int month = 1; month <= 12; ++month) {
77 Date date = createDate(2005, month, 10);
78 StringBuilder format = new StringBuilder("M");
79 for (int i = 0; i < 4; ++i) {
80 format.append('M');
81 checkFormat(format.toString(), date, new int[] {Calendar.MONTH}, Locale.ENGLISH);
82 }
83 }
84 }
85
86 @Test
87 public void testTwoDigitYears() throws IOException, ParseException {
88 checkTwoDigitYears(Locale.ENGLISH);
89 checkTwoDigitYears(Locale.GERMAN);
90 }
91
92 @Test
93 public void checkTwoDigitYears(Locale locale) throws IOException, ParseException {
94 DecimalFormat decimalFormat = new DecimalFormat("00");
95 SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yy", locale);
96 Calendar calendar = Calendar.getInstance(locale);
97 for (int year = 0; year < 100; ++year) {
98 String yearString = decimalFormat.format(year);
99 calendar.setTime(simpleDateFormat.parse(yearString));
100 Assert.assertEquals(calendar.get(Calendar.YEAR), evalParseDate(yearString, "yy", locale).get(Calendar.YEAR));
101 }
102 }
103
104 @Test
105 public void testEnglishWeekDays() throws IOException {
106 for (int day = 1; day <= 7; ++day) {
107 Calendar calendar = Calendar.getInstance();
108 calendar.set(Calendar.DAY_OF_WEEK, day);
109 Date date = calendar.getTime();
110
111 StringBuilder format = new StringBuilder();
112 for (int i = 0; i < 4; ++i) {
113 format.append('E');
114 checkFormat(format.toString(), date, new int[0], Locale.ENGLISH);
115 }
116 }
117 }
118
119 private Object evalFormatDate(Date date, String format) {
120
121 return eval("new SimpleDateFormat(\"" + format + "\")"
122 + ".format(new Date(" + date.getTime() + "))");
123 }
124
125 @Test
126 public void testLocalization() {
127 Assert.assertEquals("January", eval("var s = new DateFormatSymbols(); s.months[0]"));
128 Assert.assertEquals("Januar", eval(createSymbols("s", Locale.GERMAN) + "s.months[0]"));
129 }
130
131 private String createSymbols(String var, Locale locale) {
132 return "var " + var + " = new DateFormatSymbols(); "
133 + var + ".months = new Array("
134 + createStringList(DateTestUtils.createMonthNames(true, locale)) + ");"
135 + var + ".shortMonths = new Array("
136 + createStringList(DateTestUtils.createMonthNames(false, locale)) + ");"
137 + var + ".weekdays = new Array("
138 + createStringList(DateTestUtils.createDayNames(true, locale)) + ");"
139 + var + ".shortWeekdays = new Array("
140 + createStringList(DateTestUtils.createDayNames(false, locale)) + ");";
141 }
142
143 private String createStringList(List<String> strings) {
144 return "'" + join(strings, "', '") + "'";
145 }
146
147 private String join(List<String> strings, String separator) {
148 StringBuilder builder = new StringBuilder();
149 for (int i = 0; i < strings.size(); i++) {
150 String s = strings.get(i);
151 if (i > 0) {
152 builder.append(separator);
153 }
154 builder.append(s);
155 }
156 return builder.toString();
157 }
158
159 private Calendar evalParseDate(String input, String format, Locale locale) {
160 long time = evalLong(createSymbols("s", locale) + ";"
161 + "new SimpleDateFormat(\"" + format + "\", s)"
162 + ".parse(\"" + input + "\").getTime()");
163 Calendar calendar = Calendar.getInstance(locale);
164 calendar.setTime(new Date(time));
165 return calendar;
166 }
167
168 private void checkFormat(String format, Date date, int[] fields,
169 Locale locale) {
170 SimpleDateFormat simpleDateFormat = new SimpleDateFormat(format, locale);
171 Assert.assertEquals(simpleDateFormat.format(date), evalFormatDate(date, format));
172 Calendar calendar1 = Calendar.getInstance(locale);
173 calendar1.setTime(date);
174 Calendar calendar2 = evalParseDate(
175 simpleDateFormat.format(date), format, locale);
176 for (int field : fields) {
177 checkField(calendar1, calendar2, field);
178 }
179 }
180
181 private void checkField(Calendar calendar1, Calendar calendar2, int field) {
182 Assert.assertEquals(calendar1.get(field), calendar2.get(field));
183 }
184
185 }