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

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

convert more unit tests to JOSMTestRules

  • Property svn:eol-style set to native
File size: 5.1 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.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult;
16import org.openstreetmap.josm.testutils.JOSMTestRules;
17
18import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
19
20/**
21 * Unit tests for class {@link JCSCachedTileLoaderJob}.
22 */
23public class JCSCachedTileLoaderJobTest {
24
25 private static class TestCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, CacheEntry> {
26 private String url;
27 private String key;
28
29 TestCachedTileLoaderJob(String url, String key) throws IOException {
30 super(getCache(), 30000, 30000, null);
31
32 this.url = url;
33 this.key = key;
34 }
35
36 @Override
37 public String getCacheKey() {
38 return key;
39 }
40
41 @Override
42 public URL getUrl() {
43 try {
44 return new URL(url);
45 } catch (MalformedURLException e) {
46 throw new RuntimeException(e);
47 }
48 }
49
50 @Override
51 protected CacheEntry createCacheEntry(byte[] content) {
52 return new CacheEntry("dummy".getBytes(StandardCharsets.UTF_8));
53 }
54 }
55
56 private static class Listener implements ICachedLoaderListener {
57 private CacheEntryAttributes attributes;
58 private boolean ready;
59 private LoadResult result;
60
61 @Override
62 public synchronized void loadingFinished(CacheEntry data, CacheEntryAttributes attributes, LoadResult result) {
63 this.attributes = attributes;
64 this.ready = true;
65 this.result = result;
66 this.notifyAll();
67 }
68 }
69
70 /**
71 * Setup test.
72 */
73 @Rule
74 @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
75 public JOSMTestRules test = new JOSMTestRules().platform();
76
77 /**
78 * Test status codes
79 * @throws InterruptedException in case of thread interruption
80 * @throws IOException in case of I/O error
81 */
82 @Test
83 public void testStatusCodes() throws IOException, InterruptedException {
84 doTestStatusCode(200);
85 // can't test for 3xx, as httpstat.us redirects finally to 200 page
86 doTestStatusCode(401);
87 doTestStatusCode(402);
88 doTestStatusCode(403);
89 doTestStatusCode(404);
90 doTestStatusCode(405);
91 doTestStatusCode(500);
92 doTestStatusCode(501);
93 doTestStatusCode(502);
94 }
95
96 /**
97 * Test unknown host
98 * @throws InterruptedException in case of thread interruption
99 * @throws IOException in case of I/O error
100 */
101 @Test
102 @SuppressFBWarnings(value = "WA_NOT_IN_LOOP")
103 public void testUnknownHost() throws IOException, InterruptedException {
104 String key = "key_unknown_host";
105 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown", key);
106 Listener listener = new Listener();
107 job.submit(listener, true);
108 synchronized (listener) {
109 if (!listener.ready) {
110 listener.wait();
111 }
112 }
113 assertEquals("java.net.UnknownHostException: unkownhost.unkownhost", listener.attributes.getErrorMessage());
114 assertEquals(LoadResult.FAILURE, listener.result); // because response will be cached, and that is checked below
115
116 ICacheAccess<String, CacheEntry> cache = getCache();
117 CacheEntry e = new CacheEntry(new byte[]{0, 1, 2, 3});
118 CacheEntryAttributes attributes = new CacheEntryAttributes();
119 attributes.setExpirationTime(2);
120 cache.put(key, e, attributes);
121
122 job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown", key);
123 listener = new Listener();
124 job.submit(listener, true);
125 synchronized (listener) {
126 if (!listener.ready) {
127 listener.wait();
128 }
129 }
130 assertEquals(LoadResult.SUCCESS, listener.result);
131 assertFalse(job.isCacheElementValid());
132 }
133
134 @SuppressFBWarnings(value = "WA_NOT_IN_LOOP")
135 private void doTestStatusCode(int responseCode) throws IOException, InterruptedException {
136 TestCachedTileLoaderJob job = getStatusLoaderJob(responseCode);
137 Listener listener = new Listener();
138 job.submit(listener, true);
139 synchronized (listener) {
140 if (!listener.ready) {
141 listener.wait();
142 }
143 }
144 assertEquals(responseCode, listener.attributes.getResponseCode());
145 }
146
147 private static TestCachedTileLoaderJob getStatusLoaderJob(int responseCode) throws IOException {
148 return new TestCachedTileLoaderJob("http://httpstat.us/" + responseCode, "key_" + responseCode);
149 }
150
151 private static ICacheAccess<String, CacheEntry> getCache() throws IOException {
152 return JCSCacheManager.getCache("test");
153 }
154}
Note: See TracBrowser for help on using the repository browser.