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.List;
|
---|
13 |
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
16 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
17 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
18 | import org.xml.sax.SAXException;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Tests the {@link GpxReader}.
|
---|
22 | */
|
---|
23 | public class GpxReaderTest {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Parses a GPX file and returns the parsed data
|
---|
27 | * @param filename the GPX file to parse
|
---|
28 | * @return the parsed GPX data
|
---|
29 | * @throws IOException if an error occurs during reading.
|
---|
30 | * @throws SAXException if a SAX error occurs
|
---|
31 | */
|
---|
32 | public static GpxData parseGpxData(String filename) throws IOException, SAXException {
|
---|
33 | final GpxData result;
|
---|
34 | try (FileInputStream in = new FileInputStream(new File(filename))) {
|
---|
35 | GpxReader reader = new GpxReader(in);
|
---|
36 | assertTrue(reader.parse(false));
|
---|
37 | result = reader.getGpxData();
|
---|
38 | }
|
---|
39 | return result;
|
---|
40 | }
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Tests the {@code munich.gpx} test file.
|
---|
44 | * @throws Exception if something goes wrong
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | public void testMunich() throws Exception {
|
---|
48 | final GpxData result = parseGpxData("data_nodist/munich.gpx");
|
---|
49 | assertEquals(2762, result.tracks.size());
|
---|
50 | assertEquals(0, result.routes.size());
|
---|
51 | assertEquals(903, result.waypoints.size());
|
---|
52 |
|
---|
53 | final WayPoint tenthWayPoint = ((List<WayPoint>) result.waypoints).get(10);
|
---|
54 | assertEquals("128970", tenthWayPoint.get(GpxData.GPX_NAME));
|
---|
55 | assertEquals(new LatLon(48.183956146240234, 11.43463134765625), tenthWayPoint.getCoor());
|
---|
56 | }
|
---|
57 |
|
---|
58 | /**
|
---|
59 | * Tests invalid data.
|
---|
60 | * @throws Exception always SAXException
|
---|
61 | */
|
---|
62 | @Test(expected = SAXException.class)
|
---|
63 | public void testException() throws Exception {
|
---|
64 | new GpxReader(new ByteArrayInputStream("--foo--bar--".getBytes(StandardCharsets.UTF_8))).parse(true);
|
---|
65 | }
|
---|
66 | }
|
---|