2010-08-15 15:20:48 +02:00
|
|
|
/**
|
2014-02-17 18:57:38 +01:00
|
|
|
*
|
|
|
|
* Copyright the original author or authors
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
2010-08-15 15:20:48 +02:00
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
2014-02-18 10:24:59 +01:00
|
|
|
package org.jivesoftware.smackx.delay.provider;
|
2010-08-15 15:20:48 +02:00
|
|
|
|
2014-03-10 21:48:46 +01:00
|
|
|
import static org.junit.Assert.assertEquals;
|
|
|
|
import static org.junit.Assert.assertNotNull;
|
|
|
|
import static org.junit.Assert.assertNull;
|
2010-08-15 15:20:48 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
import java.io.IOException;
|
2010-08-15 15:20:48 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
|
|
|
import java.util.GregorianCalendar;
|
|
|
|
import java.util.Properties;
|
|
|
|
import java.util.TimeZone;
|
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
import javax.xml.parsers.FactoryConfigurationError;
|
|
|
|
|
2014-03-10 21:48:46 +01:00
|
|
|
import org.jivesoftware.smack.packet.Presence;
|
|
|
|
import org.jivesoftware.smack.util.PacketParserUtils;
|
2014-06-01 12:23:13 +02:00
|
|
|
import org.jxmpp.util.XmppDateTime;
|
2014-03-10 21:48:46 +01:00
|
|
|
import org.jivesoftware.smackx.InitExtensions;
|
2014-06-30 13:36:49 +02:00
|
|
|
import org.jivesoftware.smackx.delay.DelayInformationManager;
|
2014-02-15 11:35:08 +01:00
|
|
|
import org.jivesoftware.smackx.delay.packet.DelayInformation;
|
2010-08-15 15:20:48 +02:00
|
|
|
import org.junit.Test;
|
|
|
|
import org.xmlpull.v1.XmlPullParser;
|
2014-09-11 09:49:16 +02:00
|
|
|
import org.xmlpull.v1.XmlPullParserException;
|
2010-08-15 15:20:48 +02:00
|
|
|
|
|
|
|
import com.jamesmurty.utils.XMLBuilder;
|
|
|
|
|
2014-03-10 21:48:46 +01:00
|
|
|
public class DelayInformationTest extends InitExtensions {
|
2010-08-15 15:20:48 +02:00
|
|
|
|
2014-09-11 09:49:16 +02:00
|
|
|
private static final Calendar calendar = new GregorianCalendar(2002, 9 - 1, 10, 23, 8, 25);
|
2010-08-15 15:20:48 +02:00
|
|
|
private static Properties outputProperties = new Properties();
|
|
|
|
static {
|
|
|
|
outputProperties.put(javax.xml.transform.OutputKeys.OMIT_XML_DECLARATION, "yes");
|
2014-09-11 09:49:16 +02:00
|
|
|
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
|
2010-08-15 15:20:48 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void delayInformationTest() throws Exception {
|
|
|
|
DelayInformationProvider p = new DelayInformationProvider();
|
|
|
|
DelayInformation delayInfo;
|
|
|
|
XmlPullParser parser;
|
|
|
|
String control;
|
|
|
|
GregorianCalendar calendar = new GregorianCalendar(2002, 9 - 1, 10, 23, 8, 25);
|
|
|
|
calendar.setTimeZone(TimeZone.getTimeZone("UTC"));
|
|
|
|
Date date = calendar.getTime();
|
|
|
|
|
|
|
|
control = XMLBuilder.create("x")
|
|
|
|
.a("xmlns", "jabber:x:delay")
|
|
|
|
.a("from", "capulet.com")
|
|
|
|
.a("stamp", "2002-09-10T23:08:25Z")
|
|
|
|
.t("Offline Storage")
|
|
|
|
.asString(outputProperties);
|
2014-03-10 21:48:46 +01:00
|
|
|
|
2014-05-25 23:31:58 +02:00
|
|
|
parser = PacketParserUtils.getParserFor(control);
|
2014-10-07 21:15:20 +02:00
|
|
|
delayInfo = (DelayInformation) p.parse(parser);
|
2010-08-15 15:20:48 +02:00
|
|
|
|
|
|
|
assertEquals("capulet.com", delayInfo.getFrom());
|
|
|
|
assertEquals(date, delayInfo.getStamp());
|
|
|
|
assertEquals("Offline Storage", delayInfo.getReason());
|
|
|
|
|
|
|
|
assertEquals(XmlPullParser.END_TAG, parser.getEventType());
|
|
|
|
assertEquals("x", parser.getName());
|
|
|
|
|
|
|
|
control = XMLBuilder.create("x")
|
|
|
|
.a("xmlns", "jabber:x:delay")
|
|
|
|
.a("from", "capulet.com")
|
|
|
|
.a("stamp", "2002-09-10T23:08:25Z")
|
|
|
|
.asString(outputProperties);
|
2014-03-10 21:48:46 +01:00
|
|
|
|
2014-05-25 23:31:58 +02:00
|
|
|
parser = PacketParserUtils.getParserFor(control);
|
2014-10-07 21:15:20 +02:00
|
|
|
delayInfo = (DelayInformation) p.parse(parser);
|
2010-08-15 15:20:48 +02:00
|
|
|
|
|
|
|
assertEquals("capulet.com", delayInfo.getFrom());
|
|
|
|
assertEquals(date, delayInfo.getStamp());
|
|
|
|
assertNull(delayInfo.getReason());
|
|
|
|
|
|
|
|
assertEquals(XmlPullParser.END_TAG, parser.getEventType());
|
|
|
|
assertEquals("x", parser.getName());
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void dateFormatsTest() throws Exception {
|
2014-06-30 13:36:49 +02:00
|
|
|
DelayInformationProvider p = new DelayInformationProvider();
|
|
|
|
DelayInformation delayInfo;
|
2010-08-15 15:20:48 +02:00
|
|
|
String control;
|
|
|
|
|
|
|
|
// 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);
|
2014-03-10 21:48:46 +01:00
|
|
|
|
2014-10-07 21:15:20 +02:00
|
|
|
delayInfo = (DelayInformation) p.parse(PacketParserUtils.getParserFor(control));
|
2010-08-15 15:20:48 +02:00
|
|
|
|
|
|
|
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);
|
2014-03-10 21:48:46 +01:00
|
|
|
|
2014-10-07 21:15:20 +02:00
|
|
|
delayInfo = (DelayInformation) p.parse(PacketParserUtils.getParserFor(control));
|
2014-03-10 21:48:46 +01:00
|
|
|
|
2010-08-15 15:20:48 +02:00
|
|
|
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);
|
|
|
|
|
2014-10-07 21:15:20 +02:00
|
|
|
delayInfo = (DelayInformation) p.parse(PacketParserUtils.getParserFor(control));
|
2010-08-15 15:20:48 +02:00
|
|
|
|
|
|
|
assertEquals(calendar.getTime(), delayInfo.getStamp());
|
2014-09-11 09:49:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void legacyDateFormatsTest() throws FactoryConfigurationError, XmlPullParserException, IOException, Exception {
|
|
|
|
LegacyDelayInformationProvider p = new LegacyDelayInformationProvider();
|
|
|
|
DelayInformation delayInfo;
|
|
|
|
|
|
|
|
String control;
|
2010-08-15 15:20:48 +02:00
|
|
|
|
|
|
|
// XEP-0091 date format
|
2014-06-30 13:36:49 +02:00
|
|
|
control = XMLBuilder.create("x")
|
|
|
|
.a("xmlns", "jabber:x:delay")
|
2010-08-15 15:20:48 +02:00
|
|
|
.a("from", "capulet.com")
|
|
|
|
.a("stamp", "20020910T23:08:25")
|
|
|
|
.asString(outputProperties);
|
|
|
|
|
2014-10-07 21:15:20 +02:00
|
|
|
delayInfo = (DelayInformation) p.parse(PacketParserUtils.getParserFor(control));
|
2010-08-15 15:20:48 +02:00
|
|
|
|
|
|
|
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);
|
|
|
|
|
2014-06-30 13:36:49 +02:00
|
|
|
control = XMLBuilder.create("x")
|
|
|
|
.a("xmlns", "jabber:x:delay")
|
2010-08-15 15:20:48 +02:00
|
|
|
.a("from", "capulet.com")
|
|
|
|
.a("stamp", dateFormat.format(dateInPast.getTime()))
|
|
|
|
.asString(outputProperties);
|
2014-03-10 21:48:46 +01:00
|
|
|
|
2014-10-07 21:15:20 +02:00
|
|
|
delayInfo = (DelayInformation) p.parse(PacketParserUtils.getParserFor(control));
|
2014-03-10 21:48:46 +01:00
|
|
|
|
2010-08-15 15:20:48 +02:00
|
|
|
assertEquals(dateInPast.getTime(), delayInfo.getStamp());
|
|
|
|
|
|
|
|
// XEP-0091 date format from SMACK-243
|
2014-06-30 13:36:49 +02:00
|
|
|
control = XMLBuilder.create("x")
|
|
|
|
.a("xmlns", "jabber:x:delay")
|
2010-08-15 15:20:48 +02:00
|
|
|
.a("from", "capulet.com")
|
|
|
|
.a("stamp", "200868T09:16:20")
|
|
|
|
.asString(outputProperties);
|
2014-03-10 21:48:46 +01:00
|
|
|
|
2014-10-07 21:15:20 +02:00
|
|
|
delayInfo = (DelayInformation) p.parse(PacketParserUtils.getParserFor(control));
|
2014-03-10 21:48:46 +01:00
|
|
|
Date controlDate = XmppDateTime.parseDate("2008-06-08T09:16:20.0Z");
|
2010-08-15 15:20:48 +02:00
|
|
|
|
|
|
|
assertEquals(controlDate, delayInfo.getStamp());
|
|
|
|
}
|
2014-03-10 21:48:46 +01:00
|
|
|
|
|
|
|
@Test
|
|
|
|
public void validatePresenceWithDelayedDelivery() throws Exception {
|
|
|
|
String stanza = "<presence from='mercutio@example.com' to='juliet@example.com'>"
|
|
|
|
+ "<delay xmlns='urn:xmpp:delay' stamp='2002-09-10T23:41:07Z'/></presence>";
|
|
|
|
|
2014-05-25 23:31:58 +02:00
|
|
|
Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza));
|
2014-03-10 21:48:46 +01:00
|
|
|
|
2014-06-30 13:36:49 +02:00
|
|
|
DelayInformation delay = DelayInformationManager.getXep203DelayInformation(presence);
|
2014-03-10 21:48:46 +01:00
|
|
|
assertNotNull(delay);
|
|
|
|
Date date = XmppDateTime.parseDate("2002-09-10T23:41:07Z");
|
|
|
|
assertEquals(date, delay.getStamp());
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void parsePresenceWithInvalidLegacyDelayed() throws Exception {
|
|
|
|
String stanza = "<presence from='mercutio@example.com' to='juliet@example.com'>"
|
|
|
|
+ "<x xmlns='jabber:x:delay'/></presence>";
|
|
|
|
|
2014-05-25 23:31:58 +02:00
|
|
|
Presence presence = PacketParserUtils.parsePresence(PacketParserUtils.getParserFor(stanza));
|
2014-06-30 13:36:49 +02:00
|
|
|
DelayInformation delay = DelayInformationManager.getXep203DelayInformation(presence);
|
2014-03-10 21:48:46 +01:00
|
|
|
assertNull((Object)delay);
|
|
|
|
}
|
2010-08-15 15:20:48 +02:00
|
|
|
}
|