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