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

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

speedup unit tests

  • Property svn:eol-style set to native
File size: 4.2 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 com.github.tomakehurst.wiremock.core.WireMockConfiguration.options;
8import static org.junit.Assert.assertEquals;
9import static org.junit.Assert.assertTrue;
10
11import java.util.List;
12
13import org.junit.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.Main;
16import org.openstreetmap.josm.TestUtils;
17import org.openstreetmap.josm.data.imagery.ImageryInfo;
18import org.openstreetmap.josm.gui.layer.TMSLayer;
19import org.openstreetmap.josm.gui.layer.WMSLayer;
20import org.openstreetmap.josm.testutils.JOSMTestRules;
21
22import com.github.tomakehurst.wiremock.junit.WireMockRule;
23
24import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
25
26/**
27 * Unit tests for class {@link AddImageryLayerAction}.
28 */
29public final class AddImageryLayerActionTest {
30 /**
31 * We need prefs for this. We need platform for actions and the OSM API for checking blacklist.
32 */
33 @Rule
34 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
35 public JOSMTestRules test = new JOSMTestRules().preferences().platform().fakeAPI();
36
37 /**
38 * HTTP mock.
39 */
40 @Rule
41 public WireMockRule wireMockRule = new WireMockRule(options().dynamicPort().usingFilesUnderDirectory(TestUtils.getTestDataRoot()));
42
43 /**
44 * Unit test of {@link AddImageryLayerAction#updateEnabledState}.
45 */
46 @Test
47 public void testEnabledState() {
48 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo")).isEnabled());
49 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).isEnabled());
50 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_bing", "http://bar", "bing", null, null)).isEnabled());
51 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_scanex", "http://bar", "scanex", null, null)).isEnabled());
52 assertTrue(new AddImageryLayerAction(new ImageryInfo("foo_wms_endpoint", "http://bar", "wms_endpoint", null, null)).isEnabled());
53 }
54
55 /**
56 * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for TMS.
57 */
58 @Test
59 public void testActionPerformedEnabledTms() {
60 assertTrue(Main.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
61 new AddImageryLayerAction(new ImageryInfo("foo_tms", "http://bar", "tms", null, null)).actionPerformed(null);
62 List<TMSLayer> tmsLayers = Main.getLayerManager().getLayersOfType(TMSLayer.class);
63 assertEquals(1, tmsLayers.size());
64 Main.getLayerManager().removeLayer(tmsLayers.get(0));
65 }
66
67 /**
68 * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for WMS.
69 */
70 @Test
71 public void testActionPerformedEnabledWms() {
72 wireMockRule.stubFor(get(urlEqualTo("/wms?VERSION=1.1.1&SERVICE=WMS&REQUEST=GetCapabilities"))
73 .willReturn(aResponse()
74 .withStatus(200)
75 .withHeader("Content-Type", "text/xml")
76 .withBodyFile("imagery/wms-capabilities.xml")));
77 new AddImageryLayerAction(new ImageryInfo("localhost", "http://localhost:" + wireMockRule.port() + "/wms?",
78 "wms_endpoint", null, null)).actionPerformed(null);
79 List<WMSLayer> wmsLayers = Main.getLayerManager().getLayersOfType(WMSLayer.class);
80 assertEquals(1, wmsLayers.size());
81
82 Main.getLayerManager().removeLayer(wmsLayers.get(0));
83 }
84
85 /**
86 * Unit test of {@link AddImageryLayerAction#actionPerformed} - disabled case.
87 */
88 @Test
89 public void testActionPerformedDisabled() {
90 assertTrue(Main.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
91 try {
92 new AddImageryLayerAction(new ImageryInfo("foo")).actionPerformed(null);
93 } catch (IllegalArgumentException expected) {
94 assertEquals("Parameter 'info.url' must not be null", expected.getMessage());
95 }
96 assertTrue(Main.getLayerManager().getLayersOfType(TMSLayer.class).isEmpty());
97 }
98}
Note: See TracBrowser for help on using the repository browser.