source: josm/trunk/test/unit/org/openstreetmap/josm/io/nmea/NmeaReaderTest.java@ 12422

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

fix #14924 - fix NPE when reading NMEA files starting with a (0,0) coordinate followed by a VTG sentence

  • Property svn:eol-style set to native
File size: 6.3 KB
RevLine 
[9666]1// License: GPL. For details, see LICENSE file.
[12421]2package org.openstreetmap.josm.io.nmea;
[9666]3
[9749]4import static org.junit.Assert.assertEquals;
[12421]5import static org.junit.Assert.assertFalse;
6import static org.junit.Assert.assertTrue;
[9740]7
8import java.io.FileInputStream;
[10906]9import java.io.IOException;
[9740]10import java.text.SimpleDateFormat;
11import java.util.ArrayList;
12import java.util.List;
[10467]13import java.util.TimeZone;
[9740]14
[10475]15import org.junit.Rule;
[9666]16import org.junit.Test;
[10906]17import org.openstreetmap.josm.TestUtils;
[9740]18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.gpx.GpxConstants;
[10906]20import org.openstreetmap.josm.data.gpx.GpxData;
21import org.openstreetmap.josm.data.gpx.GpxTrack;
22import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
[9740]23import org.openstreetmap.josm.data.gpx.WayPoint;
[12421]24import org.openstreetmap.josm.io.GpxReaderTest;
[10475]25import org.openstreetmap.josm.testutils.JOSMTestRules;
[11036]26import org.openstreetmap.josm.tools.date.DateUtils;
[10906]27import org.xml.sax.SAXException;
[9666]28
[10475]29import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
[9749]30
[9666]31/**
32 * Unit tests of {@link NmeaReader} class.
33 */
34public class NmeaReaderTest {
[10475]35 /**
36 * Set the timezone and timeout.
37 */
38 @Rule
39 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
40 public JOSMTestRules test = new JOSMTestRules();
[9666]41
42 /**
[9740]43 * Tests reading a nmea file.
44 * @throws Exception if any error occurs
45 */
46 @Test
47 public void testReader() throws Exception {
[10475]48 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
[9740]49 final NmeaReader in = new NmeaReader(new FileInputStream("data_nodist/btnmeatrack_2016-01-25.nmea"));
50 assertEquals(30, in.getNumberOfCoordinates());
51 assertEquals(0, in.getParserMalformed());
52
53 final List<WayPoint> wayPoints = new ArrayList<>(in.data.tracks.iterator().next().getSegments().iterator().next().getWayPoints());
[11036]54 assertEquals("2016-01-25T05:05:09.2Z", wayPoints.get(0).get(GpxConstants.PT_TIME));
55 assertEquals("2016-01-25T05:05:09.4Z", wayPoints.get(1).get(GpxConstants.PT_TIME));
56 assertEquals("2016-01-25T05:05:09.6Z", wayPoints.get(2).get(GpxConstants.PT_TIME));
57 assertEquals(wayPoints.get(0).getTime(), DateUtils.fromString(wayPoints.get(0).get(GpxConstants.PT_TIME).toString()));
[9740]58
59 final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
[10475]60 assertEquals("2016-01-25T06:05:09.200+01", iso8601.format(wayPoints.get(0).getTime()));
61 assertEquals("2016-01-25T06:05:09.400+01", iso8601.format(wayPoints.get(1).getTime()));
62 assertEquals("2016-01-25T06:05:09.600+01", iso8601.format(wayPoints.get(2).getTime()));
[9740]63
64 assertEquals(new LatLon(46.98807, -1.400525), wayPoints.get(0).getCoor());
65 assertEquals("38.9", wayPoints.get(0).get(GpxConstants.PT_ELE));
66 assertEquals("16", wayPoints.get(0).get(GpxConstants.PT_SAT));
67 assertEquals("3d", wayPoints.get(0).get(GpxConstants.PT_FIX));
68 assertEquals("0.7", wayPoints.get(0).get(GpxConstants.PT_HDOP).toString().trim());
69 assertEquals(null, wayPoints.get(0).get(GpxConstants.PT_VDOP));
70 assertEquals(null, wayPoints.get(0).get(GpxConstants.PT_PDOP));
71 }
[10906]72
73 private static void compareWithReference(int ticket, String filename, int numCoor) throws IOException, SAXException {
74 GpxData gpx = GpxReaderTest.parseGpxData(TestUtils.getRegressionDataFile(ticket, filename+".gpx"));
75 NmeaReader in = new NmeaReader(new FileInputStream(TestUtils.getRegressionDataFile(ticket, filename+".nmea")));
76 assertEquals(numCoor, in.getNumberOfCoordinates());
77 assertEquals(0, in.getParserMalformed());
[12422]78 assertEquals(gpx.dataSources, in.data.dataSources);
[10906]79 assertEquals(1, gpx.tracks.size());
80 assertEquals(1, in.data.tracks.size());
81 GpxTrack gpxTrack = gpx.tracks.iterator().next();
82 GpxTrack nmeaTrack = in.data.tracks.iterator().next();
83 assertEquals(gpxTrack.getBounds(), nmeaTrack.getBounds());
[12422]84 int nTracks = gpxTrack.getSegments().size();
85 assertEquals(nTracks, nmeaTrack.getSegments().size());
86 if (nTracks > 0) {
87 GpxTrackSegment gpxSeg = gpxTrack.getSegments().iterator().next();
88 GpxTrackSegment nmeaSeg = nmeaTrack.getSegments().iterator().next();
89 assertEquals(gpxSeg.getBounds(), nmeaSeg.getBounds());
90 assertEquals(numCoor, gpxSeg.getWayPoints().size());
91 assertEquals(numCoor, nmeaSeg.getWayPoints().size());
92 WayPoint gpxWpt = gpxSeg.getWayPoints().iterator().next();
93 WayPoint nmeaWpt = nmeaSeg.getWayPoints().iterator().next();
94 assertEquals(gpxWpt.getCoor().getRoundedToOsmPrecision(), nmeaWpt.getCoor().getRoundedToOsmPrecision());
95 }
[10906]96 }
97
98 /**
[12421]99 * Unit test of {@link NmeaReader#isSentence}.
100 */
101 @Test
102 public void testIsSentence() {
103 assertTrue(NmeaReader.isSentence("$GPVTG", Sentence.VTG));
104 assertTrue(NmeaReader.isSentence("$GAVTG", Sentence.VTG));
105 assertTrue(NmeaReader.isSentence("$GNVTG", Sentence.VTG));
106 assertFalse(NmeaReader.isSentence("XGAVTG", Sentence.VTG));
107 assertFalse(NmeaReader.isSentence("$GPXXX", Sentence.VTG));
108 assertFalse(NmeaReader.isSentence("$XXVTG", Sentence.VTG));
109 }
110
111 /**
[10906]112 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1433">Bug #1433</a>.
113 * @throws Exception if an error occurs
114 */
115 @Test
116 public void testTicket1433() throws Exception {
117 compareWithReference(1433, "2008-08-14-16-04-58", 1241);
118 }
119
120 /**
121 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1853">Bug #1853</a>.
122 * @throws Exception if an error occurs
123 */
124 @Test
125 public void testTicket1853() throws Exception {
126 compareWithReference(1853, "PosData-20081216-115434", 1285);
127 }
128
129 /**
130 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/2147">Bug #2147</a>.
131 * @throws Exception if an error occurs
132 */
133 @Test
134 public void testTicket2147() throws Exception {
135 compareWithReference(2147, "WG20080203171807.log", 487);
136 }
[12422]137
138 /**
139 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14924">Bug #14924</a>.
140 * @throws Exception if an error occurs
141 */
142 @Test
143 public void testTicket14924() throws Exception {
144 compareWithReference(14924, "input", 0);
145 }
[9666]146}
Note: See TracBrowser for help on using the repository browser.