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 |
|
---|
10 | import org.apache.commons.jcs.access.behavior.ICacheAccess;
|
---|
11 | import org.junit.BeforeClass;
|
---|
12 | import org.junit.Test;
|
---|
13 | import org.openstreetmap.josm.JOSMFixture;
|
---|
14 |
|
---|
15 | public class JCSCachedTileLoaderJobTest {
|
---|
16 | private static class TestCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, CacheEntry> {
|
---|
17 | private String url;
|
---|
18 |
|
---|
19 | public TestCachedTileLoaderJob(String url) throws IOException {
|
---|
20 | super(getCache(), 30000, 30000, null);
|
---|
21 | this.url = url;
|
---|
22 | }
|
---|
23 |
|
---|
24 | private static ICacheAccess<String, CacheEntry> getCache() throws IOException {
|
---|
25 | return JCSCacheManager.getCache("test");
|
---|
26 | }
|
---|
27 |
|
---|
28 | @Override
|
---|
29 | public String getCacheKey() {
|
---|
30 | return "cachekey";
|
---|
31 | }
|
---|
32 |
|
---|
33 | @Override
|
---|
34 | public URL getUrl() {
|
---|
35 | try {
|
---|
36 | return new URL(url);
|
---|
37 | } catch (MalformedURLException e) {
|
---|
38 | throw new RuntimeException(e);
|
---|
39 | }
|
---|
40 | }
|
---|
41 |
|
---|
42 | @Override
|
---|
43 | protected CacheEntry createCacheEntry(byte[] content) {
|
---|
44 | return new CacheEntry("dummy".getBytes());
|
---|
45 | }
|
---|
46 |
|
---|
47 | }
|
---|
48 |
|
---|
49 | private static class Listener implements ICachedLoaderListener {
|
---|
50 | private CacheEntry data;
|
---|
51 | private CacheEntryAttributes attributes;
|
---|
52 | private LoadResult result;
|
---|
53 | private boolean ready;
|
---|
54 |
|
---|
55 | @Override
|
---|
56 | public synchronized void loadingFinished(CacheEntry data, CacheEntryAttributes attributes, LoadResult result) {
|
---|
57 | this.data = data;
|
---|
58 | this.attributes = attributes;
|
---|
59 | this.result = result;
|
---|
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 | testStatusCode(200);
|
---|
76 | // can't test for 3xx, as httpstat.us redirects finally to 200 page
|
---|
77 | testStatusCode(401);
|
---|
78 | testStatusCode(402);
|
---|
79 | testStatusCode(403);
|
---|
80 | testStatusCode(404);
|
---|
81 | testStatusCode(405);
|
---|
82 | testStatusCode(500);
|
---|
83 | testStatusCode(501);
|
---|
84 | testStatusCode(502);
|
---|
85 | }
|
---|
86 |
|
---|
87 | @Test
|
---|
88 | public void testUnkownHost() 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 | public void testStatusCode(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 |
|
---|
113 | private static TestCachedTileLoaderJob getStatusLoaderJob(int responseCode) throws IOException {
|
---|
114 | return new TestCachedTileLoaderJob("http://httpstat.us/" + responseCode);
|
---|
115 | }
|
---|
116 |
|
---|
117 | }
|
---|
118 |
|
---|