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

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

fix #21064 - Add JUnit 5 extension for preferences (patch by taylor.smock)

File size: 2.5 KB
Line 
1// License: GPL. For details, see LICENSE file.
2package org.openstreetmap.josm.data.cache;
3
4import static org.junit.jupiter.api.Assertions.assertEquals;
5
6import java.io.File;
7import java.io.FileOutputStream;
8import java.io.IOException;
9import java.util.logging.Logger;
10
11import org.openstreetmap.josm.testutils.annotations.BasicPreferences;
12
13import net.trajano.commons.testing.UtilityClassTestUtil;
14import org.apache.commons.jcs3.access.CacheAccess;
15import org.apache.commons.jcs3.auxiliary.disk.block.BlockDiskCacheAttributes;
16import org.junit.jupiter.api.Test;
17import org.junit.jupiter.api.Timeout;
18
19/**
20 * Unit tests for class {@link JCSCacheManager}.
21 */
22@Timeout(20)
23@BasicPreferences
24class JCSCacheManagerTest {
25 /**
26 * Tests that {@code JCSCacheManager} satisfies utility class criteria.
27 * @throws ReflectiveOperationException if an error occurs
28 */
29 @Test
30 void testUtilityClass() throws ReflectiveOperationException {
31 UtilityClassTestUtil.assertUtilityClassWellDefined(JCSCacheManager.class);
32 }
33
34 /**
35 * Non-regression test for <a href="https://josm.openstreetmap.de/ticket/12054">Bug #12054</a>.
36 * @throws IOException if any I/O error occurs
37 */
38 @Test
39 void testLoggingAdaptor12054() throws IOException {
40 JCSCacheManager.getCache("foobar", 1, 0, "foobar"); // cause logging adaptor to be initialized
41 Logger.getLogger("org.apache.commons.jcs3").warning("{switch:0}");
42 }
43
44 @Test
45 void testUseBigDiskFile() throws IOException {
46 if (JCSCacheManager.USE_BLOCK_CACHE.get()) {
47 // test only when using block cache
48 File cacheFile = new File("foobar/testUseBigDiskFile_BLOCK_v2.data");
49 if (!cacheFile.exists()) {
50 if (!cacheFile.createNewFile()) {
51 System.err.println("Unable to create " + cacheFile.getAbsolutePath());
52 }
53 }
54 try (FileOutputStream fileOutputStream = new FileOutputStream(cacheFile, false)) {
55 fileOutputStream.getChannel().truncate(0);
56 fileOutputStream.write(new byte[1024*1024*10]); // create 10MB empty file
57 }
58
59 CacheAccess<Object, Object> cache = JCSCacheManager.getCache("testUseBigDiskFile", 1, 100, "foobar");
60 assertEquals(10*1024,
61 ((BlockDiskCacheAttributes) cache.getCacheControl().getAuxCaches()[0].getAuxiliaryCacheAttributes()).getMaxKeySize(),
62 "BlockDiskCache use file size to calculate its size");
63 }
64 }
65}
Note: See TracBrowser for help on using the repository browser.