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

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

see #16550 - add missing JOSMTestRules

  • Property svn:eol-style set to native
File size: 3.1 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.Rule;
15import org.junit.Test;
16import org.openstreetmap.josm.TestUtils;
17import org.openstreetmap.josm.data.Bounds;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.gpx.GpxData;
20import org.openstreetmap.josm.data.gpx.WayPoint;
21import org.openstreetmap.josm.testutils.JOSMTestRules;
22import org.xml.sax.SAXException;
23
24import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25
26/**
27 * Tests the {@link GpxReader}.
28 */
29public class GpxReaderTest {
30
31 /**
32 * Setup rule
33 */
34 @Rule
35 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
36 public JOSMTestRules test = new JOSMTestRules();
37
38 /**
39 * Parses a GPX file and returns the parsed data
40 * @param filename the GPX file to parse
41 * @return the parsed GPX data
42 * @throws IOException if an error occurs during reading.
43 * @throws SAXException if a SAX error occurs
44 */
45 public static GpxData parseGpxData(String filename) throws IOException, SAXException {
46 final GpxData result;
47 try (FileInputStream in = new FileInputStream(new File(filename))) {
48 GpxReader reader = new GpxReader(in);
49 assertTrue(reader.parse(false));
50 result = reader.getGpxData();
51 }
52 return result;
53 }
54
55 /**
56 * Tests the {@code munich.gpx} test file.
57 * @throws Exception if something goes wrong
58 */
59 @Test
60 public void testMunich() throws Exception {
61 final GpxData result = parseGpxData("data_nodist/munich.gpx");
62 assertEquals(2762, result.getTracks().size());
63 assertEquals(0, result.getRoutes().size());
64 assertEquals(903, result.getWaypoints().size());
65
66 final WayPoint tenthWayPoint = new ArrayList<>(result.getWaypoints()).get(10);
67 assertEquals("128970", tenthWayPoint.get(GpxData.GPX_NAME));
68 assertEquals(new LatLon(48.183956146240234, 11.43463134765625), tenthWayPoint.getCoor());
69 }
70
71 /**
72 * Tests invalid data.
73 * @throws Exception always SAXException
74 */
75 @Test(expected = SAXException.class)
76 public void testException() throws Exception {
77 new GpxReader(new ByteArrayInputStream("--foo--bar--".getBytes(StandardCharsets.UTF_8))).parse(true);
78 }
79
80 /**
81 * Non-regression test for ticket <a href="https://josm.openstreetmap.de/ticket/15634">#15634</a>
82 * @throws IOException if an error occurs during reading
83 * @throws SAXException if any XML error occurs
84 */
85 @Test
86 public void testTicket15634() throws IOException, SAXException {
87 assertEquals(new Bounds(53.7229357, -7.9135019, 53.9301103, -7.59656),
88 GpxReaderTest.parseGpxData(TestUtils.getRegressionDataFile(15634, "drumlish.gpx")).getMetaBounds());
89 }
90}
Note: See TracBrowser for help on using the repository browser.