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

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

fix #16796 - Rework of GPX track colors / layer preferences (patch by Bjoeni)

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