| 1 | // License: GPL. For details, see LICENSE file.
|
|---|
| 2 | package org.openstreetmap.josm.data.cache;
|
|---|
| 3 |
|
|---|
| 4 | import static org.junit.jupiter.api.Assertions.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.openstreetmap.josm.testutils.annotations.BasicPreferences;
|
|---|
| 12 |
|
|---|
| 13 | import net.trajano.commons.testing.UtilityClassTestUtil;
|
|---|
| 14 | import org.apache.commons.jcs3.access.CacheAccess;
|
|---|
| 15 | import org.apache.commons.jcs3.auxiliary.disk.block.BlockDiskCacheAttributes;
|
|---|
| 16 | import org.junit.jupiter.api.Test;
|
|---|
| 17 | import org.junit.jupiter.api.Timeout;
|
|---|
| 18 |
|
|---|
| 19 | /**
|
|---|
| 20 | * Unit tests for class {@link JCSCacheManager}.
|
|---|
| 21 | */
|
|---|
| 22 | @Timeout(20)
|
|---|
| 23 | @BasicPreferences
|
|---|
| 24 | class 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 | }
|
|---|