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