• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.geojson.jackson;
2 
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import org.geojson.LngLatAlt;
5 import org.geojson.Polygon;
6 import org.junit.Test;
7 
8 import java.util.Arrays;
9 import java.util.Collections;
10 import java.util.List;
11 
12 import static org.junit.Assert.assertEquals;
13 
14 public class PolygonTest {
15 
16 	private ObjectMapper mapper = new ObjectMapper();
17 
18 	@Test
itShouldSerialize()19 	public void itShouldSerialize() throws Exception {
20 		Polygon polygon = new Polygon(MockData.EXTERNAL);
21 		assertEquals("{\"type\":\"Polygon\",\"coordinates\":"
22 				+ "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]]]}",
23 				mapper.writeValueAsString(polygon));
24 	}
25 
26 	@Test
itShouldSerializeWithHole()27 	public void itShouldSerializeWithHole() throws Exception {
28 		Polygon polygon = new Polygon(MockData.EXTERNAL);
29 		polygon.addInteriorRing(MockData.INTERNAL);
30 		assertEquals("{\"type\":\"Polygon\",\"coordinates\":"
31 				+ "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],"
32 				+ "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]}", mapper.writeValueAsString(polygon));
33 	}
34 
35 	@Test(expected = RuntimeException.class)
itShouldFailOnAddInteriorRingWithoutExteriorRing()36 	public void itShouldFailOnAddInteriorRingWithoutExteriorRing() throws Exception {
37 		Polygon polygon = new Polygon();
38 		polygon.addInteriorRing(MockData.EXTERNAL);
39 	}
40 
41 	@Test
itShouldDeserialize()42 	public void itShouldDeserialize() throws Exception {
43 		Polygon polygon = mapper.readValue("{\"type\":\"Polygon\",\"coordinates\":"
44 				+ "[[[100.0,0.0],[101.0,0.0],[101.0,1.0],[100.0,1.0],[100.0,0.0]],"
45 				+ "[[100.2,0.2],[100.8,0.2],[100.8,0.8],[100.2,0.8],[100.2,0.2]]]}", Polygon.class);
46 		assertListEquals(MockData.EXTERNAL, polygon.getExteriorRing());
47 		assertListEquals(MockData.INTERNAL, polygon.getInteriorRing(0));
48 		assertListEquals(MockData.INTERNAL, polygon.getInteriorRings().get(0));
49 	}
50 
51 	@Test
itShouldSetExteriorRing()52 	public void itShouldSetExteriorRing() throws Exception {
53 		Polygon polygon = new Polygon();
54 		polygon.setExteriorRing(MockData.EXTERNAL);
55 		assertEquals(MockData.EXTERNAL, polygon.getExteriorRing());
56 	}
57 
58 	@Test
itShouldReplaceExteriorRing()59 	public void itShouldReplaceExteriorRing() throws Exception {
60 		Polygon polygon = new Polygon(Arrays.asList(
61 					new LngLatAlt(0, 0), new LngLatAlt(1, 0), new LngLatAlt(1, 1), new LngLatAlt(0, 1), new LngLatAlt(0, 0)));
62 		polygon.setExteriorRing(MockData.EXTERNAL);
63 		assertEquals(MockData.EXTERNAL, polygon.getExteriorRing());
64 		assertEquals(0, polygon.getInteriorRings().size());
65 	}
66 
assertListEquals(List<LngLatAlt> expectedList, List<LngLatAlt> actualList)67 	private void assertListEquals(List<LngLatAlt> expectedList, List<LngLatAlt> actualList) {
68 		for (int x = 0; x < actualList.size(); x++) {
69 			LngLatAlt expected = expectedList.get(x);
70 			LngLatAlt actual = actualList.get(x);
71 			PointTest.assertLngLatAlt(expected.getLongitude(), expected.getLatitude(), expected.getAltitude(), actual);
72 		}
73 	}
74 }
75