1 package org.geojson.jackson; 2 3 import com.fasterxml.jackson.core.JsonProcessingException; 4 import com.fasterxml.jackson.databind.ObjectMapper; 5 import org.geojson.GeoJsonObject; 6 import org.geojson.LngLatAlt; 7 import org.geojson.Point; 8 import org.junit.Test; 9 10 import java.io.IOException; 11 import java.util.Arrays; 12 13 import static org.junit.Assert.*; 14 15 public class PointTest { 16 17 private ObjectMapper mapper = new ObjectMapper(); 18 assertLngLatAlt(double expectedLongitude, double expectedLatitude, double expectedAltitude, LngLatAlt point)19 public static void assertLngLatAlt(double expectedLongitude, double expectedLatitude, double expectedAltitude, 20 LngLatAlt point) { 21 assertLngLatAlt(expectedLongitude, expectedLatitude, expectedAltitude, new double[0], point); 22 } 23 assertLngLatAlt(double expectedLongitude, double expectedLatitude, double expectedAltitude, double[] expectedAdditionalElements, LngLatAlt point)24 public static void assertLngLatAlt(double expectedLongitude, double expectedLatitude, double expectedAltitude, 25 double[] expectedAdditionalElements, LngLatAlt point) { 26 assertEquals(expectedLongitude, point.getLongitude(), 0.00001); 27 assertEquals(expectedLatitude, point.getLatitude(), 0.00001); 28 if (Double.isNaN(expectedAltitude)) { 29 assertFalse(point.hasAltitude()); 30 } else { 31 assertEquals(expectedAltitude, point.getAltitude(), 0.00001); 32 assertTrue(Arrays.equals(expectedAdditionalElements, point.getAdditionalElements())); 33 } 34 } 35 36 @Test itShouldSerializeAPoint()37 public void itShouldSerializeAPoint() throws Exception { 38 Point point = new Point(100, 0); 39 assertEquals("{\"type\":\"Point\",\"coordinates\":[100.0,0.0]}", 40 mapper.writeValueAsString(point)); 41 } 42 43 @Test itShouldDeserializeAPoint()44 public void itShouldDeserializeAPoint() throws Exception { 45 GeoJsonObject value = mapper 46 .readValue("{\"type\":\"Point\",\"coordinates\":[100.0,5.0]}", GeoJsonObject.class); 47 assertNotNull(value); 48 assertTrue(value instanceof Point); 49 Point point = (Point)value; 50 assertLngLatAlt(100, 5, Double.NaN, point.getCoordinates()); 51 } 52 53 @Test itShouldDeserializeAPointWithAltitude()54 public void itShouldDeserializeAPointWithAltitude() throws Exception { 55 GeoJsonObject value = mapper.readValue("{\"type\":\"Point\",\"coordinates\":[100.0,5.0,123]}", 56 GeoJsonObject.class); 57 Point point = (Point)value; 58 assertLngLatAlt(100, 5, 123, point.getCoordinates()); 59 } 60 61 @Test itShouldSerializeAPointWithAltitude()62 public void itShouldSerializeAPointWithAltitude() throws Exception { 63 Point point = new Point(100, 0, 256); 64 assertEquals("{\"type\":\"Point\",\"coordinates\":[100.0,0.0,256.0]}", 65 mapper.writeValueAsString(point)); 66 } 67 68 @Test itShouldDeserializeAPointWithAdditionalAttributes()69 public void itShouldDeserializeAPointWithAdditionalAttributes() throws IOException { 70 GeoJsonObject value = mapper.readValue("{\"type\":\"Point\",\"coordinates\":[100.0,5.0,123,456,789.2]}", 71 GeoJsonObject.class); 72 Point point = (Point)value; 73 assertLngLatAlt(100, 5, 123, new double[] {456d, 789.2}, point.getCoordinates()); 74 } 75 76 @Test itShouldSerializeAPointWithAdditionalAttributes()77 public void itShouldSerializeAPointWithAdditionalAttributes() throws JsonProcessingException { 78 Point point = new Point(100, 0, 256, 345d, 678d); 79 assertEquals("{\"type\":\"Point\",\"coordinates\":[100.0,0.0,256.0,345.0,678.0]}", 80 mapper.writeValueAsString(point)); 81 } 82 83 @Test itShouldSerializeAPointWithAdditionalAttributesAndNull()84 public void itShouldSerializeAPointWithAdditionalAttributesAndNull() throws JsonProcessingException { 85 Point point = new Point(100, 0, 256, 345d, 678d); 86 assertEquals("{\"type\":\"Point\",\"coordinates\":[100.0,0.0,256.0,345.0,678.0]}", 87 mapper.writeValueAsString(point)); 88 } 89 } 90