source: josm/trunk/test/unit/org/openstreetmap/josm/io/GpxReaderTest.java@ 13878

Last change on this file since 13878 was 13194, checked in by Don-vip, 6 years ago

fix #15634 - add robustness against invalid GPX files

  • Property svn:eol-style set to native
File size: 2.8 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.ByteArrayInputStream;
8import java.io.File;
9import java.io.FileInputStream;
10import java.io.IOException;
11import java.nio.charset.StandardCharsets;
12import java.util.ArrayList;
13
14import org.junit.Test;
15import org.openstreetmap.josm.TestUtils;
16import org.openstreetmap.josm.data.Bounds;
17import org.openstreetmap.josm.data.coor.LatLon;
18import org.openstreetmap.josm.data.gpx.GpxData;
19import org.openstreetmap.josm.data.gpx.WayPoint;
20import org.xml.sax.SAXException;
21
22/**
23 * Tests the {@link GpxReader}.
24 */
25public 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}
Note: See TracBrowser for help on using the repository browser.