source: josm/trunk/test/unit/org/openstreetmap/josm/data/cache/JCSCacheManagerTest.java@ 11921

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

improve unit test coverage of utilities classes thanks to https://trajano.github.io/commons-testing

File size: 2.6 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.File;
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.util.logging.Logger;
10
11import org.apache.commons.jcs.access.CacheAccess;
12import org.apache.commons.jcs.auxiliary.disk.block.BlockDiskCacheAttributes;
13import org.junit.Rule;
14import org.junit.Test;
15import org.openstreetmap.josm.testutils.JOSMTestRules;
16
17import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
18import net.trajano.commons.testing.UtilityClassTestUtil;
19
20/**
21 * Unit tests for class {@link JCSCacheManager}.
22 */
23public 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();
31
32 /**
33 * Tests that {@code JCSCacheManager} satisfies utility class criterias.
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}
Note: See TracBrowser for help on using the repository browser.