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.File;
|
---|
7 | import java.io.FileOutputStream;
|
---|
8 | import java.io.IOException;
|
---|
9 | import java.util.logging.Logger;
|
---|
10 |
|
---|
11 | import org.apache.commons.jcs.access.CacheAccess;
|
---|
12 | import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheAttributes;
|
---|
13 | import org.junit.Rule;
|
---|
14 | import org.junit.Test;
|
---|
15 | import org.openstreetmap.josm.testutils.JOSMTestRules;
|
---|
16 |
|
---|
17 | import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
|
---|
18 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
---|
19 |
|
---|
20 | /**
|
---|
21 | * Unit tests for class {@link JCSCacheManager}.
|
---|
22 | */
|
---|
23 | public class JCSCacheManagerTest {
|
---|
24 |
|
---|
25 | /**
|
---|
26 | * Setup test.
|
---|
27 | */
|
---|
28 | @Rule
|
---|
29 | @SuppressFBWarnings(value = "URF_UNREAD_PUBLIC_OR_PROTECTED_FIELD")
|
---|
30 | public JOSMTestRules test = new JOSMTestRules().preferences().timeout(20000);
|
---|
31 |
|
---|
32 | /**
|
---|
33 | * Tests that {@code JCSCacheManager} satisfies utility class criteria.
|
---|
34 | * @throws ReflectiveOperationException if an error occurs
|
---|
35 | */
|
---|
36 | @Test
|
---|
37 | public void testUtilityClass() throws ReflectiveOperationException {
|
---|
38 | UtilityClassTestUtil.assertUtilityClassWellDefined(JCSCacheManager.class);
|
---|
39 | }
|
---|
40 |
|
---|
41 | /**
|
---|
42 | * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/12054">Bug #12054</a>.
|
---|
43 | * @throws IOException if any I/O error occurs
|
---|
44 | */
|
---|
45 | @Test
|
---|
46 | public void testLoggingAdaptor12054() throws IOException {
|
---|
47 | JCSCacheManager.getCache("foobar", 1, 0, "foobar"); // cause logging adaptor to be initialized
|
---|
48 | Logger.getLogger("org.apache.commons.jcs").warning("{switch:0}");
|
---|
49 | }
|
---|
50 |
|
---|
51 | @Test
|
---|
52 | public void testUseBigDiskFile() throws IOException {
|
---|
53 | if (JCSCacheManager.USE_BLOCK_CACHE.get()) {
|
---|
54 | // test only when using block cache
|
---|
55 | File cacheFile = new File("foobar/testUseBigDiskFile_BLOCK_v2.data");
|
---|
56 | if (!cacheFile.exists()) {
|
---|
57 | if (!cacheFile.createNewFile()) {
|
---|
58 | System.err.println("Unable to create " + cacheFile.getAbsolutePath());
|
---|
59 | }
|
---|
60 | }
|
---|
61 | try (FileOutputStream fileOutputStream = new FileOutputStream(cacheFile, false)) {
|
---|
62 | fileOutputStream.getChannel().truncate(0);
|
---|
63 | fileOutputStream.write(new byte[1024*1024*10]); // create 10MB empty file
|
---|
64 | }
|
---|
65 |
|
---|
66 | CacheAccess<Object, Object> cache = JCSCacheManager.getCache("testUseBigDiskFile", 1, 100, "foobar");
|
---|
67 | assertEquals("BlockDiskCache use file size to calculate its size", 10*1024,
|
---|
68 | ((BlockDiskCacheAttributes) cache.getCacheControl().getAuxCaches()[0].getAuxiliaryCacheAttributes()).getMaxKeySize());
|
---|
69 | }
|
---|
70 | }
|
---|
71 | }
|
---|