View Javadoc

1   /*
2    * $Id: DateTimePickerTagTest.java 651946 2008-04-27 13:41:38Z apetrelli $
3    *
4    * Licensed to the Apache Software Foundation (ASF) under one
5    * or more contributor license agreements.  See the NOTICE file
6    * distributed with this work for additional information
7    * regarding copyright ownership.  The ASF licenses this file
8    * to you under the Apache License, Version 2.0 (the
9    * "License"); you may not use this file except in compliance
10   * with the License.  You may obtain a copy of the License at
11   *
12   *  http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing,
15   * software distributed under the License is distributed on an
16   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
17   * KIND, either express or implied.  See the License for the
18   * specific language governing permissions and limitations
19   * under the License.
20   */
21  
22  package org.apache.struts2.dojo.views.jsp.ui;
23  
24  import java.text.DateFormat;
25  import java.text.SimpleDateFormat;
26  import java.util.Calendar;
27  import java.util.Date;
28  
29  import org.apache.struts2.dojo.components.DateTimePicker;
30  
31  /***
32   */
33  public class DateTimePickerTagTest extends AbstractUITagTest {
34      final private static SimpleDateFormat RFC3339_FORMAT = new SimpleDateFormat(
35          "yyyy-MM-dd'T'HH:mm:ss");
36  
37      public void testSimple() throws Exception {
38          DateTimePickerTag tag = new DateTimePickerTag();
39          tag.setPageContext(pageContext);
40  
41          tag.setId("id");
42  
43          tag.setAdjustWeeks("true");
44          tag.setDayWidth("b");
45          tag.setDisplayWeeks("true");
46          tag.setEndDate("%{'2008-01-01'}");
47          tag.setStartDate("%{'2008-02-02'}");
48          tag.setStaticDisplay("false");
49          tag.setWeekStartsOn("g");
50          tag.setName("h");
51          tag.setLanguage("i");
52          tag.setTemplateCssPath("j");
53          tag.setValueNotifyTopics("k");
54          tag.setValue("%{'2008-03-03'}");
55          tag.doStartTag();
56          tag.doEndTag();
57  
58          verify(DateTimePickerTagTest.class
59              .getResource("DateTimePickerTagTest-1.txt"));
60      }
61  
62      public void testSimpleDisabled() throws Exception {
63          DateTimePickerTag tag = new DateTimePickerTag();
64          tag.setPageContext(pageContext);
65  
66          tag.setId("id");
67  
68          tag.setAdjustWeeks("true");
69          tag.setDayWidth("b");
70          tag.setDisplayWeeks("true");
71          tag.setEndDate("%{'2008-01-01'}");
72          tag.setStartDate("%{'2008-02-02'}");
73          tag.setStaticDisplay("false");
74          tag.setWeekStartsOn("g");
75          tag.setName("h");
76          tag.setLanguage("i");
77          tag.setTemplateCssPath("j");
78          tag.setValueNotifyTopics("k");
79          tag.setValue("%{'2008-03-03'}");
80          tag.setDisabled("true");
81          tag.doStartTag();
82          tag.doEndTag();
83  
84          verify(DateTimePickerTagTest.class
85              .getResource("DateTimePickerTagTest-2.txt"));
86      }
87  
88      public void testTodayValue() throws Exception {
89          DateTimePickerTag tag = new DateTimePickerTag();
90          tag.setPageContext(pageContext);
91          
92          tag.setValue("%{'today'}");
93          assertDateValue("nameValue", tag, new Date(), true, false);
94      }
95      
96      public void testDateParsing() throws Exception {
97          DateTimePickerTag tag = new DateTimePickerTag();
98          tag.setPageContext(pageContext);
99  
100         Calendar calendar = Calendar.getInstance();
101         calendar.set(Calendar.YEAR, 2007);
102         calendar.set(Calendar.MONTH, Calendar.JANUARY);
103         calendar.set(Calendar.DAY_OF_MONTH, 1);
104         calendar.set(Calendar.HOUR_OF_DAY, 10);
105         calendar.set(Calendar.MINUTE, 30);
106         calendar.set(Calendar.SECOND, 10);
107         calendar.set(Calendar.MILLISECOND, 20);
108         calendar.set(Calendar.AM_PM, Calendar.AM);
109         Date date = calendar.getTime();
110 
111         //test 'nameValue'
112         stack.set("date", "01-01-2007");
113         tag.setValue("%{date}");
114         tag.setDisplayFormat("MM-dd-yyyy");
115         assertDateValue("nameValue", tag, date, true, false);
116         assertDateProperty("nameValue", tag, date);
117         
118         tag.setDisplayFormat(null);
119 
120         //test 'startDate'
121         tag.setStartDate("%{date}");
122         assertDateProperty("startDate", tag, date);
123         
124         //test 'endDate'
125         tag.setEndDate("%{date}");
126         assertDateProperty("endDate", tag, date);
127 
128     }
129 
130     private void assertDateProperty(String property, DateTimePickerTag tag, final Date date) throws Exception {
131         final DateFormat shortTimeFormat = DateFormat.getTimeInstance(DateFormat.SHORT);
132         final DateFormat shortFormat = DateFormat.getDateInstance(DateFormat.SHORT);
133         final DateFormat mediumFormat = DateFormat.getDateInstance(DateFormat.MEDIUM);
134         final DateFormat longFormat = DateFormat.getDateInstance(DateFormat.LONG);
135         final DateFormat fullFormat = DateFormat.getDateInstance(DateFormat.FULL);
136         //try a Date value
137         stack.set("date", date);
138         assertDateValue(property, tag, date, true, false);
139         
140         //try a Calendar value
141         Calendar calendar = Calendar.getInstance();
142         calendar.setTime(date);
143         stack.set("date", calendar);
144         assertDateValue(property, tag, date, true, false);
145         
146         //try an object whose to string returns a parseable date
147         stack.set("date", new Object() {
148 
149             @Override
150             public String toString() {
151                 return fullFormat.format(date);
152             }
153             
154         });
155         assertDateValue(property, tag, date, true, false);
156         
157         // try short format 
158         stack.set("date", shortFormat.format(date));
159         assertDateValue(property, tag, date, true, false);
160 
161         //try medium format 
162         stack.set("date", mediumFormat.format(date));
163         assertDateValue(property, tag, date, true, false);
164 
165         //try long format 
166         stack.set("date", longFormat.format(date));
167         assertDateValue(property, tag, date, true, false);
168 
169         //try full format 
170         stack.set("date", fullFormat.format(date));
171         assertDateValue(property, tag, date, true, false);
172 
173         //try RFC 3339 format 
174         stack.set("date", RFC3339_FORMAT.format(date));
175         assertDateValue(property, tag, date, true, false);
176         
177         //try short time format 
178         stack.set("date", shortTimeFormat.format(date));
179         assertDateValue(property, tag, date, false, true);
180     }
181     
182     private void assertDateValue(String property, DateTimePickerTag tag, Date toCompareDate,
183         boolean compareDate, boolean compareTime) throws Exception {
184         tag.doStartTag();
185         DateTimePicker picker = (DateTimePicker) tag.getComponent();
186         picker.evaluateParams();
187 
188         String dateStr = (String) tag.getComponent().getParameters()
189             .get(property);
190         Date date = RFC3339_FORMAT.parse(dateStr);
191         assertNotNull(date);
192         Calendar calendar = Calendar.getInstance();
193         calendar.setTime(date);
194 
195         Calendar toCompareCalendar = Calendar.getInstance();
196         toCompareCalendar.setTime(toCompareDate);
197 
198         if (compareDate) {
199             assertEquals(toCompareCalendar.get(Calendar.YEAR), calendar
200                 .get(Calendar.YEAR));
201             assertEquals(toCompareCalendar.get(Calendar.MONTH), calendar
202                 .get(Calendar.MONTH));
203             assertEquals(toCompareCalendar.get(Calendar.DAY_OF_MONTH), calendar
204                 .get(Calendar.DAY_OF_MONTH));
205         }
206         if (compareTime) {
207             assertEquals(toCompareCalendar.get(Calendar.HOUR_OF_DAY), calendar
208                 .get(Calendar.HOUR_OF_DAY));
209             assertEquals(toCompareCalendar.get(Calendar.MINUTE), calendar
210                 .get(Calendar.MINUTE));
211             assertEquals(toCompareCalendar.get(Calendar.AM_PM), calendar
212                 .get(Calendar.AM_PM));
213         }
214 
215     }
216 
217 }