Methods Summary |
---|
protected void | setUp()
super.setUp();
tag = new DateTag();
tag.setPageContext(pageContext);
|
protected void | tearDown()
super.tearDown();
|
public void | testCustomFormat()
String format = "yyyy/MM/dd hh:mm:ss";
Date now = new Date();
String formatted = new SimpleDateFormat(format).format(now);
context.put("myDate", now);
tag.setName("myDate");
tag.setNice(false);
tag.setFormat(format);
tag.doStartTag();
tag.doEndTag();
assertEquals(formatted, writer.toString());
|
public void | testCustomFormatAndComponent()
String format = "yyyy/MM/dd hh:mm:ss";
Date now = new Date();
String formatted = new SimpleDateFormat(format).format(now);
context.put("myDate", now);
tag.setName("myDate");
tag.setFormat(format);
tag.setNice(false);
tag.doStartTag();
// component test must be done between start and end tag
org.apache.struts2.components.Date component = (org.apache.struts2.components.Date) tag.getComponent();
assertEquals("myDate", component.getName());
assertEquals(format, component.getFormat());
assertEquals(false, component.isNice());
tag.doEndTag();
assertEquals(formatted, writer.toString());
|
public void | testDefaultFormat()
Date now = new Date();
String formatted = DateFormat.getDateTimeInstance(DateFormat.MEDIUM, DateFormat.MEDIUM,
ActionContext.getContext().getLocale()).format(now);
context.put("myDate", now);
tag.setName("myDate");
tag.setNice(false);
tag.doStartTag();
tag.doEndTag();
assertEquals(formatted, writer.toString());
|
public void | testFutureLessOneHour()
Date now = new Date();
Calendar future = Calendar.getInstance();
future.setTime(now);
future.add(Calendar.MINUTE, 36);
future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
context.put("myDate", future.getTime());
tag.setName("myDate");
tag.setNice(true);
tag.doStartTag();
tag.doEndTag();
assertEquals("in 36 minutes", writer.toString());
|
public void | testFutureLessOneMin()
Date now = new Date();
Calendar future = Calendar.getInstance();
future.setTime(now);
future.add(Calendar.SECOND, 47);
future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
context.put("myDate", future.getTime());
tag.setName("myDate");
tag.setNice(true);
tag.doStartTag();
tag.doEndTag();
assertEquals("in an instant", writer.toString());
|
public void | testFutureLessOneYear()
Date now = new Date();
Calendar future = Calendar.getInstance();
future.setTime(now);
future.add(Calendar.HOUR, 40 * 24);
future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
context.put("myDate", future.getTime());
tag.setName("myDate");
tag.setNice(true);
tag.doStartTag();
tag.doEndTag();
assertEquals("in 40 days", writer.toString());
|
public void | testFutureNiceHour()
Date now = new Date();
Calendar future = Calendar.getInstance();
future.setTime(now);
future.add(Calendar.HOUR, 1);
future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
context.put("myDate", future.getTime());
tag.setName("myDate");
tag.setNice(true);
tag.doStartTag();
tag.doEndTag();
assertEquals("in one hour", writer.toString());
|
public void | testFutureNiceHourMinSec()
Date now = new Date();
Calendar future = Calendar.getInstance();
future.setTime(now);
future.add(Calendar.HOUR, 2);
future.add(Calendar.MINUTE, 33);
future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
context.put("myDate", future.getTime());
tag.setName("myDate");
tag.setNice(true);
tag.doStartTag();
tag.doEndTag();
assertEquals("in 2 hours, 33 minutes", writer.toString());
|
public void | testFutureTwoYears()
Date now = new Date();
Calendar future = Calendar.getInstance();
future.setTime(now);
future.add(Calendar.YEAR, 2);
future.add(Calendar.SECOND, 5); // always add a little slack otherwise we could calculate wrong
context.put("myDate", future.getTime());
tag.setName("myDate");
tag.setNice(true);
tag.doStartTag();
tag.doEndTag();
// hmmm the Date component isn't the best to calculate the excat difference so we'll just check
// that it starts with in 2 years
assertTrue(writer.toString().startsWith("in 2 years"));
|
public void | testNoDateObjectInContext()
context.put("myDate", "this is not a java.util.Date object");
tag.setName("myDate");
tag.setNice(true);
tag.doStartTag();
tag.doEndTag();
//should return a blank
assertEquals("", writer.toString());
|
public void | testPastNiceHour()
Date now = new Date();
Calendar future = Calendar.getInstance();
future.setTime(now);
future.add(Calendar.HOUR, -1);
future.add(Calendar.SECOND, -5); // always add a little slack otherwise we could calculate wrong
context.put("myDate", future.getTime());
tag.setName("myDate");
tag.setNice(true);
tag.doStartTag();
tag.doEndTag();
assertEquals("one hour ago", writer.toString());
|
public void | testPastNiceHourMin()
Date now = new Date();
Calendar past = Calendar.getInstance();
past.setTime(now);
past.add(Calendar.HOUR, -4);
past.add(Calendar.MINUTE, -55);
past.add(Calendar.SECOND, -5); // always add a little slack otherwise we could calculate wrong
context.put("myDate", past.getTime());
tag.setName("myDate");
tag.setNice(true);
tag.doStartTag();
tag.doEndTag();
assertEquals("4 hours, 55 minutes ago", writer.toString());
|
public void | testSetId()
String format = "yyyy/MM/dd hh:mm:ss";
Date now = new Date();
String formatted = new SimpleDateFormat(format).format(now);
context.put("myDate", now);
tag.setName("myDate");
tag.setNice(false);
tag.setFormat(format);
tag.setId("myId");
tag.doStartTag();
tag.doEndTag();
assertEquals(formatted, context.get("myId"));
|