source: josm/trunk/test/unit/org/openstreetmap/josm/io/GeoJSONReaderTest.java

Last change on this file was 18690, checked in by taylor.smock, 14 months ago

See #16567: Convert all assertion calls to JUnit 5 (patch by gaben, modified)

The modifications are as follows:

  • Merge DomainValidatorTest.testIDN and DomainValidatorTest.testIDNJava6OrLater
  • Update some tests to use @ParameterizedTest (DomainValidatorTest)
  • Replace various exception blocks with assertThrows. These typically looked like
        try {
            // Something that should throw an exception here
            fail("An exception should have been thrown");
        } catch (Exception e) {
            // Verify the exception matches expectations here
        }
    
  • Replace assertTrue(val instanceof Clazz) with assertInstanceOf
  • Replace JUnit 4 @Suite with JUnit 5 @Suite

Both the original patch and the modified patch fix various lint issues.

  • Property svn:eol-style set to native
File size: 10.1 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5import static org.junit.jupiter.api.Assertions.assertFalse;
6import static org.junit.jupiter.api.Assertions.assertInstanceOf;
7import static org.junit.jupiter.api.Assertions.assertNull;
8import static org.junit.jupiter.api.Assertions.assertThrows;
9import static org.junit.jupiter.api.Assertions.assertTrue;
10
11import java.io.ByteArrayInputStream;
12import java.io.InputStream;
13import java.nio.charset.StandardCharsets;
14import java.nio.file.Files;
15import java.nio.file.Paths;
16import java.util.ArrayList;
17import java.util.Collection;
18import java.util.List;
19import java.util.Optional;
20import java.util.stream.IntStream;
21
22import org.openstreetmap.josm.TestUtils;
23import org.openstreetmap.josm.data.coor.LatLon;
24import org.openstreetmap.josm.data.osm.Node;
25import org.openstreetmap.josm.data.osm.OsmPrimitive;
26import org.openstreetmap.josm.data.osm.Relation;
27import org.openstreetmap.josm.data.osm.Way;
28import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
29
30import org.junit.jupiter.api.Test;
31
32/**
33 * Unit tests of {@link GeoJSONReader}.
34 */
35@BasicPreferences
36class GeoJSONReaderTest {
37 /**
38 * Test reading a GeoJSON file.
39 * @throws Exception in case of error
40 */
41 @Test
42 void testReadGeoJson() throws Exception {
43 try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "geo.json"))) {
44 final List<OsmPrimitive> primitives = new ArrayList<>(new GeoJSONReader()
45 .doParseDataSet(in, null)
46 .getPrimitives(it -> true));
47
48 assertExpectedGeoPrimitives(primitives);
49 }
50 }
51
52 /**
53 * Tests reading a GeoJSON file that is line by line separated, per RFC 7464
54 * @throws Exception in case of an error
55 */
56 @Test
57 void testReadLineByLineGeoJSON() throws Exception {
58 try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "geoLineByLine.json"))) {
59 final List<OsmPrimitive> primitives = new ArrayList<>(new GeoJSONReader()
60 .doParseDataSet(in, null)
61 .getPrimitives(it -> true));
62
63 assertExpectedGeoPrimitives(primitives);
64 }
65 }
66
67 private void assertExpectedGeoPrimitives(Collection<OsmPrimitive> primitives) {
68 assertEquals(20, primitives.size());
69
70 final Node node1 = new Node(new LatLon(0.5, 102.0));
71 final Optional<OsmPrimitive> foundNode1 = primitives.stream()
72 .filter(it -> areEqualNodes(it, node1))
73 .findFirst();
74 assertTrue(foundNode1.isPresent());
75 assertEquals("valueA", foundNode1.get().get("propA"));
76
77 final Way way1 = new Way();
78 way1.addNode(new Node(new LatLon(0.5, 102.0)));
79 way1.addNode(new Node(new LatLon(1, 103)));
80 way1.addNode(new Node(new LatLon(0, 104)));
81 way1.addNode(new Node(new LatLon(1, 105)));
82 final Optional<OsmPrimitive> foundWay1 = primitives.stream()
83 .filter(it -> areEqualWays(it, way1))
84 .findFirst();
85 assertTrue(foundWay1.isPresent());
86 assertEquals("valueB", foundWay1.get().get("propB"));
87 assertEquals("0.0", foundWay1.get().get("propB2"));
88 assertEquals(foundNode1.get(), ((Way) foundWay1.get()).firstNode());
89 assertEquals("valueA", ((Way) foundWay1.get()).firstNode().get("propA"));
90
91 final Way way2 = new Way();
92 way2.addNode(new Node(new LatLon(40, 180)));
93 way2.addNode(new Node(new LatLon(50, 180)));
94 way2.addNode(new Node(new LatLon(50, 170)));
95 way2.addNode(new Node(new LatLon(40, 170)));
96 way2.addNode(new Node(new LatLon(40, 180)));
97 final Optional<OsmPrimitive> foundWay2 = primitives.stream()
98 .filter(it -> areEqualWays(it, way2))
99 .findFirst();
100 assertTrue(foundWay2.isPresent());
101 assertEquals(
102 ((Way) foundWay2.get()).getNode(0),
103 ((Way) foundWay2.get()).getNode(((Way) foundWay2.get()).getNodesCount() - 1)
104 );
105
106 final Way way3 = new Way();
107 way3.addNode(new Node(new LatLon(40, -170)));
108 way3.addNode(new Node(new LatLon(50, -170)));
109 way3.addNode(new Node(new LatLon(50, -180)));
110 way3.addNode(new Node(new LatLon(40, -180)));
111 way3.addNode(new Node(new LatLon(40, -170)));
112 final Optional<OsmPrimitive> foundWay3 = primitives.stream()
113 .filter(it -> areEqualWays(it, way3))
114 .findFirst();
115 assertTrue(foundWay3.isPresent());
116 assertEquals(
117 ((Way) foundWay3.get()).getNode(0),
118 ((Way) foundWay3.get()).getNode(((Way) foundWay3.get()).getNodesCount() - 1)
119 );
120
121 final Way way4 = new Way();
122 way4.addNode(new Node(new LatLon(0, 100)));
123 way4.addNode(new Node(new LatLon(0, 101)));
124 way4.addNode(new Node(new LatLon(1, 101)));
125 way4.addNode(new Node(new LatLon(1, 100)));
126 way4.addNode(new Node(new LatLon(0, 100)));
127 final Optional<OsmPrimitive> foundWay4 = primitives.stream()
128 .filter(it -> areEqualWays(it, way4))
129 .findFirst();
130 assertTrue(foundWay4.isPresent());
131 assertEquals(
132 ((Way) foundWay4.get()).getNode(0),
133 ((Way) foundWay4.get()).getNode(((Way) foundWay4.get()).getNodesCount() - 1)
134 );
135 assertEquals("valueD", foundWay4.get().get("propD"));
136 assertFalse(foundWay4.get().hasTag("propD2"));
137 assertEquals("true", foundWay4.get().get("propD3"));
138 assertFalse(foundWay4.get().hasKey("propD4"));
139 assertNull(foundWay4.get().get("propD4"));
140 }
141
142 /**
143 * Test reading a GeoJSON file with a named CRS.
144 * @throws Exception in case of error
145 */
146 @Test
147 void testReadGeoJsonNamedCrs() throws Exception {
148 try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "geocrs.json"))) {
149 final List<OsmPrimitive> primitives = new ArrayList<>(new GeoJSONReader()
150 .doParseDataSet(in, null)
151 .getPrimitives(it -> true));
152 assertEquals(24, primitives.size());
153 assertTrue(primitives.stream()
154 .anyMatch(it -> areEqualNodes(it, new Node(new LatLon(52.5840213, 13.1724145)))));
155 }
156 }
157
158 /**
159 * Test reading a JSON file which is not a proper GeoJSON (type missing).
160 */
161 @Test
162 void testReadGeoJsonWithoutType() {
163 assertThrows(IllegalDataException.class, () ->
164 new GeoJSONReader().doParseDataSet(new ByteArrayInputStream("{}".getBytes(StandardCharsets.UTF_8)), null));
165 }
166
167 private static boolean areEqualNodes(final OsmPrimitive p1, final OsmPrimitive p2) {
168 return (p1 instanceof Node)
169 && (p2 instanceof Node)
170 && ((Node) p1).equalsEpsilon(((Node) p2));
171 }
172
173 private static boolean areEqualWays(final OsmPrimitive p1, final OsmPrimitive p2) {
174 if (
175 (!(p1 instanceof Way))
176 || (!(p2 instanceof Way))
177 || ((Way) p1).getNodes().size() != ((Way) p2).getNodes().size()
178 ) {
179 return false;
180 }
181 return IntStream.range(0, ((Way) p1).getNodes().size())
182 .allMatch(i -> areEqualNodes(((Way) p1).getNode(i), ((Way) p2).getNode(i)));
183 }
184
185 /**
186 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/19822">Bug #19822</a>.
187 * @throws Exception in case of error
188 */
189 @Test
190 void testTicket19822() throws Exception {
191 try (InputStream in = TestUtils.getRegressionDataStream(19822, "data.geojson")) {
192 final List<OsmPrimitive> primitives = new ArrayList<>(
193 new GeoJSONReader().doParseDataSet(in, null).getPrimitives(it -> true));
194 assertTrue(primitives.stream().anyMatch(p -> p instanceof Relation && p.isMultipolygon()));
195 assertEquals(3, primitives.stream().filter(Way.class::isInstance).count());
196 }
197 }
198
199 /**
200 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/19822">Bug #19822</a>.
201 * @throws Exception in case of error
202 */
203 @Test
204 void testTicket19822Nested() throws Exception {
205 try (InputStream in = TestUtils.getRegressionDataStream(19822, "problem3.geojson")) {
206 final List<OsmPrimitive> primitives = new ArrayList<>(
207 new GeoJSONReader().doParseDataSet(in, null).getPrimitives(it -> true));
208 assertTrue(primitives.stream().anyMatch(p -> p instanceof Relation && p.isMultipolygon()));
209 assertEquals(3, primitives.stream().filter(Way.class::isInstance).count());
210 }
211 }
212
213 /**
214 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/21044">Bug #21044</a>.
215 * @throws Exception in case of error
216 */
217 @Test
218 void testTicket21044Duplicates() throws Exception {
219 try (InputStream in = TestUtils.getRegressionDataStream(21044, "test.geojson")) {
220 final List<OsmPrimitive> primitives = new ArrayList<>(
221 new GeoJSONReader().doParseDataSet(in, null).getPrimitives(it -> true));
222 assertEquals(1, primitives.size());
223 OsmPrimitive primitive = primitives.get(0);
224 Node n = assertInstanceOf(Node.class, primitive);
225 assertNull(n.get("addr:building"));
226 assertEquals("06883", n.get("addr:postcode"));
227 assertEquals("22;26", n.get("addr:housenumber"));
228 }
229 }
230
231 /**
232 * Tests error reporting for an invalid FeatureCollection
233 * @throws Exception in case of error
234 */
235 @Test
236 void testInvalidFeatureCollection() throws Exception {
237 String featureCollection = "{\"type\": \"FeatureCollection\", \"features\": {}}";
238 try (InputStream in = new ByteArrayInputStream(featureCollection.getBytes(StandardCharsets.UTF_8))) {
239 IllegalDataException exception = assertThrows(IllegalDataException.class,
240 () -> new GeoJSONReader().doParseDataSet(in, null));
241 assertEquals("java.lang.IllegalArgumentException: features must be ARRAY, but is OBJECT", exception.getMessage());
242 }
243 }
244}
Note: See TracBrowser for help on using the repository browser.