/** * * Copyright 2015-2017 Ishan Khanna, Fernando Ramirez * * 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 altAccuracyTest() { GeoLocation geoLocation = new GeoLocation.Builder().setAltAccuracy(1.52d).build(); assertEquals((Double) 1.52, geoLocation.getAltAccuracy()); } @Test public void toXMLMethodTest() throws Exception { // @formatter:off final String geoLocationMessageString = "" + "" + "23" + "1000" + "10" + "Delhi" + "10" + "Small Building" + "India" + "IN" + "My Description" + "90" + "top" + "25.098345" + "awesome" + "77.992034" + "110085" + "North" + "small" + "250.0" + "Wall Street" + "Unit Testing GeoLocation" + "2004-02-19" + "+5:30" + "http://xmpp.org" + "" + ""; // @formatter:on Message messageWithGeoLocation = PacketParserUtils.parseStanza(geoLocationMessageString); assertNotNull(messageWithGeoLocation); GeoLocation geoLocation = messageWithGeoLocation.getExtension(GeoLocation.class); assertNotNull(geoLocation); assertNotNull(geoLocation.toXML()); GeoLocation constructedGeoLocation = GeoLocation.builder().setAccuracy(23d).setAlt(1000d).setAltAccuracy(10d).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()); } }