Changeset 13733 in josm for trunk/test/unit/org/openstreetmap
- Timestamp:
- 2018-05-12T14:18:57+02:00 (6 years ago)
- Location:
- trunk/test/unit/org/openstreetmap/josm
- Files:
-
- 1 added
- 9 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/test/unit/org/openstreetmap/josm/TestUtils.java
r13489 r13733 15 15 import java.security.AccessController; 16 16 import java.security.PrivilegedAction; 17 import java.time.Instant; 18 import java.time.ZoneOffset; 19 import java.time.format.DateTimeFormatter; 20 import java.time.temporal.Temporal; 17 21 import java.util.Arrays; 18 22 import java.util.Collection; … … 38 42 import org.openstreetmap.josm.tools.JosmRuntimeException; 39 43 import org.openstreetmap.josm.tools.Utils; 44 45 import com.github.tomakehurst.wiremock.WireMockServer; 46 import com.github.tomakehurst.wiremock.core.WireMockConfiguration; 40 47 41 48 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 380 387 } 381 388 } 389 390 /** 391 * Return WireMock server serving files under ticker directory 392 * @param ticketId Ticket numeric identifier 393 * @return WireMock HTTP server on dynamic port 394 */ 395 public static WireMockServer getWireMockServer(int ticketId) { 396 return new WireMockServer( 397 WireMockConfiguration.options() 398 .dynamicPort() 399 .usingFilesUnderDirectory(getRegressionDataDir(ticketId)) 400 ); 401 } 402 403 /** 404 * Return WireMock server serving files under ticker directory 405 * @return WireMock HTTP server on dynamic port 406 */ 407 public static WireMockServer getWireMockServer() { 408 return new WireMockServer( 409 WireMockConfiguration.options() 410 .dynamicPort() 411 ); 412 } 413 /** 414 * Renders Temporal to RFC 1123 Date Time 415 * @param time 416 * @return string representation according to RFC1123 of time 417 */ 418 public static String getHTTPDate(Temporal time) { 419 return DateTimeFormatter.RFC_1123_DATE_TIME.withZone(ZoneOffset.UTC).format(time); 420 } 421 422 /** 423 * Renders java time stamp to RFC 1123 Date Time 424 * @param time 425 * @return string representation according to RFC1123 of time 426 */ 427 public static String getHTTPDate(long time) { 428 return getHTTPDate(Instant.ofEpochMilli(time)); 429 } 430 431 382 432 } -
trunk/test/unit/org/openstreetmap/josm/actions/AddImageryLayerActionTest.java
r12636 r13733 70 70 @Test 71 71 public void testActionPerformedEnabledWms() { 72 wireMockRule.stubFor(get(urlEqualTo("/wms? VERSION=1.1.1&SERVICE=WMS&REQUEST=GetCapabilities"))72 wireMockRule.stubFor(get(urlEqualTo("/wms?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.1.1")) 73 73 .willReturn(aResponse() 74 .withStatus(200) 75 .withHeader("Content-Type", "text/xml") 76 .withBodyFile("imagery/wms-capabilities.xml"))); 74 .withStatus(200) 75 .withHeader("Content-Type", "text/xml") 76 .withBodyFile("imagery/wms-capabilities.xml"))); 77 wireMockRule.stubFor(get(urlEqualTo("/wms?SERVICE=WMS&REQUEST=GetCapabilities")) 78 .willReturn(aResponse() 79 .withStatus(404))); 80 wireMockRule.stubFor(get(urlEqualTo("/wms?SERVICE=WMS&REQUEST=GetCapabilities&VERSION=1.3.0")) 81 .willReturn(aResponse() 82 .withStatus(404))); 83 77 84 new AddImageryLayerAction(new ImageryInfo("localhost", "http://localhost:" + wireMockRule.port() + "/wms?", 78 85 "wms_endpoint", null, null)).actionPerformed(null); -
trunk/test/unit/org/openstreetmap/josm/data/cache/HostLimitQueueTest.java
r12620 r13733 14 14 import org.junit.Rule; 15 15 import org.junit.Test; 16 import org.openstreetmap.josm.data.imagery.TileJobOptions; 16 17 import org.openstreetmap.josm.testutils.JOSMTestRules; 17 18 import org.openstreetmap.josm.tools.Logging; … … 54 55 55 56 Task(ICacheAccess<String, CacheEntry> cache, URL url, AtomicInteger counter) { 56 super(cache, 1, 1, null);57 super(cache, new TileJobOptions(1, 1, null, 10)); 57 58 this.url = url; 58 59 this.counter = counter; -
trunk/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java
r13358 r13733 2 2 package org.openstreetmap.josm.data.cache; 3 3 4 import static org.junit.Assert.assertArrayEquals; 4 5 import static org.junit.Assert.assertEquals; 5 6 import static org.junit.Assert.assertFalse; 7 import static org.junit.Assert.assertTrue; 6 8 7 9 import java.io.IOException; … … 9 11 import java.net.URL; 10 12 import java.nio.charset.StandardCharsets; 13 import java.util.concurrent.TimeUnit; 11 14 12 15 import org.apache.commons.jcs.access.behavior.ICacheAccess; 16 import org.apache.commons.jcs.engine.behavior.ICacheElement; 13 17 import org.junit.Before; 14 18 import org.junit.Rule; 15 19 import org.junit.Test; 20 import org.openstreetmap.josm.TestUtils; 16 21 import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult; 22 import org.openstreetmap.josm.data.imagery.TileJobOptions; 17 23 import org.openstreetmap.josm.testutils.JOSMTestRules; 18 24 import org.openstreetmap.josm.tools.Logging; 25 26 import com.github.tomakehurst.wiremock.client.WireMock; 27 import com.github.tomakehurst.wiremock.core.WireMockConfiguration; 28 import com.github.tomakehurst.wiremock.junit.WireMockRule; 29 import com.github.tomakehurst.wiremock.matching.UrlPattern; 19 30 20 31 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 25 36 public class JCSCachedTileLoaderJobTest { 26 37 38 /** 39 * mocked tile server 40 */ 41 @Rule 42 public WireMockRule tileServer = new WireMockRule(WireMockConfiguration.options() 43 .dynamicPort()); 44 27 45 private static class TestCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, CacheEntry> { 28 46 private String url; 29 47 private String key; 30 48 31 TestCachedTileLoaderJob(String url, String key) throws IOException { 32 super(getCache(), 30000, 30000, null); 49 TestCachedTileLoaderJob(String url, String key) { 50 this(url, key, (int) TimeUnit.DAYS.toSeconds(1)); 51 } 52 53 TestCachedTileLoaderJob(String url, String key, int minimumExpiry) { 54 super(getCache(), new TileJobOptions(30000, 30000, null, minimumExpiry)); 33 55 34 56 this.url = url; 35 57 this.key = key; 36 58 } 59 37 60 38 61 @Override … … 52 75 @Override 53 76 protected CacheEntry createCacheEntry(byte[] content) { 54 return new CacheEntry( "dummy".getBytes(StandardCharsets.UTF_8));77 return new CacheEntry(content); 55 78 } 56 79 } … … 60 83 private boolean ready; 61 84 private LoadResult result; 85 private byte[] data; 62 86 63 87 @Override … … 66 90 this.ready = true; 67 91 this.result = result; 92 if (data != null) { 93 this.data = data.content; 94 } 68 95 this.notifyAll(); 69 96 } … … 113 140 String key = "key_unknown_host"; 114 141 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown", key); 115 Listener listener = new Listener(); 116 job.submit(listener, true); 117 synchronized (listener) { 118 while (!listener.ready) { 119 try { 120 listener.wait(); 121 } catch (InterruptedException e1) { 122 // do nothing, still wait 123 Logging.trace(e1); 124 } 125 } 126 } 142 Listener listener = submitJob(job); 127 143 assertEquals(LoadResult.FAILURE, listener.result); // because response will be cached, and that is checked below 128 144 assertEquals("java.net.UnknownHostException: unkownhost.unkownhost", listener.attributes.getErrorMessage()); … … 135 151 136 152 job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown", key); 137 listener = new Listener(); 138 job.submit(listener, true); 153 listener = submitJob(job); 154 assertEquals(LoadResult.SUCCESS, listener.result); 155 assertFalse(job.isCacheElementValid()); 156 } 157 158 private void doTestStatusCode(int responseCode) throws IOException { 159 TestCachedTileLoaderJob job = getStatusLoaderJob(responseCode); 160 Listener listener = submitJob(job); 161 assertEquals(responseCode, listener.attributes.getResponseCode()); 162 } 163 164 private Listener submitJob(TestCachedTileLoaderJob job) throws IOException { 165 return submitJob(job, true); 166 } 167 168 private Listener submitJob(TestCachedTileLoaderJob job, boolean force) throws IOException { 169 Listener listener = new Listener(); 170 job.submit(listener, force); 139 171 synchronized (listener) { 140 172 while (!listener.ready) { 141 173 try { 142 174 listener.wait(); 143 } catch (InterruptedException e 1) {175 } catch (InterruptedException e) { 144 176 // do nothing, wait 145 Logging.trace(e 1);177 Logging.trace(e); 146 178 } 147 179 } 148 180 } 149 assertEquals(LoadResult.SUCCESS, listener.result); 150 assertFalse(job.isCacheElementValid()); 151 } 152 153 @SuppressFBWarnings(value = "WA_NOT_IN_LOOP") 154 private void doTestStatusCode(int responseCode) throws IOException, InterruptedException { 155 TestCachedTileLoaderJob job = getStatusLoaderJob(responseCode); 156 Listener listener = new Listener(); 157 job.submit(listener, true); 158 synchronized (listener) { 159 if (!listener.ready) { 160 listener.wait(); 161 } 162 } 163 assertEquals(responseCode, listener.attributes.getResponseCode()); 164 } 165 166 private static TestCachedTileLoaderJob getStatusLoaderJob(int responseCode) throws IOException { 181 return listener; 182 } 183 184 /** 185 * That no requst is made when entry is in cache and force == false 186 * @throws IOException 187 */ 188 @Test 189 public void testNoRequestMadeWhenEntryInCache() throws IOException { 190 ICacheAccess<String, CacheEntry> cache = getCache(); 191 long expires = TimeUnit.DAYS.toMillis(1); 192 long testStart = System.currentTimeMillis(); 193 cache.put("test", 194 new CacheEntry("cached entry".getBytes(StandardCharsets.UTF_8)), 195 createEntryAttributes(expires, 200, testStart, "eTag") 196 ); 197 createHeadGetStub(WireMock.urlEqualTo("/test"), expires, testStart, "eTag", "mock entry"); 198 199 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 200 Listener listener = submitJob(job, false); 201 tileServer.verify(0, WireMock.getRequestedFor(WireMock.anyUrl())); 202 assertArrayEquals("cached entry".getBytes(StandardCharsets.UTF_8), listener.data); 203 } 204 205 /** 206 * that request is made, when object is in cache, but force mode is used 207 * @throws IOException 208 */ 209 @Test 210 public void testRequestMadeWhenEntryInCacheAndForce() throws IOException { 211 ICacheAccess<String, CacheEntry> cache = getCache(); 212 long expires = TimeUnit.DAYS.toMillis(1); 213 long testStart = System.currentTimeMillis(); 214 cache.put("test", 215 new CacheEntry("cached dummy".getBytes(StandardCharsets.UTF_8)), 216 createEntryAttributes(expires, 200, testStart + expires, "eTag") 217 ); 218 createHeadGetStub(WireMock.urlEqualTo("/test"), expires, testStart, "eTag", "mock entry"); 219 220 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 221 Listener listener = submitJob(job, true); 222 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 223 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 224 } 225 226 /** 227 * Mock returns no cache-control / expires headers 228 * Expire time should be set to DEFAULT_EXPIRE_TIME 229 * @throws IOException 230 */ 231 @Test 232 public void testSettingMinimumExpiryWhenNoExpires() throws IOException { 233 long testStart = System.currentTimeMillis(); 234 tileServer.stubFor( 235 WireMock.get(WireMock.urlEqualTo("/test")) 236 .willReturn(WireMock.aResponse() 237 .withBody("mock entry") 238 ) 239 ); 240 241 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 242 Listener listener = submitJob(job, false); 243 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 244 245 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " + 246 JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME + " (DEFAULT_EXPIRE_TIME)", 247 listener.attributes.getExpirationTime() >= testStart + JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME); 248 249 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - System.currentTimeMillis()) + " which is not less than " + 250 JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME + " (DEFAULT_EXPIRE_TIME)", 251 listener.attributes.getExpirationTime() <= System.currentTimeMillis() + JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME); 252 253 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 254 } 255 256 /** 257 * Mock returns expires headers, but Cache-Control 258 * Expire time should be set to max-age 259 * @throws IOException 260 */ 261 @Test 262 public void testSettingExpireByMaxAge() throws IOException { 263 long testStart = System.currentTimeMillis(); 264 long expires = TimeUnit.DAYS.toSeconds(1); 265 tileServer.stubFor( 266 WireMock.get(WireMock.urlEqualTo("/test")) 267 .willReturn(WireMock.aResponse() 268 .withHeader("Cache-control", "max-age=" + expires) 269 .withBody("mock entry") 270 ) 271 ); 272 273 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 274 Listener listener = submitJob(job, false); 275 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 276 277 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " + 278 TimeUnit.SECONDS.toMillis(expires) + " (max-age)", 279 listener.attributes.getExpirationTime() >= testStart + TimeUnit.SECONDS.toMillis(expires)); 280 281 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - System.currentTimeMillis()) + " which is not less than " + 282 TimeUnit.SECONDS.toMillis(expires) + " (max-age)", 283 listener.attributes.getExpirationTime() <= System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(expires)); 284 285 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 286 } 287 288 /** 289 * mock returns expiration: JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10 290 * minimum expire time: JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 2 291 * @throws IOException 292 */ 293 @Test 294 public void testSettingMinimumExpiryByMinimumExpiryTimeLessThanDefault() throws IOException { 295 long testStart = System.currentTimeMillis(); 296 int minimumExpiryTimeSeconds = (int)(JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 2); 297 298 createHeadGetStub(WireMock.urlEqualTo("/test"), (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10), testStart, "eTag", "mock entry"); 299 300 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds); 301 Listener listener = submitJob(job, false); 302 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 303 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 304 305 306 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " + 307 TimeUnit.SECONDS.toMillis(minimumExpiryTimeSeconds) + " (minimumExpireTime)", 308 listener.attributes.getExpirationTime() >= testStart + TimeUnit.SECONDS.toMillis(minimumExpiryTimeSeconds) ); 309 310 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - System.currentTimeMillis()) + " which is not less than " + 311 TimeUnit.SECONDS.toMillis(minimumExpiryTimeSeconds) + " (minimumExpireTime)", 312 listener.attributes.getExpirationTime() <= System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(minimumExpiryTimeSeconds)); 313 } 314 315 /** 316 * mock returns expiration: JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10 317 * minimum expire time: JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME * 2 318 * @throws IOException 319 */ 320 321 @Test 322 public void testSettingMinimumExpiryByMinimumExpiryTimeGreaterThanDefault() throws IOException { 323 long testStart = System.currentTimeMillis(); 324 int minimumExpiryTimeSeconds = (int)(JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME * 2); 325 326 createHeadGetStub(WireMock.urlEqualTo("/test"), (JCSCachedTileLoaderJob.DEFAULT_EXPIRE_TIME / 10), testStart, "eTag", "mock entry"); 327 328 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test", minimumExpiryTimeSeconds); 329 Listener listener = submitJob(job, false); 330 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 331 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 332 333 334 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - testStart) + " which is not larger than " + 335 TimeUnit.SECONDS.toMillis(minimumExpiryTimeSeconds) + " (minimumExpireTime)", 336 listener.attributes.getExpirationTime() >= testStart + TimeUnit.SECONDS.toMillis(minimumExpiryTimeSeconds) ); 337 338 assertTrue("Cache entry expiration is " + (listener.attributes.getExpirationTime() - System.currentTimeMillis()) + " which is not less than " + 339 TimeUnit.SECONDS.toMillis(minimumExpiryTimeSeconds) + " (minimumExpireTime)", 340 listener.attributes.getExpirationTime() <= System.currentTimeMillis() + TimeUnit.SECONDS.toMillis(minimumExpiryTimeSeconds)); 341 } 342 343 /** 344 * Check if verifying cache entries using HEAD requests work properly 345 * @throws IOException 346 */ 347 @Test 348 public void testCheckUsingHead() throws IOException { 349 ICacheAccess<String, CacheEntry> cache = getCache(); 350 long expires = TimeUnit.DAYS.toMillis(1); 351 long testStart = System.currentTimeMillis(); 352 cache.put("test", 353 new CacheEntry("cached dummy".getBytes(StandardCharsets.UTF_8)), 354 createEntryAttributes(-1 * expires, 200, testStart, "eTag--gzip") // Jetty adds --gzip to etags when compressing output 355 ); 356 357 tileServer.stubFor( 358 WireMock.get(WireMock.urlEqualTo("/test")) 359 .willReturn(WireMock.aResponse() 360 .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) 361 .withHeader("Last-Modified", Long.toString(testStart)) 362 .withHeader("ETag", "eTag") // Jetty adds "--gzip" suffix for compressed content 363 .withBody("mock entry") 364 ) 365 ); 366 tileServer.stubFor( 367 WireMock.head(WireMock.urlEqualTo("/test")) 368 .willReturn(WireMock.aResponse() 369 .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) 370 .withHeader("Last-Modified", Long.toString(testStart)) 371 .withHeader("ETag", "eTag--gzip") // but doesn't add to uncompressed 372 ) 373 ); 374 375 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 376 Listener listener = submitJob(job, false); // cache entry is expired, no need to force refetch 377 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 378 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 379 380 // cache entry should be retrieved from cache 381 listener = submitJob(job, false); 382 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 383 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 384 385 // invalidate entry in cache 386 ICacheElement<String, CacheEntry> cacheEntry = cache.getCacheElement("test"); 387 CacheEntryAttributes attributes = (CacheEntryAttributes)cacheEntry.getElementAttributes(); 388 attributes.setExpirationTime(testStart - TimeUnit.DAYS.toMillis(1)); 389 cache.put("test", cacheEntry.getVal(), attributes); 390 391 // because cache entry is invalid - HEAD request shall be made 392 tileServer.verify(0, WireMock.headRequestedFor(WireMock.urlEqualTo("/test"))); // no head requests were made until now 393 listener = submitJob(job, false); 394 tileServer.verify(1, WireMock.headRequestedFor(WireMock.urlEqualTo("/test"))); // verify head requests were made 395 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); // verify no more get requests were made 396 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 397 assertTrue(listener.attributes.getExpirationTime() >= testStart + expires); 398 399 // cache entry should be retrieved from cache 400 listener = submitJob(job, false); // cache entry is expired, no need to force refetch 401 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 402 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 403 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), listener.data); 404 } 405 406 /** 407 * Check if server returns 304 - it will update cache attributes and not ask again for it 408 * @throws IOException 409 */ 410 @Test 411 public void testCheckUsing304() throws IOException { 412 ICacheAccess<String, CacheEntry> cache = getCache(); 413 long expires = TimeUnit.DAYS.toMillis(1); 414 long testStart = System.currentTimeMillis(); 415 cache.put("test", 416 new CacheEntry("cached dummy".getBytes(StandardCharsets.UTF_8)), 417 createEntryAttributes(-1 * expires, 200, testStart, "eTag") 418 ); 419 420 tileServer.stubFor( 421 WireMock.get(WireMock.urlEqualTo("/test")) 422 .willReturn(WireMock.status(304) 423 .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) 424 .withHeader("Last-Modified", Long.toString(testStart)) 425 .withHeader("ETag", "eTag") 426 ) 427 ); 428 429 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(tileServer.url("/test"), "test"); 430 Listener listener = submitJob(job, false); 431 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 432 assertArrayEquals("cached dummy".getBytes(StandardCharsets.UTF_8), listener.data); 433 assertTrue(testStart + expires <= listener.attributes.getExpirationTime()); 434 listener = submitJob(job, false); 435 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); // no more requests were made 436 } 437 438 private void createHeadGetStub(UrlPattern url, long expires, long lastModified, String eTag, String body) { 439 tileServer.stubFor( 440 WireMock.get(url) 441 .willReturn(WireMock.aResponse() 442 .withHeader("Expires", TestUtils.getHTTPDate(lastModified + expires)) 443 .withHeader("Last-Modified", Long.toString(lastModified)) 444 .withHeader("ETag", eTag) 445 .withBody(body) 446 ) 447 ); 448 tileServer.stubFor( 449 WireMock.head(url) 450 .willReturn(WireMock.aResponse() 451 .withHeader("Expires", TestUtils.getHTTPDate(lastModified + expires)) 452 .withHeader("Last-Modified", Long.toString(lastModified)) 453 .withHeader("ETag", eTag) 454 ) 455 ); 456 } 457 458 private CacheEntryAttributes createEntryAttributes(long maxAge, int responseCode, String eTag) { 459 long validTo = maxAge + System.currentTimeMillis(); 460 return createEntryAttributes(maxAge, responseCode, validTo, eTag); 461 } 462 463 private CacheEntryAttributes createEntryAttributes(long expirationTime, int responseCode, long lastModification, String eTag) { 464 CacheEntryAttributes entryAttributes = new CacheEntryAttributes(); 465 entryAttributes.setExpirationTime(lastModification + expirationTime); 466 entryAttributes.setResponseCode(responseCode); 467 entryAttributes.setLastModification(lastModification); 468 entryAttributes.setEtag(eTag); 469 return entryAttributes; 470 } 471 472 private static TestCachedTileLoaderJob getStatusLoaderJob(int responseCode) { 167 473 return new TestCachedTileLoaderJob("http://httpstat.us/" + responseCode, "key_" + responseCode); 168 474 } 169 475 170 private static ICacheAccess<String, CacheEntry> getCache() throws IOException{476 private static ICacheAccess<String, CacheEntry> getCache() { 171 477 return JCSCacheManager.getCache("test"); 172 478 } -
trunk/test/unit/org/openstreetmap/josm/data/imagery/TMSCachedTileLoaderJobTest.java
r13631 r13733 2 2 package org.openstreetmap.josm.data.imagery; 3 3 4 import static org.junit.Assert.assertArrayEquals; 4 5 import static org.junit.Assert.assertEquals; 5 6 import static org.junit.Assert.assertTrue; 6 7 8 import java.io.IOException; 9 import java.net.MalformedURLException; 10 import java.net.URL; 11 import java.nio.charset.StandardCharsets; 12 import java.util.concurrent.Executors; 13 import java.util.concurrent.ThreadPoolExecutor; 14 import java.util.concurrent.TimeUnit; 7 15 import java.util.regex.Matcher; 8 16 17 import org.apache.commons.jcs.access.behavior.ICacheAccess; 18 import org.junit.Before; 9 19 import org.junit.Rule; 10 20 import org.junit.Test; 21 import org.openstreetmap.gui.jmapviewer.Tile; 22 import org.openstreetmap.gui.jmapviewer.interfaces.TileLoaderListener; 23 import org.openstreetmap.gui.jmapviewer.tilesources.TMSTileSource; 24 import org.openstreetmap.josm.TestUtils; 25 import org.openstreetmap.josm.data.cache.BufferedImageCacheEntry; 26 import org.openstreetmap.josm.data.cache.CacheEntryAttributes; 27 import org.openstreetmap.josm.data.cache.JCSCacheManager; 11 28 import org.openstreetmap.josm.testutils.JOSMTestRules; 29 import org.openstreetmap.josm.tools.Logging; 12 30 import org.openstreetmap.josm.tools.Utils; 31 32 import com.github.tomakehurst.wiremock.client.WireMock; 33 import com.github.tomakehurst.wiremock.core.WireMockConfiguration; 34 import com.github.tomakehurst.wiremock.junit.WireMockRule; 13 35 14 36 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 24 46 @Rule 25 47 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 26 public JOSMTestRules test = new JOSMTestRules(); 48 public JOSMTestRules test = new JOSMTestRules().preferences(); 49 50 /** 51 * mocked tile server 52 */ 53 @Rule 54 public WireMockRule tileServer = new WireMockRule(WireMockConfiguration.options() 55 .dynamicPort()); 56 57 @Before 58 public void clearCache() throws Exception { 59 getCache().clear(); 60 } 61 62 private static ICacheAccess<String, BufferedImageCacheEntry> getCache() { 63 return JCSCacheManager.getCache("test"); 64 } 65 66 private static class TestCachedTileLoaderJob extends TMSCachedTileLoaderJob { 67 private String url; 68 private String key; 69 70 TestCachedTileLoaderJob(TileLoaderListener listener, Tile tile, String key) throws IOException { 71 this(listener, tile, key, (int) TimeUnit.DAYS.toSeconds(1)); 72 } 73 74 TestCachedTileLoaderJob(TileLoaderListener listener, Tile tile, String key, int minimumExpiry) throws IOException { 75 super(listener, tile, getCache(), new TileJobOptions(30000, 30000, null, minimumExpiry), 76 (ThreadPoolExecutor) Executors.newFixedThreadPool(1)); 77 78 this.url = tile.getUrl(); 79 this.key = key; 80 } 81 82 @Override 83 public URL getUrl() { 84 try { 85 return new URL(url); 86 } catch (MalformedURLException e) { 87 throw new RuntimeException(e); 88 } 89 } 90 91 @Override 92 protected BufferedImageCacheEntry createCacheEntry(byte[] content) { 93 return new BufferedImageCacheEntry(content); 94 } 95 96 public CacheEntryAttributes getAttributes() { 97 return attributes; 98 } 99 100 @Override 101 public boolean isObjectLoadable() { 102 // use implementation from grand parent, to avoid calling getImage on dummy data 103 if (cacheData == null) { 104 return false; 105 } 106 return cacheData.getContent().length > 0; } 107 } 108 109 private static class Listener implements TileLoaderListener { 110 private CacheEntryAttributes attributes; 111 private boolean ready; 112 private byte[] data; 113 114 115 @Override 116 public synchronized void tileLoadingFinished(Tile tile, boolean success) { 117 ready = true; 118 this.notifyAll(); 119 } 120 } 121 122 private static class MockTile extends Tile { 123 MockTile(String url) { 124 super(new MockTileSource(url), 0, 0, 0); 125 } 126 } 127 128 private static class MockTileSource extends TMSTileSource { 129 private final String url; 130 131 public MockTileSource(String url) { 132 super(new ImageryInfo("mock")); 133 this.url = url; 134 } 135 136 @Override 137 public String getTileUrl(int zoom, int tilex, int tiley) throws IOException { 138 return url; 139 } 140 } 27 141 28 142 /** … … 53 167 assertEquals(expected, Utils.strip(m.group(1))); 54 168 } 169 170 private TestCachedTileLoaderJob submitJob(MockTile tile, String key, boolean force) throws IOException { 171 return submitJob(tile, key, 0, force); 172 } 173 174 private TestCachedTileLoaderJob submitJob(MockTile tile, String key, int minimumExpiry, boolean force) throws IOException { 175 Listener listener = new Listener(); 176 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob(listener, tile, key, minimumExpiry); 177 job.submit(force); 178 synchronized (listener) { 179 while (!listener.ready) { 180 try { 181 listener.wait(); 182 } catch (InterruptedException e) { 183 // do nothing, wait 184 Logging.trace(e); 185 } 186 } 187 } 188 return job; 189 } 190 191 /** 192 * When tile server doesn't return any Expires/Cache-Control headers, expire should be at least MINIMUM_EXPIRES 193 * @throws IOException 194 */ 195 @Test 196 public void testNoCacheHeaders() throws IOException { 197 long testStart = System.currentTimeMillis(); 198 tileServer.stubFor( 199 WireMock.get(WireMock.urlEqualTo("/test")) 200 .willReturn(WireMock.aResponse() 201 .withBody("mock entry") 202 ) 203 ); 204 205 TestCachedTileLoaderJob job = submitJob(new MockTile(tileServer.url("/test")), "test", false); 206 assertExpirationAtLeast(testStart + TMSCachedTileLoaderJob.MINIMUM_EXPIRES.get(), job); 207 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); 208 job = submitJob(new MockTile(tileServer.url("/test")), "test", false); // submit another job for the same tile 209 // only one request to tile server should be made, second should come from cache 210 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 211 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); 212 } 213 214 /** 215 * When tile server doesn't return any Expires/Cache-Control headers, expire should be at least minimumExpires parameter 216 * @throws IOException 217 */ 218 @Test 219 public void testNoCacheHeadersMinimumExpires() throws IOException { 220 noCacheHeadersMinimumExpires((int) TimeUnit.MILLISECONDS.toSeconds(TMSCachedTileLoaderJob.MINIMUM_EXPIRES.get() * 2)); 221 } 222 223 /** 224 * When tile server doesn't return any Expires/Cache-Control headers, expire should be at least minimumExpires parameter, 225 * which is larger than MAXIMUM_EXPIRES 226 * @throws IOException 227 */ 228 229 @Test 230 public void testNoCacheHeadersMinimumExpiresLargerThanMaximum() throws IOException { 231 noCacheHeadersMinimumExpires((int) TimeUnit.MILLISECONDS.toSeconds(TMSCachedTileLoaderJob.MAXIMUM_EXPIRES.get() * 2)); 232 } 233 234 private void noCacheHeadersMinimumExpires(int minimumExpires) throws IOException { 235 long testStart = System.currentTimeMillis(); 236 tileServer.stubFor( 237 WireMock.get(WireMock.urlEqualTo("/test")) 238 .willReturn(WireMock.aResponse() 239 .withBody("mock entry") 240 ) 241 ); 242 TestCachedTileLoaderJob job = submitJob(new MockTile(tileServer.url("/test")), "test", minimumExpires, false); 243 assertExpirationAtLeast(testStart + minimumExpires, job); 244 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); 245 job = submitJob(new MockTile(tileServer.url("/test")), "test", false); // submit another job for the same tile 246 // only one request to tile server should be made, second should come from cache 247 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 248 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); 249 } 250 251 /** 252 * When tile server returns Expires header shorter than MINIMUM_EXPIRES, we should cache if for at least MINIMUM_EXPIRES 253 * @throws IOException 254 */ 255 @Test 256 public void testShortExpire() throws IOException { 257 long testStart = System.currentTimeMillis(); 258 long expires = TMSCachedTileLoaderJob.MINIMUM_EXPIRES.get() / 2; 259 tileServer.stubFor( 260 WireMock.get(WireMock.urlEqualTo("/test")) 261 .willReturn(WireMock.aResponse() 262 .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) 263 .withBody("mock entry") 264 ) 265 ); 266 TestCachedTileLoaderJob job = submitJob(new MockTile(tileServer.url("/test")), "test", false); 267 assertExpirationAtLeast(testStart + TMSCachedTileLoaderJob.MINIMUM_EXPIRES.get(), job); 268 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); 269 job = submitJob(new MockTile(tileServer.url("/test")), "test", false); // submit another job for the same tile 270 // only one request to tile server should be made, second should come from cache 271 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 272 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); 273 } 274 275 private void assertExpirationAtLeast(long duration, TestCachedTileLoaderJob job) { 276 assertTrue( 277 "Expiration time shorter by " + 278 -1 * (job.getAttributes().getExpirationTime() - duration) + 279 " than expected", 280 job.getAttributes().getExpirationTime() >= duration); 281 } 282 283 private void assertExpirationAtMost(long duration, TestCachedTileLoaderJob job) { 284 assertTrue( 285 "Expiration time longer by " + 286 (job.getAttributes().getExpirationTime() - duration) + 287 " than expected", 288 job.getAttributes().getExpirationTime() <= duration); 289 } 290 291 292 @Test 293 public void testLongExpire() throws IOException { 294 long testStart = System.currentTimeMillis(); 295 long expires = TMSCachedTileLoaderJob.MAXIMUM_EXPIRES.get() * 2; 296 tileServer.stubFor( 297 WireMock.get(WireMock.urlEqualTo("/test")) 298 .willReturn(WireMock.aResponse() 299 .withHeader("Expires", TestUtils.getHTTPDate(testStart + expires)) 300 .withBody("mock entry") 301 ) 302 ); 303 TestCachedTileLoaderJob job = submitJob(new MockTile(tileServer.url("/test")), "test", false); 304 // give 1 second margin 305 assertExpirationAtMost(testStart + TMSCachedTileLoaderJob.MAXIMUM_EXPIRES.get() + TimeUnit.SECONDS.toMillis(1), job); 306 307 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); 308 job = submitJob(new MockTile(tileServer.url("/test")), "test", false); // submit another job for the same tile 309 // only one request to tile server should be made, second should come from cache 310 tileServer.verify(1, WireMock.getRequestedFor(WireMock.urlEqualTo("/test"))); 311 assertArrayEquals("mock entry".getBytes(StandardCharsets.UTF_8), job.get().getContent()); 312 } 313 55 314 } -
trunk/test/unit/org/openstreetmap/josm/data/imagery/WMTSTileSourceTest.java
r12669 r13733 8 8 import java.io.IOException; 9 9 import java.net.MalformedURLException; 10 import java.nio.file.Files; 11 import java.nio.file.Paths; 10 12 import java.util.ArrayList; 11 import java.util.Collection; 12 13 import java.util.Arrays; 14 import java.util.List; 15 16 import org.junit.ClassRule; 13 17 import org.junit.Ignore; 14 import org.junit.Rule;15 18 import org.junit.Test; 16 19 import org.openstreetmap.gui.jmapviewer.tilesources.TemplatedTMSTileSource; … … 19 22 import org.openstreetmap.josm.data.Bounds; 20 23 import org.openstreetmap.josm.data.coor.LatLon; 24 import org.openstreetmap.josm.data.imagery.ImageryInfo.ImageryType; 25 import org.openstreetmap.josm.data.imagery.WMTSTileSource.WMTSGetCapabilitiesException; 21 26 import org.openstreetmap.josm.data.projection.Projections; 27 import org.openstreetmap.josm.spi.preferences.Config; 22 28 import org.openstreetmap.josm.testutils.JOSMTestRules; 29 30 import com.github.tomakehurst.wiremock.WireMockServer; 31 import com.github.tomakehurst.wiremock.client.WireMock; 23 32 24 33 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 28 37 */ 29 38 public class WMTSTileSourceTest { 39 40 /** 41 * Setup test. 42 */ 43 @ClassRule 44 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 45 public static JOSMTestRules test = new JOSMTestRules().preferences().platform(); 30 46 31 47 private ImageryInfo testImageryTMS = new ImageryInfo("test imagery", "http://localhost", "tms", null, null); … … 44 60 "wmts/bug13975-multiple-tile-matrices-for-one-layer-projection.xml"); 45 61 46 /**47 * Setup test.48 */49 @Rule50 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")51 public JOSMTestRules test = new JOSMTestRules();52 62 53 63 private static ImageryInfo getImagery(String path) { 54 64 try { 55 returnnew ImageryInfo(65 ImageryInfo ret = new ImageryInfo( 56 66 "test", 57 67 new File(path).toURI().toURL().toString() 58 68 ); 69 ret.setImageryType(ImageryType.WMTS); 70 return ret; 59 71 } catch (MalformedURLException e) { 60 72 e.printStackTrace(); … … 64 76 65 77 @Test 66 public void testPseudoMercator() throws IOException {78 public void testPseudoMercator() throws IOException, WMTSGetCapabilitiesException { 67 79 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); 68 80 WMTSTileSource testSource = new WMTSTileSource(testImageryPSEUDO_MERCATOR); … … 94 106 95 107 @Test 96 public void testWALLONIE() throws IOException {108 public void testWALLONIE() throws IOException, WMTSGetCapabilitiesException { 97 109 Main.setProjection(Projections.getProjectionByCode("EPSG:31370")); 98 110 WMTSTileSource testSource = new WMTSTileSource(testImageryWALLONIE); … … 114 126 @Test 115 127 @Ignore("disable this test, needs further working") // XXX 116 public void testWALLONIENoMatrixDimension() throws IOException {128 public void testWALLONIENoMatrixDimension() throws IOException, WMTSGetCapabilitiesException { 117 129 Main.setProjection(Projections.getProjectionByCode("EPSG:31370")); 118 130 WMTSTileSource testSource = new WMTSTileSource(getImagery("test/data/wmts/WMTSCapabilities-Wallonie-nomatrixdimension.xml")); … … 138 150 139 151 @Test 140 public void testWIEN() throws IOException {152 public void testWIEN() throws IOException, WMTSGetCapabilitiesException { 141 153 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); 142 154 WMTSTileSource testSource = new WMTSTileSource(testImageryWIEN); … … 180 192 181 193 @Test 182 public void testGeoportalTOPOPL() throws IOException {194 public void testGeoportalTOPOPL() throws IOException, WMTSGetCapabilitiesException { 183 195 Main.setProjection(Projections.getProjectionByCode("EPSG:4326")); 184 196 WMTSTileSource testSource = new WMTSTileSource(testImageryTOPO_PL); … … 202 214 203 215 @Test 204 public void testGeoportalORTOPL4326() throws IOException {216 public void testGeoportalORTOPL4326() throws IOException, WMTSGetCapabilitiesException { 205 217 Main.setProjection(Projections.getProjectionByCode("EPSG:4326")); 206 218 WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL); … … 211 223 212 224 @Test 213 public void testGeoportalORTOPL2180() throws IOException {225 public void testGeoportalORTOPL2180() throws IOException, WMTSGetCapabilitiesException { 214 226 Main.setProjection(Projections.getProjectionByCode("EPSG:2180")); 215 227 WMTSTileSource testSource = new WMTSTileSource(testImageryORTO_PL); … … 221 233 222 234 @Test 223 public void testTicket12168() throws IOException {235 public void testTicket12168() throws IOException, WMTSGetCapabilitiesException { 224 236 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); 225 237 WMTSTileSource testSource = new WMTSTileSource(testImagery12168); … … 231 243 232 244 @Test 233 @Ignore("disabled as this needs user action") // XXX234 245 public void testTwoTileSetsForOneProjection() throws Exception { 235 246 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); 236 WMTSTileSource testSource = new WMTSTileSource(testImageryOntario); 237 testSource.initProjection(Main.getProjection()); 238 verifyTile(new LatLon(45.4105023, -75.7153702), testSource, 303751, 375502, 12); 239 verifyTile(new LatLon(45.4601306, -75.7617187), testSource, 1186, 1466, 4); 240 } 241 242 @Test 243 @Ignore("disabled as this needs user action") // XXX 247 ImageryInfo ontario = getImagery(TestUtils.getTestDataRoot() + "wmts/WMTSCapabilities-Ontario.xml"); 248 ontario.setDefaultLayers(Arrays.asList(new DefaultLayer[] { 249 new DefaultLayer(ImageryType.WMTS, "Basemap_Imagery_2014", null, "default028mm") 250 })); 251 WMTSTileSource testSource = new WMTSTileSource(ontario); 252 testSource.initProjection(Main.getProjection()); 253 assertEquals( 254 "http://maps.ottawa.ca/arcgis/rest/services/Basemap_Imagery_2014/MapServer/WMTS/tile/1.0.0/Basemap_Imagery_2014/default/default028mm/4/2932/2371.jpg", 255 testSource.getTileUrl(4, 2371, 2932)); 256 verifyTile(new LatLon(45.4601306, -75.7617187), testSource, 2372, 2932, 4); 257 verifyTile(new LatLon(45.4602510, -75.7617187), testSource, 607232, 750591, 12); 258 } 259 260 @Test 261 public void testTwoTileSetsForOneProjectionSecondLayer() throws Exception { 262 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); 263 ImageryInfo ontario = getImagery(TestUtils.getTestDataRoot() + "wmts/WMTSCapabilities-Ontario.xml"); 264 ontario.setDefaultLayers(Arrays.asList(new DefaultLayer[] { 265 new DefaultLayer(ImageryType.WMTS, "Basemap_Imagery_2014", null, "GoogleMapsCompatible") 266 })); 267 WMTSTileSource testSource = new WMTSTileSource(ontario); 268 testSource.initProjection(Main.getProjection()); 269 assertEquals( 270 "http://maps.ottawa.ca/arcgis/rest/services/Basemap_Imagery_2014/MapServer/WMTS/tile/1.0.0/Basemap_Imagery_2014/default/GoogleMapsCompatible/4/2932/2371.jpg", 271 testSource.getTileUrl(4, 2371, 2932)); 272 verifyMercatorTile(testSource, 74, 91, 8); 273 verifyMercatorTile(testSource, 37952, 46912, 17); 274 } 275 276 @Test 244 277 public void testManyLayersScrollbars() throws Exception { 245 278 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); … … 271 304 Main.setProjection(Projections.getProjectionByCode("EPSG:3857")); 272 305 ImageryInfo copy = new ImageryInfo(testMultipleTileMatrixForLayer); 273 Collection<DefaultLayer> defaultLayers = new ArrayList<>(1);274 defaultLayers.add(new WMTSDefaultLayer("Mashhad_BaseMap_1", "default028mm"));306 List<DefaultLayer> defaultLayers = new ArrayList<>(1); 307 defaultLayers.add(new DefaultLayer(ImageryType.WMTS, "Mashhad_BaseMap_1", null, "default028mm")); 275 308 copy.setDefaultLayers(defaultLayers); 276 309 WMTSTileSource testSource = new WMTSTileSource(copy); … … 286 319 * Test WMTS dimension. 287 320 * @throws IOException if any I/O error occurs 321 * @throws WMTSGetCapabilitiesException 288 322 */ 289 323 @Test 290 public void testDimension() throws IOException {324 public void testDimension() throws IOException, WMTSGetCapabilitiesException { 291 325 Main.setProjection(Projections.getProjectionByCode("EPSG:21781")); 292 326 ImageryInfo info = new ImageryInfo(testImageryGeoAdminCh); 293 Collection<DefaultLayer> defaultLayers = new ArrayList<>(1);294 defaultLayers.add(new WMTSDefaultLayer("ch.are.agglomerationen_isolierte_staedte", "21781_26"));327 List<DefaultLayer> defaultLayers = new ArrayList<>(1); 328 defaultLayers.add(new DefaultLayer(ImageryType.WMTS, "ch.are.agglomerationen_isolierte_staedte", null, "21781_26")); 295 329 info.setDefaultLayers(defaultLayers); 296 330 WMTSTileSource testSource = new WMTSTileSource(info); … … 300 334 testSource.getTileUrl(1, 2, 3) 301 335 ); 336 } 337 338 @Test 339 public void testDefaultLayer() throws Exception { 340 // https://gibs.earthdata.nasa.gov/wmts/epsg3857/best/1.0.0/WMTSCapabilities.xml 341 WireMockServer getCapabilitiesMock = TestUtils.getWireMockServer(); 342 String getCapabilitiesBody = new String(Files.readAllBytes(Paths.get(TestUtils.getTestDataRoot() + "wmts/getCapabilities-lots-of-layers.xml")), "UTF-8"); 343 // do not use withFileBody as it needs different directory layout :( 344 getCapabilitiesMock.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody(getCapabilitiesBody))); 345 getCapabilitiesMock.start(); 346 347 WireMockServer mapsMock = TestUtils.getWireMockServer(); 348 mapsMock.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody( 349 "<?xml version='1.0' encoding='UTF-8'?>\n" + 350 "<imagery xmlns=\"http://josm.openstreetmap.de/maps-1.0\">\n" + 351 "<entry>\n" + 352 "<name>Landsat</name>\n" + 353 "<id>landsat</id>\n" + 354 "<type>wmts</type>\n" + 355 "<url><![CDATA[" + getCapabilitiesMock.url("/getcapabilities.xml") + "]]></url>\n" + 356 "<defaultLayers>" + 357 "<layer name=\"GEOGRAPHICALGRIDSYSTEMS.MAPS\" />" + 358 "</defaultLayers>" + 359 "</entry>\n" + 360 "</imagery>" 361 ))); 362 mapsMock.start(); 363 Config.getPref().put("josm.url", mapsMock.url("/")); 364 365 ImageryLayerInfo.instance.loadDefaults(true, null, false); 366 367 assertEquals(1, ImageryLayerInfo.instance.getDefaultLayers().size()); 368 ImageryInfo wmtsImageryInfo = ImageryLayerInfo.instance.getDefaultLayers().get(0); 369 assertEquals(1, wmtsImageryInfo.getDefaultLayers().size()); 370 assertEquals("GEOGRAPHICALGRIDSYSTEMS.MAPS", wmtsImageryInfo.getDefaultLayers().get(0).getLayerName()); 371 WMTSTileSource tileSource = new WMTSTileSource(wmtsImageryInfo); 372 tileSource.initProjection(Projections.getProjectionByCode("EPSG:3857")); 373 assertEquals("http://wxs.ign.fr/61fs25ymczag0c67naqvvmap/geoportail/wmts?SERVICE=WMTS&REQUEST=GetTile&VERSION=1.0.0&" 374 + "LAYER=GEOGRAPHICALGRIDSYSTEMS.MAPS" 375 + "&STYLE=normal&FORMAT=image/jpeg&tileMatrixSet=PM&tileMatrix=1&tileRow=1&tileCol=1", tileSource.getTileUrl(1, 1, 1)); 376 302 377 } 303 378 -
trunk/test/unit/org/openstreetmap/josm/gui/layer/AbstractTileSourceLayerTest.java
r12636 r13733 120 120 return new TileLoaderFactory() { 121 121 @Override 122 public TileLoader makeTileLoader(TileLoaderListener listener, Map<String, String> headers) { 122 public TileLoader makeTileLoader(TileLoaderListener listener, Map<String, String> headers, 123 long minimumExpiryTime) { 123 124 return null; 124 125 } -
trunk/test/unit/org/openstreetmap/josm/gui/layer/WMTSLayerTest.java
r13710 r13733 22 22 @Rule 23 23 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 24 public JOSMTestRules test = new JOSMTestRules(). timeout(20000);24 public JOSMTestRules test = new JOSMTestRules().preferences().timeout(20000); 25 25 26 26 /** -
trunk/test/unit/org/openstreetmap/josm/io/imagery/WMSImageryTest.java
r13699 r13733 6 6 7 7 import java.io.IOException; 8 import java.io.InputStream; 8 import java.nio.file.Files; 9 import java.nio.file.Path; 10 import java.nio.file.Paths; 11 import java.util.List; 9 12 10 13 import org.junit.Rule; … … 13 16 import org.openstreetmap.josm.io.imagery.WMSImagery.WMSGetCapabilitiesException; 14 17 import org.openstreetmap.josm.testutils.JOSMTestRules; 18 19 import com.github.tomakehurst.wiremock.WireMockServer; 20 import com.github.tomakehurst.wiremock.client.WireMock; 15 21 16 22 import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; … … 26 32 @Rule 27 33 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD") 28 public JOSMTestRules test = new JOSMTestRules() ;34 public JOSMTestRules test = new JOSMTestRules().platform().projection(); 29 35 30 36 /** … … 49 55 @Test 50 56 public void testTicket15730() throws IOException, WMSGetCapabilitiesException { 51 try (InputStream is = TestUtils.getRegressionDataStream(15730, "capabilities.xml")) { 52 WMSImagery wms = new WMSImagery(); 53 wms.parseCapabilities(null, is); 54 assertEquals(1, wms.getLayers().size()); 55 assertTrue(wms.getLayers().get(0).abstr.startsWith("South Carolina NAIP Imagery 2017 Resolution: 100CM ")); 56 } 57 WireMockServer wm = TestUtils.getWireMockServer(15730); 58 wm.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBodyFile("capabilities.xml"))); 59 wm.start(); 60 WMSImagery wms = new WMSImagery(wm.url("capabilities.xml")); 61 assertEquals(1, wms.getLayers().size()); 62 assertTrue(wms.getLayers().get(0).getAbstract().startsWith("South Carolina NAIP Imagery 2017 Resolution: 100CM ")); 63 wm.shutdown(); 64 } 65 66 @Test 67 public void testNestedLayers() throws Exception { 68 WireMockServer getCapabilitiesMock = TestUtils.getWireMockServer(); 69 String getCapabilitiesBody = new String(Files.readAllBytes(Paths.get(TestUtils.getTestDataRoot() + "wms/mapa-um-warszawa-pl.xml")), "UTF-8"); 70 getCapabilitiesMock.stubFor(WireMock.get(WireMock.anyUrl()).willReturn(WireMock.aResponse().withBody(getCapabilitiesBody))); 71 getCapabilitiesMock.start(); 72 WMSImagery wmsi = new WMSImagery(getCapabilitiesMock.url("/serwis")); 73 assertEquals(1, wmsi.getLayers().size()); 74 assertEquals("Server WMS m.st. Warszawy", wmsi.getLayers().get(0).toString()); 75 assertEquals(202, wmsi.getLayers().get(0).getChildren().size()); 57 76 } 58 77 … … 64 83 @Test 65 84 public void testTicket16248() throws IOException, WMSGetCapabilitiesException { 66 try (InputStream is = TestUtils.getRegressionDataStream(16248, "capabilities.xml")) { 67 WMSImagery wms = new WMSImagery(); 68 wms.parseCapabilities(null, is); 69 assertEquals("http://wms.hgis.cartomatic.pl/topo/3857/m25k", wms.getServiceUrl().toExternalForm()); 70 } 85 Path capabilitiesPath = Paths.get(TestUtils.getRegressionDataFile(16248, "capabilities.xml")); 86 WireMockServer getCapabilitiesMock = TestUtils.getWireMockServer(); 87 getCapabilitiesMock.stubFor( 88 WireMock.get(WireMock.anyUrl()) 89 .willReturn(WireMock.aResponse().withBody(Files.readAllBytes(capabilitiesPath)))); 90 getCapabilitiesMock.start(); 91 WMSImagery wms = new WMSImagery(getCapabilitiesMock.url("any")); 92 assertEquals("http://wms.hgis.cartomatic.pl/topo/3857/m25k", wms.buildRootUrl()); 93 assertEquals("wms.hgis.cartomatic.pl", wms.getLayers().get(0).getName()); 94 assertEquals("http://wms.hgis.cartomatic.pl/topo/3857/m25kFORMAT=image/png&TRANSPARENT=TRUE&VERSION=1.1.1&SERVICE=WMS&REQUEST=GetMap&" 95 + "LAYERS=wms.hgis.cartomatic.pl&STYLES=&SRS={proj}&WIDTH={width}&HEIGHT={height}&BBOX={bbox}", 96 wms.buildGetMapUrl(wms.getLayers(), (List<String>)null, true)); 71 97 } 72 98 } 99
Note:
See TracChangeset
for help on using the changeset viewer.