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

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

fix #16471 - Support NMEA files when correlating images to a GPX track

  • Property svn:eol-style set to native
File size: 6.4 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"));
[14010]50 in.parse(true);
[9740]51 assertEquals(30, in.getNumberOfCoordinates());
52 assertEquals(0, in.getParserMalformed());
53
54 final List<WayPoint> wayPoints = new ArrayList<>(in.data.tracks.iterator().next().getSegments().iterator().next().getWayPoints());
[11036]55 assertEquals("2016-01-25T05:05:09.2Z", wayPoints.get(0).get(GpxConstants.PT_TIME));
56 assertEquals("2016-01-25T05:05:09.4Z", wayPoints.get(1).get(GpxConstants.PT_TIME));
57 assertEquals("2016-01-25T05:05:09.6Z", wayPoints.get(2).get(GpxConstants.PT_TIME));
58 assertEquals(wayPoints.get(0).getTime(), DateUtils.fromString(wayPoints.get(0).get(GpxConstants.PT_TIME).toString()));
[9740]59
60 final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
[10475]61 assertEquals("2016-01-25T06:05:09.200+01", iso8601.format(wayPoints.get(0).getTime()));
62 assertEquals("2016-01-25T06:05:09.400+01", iso8601.format(wayPoints.get(1).getTime()));
63 assertEquals("2016-01-25T06:05:09.600+01", iso8601.format(wayPoints.get(2).getTime()));
[9740]64
65 assertEquals(new LatLon(46.98807, -1.400525), wayPoints.get(0).getCoor());
66 assertEquals("38.9", wayPoints.get(0).get(GpxConstants.PT_ELE));
67 assertEquals("16", wayPoints.get(0).get(GpxConstants.PT_SAT));
68 assertEquals("3d", wayPoints.get(0).get(GpxConstants.PT_FIX));
69 assertEquals("0.7", wayPoints.get(0).get(GpxConstants.PT_HDOP).toString().trim());
70 assertEquals(null, wayPoints.get(0).get(GpxConstants.PT_VDOP));
71 assertEquals(null, wayPoints.get(0).get(GpxConstants.PT_PDOP));
72 }
[10906]73
74 private static void compareWithReference(int ticket, String filename, int numCoor) throws IOException, SAXException {
75 GpxData gpx = GpxReaderTest.parseGpxData(TestUtils.getRegressionDataFile(ticket, filename+".gpx"));
76 NmeaReader in = new NmeaReader(new FileInputStream(TestUtils.getRegressionDataFile(ticket, filename+".nmea")));
[14010]77 in.parse(true);
[10906]78 assertEquals(numCoor, in.getNumberOfCoordinates());
79 assertEquals(0, in.getParserMalformed());
[12422]80 assertEquals(gpx.dataSources, in.data.dataSources);
[10906]81 assertEquals(1, gpx.tracks.size());
82 assertEquals(1, in.data.tracks.size());
83 GpxTrack gpxTrack = gpx.tracks.iterator().next();
84 GpxTrack nmeaTrack = in.data.tracks.iterator().next();
85 assertEquals(gpxTrack.getBounds(), nmeaTrack.getBounds());
[12422]86 int nTracks = gpxTrack.getSegments().size();
87 assertEquals(nTracks, nmeaTrack.getSegments().size());
88 if (nTracks > 0) {
89 GpxTrackSegment gpxSeg = gpxTrack.getSegments().iterator().next();
90 GpxTrackSegment nmeaSeg = nmeaTrack.getSegments().iterator().next();
91 assertEquals(gpxSeg.getBounds(), nmeaSeg.getBounds());
92 assertEquals(numCoor, gpxSeg.getWayPoints().size());
93 assertEquals(numCoor, nmeaSeg.getWayPoints().size());
94 WayPoint gpxWpt = gpxSeg.getWayPoints().iterator().next();
95 WayPoint nmeaWpt = nmeaSeg.getWayPoints().iterator().next();
96 assertEquals(gpxWpt.getCoor().getRoundedToOsmPrecision(), nmeaWpt.getCoor().getRoundedToOsmPrecision());
97 }
[10906]98 }
99
100 /**
[12421]101 * Unit test of {@link NmeaReader#isSentence}.
102 */
103 @Test
104 public void testIsSentence() {
105 assertTrue(NmeaReader.isSentence("$GPVTG", Sentence.VTG));
106 assertTrue(NmeaReader.isSentence("$GAVTG", Sentence.VTG));
107 assertTrue(NmeaReader.isSentence("$GNVTG", Sentence.VTG));
108 assertFalse(NmeaReader.isSentence("XGAVTG", Sentence.VTG));
109 assertFalse(NmeaReader.isSentence("$GPXXX", Sentence.VTG));
110 assertFalse(NmeaReader.isSentence("$XXVTG", Sentence.VTG));
111 }
112
113 /**
[10906]114 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1433">Bug #1433</a>.
115 * @throws Exception if an error occurs
116 */
117 @Test
118 public void testTicket1433() throws Exception {
119 compareWithReference(1433, "2008-08-14-16-04-58", 1241);
120 }
121
122 /**
123 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1853">Bug #1853</a>.
124 * @throws Exception if an error occurs
125 */
126 @Test
127 public void testTicket1853() throws Exception {
128 compareWithReference(1853, "PosData-20081216-115434", 1285);
129 }
130
131 /**
132 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/2147">Bug #2147</a>.
133 * @throws Exception if an error occurs
134 */
135 @Test
136 public void testTicket2147() throws Exception {
137 compareWithReference(2147, "WG20080203171807.log", 487);
138 }
[12422]139
140 /**
141 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14924">Bug #14924</a>.
142 * @throws Exception if an error occurs
143 */
144 @Test
145 public void testTicket14924() throws Exception {
146 compareWithReference(14924, "input", 0);
147 }
[9666]148}
Note: See TracBrowser for help on using the repository browser.