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

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

checkstyle, javadoc

  • 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.BeforeClass;
14import org.junit.Test;
15import org.openstreetmap.josm.JOSMFixture;
16import org.openstreetmap.josm.data.cache.ICachedLoaderListener.LoadResult;
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 @BeforeClass
74 public static void setUp() {
75 JOSMFixture.createUnitTestFixture().init();
76 }
77
78 /**
79 * Test status codes
80 * @throws InterruptedException in case of thread interruption
81 * @throws IOException in case of I/O error
82 */
83 @Test
84 public void testStatusCodes() throws IOException, InterruptedException {
85 doTestStatusCode(200);
86 // can't test for 3xx, as httpstat.us redirects finally to 200 page
87 doTestStatusCode(401);
88 doTestStatusCode(402);
89 doTestStatusCode(403);
90 doTestStatusCode(404);
91 doTestStatusCode(405);
92 doTestStatusCode(500);
93 doTestStatusCode(501);
94 doTestStatusCode(502);
95 }
96
97 /**
98 * Test unknown host
99 * @throws InterruptedException in case of thread interruption
100 * @throws IOException in case of I/O error
101 */
102 @Test
103 @SuppressFBWarnings(value = "WA_NOT_IN_LOOP")
104 public void testUnknownHost() throws IOException, InterruptedException {
105 String key = "key_unknown_host";
106 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown", key);
107 Listener listener = new Listener();
108 job.submit(listener, true);
109 synchronized (listener) {
110 if (!listener.ready) {
111 listener.wait();
112 }
113 }
114 assertEquals("java.net.UnknownHostException: unkownhost.unkownhost", listener.attributes.getErrorMessage());
115 assertEquals(LoadResult.FAILURE, listener.result); // because response will be cached, and that is checked below
116
117 ICacheAccess<String, CacheEntry> cache = getCache();
118 CacheEntry e = new CacheEntry(new byte[]{0, 1, 2, 3});
119 CacheEntryAttributes attributes = new CacheEntryAttributes();
120 attributes.setExpirationTime(2);
121 cache.put(key, e, attributes);
122
123 job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown", key);
124 listener = new Listener();
125 job.submit(listener, true);
126 synchronized (listener) {
127 if (!listener.ready) {
128 listener.wait();
129 }
130 }
131 assertEquals(LoadResult.SUCCESS, listener.result);
132 assertFalse(job.isCacheElementValid());
133 }
134
135 @SuppressFBWarnings(value = "WA_NOT_IN_LOOP")
136 private void doTestStatusCode(int responseCode) throws IOException, InterruptedException {
137 TestCachedTileLoaderJob job = getStatusLoaderJob(responseCode);
138 Listener listener = new Listener();
139 job.submit(listener, true);
140 synchronized (listener) {
141 if (!listener.ready) {
142 listener.wait();
143 }
144 }
145 assertEquals(responseCode, listener.attributes.getResponseCode());
146 }
147
148 private static TestCachedTileLoaderJob getStatusLoaderJob(int responseCode) throws IOException {
149 return new TestCachedTileLoaderJob("http://httpstat.us/" + responseCode, "key_" + responseCode);
150 }
151
152 private static ICacheAccess<String, CacheEntry> getCache() throws IOException {
153 return JCSCacheManager.getCache("test");
154 }
155}
Note: See TracBrowser for help on using the repository browser.