From 3401be9dfd2373e2b9d4f7e793f5619c9c8ec2d9 Mon Sep 17 00:00:00 2001 From: "taylor.smock" Date: Mon, 29 Jul 2024 15:57:38 +0000 Subject: [PATCH] Fix tests broken by r19152 git-svn-id: https://josm.openstreetmap.de/svn/trunk@19155 0c6e7542-c601-0410-84e7-c038aed88b3b --- .../actions/AddImageryLayerActionTest.java | 19 ++-- .../AbstractDownloadTaskTestParent.java | 22 +++-- .../cache/JCSCachedTileLoaderJobTest.java | 96 ++++++++++--------- .../imagery/TMSCachedTileLoaderJobTest.java | 46 +++++---- .../imagery/WMSEndpointTileSourceTest.java | 32 ++++--- .../josm/data/imagery/WMTSTileSourceTest.java | 15 ++- .../data/osm/DefaultNameFormatterTest.java | 16 +--- .../server/ApiUrlTestTaskTest.java | 27 +++--- .../josm/io/OsmServerHistoryReaderTest.java | 13 +-- .../josm/io/OverpassDownloadReaderTest.java | 16 ++-- .../josm/io/imagery/WMSImageryTest.java | 36 ++++--- .../tools/bugreport/BugReportSenderTest.java | 20 ++-- 12 files changed, 174 insertions(+), 184 deletions(-) diff --git a/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java b/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java index 5275e195db9..fa0682441ba 100644 --- a/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java +++ b/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java @@ -9,6 +9,7 @@ import java.util.List; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import org.junit.jupiter.api.Test; import org.openstreetmap.gui.jmapviewer.FeatureAdapter; import org.openstreetmap.josm.data.imagery.ImageryInfo; @@ -20,8 +21,6 @@ import org.openstreetmap.josm.testutils.annotations.OsmApi; import org.openstreetmap.josm.testutils.annotations.Projection; -import com.github.tomakehurst.wiremock.WireMockServer; - /** * Unit tests for class {@link AddImageryLayerAction}. */ @@ -30,12 +29,6 @@ @OsmApi(OsmApi.APIType.FAKE) @Projection final class AddImageryLayerActionTest { - /** - * HTTP mock. - */ - @BasicWiremock - WireMockServer wireMockServer; - /** * Unit test of {@link AddImageryLayerAction#updateEnabledState}. */ @@ -64,22 +57,22 @@ void testActionPerformedEnabledTms() { * Unit test of {@link AddImageryLayerAction#actionPerformed} - Enabled cases for WMS. */ @Test - void testActionPerformedEnabledWms() { - wireMockServer.stubFor(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1")) + void testActionPerformedEnabledWms(WireMockRuntimeInfo wireMockRuntimeInfo) { + wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1")) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "text/xml") .withBodyFile("imagery/wms-capabilities.xml"))); - wireMockServer.stubFor(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities")) + wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities")) .willReturn(aResponse() .withStatus(404))); - wireMockServer.stubFor(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0")) + wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/wms?apikey=random_key&SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0")) .willReturn(aResponse() .withStatus(404))); try { FeatureAdapter.registerApiKeyAdapter(id -> "random_key"); - final ImageryInfo imageryInfo = new ImageryInfo("localhost", wireMockServer.url("/wms?apikey={apikey}"), + final ImageryInfo imageryInfo = new ImageryInfo("localhost", wireMockRuntimeInfo.getHttpBaseUrl() + "/wms?apikey={apikey}", "wms_endpoint", null, null); imageryInfo.setId("testActionPerformedEnabledWms"); new AddImageryLayerAction(imageryInfo).actionPerformed(null); diff --git a/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java b/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java index 923a6fdc69a..ac1fa5550e5 100644 --- a/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java +++ b/test/unit/org/openstreetmap/josm/actions/downloadtasks/AbstractDownloadTaskTestParent.java @@ -5,22 +5,22 @@ import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; +import org.junit.jupiter.api.BeforeEach; import org.openstreetmap.josm.testutils.annotations.BasicWiremock; import org.openstreetmap.josm.testutils.annotations.HTTPS; -import com.github.tomakehurst.wiremock.WireMockServer; - /** * Superclass of {@link DownloadGpsTaskTest}, {@link DownloadOsmTaskTest} and {@link DownloadNotesTaskTest}. */ @BasicWiremock @HTTPS public abstract class AbstractDownloadTaskTestParent { - /** - * HTTP mock. - */ - @BasicWiremock - WireMockServer wireMockServer; + WireMockRuntimeInfo wireMockServer; + @BeforeEach + void setup(WireMockRuntimeInfo wireMockRuntimeInfo) { + this.wireMockServer = wireMockRuntimeInfo; + } /** * Returns the path to remote test file to download via http. @@ -42,14 +42,18 @@ protected String getRemoteContentType() { * @return the http URL to remote test file to download */ protected final String getRemoteFileUrl() { - return wireMockServer.url(getRemoteFile()); + final String remote = getRemoteFile(); + if (remote.startsWith("/")) { + return wireMockServer.getHttpBaseUrl() + getRemoteFile(); + } + return wireMockServer.getHttpBaseUrl() + '/' + getRemoteFile(); } /** * Mock the HTTP server. */ protected final void mockHttp() { - wireMockServer.stubFor(get(urlEqualTo("/" + getRemoteFile())) + wireMockServer.getWireMock().register(get(urlEqualTo("/" + getRemoteFile())) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", getRemoteContentType()) diff --git a/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java b/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java index 5cec059204c..987764ac633 100644 --- a/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java +++ b/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java @@ -20,6 +20,7 @@ import java.nio.charset.StandardCharsets; import java.util.concurrent.TimeUnit; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import org.apache.commons.jcs3.access.behavior.ICacheAccess; import org.apache.commons.jcs3.engine.behavior.ICacheElement; import org.junit.jupiter.api.BeforeEach; @@ -32,7 +33,6 @@ import org.openstreetmap.josm.testutils.annotations.BasicWiremock; import org.openstreetmap.josm.tools.Logging; -import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.matching.UrlPattern; /** @@ -46,8 +46,7 @@ class JCSCachedTileLoaderJobTest { /** * mocked tile server */ - @BasicWiremock - WireMockServer tileServer; + WireMockRuntimeInfo tileServer; private static class TestCachedTileLoaderJob extends JCSCachedTileLoaderJob { private final String url; @@ -104,20 +103,23 @@ public synchronized void loadingFinished(CacheEntry data, CacheEntryAttributes a /** * Always clear cache before tests - * @throws Exception when clearing fails */ @BeforeEach - void clearCache() throws Exception { + void clearCache() { getCache().clear(); } + @BeforeEach + void setup(WireMockRuntimeInfo wireMockRuntimeInfo) { + this.tileServer = wireMockRuntimeInfo; + } + /** * Test status codes - * @throws InterruptedException in case of thread interruption * @throws IOException in case of I/O error */ @Test - void testStatusCodes() throws IOException, InterruptedException { + void testStatusCodes() throws IOException { doTestStatusCode(200); doTestStatusCode(401); doTestStatusCode(402); @@ -154,7 +156,7 @@ void testUnknownHost() throws IOException { } private void doTestStatusCode(int responseCode) throws IOException { - tileServer.stubFor(get(urlEqualTo("/httpstat/" + responseCode)).willReturn(aResponse().withStatus(responseCode))); + tileServer.getWireMock().register(get(urlEqualTo("/httpstat/" + responseCode)).willReturn(aResponse().withStatus(responseCode))); TestCachedTileLoaderJob job = getStatusLoaderJob(responseCode); Listener listener = submitJob(job); assertEquals(responseCode, listener.attributes.getResponseCode()); @@ -195,9 +197,9 @@ void testNoRequestMadeWhenEntryInCache() throws IOException { ); createHeadGetStub(urlEqualTo("/test"), expires, testStart, "eTag", "mock entry"); - TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); + TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/test", "test"); Listener listener = submitJob(job, false); - tileServer.verify(0, getRequestedFor(anyUrl())); + tileServer.getWireMock().verifyThat(0, getRequestedFor(anyUrl())); assertArrayEquals("cached entry".getBytes(StandardCharsets.UTF_8), listener.data); } @@ -216,9 +218,9 @@ void testRequestMadeWhenEntryInCacheAndForce() throws IOException { ); createHeadGetStub(urlEqualTo("/test"), expires, testStart, "eTag", "mock entry"); - TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); + TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/test", "test"); Listener listener = submitJob(job, true); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); } @@ -230,11 +232,11 @@ void testRequestMadeWhenEntryInCacheAndForce() throws IOException { @Test void testSettingMinimumExpiryWhenNoExpires() throws IOException { long testStart = System.currentTimeMillis(); - tileServer.stubFor(get(urlEqualTo("/test")).willReturn(aResponse().withBody("mock entry"))); + tileServer.getWireMock().register(get(urlEqualTo("/test")).willReturn(aResponse().withBody("mock entry"))); - TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); + TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/test", "test"); Listener listener = submitJob(job, false); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertTrue(listener.attributes.getExpirationTime() >= testStart + JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME, "Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " + @@ -259,16 +261,16 @@ void testSettingMinimumExpiryWhenNoExpires() throws IOException { void testSettingExpireByMaxAge() throws IOException { long testStart = System.currentTimeMillis(); long expires = TimeUnit.DAYS.toSeconds(1); - tileServer.stubFor(get(urlEqualTo("/test")) + tileServer.getWireMock().register(get(urlEqualTo("/test")) .willReturn(aResponse() .withHeader("Cache-control", "max-age=" + expires) .withBody("mock entry") ) ); - TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); + TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/test", "test"); Listener listener = submitJob(job, false); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertTrue(listener.attributes.getExpirationTime() >= testStart + TimeUnit.SECONDS.toMillis(expires), "Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " + @@ -297,9 +299,9 @@ void testSettingMinimumExpiryByMinimumExpiryTimeLessThanDefault() throws IOExcep createHeadGetStub(urlEqualTo("/test"), (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10), testStart, "eTag", "mock entry"); - TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds); + TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/test", "test", minimumExpiryTimeSeconds); Listener listener = submitJob(job, false); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); @@ -330,9 +332,9 @@ void testSettingMinimumExpiryByMinimumExpiryTimeGreaterThanDefault() throws IOEx createHeadGetStub(urlEqualTo("/test"), (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10), testStart, "eTag", "mock entry"); - TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds); + TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/test", "test", minimumExpiryTimeSeconds); Listener listener = submitJob(job, false); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); @@ -366,7 +368,7 @@ void testCacheControlVsExpires() throws IOException { long testStart = System.currentTimeMillis(); int minimumExpiryTimeSeconds = 0; - tileServer.stubFor(get(urlEqualTo("/test")) + tileServer.getWireMock().register(get(urlEqualTo("/test")) .willReturn(aResponse() .withHeader("Expires", TestUtils.getHTTPDate(testStart + (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10))) .withHeader("Cache-Control", "max-age=" + @@ -374,16 +376,16 @@ void testCacheControlVsExpires() throws IOException { .withBody("mock entry") ) ); - tileServer.stubFor(head(urlEqualTo("/test")) + tileServer.getWireMock().register(head(urlEqualTo("/test")) .willReturn(aResponse() .withHeader("Expires", TestUtils.getHTTPDate(testStart + (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10))) .withHeader("Cache-Control", "max-age=" + TimeUnit.MILLISECONDS.toSeconds((JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 2))) ) ); - TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds); + TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/test", "test", minimumExpiryTimeSeconds); Listener listener = submitJob(job, false); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); @@ -412,7 +414,7 @@ void testMaxAgeVsSMaxAge() throws IOException { long testStart = System.currentTimeMillis(); int minimumExpiryTimeSeconds = 0; - tileServer.stubFor(get(urlEqualTo("/test")) + tileServer.getWireMock().register(get(urlEqualTo("/test")) .willReturn(aResponse() .withHeader("Cache-Control", "" + "max-age=" + TimeUnit.MILLISECONDS.toSeconds((JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10)) + "," + @@ -421,16 +423,16 @@ void testMaxAgeVsSMaxAge() throws IOException { .withBody("mock entry") ) ); - tileServer.stubFor(head(urlEqualTo("/test")) + tileServer.getWireMock().register(head(urlEqualTo("/test")) .willReturn(aResponse() .withHeader("Cache-Control", "" + "max-age=" + TimeUnit.MILLISECONDS.toSeconds((JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10)) + "," + "s-max-age=" + TimeUnit.MILLISECONDS.toSeconds((JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 2)) ) )); - TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds); + TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/test", "test", minimumExpiryTimeSeconds); Listener listener = submitJob(job, false); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); assertTrue( @@ -460,7 +462,7 @@ void testCheckUsingHead() throws IOException { createEntryAttributes(-1 * expires, 200, testStart, "eTag--gzip") // Jetty adds --gzip to etags when compressing output ); - tileServer.stubFor(get(urlEqualTo("/test")) + tileServer.getWireMock().register(get(urlEqualTo("/test")) .willReturn(aResponse() .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) .withHeader("Last-Modified", Long.toString(testStart)) @@ -468,7 +470,7 @@ void testCheckUsingHead() throws IOException { .withBody("mock entry") ) ); - tileServer.stubFor(head(urlEqualTo("/test")) + tileServer.getWireMock().register(head(urlEqualTo("/test")) .willReturn(aResponse() .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) .withHeader("Last-Modified", Long.toString(testStart)) @@ -476,14 +478,14 @@ void testCheckUsingHead() throws IOException { ) ); - TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); + TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/test", "test"); Listener listener = submitJob(job, false); // cache entry is expired, no need to force refetch - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); // cache entry should be retrieved from cache listener = submitJob(job, false); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); // invalidate entry in cache @@ -493,17 +495,17 @@ void testCheckUsingHead() throws IOException { cache.put("test", cacheEntry.getVal(), attributes); // because cache entry is invalid - HEAD request shall be made - tileServer.verify(0, headRequestedFor(urlEqualTo("/test"))); // no head requests were made until now + tileServer.getWireMock().verifyThat(0, headRequestedFor(urlEqualTo("/test"))); // no head requests were made until now listener = submitJob(job, false); - tileServer.verify(1, headRequestedFor(urlEqualTo("/test"))); // verify head requests were made - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); // verify no more get requests were made + tileServer.getWireMock().verifyThat(1, headRequestedFor(urlEqualTo("/test"))); // verify head requests were made + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); // verify no more get requests were made assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); assertTrue(listener.attributes.getExpirationTime() >= testStart + expires); // cache entry should be retrieved from cache listener = submitJob(job, false); // cache entry is expired, no need to force refetch - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); } @@ -521,7 +523,7 @@ void testCheckUsing304() throws IOException { createEntryAttributes(-1 * expires, 200, testStart, "eTag") ); - tileServer.stubFor(get(urlEqualTo("/test")) + tileServer.getWireMock().register(get(urlEqualTo("/test")) .willReturn(status(304) .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) .withHeader("Last-Modified", Long.toString(testStart)) @@ -529,17 +531,17 @@ void testCheckUsing304() throws IOException { ) ); - TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); + TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/test", "test"); Listener listener = submitJob(job, false); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); assertArrayEquals("cached dummy".getBytes(StandardCharsets.UTF_8), listener.data); assertTrue(testStart + expires <= listener.attributes.getExpirationTime()); submitJob(job, false); - tileServer.verify(1, getRequestedFor(urlEqualTo("/test"))); // no more requests were made + tileServer.getWireMock().verifyThat(1, getRequestedFor(urlEqualTo("/test"))); // no more requests were made } private void createHeadGetStub(UrlPattern url, long expires, long lastModified, String eTag, String body) { - tileServer.stubFor(get(url) + tileServer.getWireMock().register(get(url) .willReturn(aResponse() .withHeader("Expires", TestUtils.getHTTPDate(lastModified + expires)) .withHeader("Last-Modified", Long.toString(lastModified)) @@ -547,7 +549,7 @@ private void createHeadGetStub(UrlPattern url, long expires, long lastModified, .withBody(body) ) ); - tileServer.stubFor(head(url) + tileServer.getWireMock().register(head(url) .willReturn(aResponse() .withHeader("Expires", TestUtils.getHTTPDate(lastModified + expires)) .withHeader("Last-Modified", Long.toString(lastModified)) @@ -566,7 +568,7 @@ private CacheEntryAttributes createEntryAttributes(long expirationTime, int resp } private TestCachedTileLoaderJob getStatusLoaderJob(int responseCode) { - return new TestCachedTileLoaderJob(tileServer.url("/httpstat/" + responseCode), "key_" + responseCode); + return new TestCachedTileLoaderJob(tileServer.getHttpBaseUrl() + "/httpstat/" + responseCode, "key_" + responseCode); } private static ICacheAccess getCache() { diff --git a/test/unit/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJobTest.java b/test/unit/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJobTest.java index e366e80d10f..4ff5bdc3b04 100644 --- a/test/unit/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJobTest.java +++ b/test/unit/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJobTest.java @@ -15,6 +15,7 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import org.apache.commons.jcs3.access.behavior.ICacheAccess; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -30,7 +31,6 @@ import org.openstreetmap.josm.tools.Logging; import org.openstreetmap.josm.tools.Utils; -import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; /** @@ -42,14 +42,18 @@ class TMSCachedTileLoaderJobTest { /** * mocked tile server */ - @BasicWiremock - WireMockServer tileServer; + private WireMockRuntimeInfo wireMockRuntimeInfo; @BeforeEach - void clearCache() throws Exception { + void clearCache() { getCache().clear(); } + @BeforeEach + void setup(WireMockRuntimeInfo wireMockRuntimeInfo) { + this.wireMockRuntimeInfo = wireMockRuntimeInfo; + } + private static ICacheAccess getCache() { return JCSCacheManager.getCache("test"); } @@ -125,7 +129,7 @@ private static class MockTileSource extends TMSTileSource { } @Override - public String getTileUrl(int zoom, int tilex, int tiley) throws IOException { + public String getTileUrl(int zoom, int tilex, int tiley) { return url; } } @@ -218,19 +222,19 @@ private TestCachedTileLoaderJob submitJob(MockTile tile, String key, int minimum @Test void testNoCacheHeaders() throws IOException { long testStart = System.currentTimeMillis(); - tileServer.stubFor( + wireMockRuntimeInfo.getWireMock().register( WireMock.get(WireMock.urlEqualTo("/test")) .willReturn(WireMock.aResponse() .withBody("mock entry") ) ); - TestCachedTileLoaderJob job = submitJob(new MockTile(tileServer.url("/test")), "test", false); + TestCachedTileLoaderJob job = submitJob(new MockTile(wireMockRuntimeInfo.getHttpBaseUrl() + "/test"), "test", false); assertExpirationAtLeast(testStart + TMSCachedTileLoaderJob.MINIMUM_EXPIRES.get(), job); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); - job = submitJob(new MockTile(tileServer.url("/test")), "test", false); // submit another job for the same tile + job = submitJob(new MockTile(wireMockRuntimeInfo.getHttpBaseUrl() + "/test"), "test", false); // submit another job for the same tile // only one request to tile server should be made, second should come from cache - tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); + wireMockRuntimeInfo.getWireMock().verifyThat(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); } @@ -256,18 +260,18 @@ void testNoCacheHeadersMinimumExpiresLargerThanMaximum() throws IOException { private void noCacheHeadersMinimumExpires(int minimumExpires) throws IOException { long testStart = System.currentTimeMillis(); - tileServer.stubFor( + wireMockRuntimeInfo.getWireMock().register( WireMock.get(WireMock.urlEqualTo("/test")) .willReturn(WireMock.aResponse() .withBody("mock entry") ) ); - TestCachedTileLoaderJob job = submitJob(new MockTile(tileServer.url("/test")), "test", minimumExpires, false); + TestCachedTileLoaderJob job = submitJob(new MockTile(wireMockRuntimeInfo.getHttpBaseUrl() + "/test"), "test", minimumExpires, false); assertExpirationAtLeast(testStart + minimumExpires, job); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); - job = submitJob(new MockTile(tileServer.url("/test")), "test", false); // submit another job for the same tile + job = submitJob(new MockTile(wireMockRuntimeInfo.getHttpBaseUrl() + "/test"), "test", false); // submit another job for the same tile // only one request to tile server should be made, second should come from cache - tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); + wireMockRuntimeInfo.getWireMock().verifyThat(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); } @@ -279,19 +283,19 @@ private void noCacheHeadersMinimumExpires(int minimumExpires) throws IOException void testShortExpire() throws IOException { long testStart = System.currentTimeMillis(); long expires = TMSCachedTileLoaderJob.MINIMUM_EXPIRES.get() / 2; - tileServer.stubFor( + wireMockRuntimeInfo.getWireMock().register( WireMock.get(WireMock.urlEqualTo("/test")) .willReturn(WireMock.aResponse() .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) .withBody("mock entry") ) ); - TestCachedTileLoaderJob job = submitJob(new MockTile(tileServer.url("/test")), "test", false); + TestCachedTileLoaderJob job = submitJob(new MockTile(wireMockRuntimeInfo.getHttpBaseUrl() + "/test"), "test", false); assertExpirationAtLeast(testStart + TMSCachedTileLoaderJob.MINIMUM_EXPIRES.get(), job); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); - job = submitJob(new MockTile(tileServer.url("/test")), "test", false); // submit another job for the same tile + job = submitJob(new MockTile(wireMockRuntimeInfo.getHttpBaseUrl() + "/test"), "test", false); // submit another job for the same tile // only one request to tile server should be made, second should come from cache - tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); + wireMockRuntimeInfo.getWireMock().verifyThat(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); } @@ -311,21 +315,21 @@ private void assertExpirationAtMost(long duration, TestCachedTileLoaderJob job) void testLongExpire() throws IOException { long testStart = System.currentTimeMillis(); long expires = TMSCachedTileLoaderJob.MAXIMUM_EXPIRES.get() * 2; - tileServer.stubFor( + wireMockRuntimeInfo.getWireMock().register( WireMock.get(WireMock.urlEqualTo("/test")) .willReturn(WireMock.aResponse() .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) .withBody("mock entry") ) ); - TestCachedTileLoaderJob job = submitJob(new MockTile(tileServer.url("/test")), "test", false); + TestCachedTileLoaderJob job = submitJob(new MockTile(wireMockRuntimeInfo.getHttpBaseUrl() + "/test"), "test", false); // give 1 second margin assertExpirationAtMost(testStart + TMSCachedTileLoaderJob.MAXIMUM_EXPIRES.get() + TimeUnit.SECONDS.toMillis(1), job); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); - job = submitJob(new MockTile(tileServer.url("/test")), "test", false); // submit another job for the same tile + job = submitJob(new MockTile(wireMockRuntimeInfo.getHttpBaseUrl() + "/test"), "test", false); // submit another job for the same tile // only one request to tile server should be made, second should come from cache - tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); + wireMockRuntimeInfo.getWireMock().verifyThat(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); } } diff --git a/test/unit/org/openstreetmap/josm/data/imagery/WMSEndpointTileSourceTest.java b/test/unit/org/openstreetmap/josm/data/imagery/WMSEndpointTileSourceTest.java index 047a00d543d..ca923429b50 100644 --- a/test/unit/org/openstreetmap/josm/data/imagery/WMSEndpointTileSourceTest.java +++ b/test/unit/org/openstreetmap/josm/data/imagery/WMSEndpointTileSourceTest.java @@ -10,6 +10,8 @@ import java.nio.file.Paths; import java.util.Collections; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openstreetmap.gui.jmapviewer.interfaces.TemplatedTileSource; import org.openstreetmap.josm.TestUtils; @@ -21,20 +23,22 @@ import org.openstreetmap.josm.testutils.annotations.BasicWiremock; import org.openstreetmap.josm.testutils.annotations.Projection; -import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; @BasicPreferences(true) @BasicWiremock @Projection class WMSEndpointTileSourceTest implements TileSourceTest { - @BasicWiremock - WireMockServer tileServer; + WireMockRuntimeInfo tileServer; + @BeforeEach + void setup(WireMockRuntimeInfo wireMockRuntimeInfo) { + this.tileServer = wireMockRuntimeInfo; + } private void basicMock() { final byte[] response = assertDoesNotThrow(() -> Files.readAllBytes( Paths.get(TestUtils.getTestDataRoot() + "wms/geofabrik-osm-inspector.xml"))); - tileServer.stubFor( + tileServer.getWireMock().register( WireMock.get(WireMock.urlEqualTo("/capabilities?SERVICE=WMS&REQUEST=GetCapabilities")) .willReturn(WireMock.aResponse().withBody(response)) ); @@ -44,7 +48,7 @@ private void basicMock() { public ImageryInfo getInfo() { this.basicMock(); final ImageryInfo info = new ImageryInfo("WMSEndpointTileSourceTest"); - info.setExtendedUrl(tileServer.url("/capabilities")); + info.setExtendedUrl(tileServer.getHttpBaseUrl() + "/capabilities"); info.setDefaultLayers(Collections.singletonList(new DefaultLayer(ImageryType.WMS_ENDPOINT, "single_node_in_way", "default", null))); info.setImageryType(ImageryType.WMS_ENDPOINT); @@ -59,7 +63,7 @@ public TemplatedTileSource getTileSource(ImageryInfo info) { @Test void testDefaultLayerSetInMaps() throws Exception { - tileServer.stubFor( + tileServer.getWireMock().register( WireMock.get(WireMock.urlEqualTo("/capabilities?SERVICE=WMS&REQUEST=GetCapabilities")) .willReturn( WireMock.aResponse() @@ -67,7 +71,7 @@ void testDefaultLayerSetInMaps() throws Exception { ) ); - tileServer.stubFor(WireMock.get(WireMock.urlEqualTo("/other/maps")).willReturn(WireMock.aResponse().withBody( + tileServer.getWireMock().register(WireMock.get(WireMock.urlEqualTo("/other/maps")).willReturn(WireMock.aResponse().withBody( "\n" + "\n" + "\n" + @@ -75,7 +79,7 @@ void testDefaultLayerSetInMaps() throws Exception { "OSM_Inspector-Geometry\n" + "wms_endpoint\n" + "qa\n" + - "\n" + + "\n" + "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAACXBIWXMAAAsSAAALEgHS3X78AAAB5UlEQVQ4y4WTwWsTURDG" + "fy8W1yYmXZOqtGJJFyGw6KF7CEigwYuS0kthrYUi4i0iORS9BU9hQdA/ILcixVBrwENKLz1FUBB0wWOwYFAqxUNYTZq6BfM8yC5d05iBObz3vfnmm3kz4sqDh/zP" + "7szdlG5I+Of1zQ1xFA8xxI4GH2cjg4Cl+UUJcC4SJq6c7FPkKRlIoPQk0+NnuDwxHrhvuYd83+8OVuBlHouE/eDXzW8+/qO9DyHB0vyiVHoy2INSNiPdeg23XuPs" + @@ -94,7 +98,7 @@ void testDefaultLayerSetInMaps() throws Exception { "" ))); - Config.getPref().putList("imagery.layers.sites", Collections.singletonList(tileServer.url("/other/maps"))); + Config.getPref().putList("imagery.layers.sites", Collections.singletonList(tileServer.getHttpBaseUrl() + "/other/maps")); ImageryLayerInfo.instance.loadDefaults(true, null, false); assertEquals(1, ImageryLayerInfo.instance.getDefaultLayers().size()); ImageryInfo wmsImageryInfo = ImageryLayerInfo.instance.getDefaultLayers().get(0); @@ -109,7 +113,7 @@ void testDefaultLayerSetInMaps() throws Exception { @Test void testCustomHeadersServerSide() throws IOException { - tileServer.stubFor( + tileServer.getWireMock().register( WireMock.get(WireMock.urlEqualTo("/capabilities?SERVICE=WMS&REQUEST=GetCapabilities")) .willReturn( WireMock.aResponse() @@ -117,7 +121,7 @@ void testCustomHeadersServerSide() throws IOException { ) ); - tileServer.stubFor(WireMock.get(WireMock.urlEqualTo("/other/maps")).willReturn(WireMock.aResponse().withBody( + tileServer.getWireMock().register(WireMock.get(WireMock.urlEqualTo("/other/maps")).willReturn(WireMock.aResponse().withBody( "\n" + "\n" + " \n" + @@ -128,7 +132,7 @@ void testCustomHeadersServerSide() throws IOException { " photo\n" + " NO\n" + " Historic Norwegian orthophotos and maps, courtesy of Geovekst and Norkart.\n" + - " \n" + + " \n" + " \n" + " © Geovekst\n" + " https://www.norgeibilder.no/\n" + @@ -140,7 +144,7 @@ void testCustomHeadersServerSide() throws IOException { "" ))); - Config.getPref().putList("imagery.layers.sites", Collections.singletonList(tileServer.url("/other/maps"))); + Config.getPref().putList("imagery.layers.sites", Collections.singletonList(tileServer.getHttpBaseUrl() + "/other/maps")); ImageryLayerInfo.instance.loadDefaults(true, null, false); ImageryInfo wmsImageryInfo = ImageryLayerInfo.instance.getDefaultLayers().get(0); wmsImageryInfo.setDefaultLayers(Collections.singletonList(new DefaultLayer(ImageryType.WMS_ENDPOINT, "historiske-ortofoto", "", ""))); @@ -149,7 +153,7 @@ void testCustomHeadersServerSide() throws IOException { assertEquals("b8e36d51-119a-423b-b156-d744d54123d5", wmsImageryInfo.getCustomHttpHeaders().get("X-WAAPI-TOKEN")); assertEquals("b8e36d51-119a-423b-b156-d744d54123d5", tileSource.getHeaders().get("X-WAAPI-TOKEN")); assertTrue(wmsImageryInfo.isGeoreferenceValid()); - tileServer.verify( + tileServer.getWireMock().verifyThat( WireMock.getRequestedFor(WireMock.urlEqualTo("/capabilities?SERVICE=WMS&REQUEST=GetCapabilities")) .withHeader("X-WAAPI-TOKEN", WireMock.equalTo("b8e36d51-119a-423b-b156-d744d54123d5"))); assertEquals("http://waapi.webatlas.no/wms-orto-hist/?" diff --git a/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java b/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java index f94b0f86a6a..1c27dd5693c 100644 --- a/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java +++ b/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java @@ -22,6 +22,7 @@ import java.util.List; import java.util.concurrent.TimeUnit; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; @@ -44,7 +45,6 @@ import org.openstreetmap.josm.testutils.annotations.Projection; import org.openstreetmap.josm.tools.ReflectionUtils; -import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; /** @@ -55,9 +55,6 @@ @Projection @Timeout(value = 5, unit = TimeUnit.MINUTES) class WMTSTileSourceTest { - @BasicWiremock - WireMockServer tileServer; - private final ImageryInfo testImageryTMS = new ImageryInfo("test imagery", "http://localhost", "tms", null, null); private final ImageryInfo testImageryPSEUDO_MERCATOR = getImagery(TestUtils.getTestDataRoot() + "wmts/getcapabilities-pseudo-mercator.xml"); private final ImageryInfo testImageryTOPO_PL = getImagery(TestUtils.getTestDataRoot() + "wmts/getcapabilities-TOPO.xml"); @@ -366,11 +363,11 @@ void testDimension() throws IOException, WMTSGetCapabilitiesException { } @Test - void testDefaultLayer() throws Exception { + void testDefaultLayer(WireMockRuntimeInfo wireMockRuntimeInfo) throws Exception { // https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/1.0.0/WMTSCapabilities.xml // do not use withFileBody as it needs different directory layout :( - tileServer.stubFor( + wireMockRuntimeInfo.getWireMock().register( WireMock.get("/getcapabilities.xml") .willReturn( WireMock.aResponse() @@ -380,7 +377,7 @@ void testDefaultLayer() throws Exception { ) ); - tileServer.stubFor( + wireMockRuntimeInfo.getWireMock().register( WireMock.get("/other/maps") .willReturn( WireMock.aResponse().withBody( @@ -391,7 +388,7 @@ void testDefaultLayer() throws Exception { "landsat\n" + "wmts\n" + "photo\n" + - "\n" + + "\n" + "" + "" + "" + @@ -399,7 +396,7 @@ void testDefaultLayer() throws Exception { "" ))); - Config.getPref().putList("imagery.layers.sites", Collections.singletonList(tileServer.url("/other/maps"))); + Config.getPref().putList("imagery.layers.sites", Collections.singletonList(wireMockRuntimeInfo.getHttpBaseUrl() + "/other/maps")); ImageryLayerInfo.instance.loadDefaults(true, null, false); assertEquals(1, ImageryLayerInfo.instance.getDefaultLayers().size()); diff --git a/test/unit/org/openstreetmap/josm/data/osm/DefaultNameFormatterTest.java b/test/unit/org/openstreetmap/josm/data/osm/DefaultNameFormatterTest.java index 8ac052aa3ea..c0a75a50517 100644 --- a/test/unit/org/openstreetmap/josm/data/osm/DefaultNameFormatterTest.java +++ b/test/unit/org/openstreetmap/josm/data/osm/DefaultNameFormatterTest.java @@ -14,6 +14,7 @@ import java.util.stream.Collectors; import java.util.stream.IntStream; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import org.junit.jupiter.api.Test; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.gui.tagging.presets.TaggingPresetReader; @@ -25,8 +26,6 @@ import org.openstreetmap.josm.testutils.annotations.HTTP; import org.xml.sax.SAXException; -import com.github.tomakehurst.wiremock.WireMockServer; - import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; /** @@ -37,28 +36,23 @@ @BasicWiremock @HTTP class DefaultNameFormatterTest { - /** - * HTTP mock. - */ - @BasicWiremock - WireMockServer wireMockServer; - /** * Non-regression test for ticket #9632. + * @param wireMockRuntimeInfo Wiremock information * @throws IllegalDataException if an error was found while parsing the data from the source * @throws IOException if any I/O error occurs * @throws SAXException if any XML error occurs */ @Test @SuppressFBWarnings(value = "ITA_INEFFICIENT_TO_ARRAY") - void testTicket9632() throws IllegalDataException, IOException, SAXException { + void testTicket9632(WireMockRuntimeInfo wireMockRuntimeInfo) throws IllegalDataException, IOException, SAXException { String source = "presets/Presets_BicycleJunction-preset.xml"; - wireMockServer.stubFor(get(urlEqualTo("/" + source)) + wireMockRuntimeInfo.getWireMock().register(get(urlEqualTo("/" + source)) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "text/xml") .withBodyFile(source))); - TaggingPresets.addTaggingPresets(TaggingPresetReader.readAll(wireMockServer.url(source), true)); + TaggingPresets.addTaggingPresets(TaggingPresetReader.readAll(wireMockRuntimeInfo.getHttpBaseUrl() + '/' + source, true)); Comparator> comparator = DefaultNameFormatter.getInstance().getRelationComparator(); diff --git a/test/unit/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTaskTest.java b/test/unit/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTaskTest.java index 87210a150ca..7e90eab26e2 100644 --- a/test/unit/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTaskTest.java +++ b/test/unit/org/openstreetmap/josm/gui/preferences/server/ApiUrlTestTaskTest.java @@ -13,6 +13,8 @@ import javax.swing.JLabel; import java.awt.Component; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Timeout; import org.openstreetmap.josm.testutils.annotations.BasicPreferences; @@ -20,8 +22,6 @@ import org.openstreetmap.josm.testutils.annotations.HTTP; import org.openstreetmap.josm.tools.Logging; -import com.github.tomakehurst.wiremock.WireMockServer; - /** * Unit tests of {@link ApiUrlTestTask} class. */ @@ -30,14 +30,16 @@ @BasicWiremock @HTTP class ApiUrlTestTaskTest { - /** - * HTTP mock. - */ - @BasicWiremock - WireMockServer wireMockServer; + private WireMockRuntimeInfo wireMockServer; + private static final Component PARENT = new JLabel(); + @BeforeEach + void setup(WireMockRuntimeInfo wireMockServer) { + this.wireMockServer = wireMockServer; + } + /** * Unit test of {@link ApiUrlTestTask#ApiUrlTestTask} - null url. */ @@ -51,7 +53,7 @@ void testNullApiUrl() { */ @Test void testNominalUrl() { - ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.url("/__files/api")); + ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.getHttpBaseUrl() + "/__files/api"); task.run(); assertTrue(task.isSuccess()); } @@ -88,10 +90,10 @@ void testUnknownHost() { @Test void testAlertInvalidServerResult() { Logging.clearLastErrorAndWarnings(); - wireMockServer.stubFor(get(urlEqualTo("/does-not-exist/0.6/capabilities")) + wireMockServer.getWireMock().register(get(urlEqualTo("/does-not-exist/0.6/capabilities")) .willReturn(aResponse().withStatus(404))); - ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.url("/does-not-exist")); + ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.getHttpBaseUrl() + "/does-not-exist"); task.run(); assertFalse(task.isSuccess()); assertThat(Logging.getLastErrorAndWarnings().toString(), containsString( @@ -104,11 +106,12 @@ void testAlertInvalidServerResult() { @Test void testAlertInvalidCapabilities() { Logging.clearLastErrorAndWarnings(); - ApiUrlTestTask task = new ApiUrlTestTask(PARENT, wireMockServer.url("/__files/invalid_api")); + final String url = wireMockServer.getHttpBaseUrl() + "/__files/invalid_api"; + ApiUrlTestTask task = new ApiUrlTestTask(PARENT, url); task.run(); assertFalse(task.isSuccess()); assertThat(Logging.getLastErrorAndWarnings().toString(), containsString( "The OSM API server at 'XXX' did not return a valid response.
It is likely that 'XXX' is not an OSM API server." - .replace("XXX", wireMockServer.url("/__files/invalid_api")))); + .replace("XXX", url))); } } diff --git a/test/unit/org/openstreetmap/josm/io/OsmServerHistoryReaderTest.java b/test/unit/org/openstreetmap/josm/io/OsmServerHistoryReaderTest.java index fd689452def..d6b80b56027 100644 --- a/test/unit/org/openstreetmap/josm/io/OsmServerHistoryReaderTest.java +++ b/test/unit/org/openstreetmap/josm/io/OsmServerHistoryReaderTest.java @@ -5,6 +5,7 @@ import java.time.Instant; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openstreetmap.josm.data.osm.OsmPrimitiveType; @@ -16,8 +17,6 @@ import org.openstreetmap.josm.testutils.annotations.BasicWiremock; import org.openstreetmap.josm.testutils.annotations.HTTP; -import com.github.tomakehurst.wiremock.WireMockServer; - /** * Unit tests of {@link OsmServerHistoryReader} class. */ @@ -25,18 +24,12 @@ @BasicWiremock @HTTP class OsmServerHistoryReaderTest { - /** - * HTTP mock. - */ - @BasicWiremock - WireMockServer wireMockServer; - /** * Setup tests. */ @BeforeEach - void setUp() { - Config.getPref().put("osm-server.url", wireMockServer.url("/__files/api")); + void setUp(WireMockRuntimeInfo wireMockRuntimeInfo) { + Config.getPref().put("osm-server.url", wireMockRuntimeInfo.getHttpBaseUrl() + "/__files/api"); } /** diff --git a/test/unit/org/openstreetmap/josm/io/OverpassDownloadReaderTest.java b/test/unit/org/openstreetmap/josm/io/OverpassDownloadReaderTest.java index 3c163f53b03..206a80e622e 100644 --- a/test/unit/org/openstreetmap/josm/io/OverpassDownloadReaderTest.java +++ b/test/unit/org/openstreetmap/josm/io/OverpassDownloadReaderTest.java @@ -13,6 +13,7 @@ import java.time.LocalDateTime; import java.util.regex.Matcher; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openstreetmap.josm.data.Bounds; @@ -24,8 +25,6 @@ import org.openstreetmap.josm.tools.Utils; import org.openstreetmap.josm.tools.date.DateUtils; -import com.github.tomakehurst.wiremock.WireMockServer; - /** * Unit tests of {@link OverpassDownloadReader} class. */ @@ -33,11 +32,7 @@ @BasicPreferences @HTTP class OverpassDownloadReaderTest { - /** - * HTTP mock. - */ - @BasicWiremock - WireMockServer wireMockServer; + private WireMockRuntimeInfo wireMockServer; private static final String NOMINATIM_URL_PATH = "/search?format=xml&q="; @@ -45,8 +40,9 @@ class OverpassDownloadReaderTest { * Setup test. */ @BeforeEach - public void setUp() { - NameFinder.NOMINATIM_URL_PROP.put(wireMockServer.url(NOMINATIM_URL_PATH)); + public void setUp(WireMockRuntimeInfo wireMockRuntimeInfo) { + this.wireMockServer = wireMockRuntimeInfo; + NameFinder.NOMINATIM_URL_PROP.put(wireMockServer.getHttpBaseUrl() + NOMINATIM_URL_PATH); } private String getExpandedQuery(String search) { @@ -73,7 +69,7 @@ void testBbox() { } private void stubNominatim(String query) { - wireMockServer.stubFor(get(urlEqualTo(NOMINATIM_URL_PATH + query)) + wireMockServer.getWireMock().register(get(urlEqualTo(NOMINATIM_URL_PATH + query)) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "text/xml") diff --git a/test/unit/org/openstreetmap/josm/io/imagery/WMSImageryTest.java b/test/unit/org/openstreetmap/josm/io/imagery/WMSImageryTest.java index 30f0d3a5499..3b121bbb5da 100644 --- a/test/unit/org/openstreetmap/josm/io/imagery/WMSImageryTest.java +++ b/test/unit/org/openstreetmap/josm/io/imagery/WMSImageryTest.java @@ -10,6 +10,8 @@ import java.nio.file.Paths; import java.util.List; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.openstreetmap.josm.TestUtils; import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException; @@ -17,7 +19,6 @@ import org.openstreetmap.josm.testutils.annotations.BasicWiremock; import org.openstreetmap.josm.testutils.annotations.Projection; -import com.github.tomakehurst.wiremock.WireMockServer; import com.github.tomakehurst.wiremock.client.WireMock; /** @@ -27,8 +28,13 @@ @BasicWiremock @Projection class WMSImageryTest { - @BasicWiremock - WireMockServer tileServer; + + private WireMockRuntimeInfo tileServer; + + @BeforeEach + void setup(WireMockRuntimeInfo wireMockRuntimeRule) { + this.tileServer = wireMockRuntimeRule; + } /** * Unit test of {@code WMSImagery.WMSGetCapabilitiesException} class @@ -51,20 +57,20 @@ void testWMSGetCapabilitiesException() { */ @Test void testTicket15730() throws IOException, WMSGetCapabilitiesException { - tileServer.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody( + tileServer.getWireMock().register(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody( Files.readAllBytes(Paths.get(TestUtils.getRegressionDataDir(15730), "capabilities.xml")) ))); - WMSImagery wms = new WMSImagery(tileServer.url("capabilities.xml")); + WMSImagery wms = new WMSImagery(tileServer.getHttpBaseUrl() + "/capabilities.xml"); assertEquals(1, wms.getLayers().size()); assertTrue(wms.getLayers().get(0).getAbstract().startsWith("South Carolina NAIP Imagery 2017 Resolution: 100CM ")); } @Test void testNestedLayers() throws Exception { - tileServer.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody( + tileServer.getWireMock().register(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody( Files.readAllBytes(Paths.get(TestUtils.getTestDataRoot() + "wms/mapa-um-warszawa-pl.xml"))))); - WMSImagery wmsi = new WMSImagery(tileServer.url("/serwis")); + WMSImagery wmsi = new WMSImagery(tileServer.getHttpBaseUrl() + "/serwis"); assertEquals(1, wmsi.getLayers().size()); assertEquals("Server WMS m.st. Warszawy", wmsi.getLayers().get(0).toString()); assertEquals(202, wmsi.getLayers().get(0).getChildren().size()); @@ -78,8 +84,8 @@ void testNestedLayers() throws Exception { @Test void testTicket16248() throws IOException, WMSGetCapabilitiesException { byte[] capabilities = Files.readAllBytes(Paths.get(TestUtils.getRegressionDataFile(16248, "capabilities.xml"))); - tileServer.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody(capabilities))); - WMSImagery wms = new WMSImagery(tileServer.url("any")); + tileServer.getWireMock().register(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody(capabilities))); + WMSImagery wms = new WMSImagery(tileServer.getHttpBaseUrl() + "/any"); assertEquals("http://wms.hgis.cartomatic.pl/topo/3857/m25k?", wms.buildRootUrl()); assertEquals("wms.hgis.cartomatic.pl", wms.getLayers().get(0).getName()); assertEquals("http://wms.hgis.cartomatic.pl/topo/3857/m25k?FORMAT=image/png&TRANSPARENT=TRUE&VERSION=1.3.0&SERVICE=WMS&REQUEST=GetMap&" @@ -95,8 +101,8 @@ void testTicket16248() throws IOException, WMSGetCapabilitiesException { @Test void testTicket19193() throws IOException, WMSGetCapabilitiesException { byte[] capabilities = Files.readAllBytes(Paths.get(TestUtils.getRegressionDataFile(19193, "capabilities.xml"))); - tileServer.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody(capabilities))); - WMSImagery wms = new WMSImagery(tileServer.url("any")); + tileServer.getWireMock().register(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody(capabilities))); + WMSImagery wms = new WMSImagery(tileServer.getHttpBaseUrl() + "/any"); assertEquals("https://inspire.brandenburg.de/services/gn_alkis_wms?", wms.buildRootUrl()); assertEquals(1, wms.getLayers().size()); assertNull(wms.getLayers().get(0).getName()); @@ -113,13 +119,13 @@ void testTicket19193() throws IOException, WMSGetCapabilitiesException { */ @Test void testTicket16333() throws IOException, WMSGetCapabilitiesException { - tileServer.stubFor( + tileServer.getWireMock().register( WireMock.get(WireMock.anyUrl()) .willReturn(WireMock.aResponse().withBody( Files.readAllBytes(Paths.get(TestUtils.getRegressionDataFile(16333, "capabilities.xml"))) )) ); - WMSImagery wms = new WMSImagery(tileServer.url("any")); + WMSImagery wms = new WMSImagery(tileServer.getHttpBaseUrl() + "/any"); assertEquals("https://duinoord.xs4all.nl/geoserver/ows?SERVICE=WMS&", wms.buildRootUrl()); assertNull(wms.getLayers().get(0).getName()); assertEquals("", wms.getLayers().get(0).getTitle()); @@ -130,13 +136,13 @@ void testTicket16333() throws IOException, WMSGetCapabilitiesException { @Test void testForTitleWithinAttribution_ticket16940() throws IOException, WMSGetCapabilitiesException { - tileServer.stubFor( + tileServer.getWireMock().register( WireMock.get(WireMock.anyUrl()) .willReturn(WireMock.aResponse().withBody( Files.readAllBytes(Paths.get(TestUtils.getRegressionDataFile(16940, "capabilities.xml"))) )) ); - WMSImagery wms = new WMSImagery(tileServer.url("any")); + WMSImagery wms = new WMSImagery(tileServer.getHttpBaseUrl() + "/any"); assertEquals("Hipsográfico", wms.getLayers().stream().findFirst().get().getTitle()); } } diff --git a/test/unit/org/openstreetmap/josm/tools/bugreport/BugReportSenderTest.java b/test/unit/org/openstreetmap/josm/tools/bugreport/BugReportSenderTest.java index 3ce532dadef..0774716b374 100644 --- a/test/unit/org/openstreetmap/josm/tools/bugreport/BugReportSenderTest.java +++ b/test/unit/org/openstreetmap/josm/tools/bugreport/BugReportSenderTest.java @@ -15,6 +15,7 @@ import java.net.URI; import java.util.List; +import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; import org.junit.jupiter.api.Test; import org.openstreetmap.josm.actions.ShowStatusReportAction; import org.openstreetmap.josm.spi.preferences.Config; @@ -23,8 +24,6 @@ import org.openstreetmap.josm.testutils.annotations.HTTP; import org.openstreetmap.josm.testutils.mockers.OpenBrowserMocker; -import com.github.tomakehurst.wiremock.WireMockServer; - /** * Unit tests of {@link BugReportSender} class. */ @@ -32,20 +31,14 @@ @BasicWiremock @HTTP class BugReportSenderTest { - /** - * HTTP mock - */ - @BasicWiremock - WireMockServer wireMockServer; - /** * Unit test for {@link BugReportSender#BugReportSender}. * @throws InterruptedException if the thread is interrupted */ @Test - void testBugReportSender() throws InterruptedException { - Config.getPref().put("josm.url", wireMockServer.baseUrl()); - wireMockServer.stubFor(post(urlEqualTo("/josmticket")) + void testBugReportSender(WireMockRuntimeInfo wireMockRuntimeInfo) throws InterruptedException { + Config.getPref().put("josm.url", wireMockRuntimeInfo.getHttpBaseUrl()); + wireMockRuntimeInfo.getWireMock().register(post(urlEqualTo("/josmticket")) .willReturn(aResponse() .withStatus(200) .withHeader("Content-Type", "text/xml") @@ -65,10 +58,11 @@ void testBugReportSender() throws InterruptedException { assertFalse(sender.isAlive()); assertNull(sender.getErrorMessage(), sender.getErrorMessage()); - wireMockServer.verify(exactly(1), postRequestedFor(urlEqualTo("/josmticket")).withRequestBody(containing("pdata="))); + wireMockRuntimeInfo.getWireMock().verifyThat(exactly(1), + postRequestedFor(urlEqualTo("/josmticket")).withRequestBody(containing("pdata="))); List calledURIs = OpenBrowserMocker.getCalledURIs(); assertEquals(1, calledURIs.size()); - assertEquals(wireMockServer.url("/josmticket?pdata_stored=6bccff5c0417217bfbbe5fff"), calledURIs.get(0).toString()); + assertEquals(wireMockRuntimeInfo.getHttpBaseUrl() + "/josmticket?pdata_stored=6bccff5c0417217bfbbe5fff", calledURIs.get(0).toString()); } }