001: /*
002: * $Id: DateTagTest.java 471756 2006-11-06 15:01:43Z husted $
003: *
004: * Licensed to the Apache Software Foundation (ASF) under one
005: * or more contributor license agreements. See the NOTICE file
006: * distributed with this work for additional information
007: * regarding copyright ownership. The ASF licenses this file
008: * to you under the Apache License, Version 2.0 (the
009: * "License"); you may not use this file except in compliance
010: * with the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
017: * KIND, either express or implied. See the License for the
018: * specific language governing permissions and limitations
019: * under the License.
020: */
021: package org.apache.struts2.views.jsp.ui;
022:
023: import java.text.DateFormat;
024: import java.text.SimpleDateFormat;
025: import java.util.Calendar;
026: import java.util.Date;
027:
028: import org.apache.struts2.views.jsp.AbstractTagTest;
029: import org.apache.struts2.views.jsp.DateTag;
030:
031: import com.opensymphony.xwork2.ActionContext;
032:
033: /**
034: * Unit test for {@link org.apache.struts2.components.Date}.
035: *
036: */
037: public class DateTagTest extends AbstractTagTest {
038:
039: private DateTag tag;
040:
041: public void testCustomFormat() throws Exception {
042: String format = "yyyy/MM/dd hh:mm:ss";
043: Date now = new Date();
044: String formatted = new SimpleDateFormat(format).format(now);
045: context.put("myDate", now);
046:
047: tag.setName("myDate");
048: tag.setNice(false);
049: tag.setFormat(format);
050: tag.doStartTag();
051: tag.doEndTag();
052: assertEquals(formatted, writer.toString());
053: }
054:
055: public void testDefaultFormat() throws Exception {
056: Date now = new Date();
057: String formatted = DateFormat.getDateTimeInstance(
058: DateFormat.MEDIUM, DateFormat.MEDIUM,
059: ActionContext.getContext().getLocale()).format(now);
060:
061: context.put("myDate", now);
062: tag.setName("myDate");
063: tag.setNice(false);
064: tag.doStartTag();
065: tag.doEndTag();
066: assertEquals(formatted, writer.toString());
067: }
068:
069: public void testCustomFormatAndComponent() throws Exception {
070: String format = "yyyy/MM/dd hh:mm:ss";
071: Date now = new Date();
072: String formatted = new SimpleDateFormat(format).format(now);
073: context.put("myDate", now);
074:
075: tag.setName("myDate");
076: tag.setFormat(format);
077: tag.setNice(false);
078:
079: tag.doStartTag();
080:
081: // component test must be done between start and end tag
082: org.apache.struts2.components.Date component = (org.apache.struts2.components.Date) tag
083: .getComponent();
084: assertEquals("myDate", component.getName());
085: assertEquals(format, component.getFormat());
086: assertEquals(false, component.isNice());
087:
088: tag.doEndTag();
089:
090: assertEquals(formatted, writer.toString());
091: }
092:
093: public void testSetId() throws Exception {
094: String format = "yyyy/MM/dd hh:mm:ss";
095: Date now = new Date();
096: String formatted = new SimpleDateFormat(format).format(now);
097: context.put("myDate", now);
098:
099: tag.setName("myDate");
100: tag.setNice(false);
101: tag.setFormat(format);
102: tag.setId("myId");
103: tag.doStartTag();
104: tag.doEndTag();
105: assertEquals(formatted, context.get("myId"));
106: }
107:
108: public void testFutureNiceHour() throws Exception {
109: Date now = new Date();
110: Calendar future = Calendar.getInstance();
111: future.setTime(now);
112: future.add(Calendar.HOUR, 1);
113: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
114:
115: context.put("myDate", future.getTime());
116: tag.setName("myDate");
117: tag.setNice(true);
118: tag.doStartTag();
119: tag.doEndTag();
120: assertEquals("in one hour", writer.toString());
121: }
122:
123: public void testPastNiceHour() throws Exception {
124: Date now = new Date();
125: Calendar future = Calendar.getInstance();
126: future.setTime(now);
127: future.add(Calendar.HOUR, -1);
128: future.add(Calendar.SECOND, -5); // always add a little slack otherwise we could calculate wrong
129:
130: context.put("myDate", future.getTime());
131: tag.setName("myDate");
132: tag.setNice(true);
133: tag.doStartTag();
134: tag.doEndTag();
135: assertEquals("one hour ago", writer.toString());
136: }
137:
138: public void testFutureNiceHourMinSec() throws Exception {
139: Date now = new Date();
140: Calendar future = Calendar.getInstance();
141: future.setTime(now);
142: future.add(Calendar.HOUR, 2);
143: future.add(Calendar.MINUTE, 33);
144: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
145:
146: context.put("myDate", future.getTime());
147: tag.setName("myDate");
148: tag.setNice(true);
149: tag.doStartTag();
150: tag.doEndTag();
151: assertEquals("in 2 hours, 33 minutes", writer.toString());
152: }
153:
154: public void testPastNiceHourMin() throws Exception {
155: Date now = new Date();
156: Calendar past = Calendar.getInstance();
157: past.setTime(now);
158: past.add(Calendar.HOUR, -4);
159: past.add(Calendar.MINUTE, -55);
160: past.add(Calendar.SECOND, -5); // always add a little slack otherwise we could calculate wrong
161:
162: context.put("myDate", past.getTime());
163: tag.setName("myDate");
164: tag.setNice(true);
165: tag.doStartTag();
166: tag.doEndTag();
167: assertEquals("4 hours, 55 minutes ago", writer.toString());
168: }
169:
170: public void testFutureLessOneMin() throws Exception {
171: Date now = new Date();
172: Calendar future = Calendar.getInstance();
173: future.setTime(now);
174: future.add(Calendar.SECOND, 47);
175: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
176:
177: context.put("myDate", future.getTime());
178: tag.setName("myDate");
179: tag.setNice(true);
180: tag.doStartTag();
181: tag.doEndTag();
182: assertEquals("in an instant", writer.toString());
183: }
184:
185: public void testFutureLessOneHour() throws Exception {
186: Date now = new Date();
187: Calendar future = Calendar.getInstance();
188: future.setTime(now);
189: future.add(Calendar.MINUTE, 36);
190: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
191:
192: context.put("myDate", future.getTime());
193: tag.setName("myDate");
194: tag.setNice(true);
195: tag.doStartTag();
196: tag.doEndTag();
197: assertEquals("in 36 minutes", writer.toString());
198: }
199:
200: public void testFutureLessOneYear() throws Exception {
201: Date now = new Date();
202: Calendar future = Calendar.getInstance();
203: future.setTime(now);
204: future.add(Calendar.HOUR, 40 * 24);
205: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
206:
207: context.put("myDate", future.getTime());
208: tag.setName("myDate");
209: tag.setNice(true);
210: tag.doStartTag();
211: tag.doEndTag();
212: assertEquals("in 40 days", writer.toString());
213: }
214:
215: public void testFutureTwoYears() throws Exception {
216: Date now = new Date();
217: Calendar future = Calendar.getInstance();
218: future.setTime(now);
219: future.add(Calendar.YEAR, 2);
220: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
221:
222: context.put("myDate", future.getTime());
223: tag.setName("myDate");
224: tag.setNice(true);
225: tag.doStartTag();
226: tag.doEndTag();
227:
228: // hmmm the Date component isn't the best to calculate the excat difference so we'll just check
229: // that it starts with in 2 years
230: assertTrue(writer.toString().startsWith("in 2 years"));
231: }
232:
233: public void testNoDateObjectInContext() throws Exception {
234: context.put("myDate", "this is not a java.util.Date object");
235: tag.setName("myDate");
236: tag.setNice(true);
237: tag.doStartTag();
238: tag.doEndTag();
239: //should return a blank
240: assertEquals("", writer.toString());
241: }
242:
243: protected void setUp() throws Exception {
244: super .setUp();
245: tag = new DateTag();
246: tag.setPageContext(pageContext);
247: }
248:
249: protected void tearDown() throws Exception {
250: super.tearDown();
251: }
252:
253: }
|