source: josm/trunk/test/unit/org/openstreetmap/josm/gui/layer/geoimage/CorrelateGpxWithImagesTest.java@ 10335

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

fix unit test

  • Property svn:eol-style set to native
File size: 7.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer.geoimage;
3
4import static org.junit.Assert.assertEquals;
5
6import java.text.ParseException;
7import java.util.Arrays;
8import java.util.Collections;
9import java.util.TimeZone;
10
11import org.junit.BeforeClass;
12import org.junit.Test;
13import org.openstreetmap.josm.JOSMFixture;
14import org.openstreetmap.josm.data.coor.CachedLatLon;
15import org.openstreetmap.josm.data.gpx.GpxData;
16import org.openstreetmap.josm.io.GpxReaderTest;
17import org.openstreetmap.josm.tools.Pair;
18import org.openstreetmap.josm.tools.date.DateUtils;
19import org.openstreetmap.josm.tools.date.DateUtilsTest;
20
21/**
22 * Unit tests of {@link CorrelateGpxWithImagesTest} class.
23 */
24public class CorrelateGpxWithImagesTest {
25
26 /**
27 * Setup test.
28 */
29 @BeforeClass
30 public static void setUp() {
31 JOSMFixture.createUnitTestFixture().init();
32 DateUtilsTest.setTimeZone(TimeZone.getTimeZone("UTC"));
33 }
34
35 /**
36 * Tests matching of images to a GPX track.
37 * @throws Exception if the track cannot be parsed
38 */
39 @Test
40 public void testMatchGpxTrack() throws Exception {
41 final GpxData gpx = GpxReaderTest.parseGpxData("data_nodist/2094047.gpx");
42 assertEquals(4, gpx.tracks.size());
43 assertEquals(1, gpx.tracks.iterator().next().getSegments().size());
44 assertEquals(185, gpx.tracks.iterator().next().getSegments().iterator().next().getWayPoints().size());
45
46 final ImageEntry i0 = new ImageEntry();
47 i0.setExifTime(DateUtils.fromString("2016:01:03 11:59:54")); // 4 sec before start of GPX
48 i0.createTmp();
49 final ImageEntry i1 = new ImageEntry();
50 i1.setExifTime(DateUtils.fromString("2016:01:03 12:04:01"));
51 i1.createTmp();
52 final ImageEntry i2 = new ImageEntry();
53 i2.setExifTime(DateUtils.fromString("2016:01:03 12:04:57"));
54 i2.createTmp();
55 final ImageEntry i3 = new ImageEntry();
56 i3.setExifTime(DateUtils.fromString("2016:01:03 12:05:05"));
57 i3.createTmp();
58
59 assertEquals(4, CorrelateGpxWithImages.matchGpxTrack(Arrays.asList(i0, i1, i2, i3), gpx, 0));
60 assertEquals(new CachedLatLon(47.19286847859621, 8.79732714034617), i0.getPos()); // start of track
61 assertEquals(new CachedLatLon(47.196979885920882, 8.79541271366179), i1.getPos()); // exact match
62 assertEquals(new CachedLatLon(47.197319911792874, 8.792139580473304), i3.getPos()); // exact match
63 assertEquals(new CachedLatLon((47.197131179273129 + 47.197186248376966) / 2, (8.792974585667253 + 8.792809881269932) / 2),
64 i2.getPos()); // interpolated
65 }
66
67 /**
68 * Tests automatic guessing of timezone/offset
69 * @throws Exception if an error occurs
70 */
71 @Test
72 public void testAutoGuess() throws Exception {
73 final GpxData gpx = GpxReaderTest.parseGpxData("data_nodist/2094047.gpx");
74 final ImageEntry i0 = new ImageEntry();
75 i0.setExifTime(DateUtils.fromString("2016:01:03 11:59:54")); // 4 sec before start of GPX
76 i0.createTmp();
77 assertEquals(Pair.create(CorrelateGpxWithImages.Timezone.ZERO, CorrelateGpxWithImages.Offset.seconds(-4)),
78 CorrelateGpxWithImages.autoGuess(Collections.singletonList(i0), gpx));
79 }
80
81 /**
82 * Unit test of {@link CorrelateGpxWithImages.Timezone#formatTimezone}.
83 */
84 @Test
85 public void testFormatTimezone() {
86 assertEquals("+1:00", new CorrelateGpxWithImages.Timezone(1).formatTimezone());
87 assertEquals("+6:30", new CorrelateGpxWithImages.Timezone(6.5).formatTimezone());
88 assertEquals("-6:30", new CorrelateGpxWithImages.Timezone(-6.5).formatTimezone());
89 assertEquals("+3:08", new CorrelateGpxWithImages.Timezone(Math.PI).formatTimezone());
90 assertEquals("+2:43", new CorrelateGpxWithImages.Timezone(Math.E).formatTimezone());
91 }
92
93 /**
94 * Unit test of {@link CorrelateGpxWithImages.Timezone#parseTimezone}.
95 * @throws ParseException in case of parsing error
96 */
97 @Test
98 public void testParseTimezone() throws ParseException {
99 assertEquals(1, CorrelateGpxWithImages.Timezone.parseTimezone("+01:00").getHours(), 1e-3);
100 assertEquals(1, CorrelateGpxWithImages.Timezone.parseTimezone("+1:00").getHours(), 1e-3);
101 assertEquals(1.5, CorrelateGpxWithImages.Timezone.parseTimezone("+01:30").getHours(), 1e-3);
102 assertEquals(11.5, CorrelateGpxWithImages.Timezone.parseTimezone("+11:30").getHours(), 1e-3);
103 }
104
105 /**
106 * Unit test of {@link CorrelateGpxWithImages.Offset#formatOffset}.
107 */
108 @Test
109 public void testFormatOffset() {
110 assertEquals("0", CorrelateGpxWithImages.Offset.seconds(0).formatOffset());
111 assertEquals("123", CorrelateGpxWithImages.Offset.seconds(123).formatOffset());
112 assertEquals("-4242", CorrelateGpxWithImages.Offset.seconds(-4242).formatOffset());
113 assertEquals("0.1", CorrelateGpxWithImages.Offset.milliseconds(100).formatOffset());
114 assertEquals("0.120", CorrelateGpxWithImages.Offset.milliseconds(120).formatOffset());
115 assertEquals("0.123", CorrelateGpxWithImages.Offset.milliseconds(123).formatOffset());
116 assertEquals("1.2", CorrelateGpxWithImages.Offset.milliseconds(1200).formatOffset());
117 assertEquals("1.234", CorrelateGpxWithImages.Offset.milliseconds(1234).formatOffset());
118 }
119
120 /**
121 * Unit test of {@link CorrelateGpxWithImages.Offset#parseOffset}.
122 * @throws ParseException in case of parsing error
123 */
124 @Test
125 public void testParseOffest() throws ParseException {
126 assertEquals(0, CorrelateGpxWithImages.Offset.parseOffset("0").getSeconds());
127 assertEquals(4242L, CorrelateGpxWithImages.Offset.parseOffset("4242").getSeconds());
128 assertEquals(-4242L, CorrelateGpxWithImages.Offset.parseOffset("-4242").getSeconds());
129 assertEquals(0L, CorrelateGpxWithImages.Offset.parseOffset("-0").getSeconds());
130 assertEquals(100L, CorrelateGpxWithImages.Offset.parseOffset("0.1").getMilliseconds());
131 assertEquals(123L, CorrelateGpxWithImages.Offset.parseOffset("0.123").getMilliseconds());
132 assertEquals(-42420L, CorrelateGpxWithImages.Offset.parseOffset("-42.42").getMilliseconds());
133 }
134
135 /**
136 * Unit test of {@link CorrelateGpxWithImages.Offset#splitOutTimezone}.
137 */
138 @Test
139 public void testSplitOutTimezone() {
140 assertEquals("+1:00", CorrelateGpxWithImages.Offset.seconds(3602).splitOutTimezone().a.formatTimezone());
141 assertEquals("2", CorrelateGpxWithImages.Offset.seconds(3602).splitOutTimezone().b.formatOffset());
142 assertEquals("-7:00", CorrelateGpxWithImages.Offset.seconds(-7 * 3600 + 123).splitOutTimezone().a.formatTimezone());
143 assertEquals("123", CorrelateGpxWithImages.Offset.seconds(-7 * 3600 + 123).splitOutTimezone().b.formatOffset());
144 assertEquals(1, CorrelateGpxWithImages.Offset.seconds(35 * 3600 + 421).getDayOffset());
145 assertEquals(11 * 3600 + 421, CorrelateGpxWithImages.Offset.seconds(35 * 3600 + 421).withoutDayOffset().getSeconds());
146 assertEquals("+11:00", CorrelateGpxWithImages.Offset.seconds(35 * 3600 + 421).splitOutTimezone().a.formatTimezone());
147 assertEquals(86400 + 421, CorrelateGpxWithImages.Offset.seconds(35 * 3600 + 421).splitOutTimezone().b.getSeconds());
148 assertEquals(421, CorrelateGpxWithImages.Offset.seconds(35 * 3600 + 421).withoutDayOffset().splitOutTimezone().b.getSeconds());
149 assertEquals("+1:00", CorrelateGpxWithImages.Offset.milliseconds(3602987).splitOutTimezone().a.formatTimezone());
150 assertEquals("2.987", CorrelateGpxWithImages.Offset.milliseconds(3602987).splitOutTimezone().b.formatOffset());
151 }
152}
Note: See TracBrowser for help on using the repository browser.