source: josm/trunk/test/unit/org/openstreetmap/josm/data/cache/JCSCachedTileLoaderJobTest.java@ 10219

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

findbugs - reliance on default encoding

  • Property svn:eol-style set to native
File size: 3.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.cache;
3
4import static org.junit.Assert.assertEquals;
5
6import java.io.IOException;
7import java.net.MalformedURLException;
8import java.net.URL;
9import java.nio.charset.StandardCharsets;
10
11import org.apache.commons.jcs.access.behavior.ICacheAccess;
12import org.junit.BeforeClass;
13import org.junit.Test;
14import org.openstreetmap.josm.JOSMFixture;
15
16/**
17 * Unit tests for class {@link JCSCachedTileLoaderJob}.
18 */
19public class JCSCachedTileLoaderJobTest {
20
21 private static class TestCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, CacheEntry> {
22 private String url;
23
24 TestCachedTileLoaderJob(String url) throws IOException {
25 super(getCache(), 30000, 30000, null);
26 this.url = url;
27 }
28
29 private static ICacheAccess<String, CacheEntry> getCache() throws IOException {
30 return JCSCacheManager.getCache("test");
31 }
32
33 @Override
34 public String getCacheKey() {
35 return "cachekey";
36 }
37
38 @Override
39 public URL getUrl() {
40 try {
41 return new URL(url);
42 } catch (MalformedURLException e) {
43 throw new RuntimeException(e);
44 }
45 }
46
47 @Override
48 protected CacheEntry createCacheEntry(byte[] content) {
49 return new CacheEntry("dummy".getBytes(StandardCharsets.UTF_8));
50 }
51 }
52
53 private static class Listener implements ICachedLoaderListener {
54 private CacheEntryAttributes attributes;
55 private boolean ready;
56
57 @Override
58 public synchronized void loadingFinished(CacheEntry data, CacheEntryAttributes attributes, LoadResult result) {
59 this.attributes = attributes;
60 this.ready = true;
61 this.notify();
62 }
63 }
64
65 /**
66 * Setup test.
67 */
68 @BeforeClass
69 public static void setUp() {
70 JOSMFixture.createUnitTestFixture().init();
71 }
72
73 @Test
74 public void testStatusCodes() throws Exception {
75 doTestStatusCode(200);
76 // can't test for 3xx, as httpstat.us redirects finally to 200 page
77 doTestStatusCode(401);
78 doTestStatusCode(402);
79 doTestStatusCode(403);
80 doTestStatusCode(404);
81 doTestStatusCode(405);
82 doTestStatusCode(500);
83 doTestStatusCode(501);
84 doTestStatusCode(502);
85 }
86
87 @Test
88 public void testUnknownHost() throws Exception {
89 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown");
90 Listener listener = new Listener();
91 job.submit(listener, true);
92 synchronized (listener) {
93 if (!listener.ready) {
94 listener.wait();
95 }
96 }
97 assertEquals("java.net.UnknownHostException: unkownhost.unkownhost", listener.attributes.getErrorMessage());
98 }
99
100 private void doTestStatusCode(int responseCode) throws Exception {
101 TestCachedTileLoaderJob job = getStatusLoaderJob(responseCode);
102 Listener listener = new Listener();
103 job.submit(listener, true);
104 synchronized (listener) {
105 if (!listener.ready) {
106 listener.wait();
107 }
108 }
109 assertEquals(responseCode, listener.attributes.getResponseCode());
110 }
111
112 private static TestCachedTileLoaderJob getStatusLoaderJob(int responseCode) throws IOException {
113 return new TestCachedTileLoaderJob("http://httpstat.us/" + responseCode);
114 }
115}
Note: See TracBrowser for help on using the repository browser.