}
@Test
public void dateFormatsTest() throws Exception {
DelayInformationProvider p = new DelayInfoProvider();
DelayInfo delayInfo;
String control;
GregorianCalendar calendar = new GregorianCalendar(2002, 9 - 1, 10, 23, 8, 25);
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
// XEP-0082 date format
control = XMLBuilder.create("delay")
.a("xmlns", "urn:xmpp:delay")
.a("from", "capulet.com")
.a("stamp", "2002-09-10T23:08:25.12Z")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(getParser(control, "delay"));
GregorianCalendar cal = (GregorianCalendar) calendar.clone();
cal.add(Calendar.MILLISECOND, 12);
assertEquals(cal.getTime(), delayInfo.getStamp());
// XEP-0082 date format without milliseconds
control = XMLBuilder.create("delay")
.a("xmlns", "urn:xmpp:delay")
.a("from", "capulet.com")
.a("stamp", "2002-09-10T23:08:25Z")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(getParser(control, "delay"));
assertEquals(calendar.getTime(), delayInfo.getStamp());
// XEP-0082 date format without milliseconds and leading 0 in month
control = XMLBuilder.create("delay")
.a("xmlns", "urn:xmpp:delay")
.a("from", "capulet.com")
.a("stamp", "2002-9-10T23:08:25Z")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(getParser(control, "delay"));
assertEquals(calendar.getTime(), delayInfo.getStamp());
// XEP-0091 date format
control = XMLBuilder.create("delay")
.a("xmlns", "urn:xmpp:delay")
.a("from", "capulet.com")
.a("stamp", "20020910T23:08:25")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(getParser(control, "delay"));
assertEquals(calendar.getTime(), delayInfo.getStamp());
// XEP-0091 date format without leading 0 in month
SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMd'T'HH:mm:ss");
dateFormat.setTimeZone(TimeZone.getTimeZone("UTC"));
GregorianCalendar dateInPast = new GregorianCalendar();
if (dateInPast.get(Calendar.MONTH) >= 10) {
dateInPast.set(Calendar.MONTH, dateInPast.get(Calendar.MONTH) - 3);
}
dateInPast.add(Calendar.DAY_OF_MONTH, -3);
dateInPast.set(Calendar.MILLISECOND, 0);
control = XMLBuilder.create("delay")
.a("xmlns", "urn:xmpp:delay")
.a("from", "capulet.com")
.a("stamp", dateFormat.format(dateInPast.getTime()))
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(getParser(control, "delay"));
assertEquals(dateInPast.getTime(), delayInfo.getStamp());
// XEP-0091 date format from SMACK-243
control = XMLBuilder.create("delay")
.a("xmlns", "urn:xmpp:delay")
.a("from", "capulet.com")
.a("stamp", "200868T09:16:20")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(getParser(control, "delay"));
Date controlDate = StringUtils.parseXEP0082Date("2008-06-08T09:16:20.0Z");
assertEquals(controlDate, delayInfo.getStamp());
// invalid date format
control = XMLBuilder.create("delay")
.a("xmlns", "urn:xmpp:delay")
.a("from", "capulet.com")
.a("stamp", "yesterday")
.asString(outputProperties);
delayInfo = (DelayInfo) p.parseExtension(getParser(control, "delay"));
assertNotNull(delayInfo.getStamp());
}