Commit 4697f0b4 authored by csh's avatar csh

OF-638: Unit Tests for XEP-202

git-svn-id: http://svn.igniterealtime.org/svn/repos/openfire/trunk@13944 b35dd754-fafc-0310-a699-88a17e54d16e
parent 260ce296
...@@ -25,6 +25,7 @@ import org.dom4j.Element; ...@@ -25,6 +25,7 @@ import org.dom4j.Element;
import org.dom4j.QName; import org.dom4j.QName;
import org.jivesoftware.openfire.IQHandlerInfo; import org.jivesoftware.openfire.IQHandlerInfo;
import org.jivesoftware.openfire.disco.ServerFeaturesProvider; import org.jivesoftware.openfire.disco.ServerFeaturesProvider;
import org.jivesoftware.util.XMPPDateTimeFormat;
import org.xmpp.packet.IQ; import org.xmpp.packet.IQ;
import javax.xml.bind.DatatypeConverter; import javax.xml.bind.DatatypeConverter;
...@@ -45,7 +46,7 @@ public final class IQEntityTimeHandler extends IQHandler implements ServerFeatur ...@@ -45,7 +46,7 @@ public final class IQEntityTimeHandler extends IQHandler implements ServerFeatur
@Override @Override
public IQ handleIQ(IQ packet) { public IQ handleIQ(IQ packet) {
IQ response = IQ.createResultIQ(packet); IQ response = IQ.createResultIQ(packet);
Element timeElement = DocumentHelper.createElement(QName.get("time", "urn:xmpp:time")); Element timeElement = DocumentHelper.createElement(QName.get(info.getName(), info.getNamespace()));
timeElement.addElement("tzo").setText(formatsTimeZone(TimeZone.getDefault())); timeElement.addElement("tzo").setText(formatsTimeZone(TimeZone.getDefault()));
timeElement.addElement("utc").setText(getUtcDate(new Date())); timeElement.addElement("utc").setText(getUtcDate(new Date()));
response.setChildElement(timeElement); response.setChildElement(timeElement);
...@@ -59,7 +60,7 @@ public final class IQEntityTimeHandler extends IQHandler implements ServerFeatur ...@@ -59,7 +60,7 @@ public final class IQEntityTimeHandler extends IQHandler implements ServerFeatur
@Override @Override
public Iterator<String> getFeatures() { public Iterator<String> getFeatures() {
return Collections.singleton("urn:xmpp:time").iterator(); return Collections.singleton(info.getNamespace()).iterator();
} }
/** /**
...@@ -68,7 +69,8 @@ public final class IQEntityTimeHandler extends IQHandler implements ServerFeatur ...@@ -68,7 +69,8 @@ public final class IQEntityTimeHandler extends IQHandler implements ServerFeatur
* @param tz The time zone. * @param tz The time zone.
* @return The formatted time zone. * @return The formatted time zone.
*/ */
private String formatsTimeZone(TimeZone tz) { String formatsTimeZone(TimeZone tz) {
// package-private for test.
int seconds = Math.abs(tz.getRawOffset()) / 1000; int seconds = Math.abs(tz.getRawOffset()) / 1000;
int hours = seconds / 3600; int hours = seconds / 3600;
int minutes = (seconds % 3600) / 60; int minutes = (seconds % 3600) / 60;
...@@ -81,10 +83,10 @@ public final class IQEntityTimeHandler extends IQHandler implements ServerFeatur ...@@ -81,10 +83,10 @@ public final class IQEntityTimeHandler extends IQHandler implements ServerFeatur
* @param date The date. * @param date The date.
* @return The UTC formatted date. * @return The UTC formatted date.
*/ */
private String getUtcDate(Date date) { String getUtcDate(Date date) {
Calendar calendar = new GregorianCalendar(); // package-private for test.
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
calendar.setTime(date); calendar.setTime(date);
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
// This makes sure the date is formatted as the xs:dateTime type. // This makes sure the date is formatted as the xs:dateTime type.
return DatatypeConverter.printDateTime(calendar); return DatatypeConverter.printDateTime(calendar);
} }
......
package org.jivesoftware.openfire.handler;
import junit.framework.Assert;
import org.dom4j.Element;
import org.jivesoftware.util.XMPPDateTimeFormat;
import org.junit.Test;
import org.xmpp.packet.IQ;
import javax.xml.bind.DatatypeConverter;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;
/**
* @author csh
*/
public class IQEntityTimeHandlerTest {
@Test
public void testIQInfo() {
IQEntityTimeHandler iqEntityTimeHandler = new IQEntityTimeHandler();
Assert.assertTrue(iqEntityTimeHandler.getFeatures().hasNext());
Assert.assertEquals(iqEntityTimeHandler.getFeatures().next(), "urn:xmpp:time");
Assert.assertEquals(iqEntityTimeHandler.getInfo().getNamespace(), "urn:xmpp:time");
Assert.assertEquals(iqEntityTimeHandler.getInfo().getName(), "time");
}
@Test
public void testTimeZone() {
IQEntityTimeHandler iqEntityTimeHandler = new IQEntityTimeHandler();
Assert.assertEquals(iqEntityTimeHandler.formatsTimeZone(TimeZone.getTimeZone("GMT-8:00")), "-08:00");
}
@Test
public void testUtcDate() {
IQEntityTimeHandler iqEntityTimeHandler = new IQEntityTimeHandler();
Date date = new Date();
Calendar calendar = new GregorianCalendar();
calendar.setTime(date);
calendar.setTimeZone(TimeZone.getTimeZone("GMT"));
Assert.assertEquals(iqEntityTimeHandler.getUtcDate(date), DatatypeConverter.printDateTime(calendar));
}
@Test
public void testPerformanceDatatypeConvertVsXMPPDateFormat() {
Date date = new Date();
Calendar calendar = new GregorianCalendar(TimeZone.getTimeZone("GMT"));
calendar.setTime(date);
long start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
DatatypeConverter.printDateTime(calendar);
}
System.out.println(System.currentTimeMillis() - start);
start = System.currentTimeMillis();
for (int i = 0; i < 1000000; i++) {
XMPPDateTimeFormat.format(date);
}
System.out.println(System.currentTimeMillis() - start);
}
@Test
public void testIQ() {
IQEntityTimeHandler iqEntityTimeHandler = new IQEntityTimeHandler();
IQ input = new IQ(IQ.Type.get, "1");
IQ result = iqEntityTimeHandler.handleIQ(input);
Assert.assertEquals(result.getChildElement().getName(), "time");
Assert.assertEquals(result.getChildElement().getNamespace().getText(), "urn:xmpp:time");
Assert.assertEquals(result.getChildElement().content().size(), 2);
Assert.assertTrue(result.getChildElement().content().get(0) instanceof Element);
Assert.assertTrue(result.getChildElement().content().get(1) instanceof Element);
Assert.assertEquals(((Element) result.getChildElement().content().get(0)).getName(), "tzo");
Assert.assertEquals(((Element) result.getChildElement().content().get(1)).getName(), "utc");
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment