View Javadoc

1   /*
2    * $Id: DateTagTest.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.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.views.jsp.AbstractTagTest;
30  import org.apache.struts2.views.jsp.DateTag;
31  
32  import com.opensymphony.xwork2.ActionContext;
33  
34  /***
35   * Unit test for {@link org.apache.struts2.components.Date}.
36   *
37   */
38  public class DateTagTest extends AbstractTagTest {
39  
40      private DateTag tag;
41  
42      public void testCustomFormat() throws Exception {
43          String format = "yyyy/MM/dd hh:mm:ss";
44          Date now = new Date();
45          String formatted = new SimpleDateFormat(format).format(now);
46          context.put("myDate", now);
47  
48          tag.setName("myDate");
49          tag.setNice(false);
50          tag.setFormat(format);
51          tag.doStartTag();
52          tag.doEndTag();
53          assertEquals(formatted, writer.toString());
54      }
55      
56      public void testCustomFormatCalendar() throws Exception {
57          String format = "yyyy/MM/dd hh:mm:ss";
58          Calendar calendar = Calendar.getInstance();
59          String formatted = new SimpleDateFormat(format).format(calendar.getTime());
60          context.put("myDate", calendar);
61  
62          tag.setName("myDate");
63          tag.setNice(false);
64          tag.setFormat(format);
65          tag.doStartTag();
66          tag.doEndTag();
67          assertEquals(formatted, writer.toString());
68      }
69  
70      public void testDefaultFormat() throws Exception {
71          Date now = new Date();
72          String formatted = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM,
73                  ActionContext.getContext().getLocale()).format(now);
74  
75          context.put("myDate", now);
76          tag.setName("myDate");
77          tag.setNice(false);
78          tag.doStartTag();
79          tag.doEndTag();
80          assertEquals(formatted, writer.toString());
81      }
82  
83      public void testCustomFormatAndComponent() throws Exception {
84          String format = "yyyy/MM/dd hh:mm:ss";
85          Date now = new Date();
86          String formatted = new SimpleDateFormat(format).format(now);
87          context.put("myDate", now);
88  
89          tag.setName("myDate");
90          tag.setFormat(format);
91          tag.setNice(false);
92  
93          tag.doStartTag();
94  
95          // component test must be done between start and end tag
96          org.apache.struts2.components.Date component = (org.apache.struts2.components.Date) tag.getComponent();
97          assertEquals("myDate", component.getName());
98          assertEquals(format, component.getFormat());
99          assertEquals(false, component.isNice());
100 
101         tag.doEndTag();
102 
103         assertEquals(formatted, writer.toString());
104     }
105 
106     public void testSetId() throws Exception {
107         String format = "yyyy/MM/dd hh:mm:ss";
108         Date now = new Date();
109         String formatted = new SimpleDateFormat(format).format(now);
110         context.put("myDate", now);
111 
112         tag.setName("myDate");
113         tag.setNice(false);
114         tag.setFormat(format);
115         tag.setId("myId");
116         tag.doStartTag();
117         tag.doEndTag();
118         assertEquals(formatted, context.get("myId"));
119     }
120 
121     public void testFutureNiceHour() throws Exception {
122         Date now = new Date();
123         Calendar future = Calendar.getInstance();
124         future.setTime(now);
125         future.add(Calendar.HOUR, 1);
126         future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
127 
128         context.put("myDate", future.getTime());
129         tag.setName("myDate");
130         tag.setNice(true);
131         tag.doStartTag();
132         tag.doEndTag();
133         assertEquals("in one hour", writer.toString());
134     }
135 
136     public void testPastNiceHour() throws Exception {
137         Date now = new Date();
138         Calendar future = Calendar.getInstance();
139         future.setTime(now);
140         future.add(Calendar.HOUR, -1);
141         future.add(Calendar.SECOND, -5); // always add a little slack otherwise we could calculate wrong
142 
143         context.put("myDate", future.getTime());
144         tag.setName("myDate");
145         tag.setNice(true);
146         tag.doStartTag();
147         tag.doEndTag();
148         assertEquals("one hour ago", writer.toString());
149     }
150 
151     public void testFutureNiceHourMinSec() throws Exception {
152         Date now = new Date();
153         Calendar future = Calendar.getInstance();
154         future.setTime(now);
155         future.add(Calendar.HOUR, 2);
156         future.add(Calendar.MINUTE, 33);
157         future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
158 
159         context.put("myDate", future.getTime());
160         tag.setName("myDate");
161         tag.setNice(true);
162         tag.doStartTag();
163         tag.doEndTag();
164         assertEquals("in 2 hours, 33 minutes", writer.toString());
165     }
166 
167     public void testPastNiceHourMin() throws Exception {
168         Date now = new Date();
169         Calendar past = Calendar.getInstance();
170         past.setTime(now);
171         past.add(Calendar.HOUR, -4);
172         past.add(Calendar.MINUTE, -55);
173         past.add(Calendar.SECOND, -5); // always add a little slack otherwise we could calculate wrong
174 
175         context.put("myDate", past.getTime());
176         tag.setName("myDate");
177         tag.setNice(true);
178         tag.doStartTag();
179         tag.doEndTag();
180         assertEquals("4 hours, 55 minutes ago", writer.toString());
181     }
182 
183     public void testFutureLessOneMin() throws Exception {
184         Date now = new Date();
185         Calendar future = Calendar.getInstance();
186         future.setTime(now);
187         future.add(Calendar.SECOND, 47);
188         future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
189 
190         context.put("myDate", future.getTime());
191         tag.setName("myDate");
192         tag.setNice(true);
193         tag.doStartTag();
194         tag.doEndTag();
195         assertEquals("in an instant", writer.toString());
196     }
197 
198     public void testFutureLessOneHour() throws Exception {
199         Date now = new Date();
200         Calendar future = Calendar.getInstance();
201         future.setTime(now);
202         future.add(Calendar.MINUTE, 36);
203         future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
204 
205         context.put("myDate", future.getTime());
206         tag.setName("myDate");
207         tag.setNice(true);
208         tag.doStartTag();
209         tag.doEndTag();
210         assertEquals("in 36 minutes", writer.toString());
211     }
212 
213     public void testFutureLessOneYear() throws Exception {
214         Date now = new Date();
215         Calendar future = Calendar.getInstance();
216         future.setTime(now);
217         future.add(Calendar.HOUR, 40 * 24);
218         future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
219 
220         context.put("myDate", future.getTime());
221         tag.setName("myDate");
222         tag.setNice(true);
223         tag.doStartTag();
224         tag.doEndTag();
225         assertEquals("in 40 days", writer.toString());
226     }
227 
228     public void testFutureTwoYears() throws Exception {
229         Date now = new Date();
230         Calendar future = Calendar.getInstance();
231         future.setTime(now);
232         future.add(Calendar.YEAR, 2);
233         future.add(Calendar.DATE, 1);
234         future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
235 
236         context.put("myDate", future.getTime());
237         tag.setName("myDate");
238         tag.setNice(true);
239         tag.doStartTag();
240         tag.doEndTag();
241 
242         // hmmm the Date component isn't the best to calculate the excat difference so we'll just check
243         // that it starts with in 2 years
244         assertTrue(writer.toString().startsWith("in 2 years"));
245     }
246 
247     public void testNoDateObjectInContext() throws Exception {
248         context.put("myDate", "this is not a java.util.Date object");
249         tag.setName("myDate");
250         tag.setNice(true);
251         tag.doStartTag();
252         tag.doEndTag();
253         //should return a blank
254         assertEquals("", writer.toString());
255     }
256 
257     protected void setUp() throws Exception {
258         super.setUp();
259         tag = new DateTag();
260         tag.setPageContext(pageContext);
261     }
262 
263     protected void tearDown() throws Exception {
264         super.tearDown();
265         tag = null;
266     }
267 
268 }