source: josm/trunk/test/unit/org/openstreetmap/josm/io/GeoJSONWriterTest.java@ 16006

Last change on this file since 16006 was 16006, checked in by Don-vip, 4 years ago

see #18140 - reorganization of data(_nodist), images(_nodist), styles(_nodist), IDE and native files in a more practical file tree.

  • Everything belonging to the jar is now in resources (data, images, styles)
  • Everything not belonging to the jar is now in nodist (data, images, styles)
  • Everything related to OS native functions is now in native (linux, macosx, windows)
  • Everything related to an IDE is now in ide (eclipse, netbeans)
File size: 4.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertTrue;
6
7import java.io.InputStream;
8import java.nio.file.Files;
9import java.nio.file.Paths;
10import java.util.Arrays;
11
12import org.junit.BeforeClass;
13import org.junit.Test;
14import org.openstreetmap.josm.JOSMFixture;
15import org.openstreetmap.josm.TestUtils;
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.osm.DataSet;
18import org.openstreetmap.josm.data.osm.Node;
19import org.openstreetmap.josm.data.osm.Way;
20
21/**
22 * Unit tests of {@link GeoJSONWriter} class.
23 */
24public class GeoJSONWriterTest {
25
26 /**
27 * Setup test.
28 */
29 @BeforeClass
30 public static void setUp() {
31 JOSMFixture.createUnitTestFixture().init();
32 }
33
34 /**
35 * Unit test for Point
36 */
37 @Test
38 public void testPoint() {
39 final Node node = new Node(new LatLon(12.3, 4.56));
40 node.put("name", "foo");
41 node.put("source", "code");
42 final DataSet ds = new DataSet();
43 ds.addPrimitive(node);
44 final GeoJSONWriter writer = new GeoJSONWriter(ds);
45 assertEquals(("" +
46 "{\n" +
47 " 'type': 'FeatureCollection',\n" +
48 " 'generator': 'JOSM',\n" +
49 " 'features': [\n" +
50 " {\n" +
51 " 'type': 'Feature',\n" +
52 " 'properties': {\n" +
53 " 'name': 'foo',\n" +
54 " 'source': 'code'\n" +
55 " },\n" +
56 " 'geometry': {\n" +
57 " 'type': 'Point',\n" +
58 " 'coordinates': [\n" +
59 " 4.56000000000,\n" +
60 " 12.30000000000\n" +
61 " ]\n" +
62 " }\n" +
63 " }\n" +
64 " ]\n" +
65 "}").replace("'", "\""), writer.write().trim());
66 }
67
68 /**
69 * Unit test for LineString
70 */
71 @Test
72 public void testLineString() {
73 final DataSet ds = new DataSet();
74 final Node n1 = new Node(new LatLon(12.3, 4.56));
75 final Node n2 = new Node(new LatLon(12.4, 4.57));
76 ds.addPrimitive(n1);
77 ds.addPrimitive(n2);
78 final Way way = new Way();
79 way.put("highway", "footway");
80 way.setNodes(Arrays.asList(n1, n2));
81 ds.addPrimitive(way);
82 final GeoJSONWriter writer = new GeoJSONWriter(ds);
83 assertEquals(("" +
84 "{\n" +
85 " 'type': 'FeatureCollection',\n" +
86 " 'generator': 'JOSM',\n" +
87 " 'features': [\n" +
88 " {\n" +
89 " 'type': 'Feature',\n" +
90 " 'properties': {\n" +
91 " 'highway': 'footway'\n" +
92 " },\n" +
93 " 'geometry': {\n" +
94 " 'type': 'LineString',\n" +
95 " 'coordinates': [\n" +
96 " [\n" +
97 " 4.56000000000,\n" +
98 " 12.30000000000\n" +
99 " ],\n" +
100 " [\n" +
101 " 4.57000000000,\n" +
102 " 12.40000000000\n" +
103 " ]\n" +
104 " ]\n" +
105 " }\n" +
106 " }\n" +
107 " ]\n" +
108 "}").replace("'", "\""), writer.write().trim());
109 }
110
111 /**
112 * Unit test for multipolygon
113 * @throws Exception if an error occurs
114 */
115 @Test
116 public void testMultipolygon() throws Exception {
117 try (InputStream in = Files.newInputStream(Paths.get(TestUtils.getTestDataRoot(), "multipolygon.osm"))) {
118 DataSet ds = OsmReader.parseDataSet(in, null);
119 final GeoJSONWriter writer = new GeoJSONWriter(ds);
120 assertTrue(writer.write().contains("MultiPolygon"));
121 }
122 }
123
124 /**
125 * Unit test for exporting invalid multipolygons, see #13827
126 * @throws Exception if an error occurs
127 */
128 @Test
129 public void testMultipolygonRobustness() throws Exception {
130 try (InputStream in = Files.newInputStream(Paths.get("nodist/data/multipolygon.osm"))) {
131 DataSet ds = OsmReader.parseDataSet(in, null);
132 final GeoJSONWriter writer = new GeoJSONWriter(ds);
133 assertTrue(writer.write().contains("MultiPolygon"));
134 }
135 }
136}
Note: See TracBrowser for help on using the repository browser.