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

Last change on this file since 11036 was 11036, checked in by simon04, 8 years ago

see #13376 - Fix unit test

  • Property svn:eol-style set to native
File size: 5.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.io;
3
4import static org.junit.Assert.assertEquals;
5
6import java.io.FileInputStream;
7import java.io.IOException;
8import java.text.SimpleDateFormat;
9import java.util.ArrayList;
10import java.util.List;
11import java.util.TimeZone;
12
13import org.junit.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.TestUtils;
16import org.openstreetmap.josm.data.coor.LatLon;
17import org.openstreetmap.josm.data.gpx.GpxConstants;
18import org.openstreetmap.josm.data.gpx.GpxData;
19import org.openstreetmap.josm.data.gpx.GpxTrack;
20import org.openstreetmap.josm.data.gpx.GpxTrackSegment;
21import org.openstreetmap.josm.data.gpx.WayPoint;
22import org.openstreetmap.josm.testutils.JOSMTestRules;
23import org.openstreetmap.josm.tools.date.DateUtils;
24import org.xml.sax.SAXException;
25
26import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
27
28/**
29 * Unit tests of {@link NmeaReader} class.
30 */
31public class NmeaReaderTest {
32 /**
33 * Set the timezone and timeout.
34 */
35 @Rule
36 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
37 public JOSMTestRules test = new JOSMTestRules();
38
39 /**
40 * Tests reading a nmea file.
41 * @throws Exception if any error occurs
42 */
43 @Test
44 public void testReader() throws Exception {
45 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
46 final NmeaReader in = new NmeaReader(new FileInputStream("data_nodist/btnmeatrack_2016-01-25.nmea"));
47 assertEquals(30, in.getNumberOfCoordinates());
48 assertEquals(0, in.getParserMalformed());
49
50 final List<WayPoint> wayPoints = new ArrayList<>(in.data.tracks.iterator().next().getSegments().iterator().next().getWayPoints());
51 assertEquals("2016-01-25T05:05:09.2Z", wayPoints.get(0).get(GpxConstants.PT_TIME));
52 assertEquals("2016-01-25T05:05:09.4Z", wayPoints.get(1).get(GpxConstants.PT_TIME));
53 assertEquals("2016-01-25T05:05:09.6Z", wayPoints.get(2).get(GpxConstants.PT_TIME));
54 assertEquals(wayPoints.get(0).getTime(), DateUtils.fromString(wayPoints.get(0).get(GpxConstants.PT_TIME).toString()));
55
56 final SimpleDateFormat iso8601 = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSX");
57 assertEquals("2016-01-25T06:05:09.200+01", iso8601.format(wayPoints.get(0).getTime()));
58 assertEquals("2016-01-25T06:05:09.400+01", iso8601.format(wayPoints.get(1).getTime()));
59 assertEquals("2016-01-25T06:05:09.600+01", iso8601.format(wayPoints.get(2).getTime()));
60
61 assertEquals(new LatLon(46.98807, -1.400525), wayPoints.get(0).getCoor());
62 assertEquals("38.9", wayPoints.get(0).get(GpxConstants.PT_ELE));
63 assertEquals("16", wayPoints.get(0).get(GpxConstants.PT_SAT));
64 assertEquals("3d", wayPoints.get(0).get(GpxConstants.PT_FIX));
65 assertEquals("0.7", wayPoints.get(0).get(GpxConstants.PT_HDOP).toString().trim());
66 assertEquals(null, wayPoints.get(0).get(GpxConstants.PT_VDOP));
67 assertEquals(null, wayPoints.get(0).get(GpxConstants.PT_PDOP));
68 }
69
70 private static void compareWithReference(int ticket, String filename, int numCoor) throws IOException, SAXException {
71 GpxData gpx = GpxReaderTest.parseGpxData(TestUtils.getRegressionDataFile(ticket, filename+".gpx"));
72 NmeaReader in = new NmeaReader(new FileInputStream(TestUtils.getRegressionDataFile(ticket, filename+".nmea")));
73 assertEquals(numCoor, in.getNumberOfCoordinates());
74 assertEquals(0, in.getParserMalformed());
75 assertEquals(in.data.dataSources, gpx.dataSources);
76 assertEquals(1, gpx.tracks.size());
77 assertEquals(1, in.data.tracks.size());
78 GpxTrack gpxTrack = gpx.tracks.iterator().next();
79 GpxTrack nmeaTrack = in.data.tracks.iterator().next();
80 assertEquals(gpxTrack.getBounds(), nmeaTrack.getBounds());
81 assertEquals(1, gpxTrack.getSegments().size());
82 assertEquals(1, nmeaTrack.getSegments().size());
83 GpxTrackSegment gpxSeg = gpxTrack.getSegments().iterator().next();
84 GpxTrackSegment nmeaSeg = nmeaTrack.getSegments().iterator().next();
85 assertEquals(gpxSeg.getBounds(), nmeaSeg.getBounds());
86 assertEquals(numCoor, gpxSeg.getWayPoints().size());
87 assertEquals(numCoor, nmeaSeg.getWayPoints().size());
88 WayPoint gpxWpt = gpxSeg.getWayPoints().iterator().next();
89 WayPoint nmeaWpt = nmeaSeg.getWayPoints().iterator().next();
90 assertEquals(gpxWpt.getCoor().getRoundedToOsmPrecision(), nmeaWpt.getCoor().getRoundedToOsmPrecision());
91 }
92
93 /**
94 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1433">Bug #1433</a>.
95 * @throws Exception if an error occurs
96 */
97 @Test
98 public void testTicket1433() throws Exception {
99 compareWithReference(1433, "2008-08-14-16-04-58", 1241);
100 }
101
102 /**
103 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/1853">Bug #1853</a>.
104 * @throws Exception if an error occurs
105 */
106 @Test
107 public void testTicket1853() throws Exception {
108 compareWithReference(1853, "PosData-20081216-115434", 1285);
109 }
110
111 /**
112 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/2147">Bug #2147</a>.
113 * @throws Exception if an error occurs
114 */
115 @Test
116 public void testTicket2147() throws Exception {
117 compareWithReference(2147, "WG20080203171807.log", 487);
118 }
119}
Note: See TracBrowser for help on using the repository browser.