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.FileInputStream;
|
---|
9 | import java.io.IOException;
|
---|
10 | import java.text.SimpleDateFormat;
|
---|
11 | import java.util.ArrayList;
|
---|
12 | import java.util.List;
|
---|
13 | import java.util.TimeZone;
|
---|
14 |
|
---|
15 | import org.junit.Rule;
|
---|
16 | import org.junit.Test;
|
---|
17 | import org.openstreetmap.josm.TestUtils;
|
---|
18 | import org.openstreetmap.josm.data.coor.LatLon;
|
---|
19 | import org.openstreetmap.josm.data.gpx.GpxConstants;
|
---|
20 | import org.openstreetmap.josm.data.gpx.GpxData;
|
---|
21 | import org.openstreetmap.josm.data.gpx.GpxTrack;
|
---|
22 | import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
|
---|
23 | import org.openstreetmap.josm.data.gpx.WayPoint;
|
---|
24 | import org.openstreetmap.josm.io.GpxReaderTest;
|
---|
25 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
26 | import org.openstreetmap.josm.tools.date.DateUtils;
|
---|
27 | import org.xml.sax.SAXException;
|
---|
28 |
|
---|
29 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
30 |
|
---|
31 | /**
|
---|
32 | * Unit tests of {@link NmeaReader} class.
|
---|
33 | */
|
---|
34 | public class NmeaReaderTest {
|
---|
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();
|
---|
41 |
|
---|
42 | /**
|
---|
43 | * Tests reading a nmea file.
|
---|
44 | * @throws Exception if any error occurs
|
---|
45 | */
|
---|
46 | @Test
|
---|
47 | public void testReader() throws Exception {
|
---|
48 | TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
|
---|
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());
|
---|
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()));
|
---|
58 |
|
---|
59 | final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
|
---|
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()));
|
---|
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 | }
|
---|
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());
|
---|
78 | assertEquals(in.data.dataSources, gpx.dataSources);
|
---|
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());
|
---|
84 | assertEquals(1, gpxTrack.getSegments().size());
|
---|
85 | assertEquals(1, nmeaTrack.getSegments().size());
|
---|
86 | GpxTrackSegment gpxSeg = gpxTrack.getSegments().iterator().next();
|
---|
87 | GpxTrackSegment nmeaSeg = nmeaTrack.getSegments().iterator().next();
|
---|
88 | assertEquals(gpxSeg.getBounds(), nmeaSeg.getBounds());
|
---|
89 | assertEquals(numCoor, gpxSeg.getWayPoints().size());
|
---|
90 | assertEquals(numCoor, nmeaSeg.getWayPoints().size());
|
---|
91 | WayPoint gpxWpt = gpxSeg.getWayPoints().iterator().next();
|
---|
92 | WayPoint nmeaWpt = nmeaSeg.getWayPoints().iterator().next();
|
---|
93 | assertEquals(gpxWpt.getCoor().getRoundedToOsmPrecision(), nmeaWpt.getCoor().getRoundedToOsmPrecision());
|
---|
94 | }
|
---|
95 |
|
---|
96 | /**
|
---|
97 | * Unit test of {@link NmeaReader#isSentence}.
|
---|
98 | */
|
---|
99 | @Test
|
---|
100 | public void testIsSentence() {
|
---|
101 | assertTrue(NmeaReader.isSentence("$GPVTG", Sentence.VTG));
|
---|
102 | assertTrue(NmeaReader.isSentence("$GAVTG", Sentence.VTG));
|
---|
103 | assertTrue(NmeaReader.isSentence("$GNVTG", Sentence.VTG));
|
---|
104 | assertFalse(NmeaReader.isSentence("XGAVTG", Sentence.VTG));
|
---|
105 | assertFalse(NmeaReader.isSentence("$GPXXX", Sentence.VTG));
|
---|
106 | assertFalse(NmeaReader.isSentence("$XXVTG", Sentence.VTG));
|
---|
107 | }
|
---|
108 |
|
---|
109 | /**
|
---|
110 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1433">Bug #1433</a>.
|
---|
111 | * @throws Exception if an error occurs
|
---|
112 | */
|
---|
113 | @Test
|
---|
114 | public void testTicket1433() throws Exception {
|
---|
115 | compareWithReference(1433, "2008-08-14-16-04-58", 1241);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1853">Bug #1853</a>.
|
---|
120 | * @throws Exception if an error occurs
|
---|
121 | */
|
---|
122 | @Test
|
---|
123 | public void testTicket1853() throws Exception {
|
---|
124 | compareWithReference(1853, "PosData-20081216-115434", 1285);
|
---|
125 | }
|
---|
126 |
|
---|
127 | /**
|
---|
128 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/2147">Bug #2147</a>.
|
---|
129 | * @throws Exception if an error occurs
|
---|
130 | */
|
---|
131 | @Test
|
---|
132 | public void testTicket2147() throws Exception {
|
---|
133 | compareWithReference(2147, "WG20080203171807.log", 487);
|
---|
134 | }
|
---|
135 | }
|
---|