mirror of
https://github.com/vanitasvitae/Smack.git
synced 2025-09-15 03:59:38 +02:00
Add Support for XEP-80: User Location
SMACK-610
This commit is contained in:
parent
e126469003
commit
0d8f3185e5
8 changed files with 1021 additions and 0 deletions
|
@ -0,0 +1,125 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015-2016 Ishan Khanna
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* 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.
|
||||
*/
|
||||
package org.jivesoftware.smackx.geoloc.packet;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.net.URI;
|
||||
import java.util.Calendar;
|
||||
import java.util.TimeZone;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.jivesoftware.smackx.InitExtensions;
|
||||
import org.jivesoftware.smackx.time.packet.Time;
|
||||
import org.junit.Test;
|
||||
import org.jxmpp.util.XmppDateTime;
|
||||
|
||||
/**
|
||||
* Unit tests for GeoLocation.
|
||||
*
|
||||
* @author Ishan Khanna
|
||||
*/
|
||||
public class GeoLocationTest extends InitExtensions {
|
||||
|
||||
@Test
|
||||
public void negativeTimezoneTest() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeZone(TimeZone.getTimeZone("GMT-830"));
|
||||
|
||||
Time time = new Time(calendar);
|
||||
|
||||
GeoLocation geoLocation = new GeoLocation.Builder().setTzo(time.getTzo()).build();
|
||||
|
||||
assertEquals("-8:30", geoLocation.getTzo());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void positiveTimezonTest() {
|
||||
Calendar calendar = Calendar.getInstance();
|
||||
calendar.setTimeZone(TimeZone.getTimeZone("GMT+530"));
|
||||
|
||||
Time time = new Time(calendar);
|
||||
|
||||
GeoLocation geoLocation = new GeoLocation.Builder().setTzo(time.getTzo()).build();
|
||||
|
||||
assertEquals("+5:30", geoLocation.getTzo());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void accuracyTest() {
|
||||
|
||||
GeoLocation geoLocation = new GeoLocation.Builder().setAccuracy(1.34d).build();
|
||||
|
||||
assertEquals((Double) 1.34, geoLocation.getAccuracy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toXMLMethodTest() throws Exception {
|
||||
|
||||
// @formatter:off
|
||||
final String geoLocationMessageString = "<message from='portia@merchantofvenice.lit'"
|
||||
+" to='bassanio@merchantofvenice.lit'>"
|
||||
+"<geoloc xmlns='http://jabber.org/protocol/geoloc'>"
|
||||
+"<accuracy>23</accuracy>"
|
||||
+"<alt>1000</alt>"
|
||||
+"<area>Delhi</area>"
|
||||
+"<bearing>10</bearing>"
|
||||
+"<building>Small Building</building>"
|
||||
+"<country>India</country>"
|
||||
+"<countrycode>IN</countrycode>"
|
||||
+"<description>My Description</description>"
|
||||
+"<error>90</error>"
|
||||
+"<floor>top</floor>"
|
||||
+"<lat>25.098345</lat>"
|
||||
+"<locality>awesome</locality>"
|
||||
+"<lon>77.992034</lon>"
|
||||
+"<postalcode>110085</postalcode>"
|
||||
+"<region>North</region>"
|
||||
+"<room>small</room>"
|
||||
+"<speed>250.0</speed>"
|
||||
+"<street>Wall Street</street>"
|
||||
+"<text>Unit Testing GeoLocation</text>"
|
||||
+"<timestamp>2004-02-19</timestamp>"
|
||||
+"<tzo>+5:30</tzo>"
|
||||
+"<uri>http://xmpp.org</uri>"
|
||||
+"</geoloc>"
|
||||
+"</message>";
|
||||
// @formatter:on
|
||||
|
||||
Message messageWithGeoLocation = (Message) PacketParserUtils.parseStanza(geoLocationMessageString);
|
||||
assertNotNull(messageWithGeoLocation);
|
||||
|
||||
GeoLocation geoLocation = (GeoLocation) messageWithGeoLocation.getExtension(GeoLocation.ELEMENT,
|
||||
GeoLocation.NAMESPACE);
|
||||
assertNotNull(geoLocation);
|
||||
assertNotNull(geoLocation.toXML());
|
||||
|
||||
GeoLocation constructedGeoLocation = GeoLocation.builder().setAccuracy(23d).setAlt(1000d).setArea("Delhi").setBearing(
|
||||
10d).setBuilding("Small Building").setCountry("India").setCountryCode("IN").setDescription(
|
||||
"My Description").setError(90d).setFloor("top").setLat(25.098345d).setLocality("awesome").setLon(
|
||||
77.992034).setPostalcode("110085").setRegion("North").setRoom("small").setSpeed(250.0d).setStreet(
|
||||
"Wall Street").setText("Unit Testing GeoLocation").setTimestamp(
|
||||
XmppDateTime.parseDate("2004-02-19")).setTzo("+5:30").setUri(new URI("http://xmpp.org")).build();
|
||||
|
||||
assertEquals(constructedGeoLocation.toXML().toString(), geoLocation.toXML().toString());
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,229 @@
|
|||
/**
|
||||
*
|
||||
* Copyright 2015-2016 Ishan Khanna
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* 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.
|
||||
*/
|
||||
package org.jivesoftware.smackx.geoloc.provider;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertNull;
|
||||
|
||||
import java.net.URI;
|
||||
|
||||
import org.jivesoftware.smack.packet.Message;
|
||||
import org.jivesoftware.smack.util.PacketParserUtils;
|
||||
import org.jivesoftware.smackx.InitExtensions;
|
||||
import org.jivesoftware.smackx.geoloc.packet.GeoLocation;
|
||||
import org.junit.Test;
|
||||
import org.jxmpp.util.XmppDateTime;
|
||||
|
||||
public class GeoLocationProviderTest extends InitExtensions {
|
||||
|
||||
@Test
|
||||
public void testGeoLocationProviderWithNoDatumSet() throws Exception {
|
||||
// @formatter:off
|
||||
final String geoLocationString = "<message from='portia@merchantofvenice.lit'"
|
||||
+" to='bassanio@merchantofvenice.lit'>"
|
||||
+"<geoloc xmlns='http://jabber.org/protocol/geoloc'>"
|
||||
+"<accuracy>23</accuracy>"
|
||||
+"<alt>1000</alt>"
|
||||
+"<area>Delhi</area>"
|
||||
+"<bearing>10</bearing>"
|
||||
+"<building>Small Building</building>"
|
||||
+"<country>India</country>"
|
||||
+"<countrycode>IN</countrycode>"
|
||||
+"<description>My Description</description>"
|
||||
+"<error>90</error>"
|
||||
+"<floor>top</floor>"
|
||||
+"<lat>25.098345</lat>"
|
||||
+"<locality>awesome</locality>"
|
||||
+"<lon>77.992034</lon>"
|
||||
+"<postalcode>110085</postalcode>"
|
||||
+"<region>North</region>"
|
||||
+"<room>small</room>"
|
||||
+"<speed>250.0</speed>"
|
||||
+"<street>Wall Street</street>"
|
||||
+"<text>Unit Testing GeoLocation</text>"
|
||||
+"<timestamp>2004-02-19</timestamp>"
|
||||
+"<tzo>+5:30</tzo>"
|
||||
+"<uri>http://xmpp.org</uri>"
|
||||
+"</geoloc>"
|
||||
+"</message>";
|
||||
// @formatter:on
|
||||
|
||||
Message messageWithGeoLocation = (Message) PacketParserUtils.parseStanza(geoLocationString);
|
||||
assertNotNull(messageWithGeoLocation);
|
||||
|
||||
GeoLocation geoLocation = (GeoLocation) messageWithGeoLocation.getExtension(GeoLocation.ELEMENT,
|
||||
GeoLocation.NAMESPACE);
|
||||
assertNotNull(geoLocation);
|
||||
|
||||
assertEquals((Double) 23d, geoLocation.getAccuracy());
|
||||
assertEquals((Double) 1000d, geoLocation.getAlt());
|
||||
assertEquals("Delhi", geoLocation.getArea());
|
||||
assertEquals((Double) 10d, geoLocation.getBearing());
|
||||
assertEquals("Small Building", geoLocation.getBuilding());
|
||||
assertEquals("India", geoLocation.getCountry());
|
||||
assertEquals("IN", geoLocation.getCountryCode());
|
||||
assertEquals("WGS84", geoLocation.getDatum());
|
||||
assertEquals("My Description", geoLocation.getDescription());
|
||||
assertNull(geoLocation.getError());
|
||||
assertEquals("top", geoLocation.getFloor());
|
||||
assertEquals((Double) 25.098345d, geoLocation.getLat());
|
||||
assertEquals("awesome", geoLocation.getLocality());
|
||||
assertEquals((Double) 77.992034d, geoLocation.getLon());
|
||||
assertEquals("110085", geoLocation.getPostalcode());
|
||||
assertEquals("North", geoLocation.getRegion());
|
||||
assertEquals("small", geoLocation.getRoom());
|
||||
assertEquals((Double) 250d, geoLocation.getSpeed());
|
||||
assertEquals("Wall Street", geoLocation.getStreet());
|
||||
assertEquals("Unit Testing GeoLocation", geoLocation.getText());
|
||||
assertEquals(XmppDateTime.parseDate("2004-02-19"), geoLocation.getTimestamp());
|
||||
assertEquals("+5:30", geoLocation.getTzo());
|
||||
assertEquals(new URI("http://xmpp.org"), geoLocation.getUri());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeoLocationWithDatumSet() throws Exception {
|
||||
|
||||
// @formatter:off
|
||||
final String geoLocationString = "<message from='portia@merchantofvenice.lit'"
|
||||
+" to='bassanio@merchantofvenice.lit'>"
|
||||
+"<geoloc xmlns='http://jabber.org/protocol/geoloc'>"
|
||||
+"<accuracy>23</accuracy>"
|
||||
+"<alt>1000</alt>"
|
||||
+"<area>Delhi</area>"
|
||||
+"<bearing>10</bearing>"
|
||||
+"<building>Small Building</building>"
|
||||
+"<country>India</country>"
|
||||
+"<countrycode>IN</countrycode>"
|
||||
+"<datum>Test Datum</datum>"
|
||||
+"<description>My Description</description>"
|
||||
+"<error>90</error>"
|
||||
+"<floor>top</floor>"
|
||||
+"<lat>25.098345</lat>"
|
||||
+"<locality>awesome</locality>"
|
||||
+"<lon>77.992034</lon>"
|
||||
+"<postalcode>110085</postalcode>"
|
||||
+"<region>North</region>"
|
||||
+"<room>small</room>"
|
||||
+"<speed>250.0</speed>"
|
||||
+"<street>Wall Street</street>"
|
||||
+"<text>Unit Testing GeoLocation</text>"
|
||||
+"<timestamp>2004-02-19</timestamp>"
|
||||
+"<tzo>+5:30</tzo>"
|
||||
+"<uri>http://xmpp.org</uri>"
|
||||
+"</geoloc>"
|
||||
+"</message>";
|
||||
// @formatter:on
|
||||
|
||||
Message messageWithGeoLocation = (Message) PacketParserUtils.parseStanza(geoLocationString);
|
||||
assertNotNull(messageWithGeoLocation);
|
||||
|
||||
GeoLocation geoLocation = (GeoLocation) messageWithGeoLocation.getExtension(GeoLocation.ELEMENT,
|
||||
GeoLocation.NAMESPACE);
|
||||
assertNotNull(geoLocation);
|
||||
|
||||
assertEquals((Double) 23d, geoLocation.getAccuracy());
|
||||
assertEquals((Double) 1000d, geoLocation.getAlt());
|
||||
assertEquals("Delhi", geoLocation.getArea());
|
||||
assertEquals((Double) 10d, geoLocation.getBearing());
|
||||
assertEquals("Small Building", geoLocation.getBuilding());
|
||||
assertEquals("India", geoLocation.getCountry());
|
||||
assertEquals("IN", geoLocation.getCountryCode());
|
||||
assertEquals("Test Datum", geoLocation.getDatum());
|
||||
assertEquals("My Description", geoLocation.getDescription());
|
||||
assertNull(geoLocation.getError());
|
||||
assertEquals("top", geoLocation.getFloor());
|
||||
assertEquals((Double) 25.098345d, geoLocation.getLat());
|
||||
assertEquals("awesome", geoLocation.getLocality());
|
||||
assertEquals((Double) 77.992034d, geoLocation.getLon());
|
||||
assertEquals("110085", geoLocation.getPostalcode());
|
||||
assertEquals("North", geoLocation.getRegion());
|
||||
assertEquals("small", geoLocation.getRoom());
|
||||
assertEquals((Double) 250d, geoLocation.getSpeed());
|
||||
assertEquals("Wall Street", geoLocation.getStreet());
|
||||
assertEquals("Unit Testing GeoLocation", geoLocation.getText());
|
||||
assertEquals(XmppDateTime.parseDate("2004-02-19"), geoLocation.getTimestamp());
|
||||
assertEquals("+5:30", geoLocation.getTzo());
|
||||
assertEquals(new URI("http://xmpp.org"), geoLocation.getUri());
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeoLocationWithoutAccuracySetAndWithErrorSet() throws Exception {
|
||||
|
||||
// @formatter:off
|
||||
final String geoLocationString = "<message from='portia@merchantofvenice.lit'"
|
||||
+" to='bassanio@merchantofvenice.lit'>"
|
||||
+"<geoloc xmlns='http://jabber.org/protocol/geoloc'>"
|
||||
+"<error>90</error>"
|
||||
+"</geoloc>"
|
||||
+"</message>";
|
||||
// @formatter:on
|
||||
|
||||
Message messageWithGeoLocation = (Message) PacketParserUtils.parseStanza(geoLocationString);
|
||||
|
||||
GeoLocation geoLocation = (GeoLocation) messageWithGeoLocation.getExtension(GeoLocation.ELEMENT,
|
||||
GeoLocation.NAMESPACE);
|
||||
|
||||
assertEquals((Double) 90d, geoLocation.getError());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeoLocationWithAccuracySetAndWithoutErrorSet() throws Exception {
|
||||
|
||||
// @formatter:off
|
||||
final String geoLocationString = "<message from='portia@merchantofvenice.lit'"
|
||||
+" to='bassanio@merchantofvenice.lit'>"
|
||||
+"<geoloc xmlns='http://jabber.org/protocol/geoloc'>"
|
||||
+"<accuracy>90</accuracy>"
|
||||
+"</geoloc>"
|
||||
+"</message>";
|
||||
// @formatter:on
|
||||
|
||||
Message messageWithGeoLocation = (Message) PacketParserUtils.parseStanza(geoLocationString);
|
||||
|
||||
GeoLocation geoLocation = (GeoLocation) messageWithGeoLocation.getExtension(GeoLocation.ELEMENT,
|
||||
GeoLocation.NAMESPACE);
|
||||
|
||||
assertEquals((Double) 90d, geoLocation.getAccuracy());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGeoLocationWithAccuracySetAndErrorSet() throws Exception {
|
||||
|
||||
// @formatter:off
|
||||
final String geoLocationString = "<message from='portia@merchantofvenice.lit'"
|
||||
+" to='bassanio@merchantofvenice.lit'>"
|
||||
+"<geoloc xmlns='http://jabber.org/protocol/geoloc'>"
|
||||
+"<accuracy>90</accuracy>"
|
||||
+"<error>100</error>"
|
||||
+"</geoloc>"
|
||||
+"</message>";
|
||||
// @formatter:on
|
||||
|
||||
Message messageWithGeoLocation = (Message) PacketParserUtils.parseStanza(geoLocationString);
|
||||
|
||||
GeoLocation geoLocation = (GeoLocation) messageWithGeoLocation.getExtension(GeoLocation.ELEMENT,
|
||||
GeoLocation.NAMESPACE);
|
||||
|
||||
assertEquals((Double) 90d, geoLocation.getAccuracy());
|
||||
assertNull(geoLocation.getError());
|
||||
|
||||
}
|
||||
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue