View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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); // XXX
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); // XXX new int[] {Calendar.DAY_OF_WEEK}
115       }
116     }
117   }
118 
119   private Object evalFormatDate(Date date, String format) {
120     // SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
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 }