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

Last change on this file since 14010 was 14010, checked in by Don-vip, 3 months ago

fix #16471 - Support NMEA files when correlating images to a GPX track

  • Property svn:eol-style set to native
File size: 6.4 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.FileInputStream;
9import java.io.IOException;
10import java.text.SimpleDateFormat;
11import java.util.ArrayList;
12import java.util.List;
13import java.util.TimeZone;
14
15import org.junit.Rule;
16import org.junit.Test;
17import org.openstreetmap.josm.TestUtils;
18import org.openstreetmap.josm.data.coor.LatLon;
19import org.openstreetmap.josm.data.gpx.GpxConstants;
20import org.openstreetmap.josm.data.gpx.GpxData;
21import org.openstreetmap.josm.data.gpx.GpxTrack;
22import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
23import org.openstreetmap.josm.data.gpx.WayPoint;
24import org.openstreetmap.josm.io.GpxReaderTest;
25import org.openstreetmap.josm.testutils.JOSMTestRules;
26import org.openstreetmap.josm.tools.date.DateUtils;
27import org.xml.sax.SAXException;
28
29import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
30
31/**
32 * Unit tests of {@link NmeaReader} class.
33 */
34public 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        in.parse(true);
51        assertEquals(30, in.getNumberOfCoordinates());
52        assertEquals(0, in.getParserMalformed());
53
54        final List<WayPoint> wayPoints = new ArrayList<>(in.data.tracks.iterator().next().getSegments().iterator().next().getWayPoints());
55        assertEquals("2016-01-25T05:05:09.2Z", wayPoints.get(0).get(GpxConstants.PT_TIME));
56        assertEquals("2016-01-25T05:05:09.4Z", wayPoints.get(1).get(GpxConstants.PT_TIME));
57        assertEquals("2016-01-25T05:05:09.6Z", wayPoints.get(2).get(GpxConstants.PT_TIME));
58        assertEquals(wayPoints.get(0).getTime(), DateUtils.fromString(wayPoints.get(0).get(GpxConstants.PT_TIME).toString()));
59
60        final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
61        assertEquals("2016-01-25T06:05:09.200+01", iso8601.format(wayPoints.get(0).getTime()));
62        assertEquals("2016-01-25T06:05:09.400+01", iso8601.format(wayPoints.get(1).getTime()));
63        assertEquals("2016-01-25T06:05:09.600+01", iso8601.format(wayPoints.get(2).getTime()));
64
65        assertEquals(new LatLon(46.98807, -1.400525), wayPoints.get(0).getCoor());
66        assertEquals("38.9", wayPoints.get(0).get(GpxConstants.PT_ELE));
67        assertEquals("16", wayPoints.get(0).get(GpxConstants.PT_SAT));
68        assertEquals("3d", wayPoints.get(0).get(GpxConstants.PT_FIX));
69        assertEquals("0.7", wayPoints.get(0).get(GpxConstants.PT_HDOP).toString().trim());
70        assertEquals(null, wayPoints.get(0).get(GpxConstants.PT_VDOP));
71        assertEquals(null, wayPoints.get(0).get(GpxConstants.PT_PDOP));
72    }
73
74    private static void compareWithReference(int ticket, String filename, int numCoor) throws IOException, SAXException {
75        GpxData gpx = GpxReaderTest.parseGpxData(TestUtils.getRegressionDataFile(ticket, filename+".gpx"));
76        NmeaReader in = new NmeaReader(new FileInputStream(TestUtils.getRegressionDataFile(ticket, filename+".nmea")));
77        in.parse(true);
78        assertEquals(numCoor, in.getNumberOfCoordinates());
79        assertEquals(0, in.getParserMalformed());
80        assertEquals(gpx.dataSources, in.data.dataSources);
81        assertEquals(1, gpx.tracks.size());
82        assertEquals(1, in.data.tracks.size());
83        GpxTrack gpxTrack = gpx.tracks.iterator().next();
84        GpxTrack nmeaTrack = in.data.tracks.iterator().next();
85        assertEquals(gpxTrack.getBounds(), nmeaTrack.getBounds());
86        int nTracks = gpxTrack.getSegments().size();
87        assertEquals(nTracks, nmeaTrack.getSegments().size());
88        if (nTracks > 0) {
89            GpxTrackSegment gpxSeg = gpxTrack.getSegments().iterator().next();
90            GpxTrackSegment nmeaSeg = nmeaTrack.getSegments().iterator().next();
91            assertEquals(gpxSeg.getBounds(), nmeaSeg.getBounds());
92            assertEquals(numCoor, gpxSeg.getWayPoints().size());
93            assertEquals(numCoor, nmeaSeg.getWayPoints().size());
94            WayPoint gpxWpt = gpxSeg.getWayPoints().iterator().next();
95            WayPoint nmeaWpt = nmeaSeg.getWayPoints().iterator().next();
96            assertEquals(gpxWpt.getCoor().getRoundedToOsmPrecision(), nmeaWpt.getCoor().getRoundedToOsmPrecision());
97        }
98    }
99
100    /**
101     * Unit test of {@link NmeaReader#isSentence}.
102     */
103    @Test
104    public void testIsSentence() {
105        assertTrue(NmeaReader.isSentence("$GPVTG", Sentence.VTG));
106        assertTrue(NmeaReader.isSentence("$GAVTG", Sentence.VTG));
107        assertTrue(NmeaReader.isSentence("$GNVTG", Sentence.VTG));
108        assertFalse(NmeaReader.isSentence("XGAVTG", Sentence.VTG));
109        assertFalse(NmeaReader.isSentence("$GPXXX", Sentence.VTG));
110        assertFalse(NmeaReader.isSentence("$XXVTG", Sentence.VTG));
111    }
112
113    /**
114     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1433">Bug #1433</a>.
115     * @throws Exception if an error occurs
116     */
117    @Test
118    public void testTicket1433() throws Exception {
119        compareWithReference(1433, "2008-08-14-16-04-58", 1241);
120    }
121
122    /**
123     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1853">Bug #1853</a>.
124     * @throws Exception if an error occurs
125     */
126    @Test
127    public void testTicket1853() throws Exception {
128        compareWithReference(1853, "PosData-20081216-115434", 1285);
129    }
130
131    /**
132     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/2147">Bug #2147</a>.
133     * @throws Exception if an error occurs
134     */
135    @Test
136    public void testTicket2147() throws Exception {
137        compareWithReference(2147, "WG20080203171807.log", 487);
138    }
139
140    /**
141     * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/14924">Bug #14924</a>.
142     * @throws Exception if an error occurs
143     */
144    @Test
145    public void testTicket14924() throws Exception {
146        compareWithReference(14924, "input", 0);
147    }
148}
Note: See TracBrowser for help on using the repository browser.