View Javadoc

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