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

Last change on this file since 18690 was 18690, checked in by taylor.smock, 14 months ago

See #16567: Convert all assertion calls to JUnit 5 (patch by gaben, modified)

The modifications are as follows:

  • Merge DomainValidatorTest.testIDN and DomainValidatorTest.testIDNJava6OrLater
  • Update some tests to use @ParameterizedTest (DomainValidatorTest)
  • Replace various exception blocks with assertThrows. These typically looked like
        try {
            // Something that should throw an exception here
            fail("An exception should have been thrown");
        } catch (Exception e) {
            // Verify the exception matches expectations here
        }
    
  • Replace assertTrue(val instanceof Clazz) with assertInstanceOf
  • Replace JUnit 4 @Suite with JUnit 5 @Suite

Both the original patch and the modified patch fix various lint issues.

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