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

Last change on this file since 18690 was 18690, checked in by taylor.smock, 13 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: 12.3 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.gui.layer;
3
4import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
5import static org.junit.jupiter.api.Assertions.assertEquals;
6import static org.junit.jupiter.api.Assertions.assertFalse;
7import static org.junit.jupiter.api.Assertions.assertInstanceOf;
8import static org.junit.jupiter.api.Assertions.assertNull;
9import static org.junit.jupiter.api.Assertions.assertThrows;
10import static org.junit.jupiter.api.Assertions.assertTrue;
11
12import java.awt.Color;
13import java.awt.Component;
14import java.io.IOException;
15import java.util.ArrayList;
16import java.util.Collection;
17import java.util.HashMap;
18import java.util.Locale;
19import java.util.TimeZone;
20
21import javax.swing.JScrollPane;
22
23import org.junit.jupiter.api.BeforeEach;
24import org.junit.jupiter.api.Test;
25import org.junit.jupiter.api.extension.RegisterExtension;
26import org.openstreetmap.josm.TestUtils;
27import org.openstreetmap.josm.data.gpx.GpxData;
28import org.openstreetmap.josm.data.gpx.GpxTrack;
29import org.openstreetmap.josm.data.gpx.IGpxTrackSegment;
30import org.openstreetmap.josm.data.gpx.WayPoint;
31import org.openstreetmap.josm.data.osm.DataSet;
32import org.openstreetmap.josm.data.osm.visitor.BoundingXYVisitor;
33import org.openstreetmap.josm.data.projection.CustomProjection;
34import org.openstreetmap.josm.data.projection.Projections;
35import org.openstreetmap.josm.gui.MainApplication;
36import org.openstreetmap.josm.gui.widgets.HtmlPanel;
37import org.openstreetmap.josm.io.GpxReaderTest;
38import org.openstreetmap.josm.testutils.JOSMTestRules;
39import org.openstreetmap.josm.tools.date.DateUtils;
40import org.xml.sax.SAXException;
41
42import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
43
44/**
45 * Unit tests of {@link GpxLayer} class.
46 */
47public class GpxLayerTest {
48
49 /**
50 * Setup tests
51 */
52 @RegisterExtension
53 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
54 public JOSMTestRules test = new JOSMTestRules().main().projection().i18n().metricSystem();
55
56 /**
57 * Setup test.
58 */
59 @BeforeEach
60 void setUp() {
61 Locale.setDefault(Locale.ROOT);
62 DateUtils.PROP_ISO_DATES.put(true);
63 }
64
65 private static String getHtml(GpxLayer layer) {
66 return ((HtmlPanel) ((JScrollPane) layer.getInfoComponent()).getViewport().getView()).getEditorPane().getText();
67 }
68
69 /**
70 * Returns minimal GPX data.
71 * @return minimal GPX data, with a single waypoint, a single track composed of a single segment
72 * @throws IOException if any I/O error occurs
73 * @throws SAXException if any SAX error occurs
74 */
75 public static GpxData getMinimalGpxData() throws IOException, SAXException {
76 return GpxReaderTest.parseGpxData(TestUtils.getTestDataRoot() + "minimal.gpx");
77 }
78
79 /**
80 * Returns minimal GPX layer.
81 * @return minimal GPX layer, with a single waypoint, a single track composed of a single segment
82 * @throws IOException if any I/O error occurs
83 * @throws SAXException if any SAX error occurs
84 */
85 public static GpxLayer getMinimalGpxLayer() throws IOException, SAXException {
86 return new GpxLayer(getMinimalGpxData(), "Bananas");
87 }
88
89 /**
90 * Unit test of {@link GpxLayer#GpxLayer}.
91 */
92 @Test
93 void testGpxLayer() {
94 GpxLayer layer = new GpxLayer(new GpxData(), "foo", false);
95 GpxTrack trk = new GpxTrack(new ArrayList<IGpxTrackSegment>(), new HashMap<>());
96 trk.getExtensions().add("gpxd", "color", "#FF0000");
97 layer.data.addTrack(trk);
98
99 assertEquals("foo", layer.getName());
100 assertFalse(layer.isLocalFile());
101 assertEquals(layer.getColor(), Color.RED);
102 assertEquals("<html>1 track (0 segments), 0 routes, 0 waypoints<br>Length: < 0.01 m<br></html>", layer.getToolTipText());
103
104 GpxLayer layer2 = new GpxLayer(new GpxData(), "bar", true);
105 assertEquals("bar", layer2.getName());
106 assertTrue(layer2.isLocalFile());
107 assertNull(layer2.getColor());
108 assertEquals("<html>0 tracks (0 segments), 0 routes, 0 waypoints<br>Length: < 0.01 m<br></html>", layer2.getToolTipText());
109
110 assertTrue(layer.checkSaveConditions());
111 assertTrue(layer.isInfoResizable());
112 assertTrue(layer.isSavable());
113 assertTrue(layer.isMergable(layer2));
114
115 layer.projectionChanged(null, null);
116 layer.projectionChanged(null, Projections.getProjectionByCode("EPSG:3857"));
117 }
118
119 /**
120 * Unit test of {@link GpxLayer#getInfoComponent}.
121 * @throws Exception if any error occurs
122 */
123 @Test
124 void testGetInfoComponent() throws Exception {
125 assertEquals("<html>\n"+
126 " <head>\n" +
127 " <style type=\"text/css\">\n" +
128 " <!--\n" +
129 " td { padding-top: 4px; padding-bottom: 4px; padding-right: 16px; padding-left: 16px }\n" +
130 " -->\n" +
131 " </style>\n" +
132 " \n" +
133 " </head>\n" +
134 " <body>\n" +
135 " Length: 0.01 m<br>0 routes, 0 waypoints<br>\n" +
136 " </body>\n" +
137 "</html>\n",
138 getHtml(new GpxLayer(new GpxData())));
139
140 assertEquals("<html>\n"+
141 " <head>\n" +
142 " <style type=\"text/css\">\n" +
143 " <!--\n" +
144 " td { padding-top: 4px; padding-bottom: 4px; padding-right: 16px; padding-left: 16px }\n" +
145 " -->\n" +
146 " </style>\n" +
147 " \n" +
148 " </head>\n" +
149 " <body>\n" +
150 " Creator: MapSource 6.16.3<br>\n\n" +
151 " <table>\n" +
152 " <tr align=\"center\">\n" +
153 " <td colspan=\"5\">\n" +
154 " 1 track, 1 track segments\n" +
155 " </td>\n" +
156 " </tr>\n" +
157 " <tr align=\"center\">\n" +
158 " <td>\n" +
159 " Name\n" +
160 " </td>\n" +
161 " <td>\n" +
162 " Description\n" +
163 " </td>\n" +
164 " <td>\n" +
165 " Timespan\n" +
166 " </td>\n" +
167 " <td>\n" +
168 " Length\n" +
169 " </td>\n" +
170 " <td>\n" +
171 " Number of<br>Segments\n" +
172 " </td>\n" +
173 " <td>\n" +
174 " URL\n" +
175 " </td>\n" +
176 " </tr>\n" +
177 " <tr>\n" +
178 " <td>\n" +
179 " 2016-01-03 20:40:14\n" +
180 " </td>\n" +
181 " <td>\n" +
182 " \n" +
183 " </td>\n" +
184 " <td>\n" +
185 " 2016-01-03 11:59:58 &#8211; 12:00:00 (2.0 s)\n" +
186 " </td>\n" +
187 " <td>\n" +
188 " 12.0 m\n" +
189 " </td>\n" +
190 " <td>\n" +
191 " 1\n" +
192 " </td>\n" +
193 " <td>\n" +
194 " \n" +
195 " </td>\n" +
196 " </tr>\n" +
197 " </table>\n" +
198 " <br>\n" +
199 " <br>\n" +
200 " Length: 12.0 m<br>0 routes, 1 waypoint<br>\n" +
201 " </body>\n" +
202 "</html>\n",
203 getHtml(getMinimalGpxLayer()));
204 }
205
206 /**
207 * Unit test of {@link GpxLayer#getTimespanForTrack}.
208 * @throws Exception if any error occurs
209 */
210 @Test
211 void testGetTimespanForTrack() throws Exception {
212 assertEquals("", GpxLayer.getTimespanForTrack(
213 new GpxTrack(new ArrayList<Collection<WayPoint>>(), new HashMap<>())));
214
215 assertEquals("2016-01-03 11:59:58 \u2013 12:00:00 (2.0 s)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
216
217 TimeZone.setDefault(TimeZone.getTimeZone("Europe/Berlin"));
218 assertEquals("2016-01-03 12:59:58 \u2013 13:00:00 (2.0 s)", GpxLayer.getTimespanForTrack(getMinimalGpxData().tracks.iterator().next()));
219 }
220
221 /**
222 * Unit test of {@link GpxLayer#mergeFrom}.
223 * @throws Exception if any error occurs
224 */
225 @Test
226 void testMergeFrom() throws Exception {
227 GpxLayer layer = new GpxLayer(new GpxData());
228 assertTrue(layer.data.isEmpty());
229 layer.mergeFrom(getMinimalGpxLayer());
230 assertFalse(layer.data.isEmpty());
231 assertEquals(1, layer.data.tracks.size());
232 assertEquals(1, layer.data.waypoints.size());
233 }
234
235 /**
236 * Test that {@link GpxLayer#mergeFrom} throws IAE for invalid arguments
237 */
238 @Test
239 void testMergeFromIAE() {
240 final GpxLayer gpxLayer = new GpxLayer(new GpxData());
241 final OsmDataLayer osmDataLayer = new OsmDataLayer(new DataSet(), "testMergeFromIAE", null);
242 assertThrows(IllegalArgumentException.class, () -> gpxLayer.mergeFrom(osmDataLayer));
243 }
244
245 /**
246 * Unit test of {@link GpxLayer#paint}.
247 * @throws Exception if any error occurs
248 */
249 @Test
250 void testPaint() throws Exception {
251 GpxLayer layer = getMinimalGpxLayer();
252 try {
253 MainApplication.getLayerManager().addLayer(layer);
254 assertTrue(layer.getMenuEntries().length > 0);
255 layer.paint(TestUtils.newGraphics(), MainApplication.getMap().mapView, layer.data.getMetaBounds());
256 } finally {
257 MainApplication.getLayerManager().removeLayer(layer);
258 }
259 }
260
261 /**
262 * Unit test of {@link GpxLayer#getChangesetSourceTag}.
263 */
264 @Test
265 void testGetChangesetSourceTag() {
266 assertEquals("survey", new GpxLayer(new GpxData(), "", true).getChangesetSourceTag());
267 assertNull(new GpxLayer(new GpxData(), "", false).getChangesetSourceTag());
268 }
269
270 /**
271 * Checks that potential operations that could be called after destroy() are harmless
272 */
273 @Test
274 void testRobustnessAfterDestroy() {
275 GpxData data = new GpxData();
276 GpxLayer layer = new GpxLayer(data, "1", false);
277 GpxLayer otherLayer = new GpxLayer(new GpxData(), "2", false);
278 assertEquals(data, layer.getData());
279 assertTrue(layer.isMergable(otherLayer));
280 assertTrue(layer.hasColor());
281 assertTrue(layer.isSavable());
282 assertTrue(layer.checkSaveConditions());
283 assertFalse(layer.isModified());
284 assertFalse(layer.requiresSaveToFile());
285 assertNull(layer.getChangesetSourceTag());
286 assertNull(layer.getAssociatedFile());
287
288 layer.destroy();
289
290 assertNull(layer.getData());
291 assertNull(layer.getColor());
292 assertFalse(layer.hasColor());
293 assertFalse(layer.isMergable(otherLayer));
294 assertFalse(layer.isSavable());
295 assertFalse(layer.checkSaveConditions());
296 assertFalse(layer.isModified());
297 assertFalse(layer.requiresSaveToFile());
298 assertNull(layer.getChangesetSourceTag());
299 assertNull(layer.getAssociatedFile());
300 Object infoComponent = layer.getInfoComponent();
301 Component view = assertInstanceOf(JScrollPane.class, infoComponent).getViewport().getView();
302 String text = assertInstanceOf(HtmlPanel.class, view).getEditorPane().getText().trim();
303 assertTrue(text.startsWith("<html>"), text);
304 assertTrue(text.endsWith("</html>"), text);
305 assertEquals("<html><br></html>", layer.getToolTipText());
306 assertDoesNotThrow(layer::jumpToNextMarker);
307 assertDoesNotThrow(layer::jumpToPreviousMarker);
308 assertDoesNotThrow(() -> layer.visitBoundingBox(new BoundingXYVisitor()));
309 assertDoesNotThrow(() -> layer.filterTracksByDate(null, null, false));
310 assertDoesNotThrow(() -> layer.projectionChanged(new CustomProjection(), new CustomProjection()));
311 }
312}
Note: See TracBrowser for help on using the repository browser.