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

Last change on this file since 9130 was 8857, checked in by Don-vip, 9 years ago

improve/cleanup unit tests

  • Property svn:eol-style set to native
File size: 3.4 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.cache;
3
4import static org.junit.Assert.assertEquals;
5
6import java.io.IOException;
7import java.net.MalformedURLException;
8import java.net.URL;
9
10import org.apache.commons.jcs.access.behavior.ICacheAccess;
11import org.junit.BeforeClass;
12import org.junit.Test;
13import org.openstreetmap.josm.JOSMFixture;
14
15/**
16 * Unit tests for class {@link JCSCachedTileLoaderJob}.
17 */
18public class JCSCachedTileLoaderJobTest {
19
20 private static class TestCachedTileLoaderJob extends JCSCachedTileLoaderJob<String, CacheEntry> {
21 private String url;
22
23 TestCachedTileLoaderJob(String url) throws IOException {
24 super(getCache(), 30000, 30000, null);
25 this.url = url;
26 }
27
28 private static ICacheAccess<String, CacheEntry> getCache() throws IOException {
29 return JCSCacheManager.getCache("test");
30 }
31
32 @Override
33 public String getCacheKey() {
34 return "cachekey";
35 }
36
37 @Override
38 public URL getUrl() {
39 try {
40 return new URL(url);
41 } catch (MalformedURLException e) {
42 throw new RuntimeException(e);
43 }
44 }
45
46 @Override
47 protected CacheEntry createCacheEntry(byte[] content) {
48 return new CacheEntry("dummy".getBytes());
49 }
50 }
51
52 private static class Listener implements ICachedLoaderListener {
53 private CacheEntryAttributes attributes;
54 private boolean ready;
55
56 @Override
57 public synchronized void loadingFinished(CacheEntry data, CacheEntryAttributes attributes, LoadResult result) {
58 this.attributes = attributes;
59 this.ready = true;
60 this.notify();
61 }
62 }
63
64 /**
65 * Setup test.
66 */
67 @BeforeClass
68 public static void setUp() {
69 JOSMFixture.createUnitTestFixture().init();
70 }
71
72 @Test
73 public void testStatusCodes() throws Exception {
74 testStatusCode(200);
75 // can't test for 3xx, as httpstat.us redirects finally to 200 page
76 testStatusCode(401);
77 testStatusCode(402);
78 testStatusCode(403);
79 testStatusCode(404);
80 testStatusCode(405);
81 testStatusCode(500);
82 testStatusCode(501);
83 testStatusCode(502);
84 }
85
86 @Test
87 public void testUnkownHost() throws Exception {
88 TestCachedTileLoaderJob job = new TestCachedTileLoaderJob("http://unkownhost.unkownhost/unkown");
89 Listener listener = new Listener();
90 job.submit(listener, true);
91 synchronized (listener) {
92 if (!listener.ready) {
93 listener.wait();
94 }
95 }
96 assertEquals("java.net.UnknownHostException: unkownhost.unkownhost", listener.attributes.getErrorMessage());
97 }
98
99 public void testStatusCode(int responseCode) throws Exception {
100 TestCachedTileLoaderJob job = getStatusLoaderJob(responseCode);
101 Listener listener = new Listener();
102 job.submit(listener, true);
103 synchronized (listener) {
104 if (!listener.ready) {
105 listener.wait();
106 }
107 }
108 assertEquals(responseCode, listener.attributes.getResponseCode());
109 }
110
111 private static TestCachedTileLoaderJob getStatusLoaderJob(int responseCode) throws IOException {
112 return new TestCachedTileLoaderJob("http://httpstat.us/" + responseCode);
113 }
114}
Note: See TracBrowser for help on using the repository browser.