1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io;
|
---|
3 |
|
---|
4 | import static org.junit.Assert.assertEquals;
|
---|
5 | import static org.junit.Assert.assertTrue;
|
---|
6 |
|
---|
7 | import java.io.ByteArrayInputStream;
|
---|
8 | import java.io.File;
|
---|
9 | import java.io.FileInputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.util.ArrayList;
|
---|
13 |
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.openstreetmap.josm.TestUtils;
|
---|
16 | import org.openstreetmap.josm.data.Bounds;
|
---|
17 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
18 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
19 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
20 | import org.xml.sax.SAXException;
|
---|
21 |
|
---|
22 | /**
|
---|
23 | * Tests the {@link GpxReader}.
|
---|
24 | */
|
---|
25 | public class GpxReaderTest {
|
---|
26 |
|
---|
27 | /**
|
---|
28 | * Parses a GPX file and returns the parsed data
|
---|
29 | * @param filename the GPX file to parse
|
---|
30 | * @return the parsed GPX data
|
---|
31 | * @throws IOException if an error occurs during reading.
|
---|
32 | * @throws SAXException if a SAX error occurs
|
---|
33 | */
|
---|
34 | public static GpxData parseGpxData(String filename) throws IOException, SAXException {
|
---|
35 | final GpxData result;
|
---|
36 | try (FileInputStream in = new FileInputStream(new File(filename))) {
|
---|
37 | GpxReader reader = new GpxReader(in);
|
---|
38 | assertTrue(reader.parse(false));
|
---|
39 | result = reader.getGpxData();
|
---|
40 | }
|
---|
41 | return result;
|
---|
42 | }
|
---|
43 |
|
---|
44 | /**
|
---|
45 | * Tests the {@code munich.gpx} test file.
|
---|
46 | * @throws Exception if something goes wrong
|
---|
47 | */
|
---|
48 | @Test
|
---|
49 | public void testMunich() throws Exception {
|
---|
50 | final GpxData result = parseGpxData("data_nodist/munich.gpx");
|
---|
51 | assertEquals(2762, result.getTracks().size());
|
---|
52 | assertEquals(0, result.getRoutes().size());
|
---|
53 | assertEquals(903, result.getWaypoints().size());
|
---|
54 |
|
---|
55 | final WayPoint tenthWayPoint = new ArrayList<>(result.getWaypoints()).get(10);
|
---|
56 | assertEquals("128970", tenthWayPoint.get(GpxData.GPX_NAME));
|
---|
57 | assertEquals(new LatLon(48.183956146240234, 11.43463134765625), tenthWayPoint.getCoor());
|
---|
58 | }
|
---|
59 |
|
---|
60 | /**
|
---|
61 | * Tests invalid data.
|
---|
62 | * @throws Exception always SAXException
|
---|
63 | */
|
---|
64 | @Test(expected = SAXException.class)
|
---|
65 | public void testException() throws Exception {
|
---|
66 | new GpxReader(new ByteArrayInputStream("--foo--bar--".getBytes(StandardCharsets.UTF_8))).parse(true);
|
---|
67 | }
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/15634">#15634</a>
|
---|
71 | * @throws IOException if an error occurs during reading
|
---|
72 | * @throws SAXException if any XML error occurs
|
---|
73 | */
|
---|
74 | @Test
|
---|
75 | public void testTicket15634() throws IOException, SAXException {
|
---|
76 | assertEquals(new Bounds(53.7229357, -7.9135019, 53.9301103, -7.59656),
|
---|
77 | GpxReaderTest.parseGpxData(TestUtils.getRegressionDataFile(15634, "drumlish.gpx")).getMetaBounds());
|
---|
78 | }
|
---|
79 | }
|
---|