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

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

fix #15830 - Support (and autodetect) WMS 1.3.0

  • Property svn:eol-style set to native
File size: 5.6 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.cache;
3
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.assertFalse;
6
7import java.io.IOException;
8import java.net.MalformedURLException;
9import java.net.URL;
10import java.nio.charset.StandardCharsets;
11
12import org.apache.commons.jcs.access.behavior.ICacheAccess;
13import org.junit.Before;
14import org.junit.Rule;
15import org.junit.Test;
16import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult;
17import org.openstreetmap.josm.testutils.JOSMTestRules;
18import org.openstreetmap.josm.tools.Logging;
19
20import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
21
22/**
23 * Unit tests for class {@link JCSCachedTileLoaderJob}.
24 */
25public class JCSCachedTileLoaderJobTest {
26
27 private static class TestCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, CacheEntry> {
28 private String url;
29 private String key;
30
31 TestCachedTileLoaderJob(String url, String key) throws IOException {
32 super(getCache(), 30000, 30000, null);
33
34 this.url = url;
35 this.key = key;
36 }
37
38 @Override
39 public String getCacheKey() {
40 return key;
41 }
42
43 @Override
44 public URL getUrl() {
45 try {
46 return new URL(url);
47 } catch (MalformedURLException e) {
48 throw new RuntimeException(e);
49 }
50 }
51
52 @Override
53 protected CacheEntry createCacheEntry(byte[] content) {
54 return new CacheEntry("dummy".getBytes(StandardCharsets.UTF_8));
55 }
56 }
57
58 private static class Listener implements ICachedLoaderListener {
59 private CacheEntryAttributes attributes;
60 private boolean ready;
61 private LoadResult result;
62
63 @Override
64 public synchronized void loadingFinished(CacheEntry data, CacheEntryAttributes attributes, LoadResult result) {
65 this.attributes = attributes;
66 this.ready = true;
67 this.result = result;
68 this.notifyAll();
69 }
70 }
71
72 /**
73 * Setup test.
74 */
75 @Rule
76 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
77 public JOSMTestRules test = new JOSMTestRules().preferences();
78
79 /**
80 * Always clear cache before tests
81 * @throws Exception when clearing fails
82 */
83 @Before
84 public void clearCache() throws Exception {
85 getCache().clear();
86 }
87
88 /**
89 * Test status codes
90 * @throws InterruptedException in case of thread interruption
91 * @throws IOException in case of I/O error
92 */
93 @Test
94 public void testStatusCodes() throws IOException, InterruptedException {
95 doTestStatusCode(200);
96 // can't test for 3xx, as httpstat.us redirects finally to 200 page
97 doTestStatusCode(401);
98 doTestStatusCode(402);
99 doTestStatusCode(403);
100 doTestStatusCode(404);
101 doTestStatusCode(405);
102 doTestStatusCode(500);
103 doTestStatusCode(501);
104 doTestStatusCode(502);
105 }
106
107 /**
108 * Test unknown host
109 * @throws IOException in case of I/O error
110 */
111 @Test
112 public void testUnknownHost() throws IOException {
113 String key = "key_unknown_host";
114 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 }
127 assertEquals(LoadResult.FAILURE, listener.result); // because response will be cached, and that is checked below
128 assertEquals("java.net.UnknownHostException: unkownhost.unkownhost", listener.attributes.getErrorMessage());
129
130 ICacheAccess<String, CacheEntry> cache = getCache();
131 CacheEntry e = new CacheEntry(new byte[]{0, 1, 2, 3});
132 CacheEntryAttributes attributes = new CacheEntryAttributes();
133 attributes.setExpirationTime(2);
134 cache.put(key, e, attributes);
135
136 job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown", key);
137 listener = new Listener();
138 job.submit(listener, true);
139 synchronized (listener) {
140 while (!listener.ready) {
141 try {
142 listener.wait();
143 } catch (InterruptedException e1) {
144 // do nothing, wait
145 Logging.trace(e1);
146 }
147 }
148 }
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 {
167 return new TestCachedTileLoaderJob("http://httpstat.us/" + responseCode, "key_" + responseCode);
168 }
169
170 private static ICacheAccess<String, CacheEntry> getCache() throws IOException {
171 return JCSCacheManager.getCache("test");
172 }
173}
Note: See TracBrowser for help on using the repository browser.