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