1 | // License: GPL. For details, see LICENSE file.
|
---|
2 | package org.openstreetmap.josm.io.nmea;
|
---|
3 |
|
---|
4 | import static org.junit.jupiter.api.Assertions.assertEquals;
|
---|
5 | import static org.junit.jupiter.api.Assertions.assertFalse;
|
---|
6 | import static org.junit.jupiter.api.Assertions.assertNull;
|
---|
7 | import static org.junit.jupiter.api.Assertions.assertTrue;
|
---|
8 |
|
---|
9 | import java.io.ByteArrayInputStream;
|
---|
10 | import java.io.IOException;
|
---|
11 | import java.nio.charset.StandardCharsets;
|
---|
12 | import java.nio.file.Files;
|
---|
13 | import java.nio.file.Paths;
|
---|
14 | import java.time.Instant;
|
---|
15 | import java.util.ArrayList;
|
---|
16 | import java.util.List;
|
---|
17 | import java.util.TimeZone;
|
---|
18 |
|
---|
19 | import org.junit.jupiter.api.Test;
|
---|
20 | import org.openstreetmap.josm.TestUtils;
|
---|
21 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
22 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
23 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
24 | import org.openstreetmap.josm.data.gpx.IGpxTrack;
|
---|
25 | import org.openstreetmap.josm.data.gpx.IGpxTrackSegment;
|
---|
26 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
27 | import org.openstreetmap.josm.io.GpxReaderTest;
|
---|
28 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
29 | import org.xml.sax.SAXException;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Unit tests of {@link NmeaReader} class.
|
---|
33 | */
|
---|
34 | class NmeaReaderTest {
|
---|
35 | /**
|
---|
36 | * Tests reading a nmea file.
|
---|
37 | * @throws Exception if any error occurs
|
---|
38 | */
|
---|
39 | @Test
|
---|
40 | void testReader() throws Exception {
|
---|
41 | TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
|
---|
42 | final NmeaReader in = new NmeaReader(Files.newInputStream(Paths.get("nodist/data/btnmeatrack_2016-01-25.nmea")));
|
---|
43 | in.parse(true);
|
---|
44 | assertEquals(30, in.getNumberOfCoordinates());
|
---|
45 | assertEquals(0, in.getParserMalformed());
|
---|
46 |
|
---|
47 | final List<WayPoint> wayPoints = new ArrayList<>(in.data.tracks.iterator().next().getSegments().iterator().next().getWayPoints());
|
---|
48 | assertEquals(DateUtils.parseInstant("2016-01-25T05:05:09.200Z"), wayPoints.get(0).get(GpxConstants.PT_TIME));
|
---|
49 | assertEquals(DateUtils.parseInstant("2016-01-25T05:05:09.400Z"), wayPoints.get(1).get(GpxConstants.PT_TIME));
|
---|
50 | assertEquals(DateUtils.parseInstant("2016-01-25T05:05:09.600Z"), wayPoints.get(2).get(GpxConstants.PT_TIME));
|
---|
51 | assertEquals(wayPoints.get(0).getInstant(), wayPoints.get(0).get(GpxConstants.PT_TIME));
|
---|
52 |
|
---|
53 | assertEquals(DateUtils.parseInstant("2016-01-25T05:05:09.200Z"), wayPoints.get(0).getInstant());
|
---|
54 | assertEquals(DateUtils.parseInstant("2016-01-25T05:05:09.400Z"), wayPoints.get(1).getInstant());
|
---|
55 | assertEquals(DateUtils.parseInstant("2016-01-25T05:05:09.600Z"), wayPoints.get(2).getInstant());
|
---|
56 |
|
---|
57 | assertEquals(new LatLon(46.98807, -1.400525), wayPoints.get(0).getCoor());
|
---|
58 | assertEquals("38.9", wayPoints.get(0).get(GpxConstants.PT_ELE));
|
---|
59 | assertEquals("16", wayPoints.get(0).get(GpxConstants.PT_SAT));
|
---|
60 | assertEquals("3d", wayPoints.get(0).get(GpxConstants.PT_FIX));
|
---|
61 | assertEquals("0.7", wayPoints.get(0).get(GpxConstants.PT_HDOP).toString().trim());
|
---|
62 | assertNull(wayPoints.get(0).get(GpxConstants.PT_VDOP));
|
---|
63 | assertNull(wayPoints.get(0).get(GpxConstants.PT_PDOP));
|
---|
64 | }
|
---|
65 |
|
---|
66 | private static void compareWithReference(int ticket, String filename, int numCoor) throws IOException, SAXException {
|
---|
67 | GpxData gpx = GpxReaderTest.parseGpxData(TestUtils.getRegressionDataFile(ticket, filename+".gpx"));
|
---|
68 | NmeaReader in = new NmeaReader(Files.newInputStream(Paths.get(TestUtils.getRegressionDataFile(ticket, filename+".nmea"))));
|
---|
69 | in.parse(true);
|
---|
70 | assertEquals(numCoor, in.getNumberOfCoordinates());
|
---|
71 | assertEquals(0, in.getParserMalformed());
|
---|
72 | assertEquals(gpx.dataSources, in.data.dataSources);
|
---|
73 | assertEquals(1, gpx.tracks.size());
|
---|
74 | assertEquals(1, in.data.tracks.size());
|
---|
75 | IGpxTrack gpxTrack = gpx.tracks.iterator().next();
|
---|
76 | IGpxTrack nmeaTrack = in.data.tracks.iterator().next();
|
---|
77 | assertEquals(gpxTrack.getBounds(), nmeaTrack.getBounds());
|
---|
78 | int nTracks = gpxTrack.getSegments().size();
|
---|
79 | assertEquals(nTracks, nmeaTrack.getSegments().size());
|
---|
80 | if (nTracks > 0) {
|
---|
81 | IGpxTrackSegment gpxSeg = gpxTrack.getSegments().iterator().next();
|
---|
82 | IGpxTrackSegment nmeaSeg = nmeaTrack.getSegments().iterator().next();
|
---|
83 | assertEquals(gpxSeg.getBounds(), nmeaSeg.getBounds());
|
---|
84 | assertEquals(numCoor, gpxSeg.getWayPoints().size());
|
---|
85 | assertEquals(numCoor, nmeaSeg.getWayPoints().size());
|
---|
86 | WayPoint gpxWpt = gpxSeg.getWayPoints().iterator().next();
|
---|
87 | WayPoint nmeaWpt = nmeaSeg.getWayPoints().iterator().next();
|
---|
88 | assertEquals(gpxWpt.getCoor().getRoundedToOsmPrecision(), nmeaWpt.getCoor().getRoundedToOsmPrecision());
|
---|
89 | }
|
---|
90 | }
|
---|
91 |
|
---|
92 | /**
|
---|
93 | * Unit test of {@link NmeaReader#isSentence}.
|
---|
94 | */
|
---|
95 | @Test
|
---|
96 | void testIsSentence() {
|
---|
97 | assertTrue(NmeaParser.isSentence("$GPVTG", Sentence.VTG));
|
---|
98 | assertTrue(NmeaParser.isSentence("$GAVTG", Sentence.VTG));
|
---|
99 | assertTrue(NmeaParser.isSentence("$GNVTG", Sentence.VTG));
|
---|
100 | assertFalse(NmeaParser.isSentence("XGAVTG", Sentence.VTG));
|
---|
101 | assertFalse(NmeaParser.isSentence("$GPXXX", Sentence.VTG));
|
---|
102 | assertFalse(NmeaParser.isSentence("$XXVTG", Sentence.VTG));
|
---|
103 | }
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1433">Bug #1433</a>.
|
---|
107 | * @throws Exception if an error occurs
|
---|
108 | */
|
---|
109 | @Test
|
---|
110 | void testTicket1433() throws Exception {
|
---|
111 | compareWithReference(1433, "2008-08-14-16-04-58", 1241);
|
---|
112 | }
|
---|
113 |
|
---|
114 | /**
|
---|
115 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1853">Bug #1853</a>.
|
---|
116 | * @throws Exception if an error occurs
|
---|
117 | */
|
---|
118 | @Test
|
---|
119 | void testTicket1853() throws Exception {
|
---|
120 | compareWithReference(1853, "PosData-20081216-115434", 1285);
|
---|
121 | }
|
---|
122 |
|
---|
123 | /**
|
---|
124 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/2147">Bug #2147</a>.
|
---|
125 | * @throws Exception if an error occurs
|
---|
126 | */
|
---|
127 | @Test
|
---|
128 | void testTicket2147() throws Exception {
|
---|
129 | compareWithReference(2147, "WG20080203171807.log", 487);
|
---|
130 | }
|
---|
131 |
|
---|
132 | /**
|
---|
133 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14924">Bug #14924</a>.
|
---|
134 | * @throws Exception if an error occurs
|
---|
135 | */
|
---|
136 | @Test
|
---|
137 | void testTicket14924() throws Exception {
|
---|
138 | compareWithReference(14924, "input", 0);
|
---|
139 | }
|
---|
140 |
|
---|
141 | private static GpxData read(String nmeaLine) throws IOException, SAXException {
|
---|
142 | NmeaReader in = new NmeaReader(new ByteArrayInputStream(nmeaLine.getBytes(StandardCharsets.UTF_8)));
|
---|
143 | in.parse(true);
|
---|
144 | return in.data;
|
---|
145 | }
|
---|
146 |
|
---|
147 | private static WayPoint readWayPoint(String nmeaLine) throws IOException, SAXException {
|
---|
148 | return read(nmeaLine).tracks.iterator().next().getSegments().iterator().next().getWayPoints().iterator().next();
|
---|
149 | }
|
---|
150 |
|
---|
151 | private static Instant readDate(String nmeaLine) throws IOException, SAXException {
|
---|
152 | return readWayPoint(nmeaLine).getInstant();
|
---|
153 | }
|
---|
154 |
|
---|
155 | private static double readSpeed(String nmeaLine) throws IOException, SAXException {
|
---|
156 | return Double.parseDouble(readWayPoint(nmeaLine).getString("speed"));
|
---|
157 | }
|
---|
158 |
|
---|
159 | /**
|
---|
160 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/16496">Bug #16496</a>.
|
---|
161 | * @throws Exception if an error occurs
|
---|
162 | */
|
---|
163 | @Test
|
---|
164 | void testTicket16496() throws Exception {
|
---|
165 | assertEquals(DateUtils.parseInstant("2018-05-30T16:28:59.400Z"),
|
---|
166 | readDate("$GNRMC,162859.400,A,4543.03388,N,00058.19870,W,45.252,209.07,300518,,,D,V*13"));
|
---|
167 | assertEquals(DateUtils.parseInstant("2018-05-30T16:28:59.400Z"),
|
---|
168 | readDate("$GNRMC,162859.40,A,4543.03388,N,00058.19870,W,45.252,209.07,300518,,,D,V*23"));
|
---|
169 | assertEquals(DateUtils.parseInstant("2018-05-30T16:28:59.400Z"),
|
---|
170 | readDate("$GNRMC,162859.4,A,4543.03388,N,00058.19870,W,45.252,209.07,300518,,,D,V*13"));
|
---|
171 | }
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/16554">Bug #16554</a>.
|
---|
175 | * @throws Exception if an error occurs
|
---|
176 | */
|
---|
177 | @Test
|
---|
178 | void testTicket16554() throws Exception {
|
---|
179 | assertEquals(63.2420959, readSpeed(
|
---|
180 | "$GNRMC,141448.80,A,4659.05514,N,00130.44695,W,34.148,289.80,300718,,,D,V*26"), 1e-7);
|
---|
181 | assertEquals(63.2430000, readSpeed(
|
---|
182 | "$GNRMC,141448.80,A,4659.05514,N,00130.44695,W,34.148,289.80,300718,,,D,V*26"
|
---|
183 | + "$GNVTG,289.80,T,,M,34.148,N,63.243,K,D*27"), 1e-7);
|
---|
184 | }
|
---|
185 | }
|
---|