001: /*
002: * Copyright (c) 2002-2006 by OpenSymphony
003: * All rights reserved.
004: */
005: package com.opensymphony.webwork.views.jsp.ui;
006:
007: import com.opensymphony.webwork.views.jsp.AbstractTagTest;
008: import com.opensymphony.webwork.views.jsp.DateTag;
009: import com.opensymphony.xwork.ActionContext;
010:
011: import java.text.DateFormat;
012: import java.text.SimpleDateFormat;
013: import java.util.Calendar;
014: import java.util.Date;
015:
016: /**
017: * Unit test for {@link com.opensymphony.webwork.components.Date}.
018: *
019: * @author Claus Ibsen
020: */
021: public class DateTagTest extends AbstractTagTest {
022:
023: private DateTag tag;
024:
025: public void testCustomFormat() throws Exception {
026: String format = "yyyy/MM/dd hh:mm:ss";
027: Date now = new Date();
028: String formatted = new SimpleDateFormat(format).format(now);
029: context.put("myDate", now);
030:
031: tag.setName("myDate");
032: tag.setNice(false);
033: tag.setFormat(format);
034: tag.doStartTag();
035: tag.doEndTag();
036: assertEquals(formatted, writer.toString());
037: }
038:
039: public void testDefaultFormat() throws Exception {
040: Date now = new Date();
041: String formatted = DateFormat.getDateTimeInstance(
042: DateFormat.MEDIUM, DateFormat.MEDIUM,
043: ActionContext.getContext().getLocale()).format(now);
044:
045: context.put("myDate", now);
046: tag.setName("myDate");
047: tag.setNice(false);
048: tag.doStartTag();
049: tag.doEndTag();
050: assertEquals(formatted, writer.toString());
051: }
052:
053: public void testCustomFormatAndComponent() throws Exception {
054: String format = "yyyy/MM/dd hh:mm:ss";
055: Date now = new Date();
056: String formatted = new SimpleDateFormat(format).format(now);
057: context.put("myDate", now);
058:
059: tag.setName("myDate");
060: tag.setFormat(format);
061: tag.setNice(false);
062:
063: tag.doStartTag();
064:
065: // component test must be done between start and end tag
066: com.opensymphony.webwork.components.Date component = (com.opensymphony.webwork.components.Date) tag
067: .getComponent();
068: assertEquals("myDate", component.getName());
069: assertEquals(format, component.getFormat());
070: assertEquals(false, component.isNice());
071:
072: tag.doEndTag();
073:
074: assertEquals(formatted, writer.toString());
075: }
076:
077: public void testSetId() throws Exception {
078: String format = "yyyy/MM/dd hh:mm:ss";
079: Date now = new Date();
080: String formatted = new SimpleDateFormat(format).format(now);
081: context.put("myDate", now);
082:
083: tag.setName("myDate");
084: tag.setNice(false);
085: tag.setFormat(format);
086: tag.setId("myId");
087: tag.doStartTag();
088: tag.doEndTag();
089: assertEquals(formatted, context.get("myId"));
090: }
091:
092: public void testFutureNiceHour() throws Exception {
093: Date now = new Date();
094: Calendar future = Calendar.getInstance();
095: future.setTime(now);
096: future.add(Calendar.HOUR, 1);
097: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
098:
099: context.put("myDate", future.getTime());
100: tag.setName("myDate");
101: tag.setNice(true);
102: tag.doStartTag();
103: tag.doEndTag();
104: assertEquals("in one hour", writer.toString());
105: }
106:
107: public void testPastNiceHour() throws Exception {
108: Date now = new Date();
109: Calendar future = Calendar.getInstance();
110: future.setTime(now);
111: future.add(Calendar.HOUR, -1);
112: future.add(Calendar.SECOND, -5); // always add a little slack otherwise we could calculate wrong
113:
114: context.put("myDate", future.getTime());
115: tag.setName("myDate");
116: tag.setNice(true);
117: tag.doStartTag();
118: tag.doEndTag();
119: assertEquals("one hour ago", writer.toString());
120: }
121:
122: public void testFutureNiceHourMinSec() throws Exception {
123: Date now = new Date();
124: Calendar future = Calendar.getInstance();
125: future.setTime(now);
126: future.add(Calendar.HOUR, 2);
127: future.add(Calendar.MINUTE, 33);
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("in 2 hours, 33 minutes", writer.toString());
136: }
137:
138: public void testPastNiceHourMin() throws Exception {
139: Date now = new Date();
140: Calendar past = Calendar.getInstance();
141: past.setTime(now);
142: past.add(Calendar.HOUR, -4);
143: past.add(Calendar.MINUTE, -55);
144: past.add(Calendar.SECOND, -5); // always add a little slack otherwise we could calculate wrong
145:
146: context.put("myDate", past.getTime());
147: tag.setName("myDate");
148: tag.setNice(true);
149: tag.doStartTag();
150: tag.doEndTag();
151: assertEquals("4 hours, 55 minutes ago", writer.toString());
152: }
153:
154: public void testFutureLessOneMin() throws Exception {
155: Date now = new Date();
156: Calendar future = Calendar.getInstance();
157: future.setTime(now);
158: future.add(Calendar.SECOND, 47);
159: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
160:
161: context.put("myDate", future.getTime());
162: tag.setName("myDate");
163: tag.setNice(true);
164: tag.doStartTag();
165: tag.doEndTag();
166: assertEquals("in an instant", writer.toString());
167: }
168:
169: public void testFutureLessOneHour() throws Exception {
170: Date now = new Date();
171: Calendar future = Calendar.getInstance();
172: future.setTime(now);
173: future.add(Calendar.MINUTE, 36);
174: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
175:
176: context.put("myDate", future.getTime());
177: tag.setName("myDate");
178: tag.setNice(true);
179: tag.doStartTag();
180: tag.doEndTag();
181: assertEquals("in 36 minutes", writer.toString());
182: }
183:
184: public void testFutureLessOneYear() throws Exception {
185: Date now = new Date();
186: Calendar future = Calendar.getInstance();
187: future.setTime(now);
188: future.add(Calendar.HOUR, 40 * 24);
189: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
190:
191: context.put("myDate", future.getTime());
192: tag.setName("myDate");
193: tag.setNice(true);
194: tag.doStartTag();
195: tag.doEndTag();
196: assertEquals("in 40 days", writer.toString());
197: }
198:
199: public void testFutureTwoYears() throws Exception {
200: Date now = new Date();
201: Calendar future = Calendar.getInstance();
202: future.setTime(now);
203: future.add(Calendar.YEAR, 2);
204: future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
205:
206: context.put("myDate", future.getTime());
207: tag.setName("myDate");
208: tag.setNice(true);
209: tag.doStartTag();
210: tag.doEndTag();
211:
212: // hmmm the Date component isn't the best to calculate the excat difference so we'll just check
213: // that it starts with in 2 years
214: assertTrue(writer.toString().startsWith("in 2 years"));
215: }
216:
217: public void testNoDateObjectInContext() throws Exception {
218: context.put("myDate", "this is not a java.util.Date object");
219: tag.setName("myDate");
220: tag.setNice(true);
221: tag.doStartTag();
222: tag.doEndTag();
223: //should return a blank
224: assertEquals("", writer.toString());
225: }
226:
227: protected void setUp() throws Exception {
228: super .setUp();
229: tag = new DateTag();
230: tag.setPageContext(pageContext);
231: }
232:
233: protected void tearDown() throws Exception {
234: super.tearDown();
235: }
236:
237: }
|