source: josm/trunk/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java

Last change on this file was 18870, checked in by taylor.smock, 6 months ago

See #16567: Update to JUnit 5

This converts most tests to use @Annotations. There are also some performance
improvements as it relates to tests.

  • Property svn:eol-style set to native
File size: 4.8 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.actions;
3
4import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
5import static com.github.tomakehurst.wiremock.client.WireMock.get;
6import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
7import static org.junit.jupiter.api.Assertions.assertEquals;
8import static org.junit.jupiter.api.Assertions.assertTrue;
9
10import java.util.List;
11
12import org.junit.jupiter.api.Test;
13import org.openstreetmap.gui.jmapviewer.FeatureAdapter;
14import org.openstreetmap.josm.data.imagery.ImageryInfo;
15import org.openstreetmap.josm.gui.MainApplication;
16import org.openstreetmap.josm.gui.layer.TMSLayer;
17import org.openstreetmap.josm.gui.layer.WMSLayer;
18import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
19import org.openstreetmap.josm.testutils.annotations.BasicWiremock;
20import org.openstreetmap.josm.testutils.annotations.OsmApi;
21import org.openstreetmap.josm.testutils.annotations.Projection;
22
23import com.github.tomakehurst.wiremock.WireMockServer;
24
25/**
26 * Unit tests for class {@link AddImageryLayerAction}.
27 */
28@BasicPreferences
29@BasicWiremock
30@OsmApi(OsmApi.APIType.FAKE)
31@Projection
32final class AddImageryLayerActionTest {
33 /**
34 * HTTP mock.
35 */
36 @BasicWiremock
37 WireMockServer wireMockServer;
38
39 /**
40 * Unit test of {@link AddImageryLayerAction#updateEnabledState}.
41 */
42 @Test
43 void testEnabledState() {
44 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo")).isEnabled());
45 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).isEnabled());
46 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_bing", "http://bar", "bing", null, null)).isEnabled());
47 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_scanex", "http://bar", "scanex", null, null)).isEnabled());
48 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_wms_endpoint", "http://bar", "wms_endpoint", null, null)).isEnabled());
49 }
50
51 /**
52 * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for TMS.
53 */
54 @Test
55 void testActionPerformedEnabledTms() {
56 assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
57 new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).actionPerformed(null);
58 List<TMSLayer> tmsLayers = MainApplication.getLayerManager().getLayersOfType(TMSLayer.class);
59 assertEquals(1, tmsLayers.size());
60 MainApplication.getLayerManager().removeLayer(tmsLayers.get(0));
61 }
62
63 /**
64 * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for WMS.
65 */
66 @Test
67 void testActionPerformedEnabledWms() {
68 wireMockServer.stubFor(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1"))
69 .willReturn(aResponse()
70 .withStatus(200)
71 .withHeader("Content-Type", "text/xml")
72 .withBodyFile("imagery/wms-capabilities.xml")));
73 wireMockServer.stubFor(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities"))
74 .willReturn(aResponse()
75 .withStatus(404)));
76 wireMockServer.stubFor(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0"))
77 .willReturn(aResponse()
78 .withStatus(404)));
79
80 try {
81 FeatureAdapter.registerApiKeyAdapter(id -> "random_key");
82 final ImageryInfo imageryInfo = new ImageryInfo("localhost", wireMockServer.url("/wms?apikey={apikey}"),
83 "wms_endpoint", null, null);
84 imageryInfo.setId("testActionPerformedEnabledWms");
85 new AddImageryLayerAction(imageryInfo).actionPerformed(null);
86 List<WMSLayer> wmsLayers = MainApplication.getLayerManager().getLayersOfType(WMSLayer.class);
87 assertEquals(1, wmsLayers.size());
88
89 MainApplication.getLayerManager().removeLayer(wmsLayers.get(0));
90 } finally {
91 FeatureAdapter.registerApiKeyAdapter(new FeatureAdapter.DefaultApiKeyAdapter());
92 }
93 }
94
95 /**
96 * Unit test of {@link AddImageryLayerAction#actionPerformed} - disabled case.
97 */
98 @Test
99 void testActionPerformedDisabled() {
100 assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
101 try {
102 new AddImageryLayerAction(new ImageryInfo("foo")).actionPerformed(null);
103 } catch (IllegalArgumentException expected) {
104 assertEquals("Parameter 'info.url' must not be null", expected.getMessage());
105 }
106 assertTrue(MainApplication.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
107 }
108}
Note: See TracBrowser for help on using the repository browser.