source: josm/trunk/test/unit/org/openstreetmap/josm/gui/layer/GpxLayerTest.java@ 17531

Last change on this file since 17531 was 17275, checked in by Don-vip, 3 years ago

see #16567 - upgrade almost all tests to JUnit 5, except those depending on WiremockRule

See https://github.com/tomakehurst/wiremock/issues/684

  • Property svn:eol-style set to native
File size: 9.7 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
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;
8import static org.junit.jupiter.api.Assertions.assertThrows;
9
10import java.awt.Color;
11import java.io.IOException;
12import java.util.ArrayList;
13import java.util.Collection;
14import java.util.HashMap;
15import java.util.TimeZone;
16
17import javax.swing.JScrollPane;
18
19import org.junit.jupiter.api.Test;
20import org.junit.jupiter.api.extension.RegisterExtension;
21import org.openstreetmap.josm.TestUtils;
22import org.openstreetmap.josm.data.gpx.GpxData;
23import org.openstreetmap.josm.data.gpx.GpxTrack;
24import org.openstreetmap.josm.data.gpx.IGpxTrackSegment;
25import org.openstreetmap.josm.data.gpx.WayPoint;
26import org.openstreetmap.josm.data.osm.DataSet;
27import org.openstreetmap.josm.data.projection.Projections;
28import org.openstreetmap.josm.gui.MainApplication;
29import org.openstreetmap.josm.gui.widgets.HtmlPanel;
30import org.openstreetmap.josm.io.GpxReaderTest;
31import org.openstreetmap.josm.testutils.JOSMTestRules;
32import org.xml.sax.SAXException;
33
34import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
35
36/**
37 * Unit tests of {@link GpxLayer} class.
38 */
39public class GpxLayerTest {
40
41 /**
42 * Setup tests
43 */
44 @RegisterExtension
45 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
46 public JOSMTestRules test = new JOSMTestRules().main().projection().i18n().metricSystem();
47
48 private static String getHtml(GpxLayer layer) {
49 return ((HtmlPanel) ((JScrollPane) layer.getInfoComponent()).getViewport().getView()).getEditorPane().getText();
50 }
51
52 /**
53 * Returns minimal GPX data.
54 * @return minimal GPX data, with a single waypoint, a single track composed of a single segment
55 * @throws IOException if any I/O error occurs
56 * @throws SAXException if any SAX error occurs
57 */
58 public static GpxData getMinimalGpxData() throws IOException, SAXException {
59 return GpxReaderTest.parseGpxData(TestUtils.getTestDataRoot() + "minimal.gpx");
60 }
61
62 /**
63 * Returns minimal GPX layer.
64 * @return minimal GPX layer, with a single waypoint, a single track composed of a single segment
65 * @throws IOException if any I/O error occurs
66 * @throws SAXException if any SAX error occurs
67 */
68 public static GpxLayer getMinimalGpxLayer() throws IOException, SAXException {
69 return new GpxLayer(getMinimalGpxData(), "Bananas");
70 }
71
72 /**
73 * Unit test of {@link GpxLayer#GpxLayer}.
74 * @throws Exception if any error occurs
75 */
76 @Test
77 void testGpxLayer() throws Exception {
78 GpxLayer layer = new GpxLayer(new GpxData(), "foo", false);
79 GpxTrack trk = new GpxTrack(new ArrayList<IGpxTrackSegment>(), new HashMap<>());
80 trk.getExtensions().add("gpxd", "color", "#FF0000");
81 layer.data.addTrack(trk);
82
83 assertEquals("foo", layer.getName());
84 assertFalse(layer.isLocalFile());
85 assertEquals(layer.getColor(), Color.RED);
86 assertEquals("<html>1 track (0 segments), 0 routes, 0 waypoints<br>Length: < 0.01 m<br></html>", layer.getToolTipText());
87
88 GpxLayer layer2 = new GpxLayer(new GpxData(), "bar", true);
89 assertEquals("bar", layer2.getName());
90 assertTrue(layer2.isLocalFile());
91 assertNull(layer2.getColor());
92 assertEquals("<html>0 tracks (0 segments), 0 routes, 0 waypoints<br>Length: < 0.01 m<br></html>", layer2.getToolTipText());
93
94 assertTrue(layer.checkSaveConditions());
95 assertTrue(layer.isInfoResizable());
96 assertTrue(layer.isSavable());
97 assertTrue(layer.isMergable(layer2));
98
99 layer.projectionChanged(null, null);
100 layer.projectionChanged(null, Projections.getProjectionByCode("EPSG:3857"));
101 }
102
103 /**
104 * Unit test of {@link GpxLayer#getInfoComponent}.
105 * @throws Exception if any error occurs
106 */
107 @Test
108 void testGetInfoComponent() throws Exception {
109 assertEquals("<html>\n"+
110 " <head>\n" +
111 " <style type=\"text/css\">\n" +
112 " <!--\n" +
113 " td { padding-top: 4px; padding-bottom: 4px; padding-right: 16px; padding-left: 16px }\n" +
114 " -->\n" +
115 " </style>\n" +
116 " \n" +
117 " </head>\n" +
118 " <body>\n" +
119 " Length: 0.01 m<br>0 routes, 0 waypoints<br>\n" +
120 " </body>\n" +
121 "</html>\n",
122 getHtml(new GpxLayer(new GpxData())));
123
124 assertEquals("<html>\n"+
125 " <head>\n" +
126 " <style type=\"text/css\">\n" +
127 " <!--\n" +
128 " td { padding-top: 4px; padding-bottom: 4px; padding-right: 16px; padding-left: 16px }\n" +
129 " -->\n" +
130 " </style>\n" +
131 " \n" +
132 " </head>\n" +
133 " <body>\n" +
134 " Creator: MapSource 6.16.3<br>\n\n" +
135 " <table>\n" +
136 " <tr align=\"center\">\n" +
137 " <td colspan=\"5\">\n" +
138 " 1 track, 1 track segments\n" +
139 " </td>\n" +
140 " </tr>\n" +
141 " <tr align=\"center\">\n" +
142 " <td>\n" +
143 " Name\n" +
144 " </td>\n" +
145 " <td>\n" +
146 " Description\n" +
147 " </td>\n" +
148 " <td>\n" +
149 " Timespan\n" +
150 " </td>\n" +
151 " <td>\n" +
152 " Length\n" +
153 " </td>\n" +
154 " <td>\n" +
155 " Number of<br>Segments\n" +
156 " </td>\n" +
157 " <td>\n" +
158 " URL\n" +
159 " </td>\n" +
160 " </tr>\n" +
161 " <tr>\n" +
162 " <td>\n" +
163 " 2016-01-03 20:40:14\n" +
164 " </td>\n" +
165 " <td>\n" +
166 " \n" +
167 " </td>\n" +
168 " <td>\n" +
169 " 1/3/16 11:59 AM - 12:00 PM (0:00)\n" +
170 " </td>\n" +
171 " <td>\n" +
172 " 12.0 m\n" +
173 " </td>\n" +
174 " <td>\n" +
175 " 1\n" +
176 " </td>\n" +
177 " <td>\n" +
178 " \n" +
179 " </td>\n" +
180 " </tr>\n" +
181 " </table>\n" +
182 " <br>\n" +
183 " <br>\n" +
184 " Length: 12.0 m<br>0 routes, 1 waypoint<br>\n" +
185 " </body>\n" +
186 "</html>\n",
187 getHtml(getMinimalGpxLayer()));
188 }
189
190 /**
191 * Unit test of {@link GpxLayer#getTimespanForTrack}.
192 * @throws Exception if any error occurs
193 */
194 @Test
195 void testGetTimespanForTrack() throws Exception {
196 assertEquals("", GpxLayer.getTimespanForTrack(
197 new GpxTrack(new ArrayList<Collection<WayPoint>>(), new HashMap<String, Object>())));
198
199 assertEquals("1/3/16 11:59 AM - 12:00 PM (0:00)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
200
201 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
202 assertEquals("1/3/16 12:59 PM - 1:00 PM (0:00)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
203 }
204
205 /**
206 * Unit test of {@link GpxLayer#mergeFrom}.
207 * @throws Exception if any error occurs
208 */
209 @Test
210 void testMergeFrom() throws Exception {
211 GpxLayer layer = new GpxLayer(new GpxData());
212 assertTrue(layer.data.isEmpty());
213 layer.mergeFrom(getMinimalGpxLayer());
214 assertFalse(layer.data.isEmpty());
215 assertEquals(1, layer.data.tracks.size());
216 assertEquals(1, layer.data.waypoints.size());
217 }
218
219 /**
220 * Test that {@link GpxLayer#mergeFrom} throws IAE for invalid arguments
221 */
222 @Test
223 void testMergeFromIAE() {
224 assertThrows(IllegalArgumentException.class, () -> new GpxLayer(new GpxData()).mergeFrom(new OsmDataLayer(new DataSet(), "", null)));
225 }
226
227 /**
228 * Unit test of {@link GpxLayer#paint}.
229 * @throws Exception if any error occurs
230 */
231 @Test
232 void testPaint() throws Exception {
233 GpxLayer layer = getMinimalGpxLayer();
234 try {
235 MainApplication.getLayerManager().addLayer(layer);
236 assertTrue(layer.getMenuEntries().length > 0);
237 layer.paint(TestUtils.newGraphics(), MainApplication.getMap().mapView, layer.data.getMetaBounds());
238 } finally {
239 MainApplication.getLayerManager().removeLayer(layer);
240 }
241 }
242
243 /**
244 * Unit test of {@link GpxLayer#getChangesetSourceTag}.
245 */
246 @Test
247 void testGetChangesetSourceTag() {
248 assertEquals("survey", new GpxLayer(new GpxData(), "", true).getChangesetSourceTag());
249 assertNull(new GpxLayer(new GpxData(), "", false).getChangesetSourceTag());
250 }
251}
Note: See TracBrowser for help on using the repository browser.