1
2
3
4
5
6
7
8
9
10 package org.eclipse.jgit.util;
11
12 import static org.junit.Assert.assertEquals;
13 import static org.junit.Assert.assertNotNull;
14 import static org.junit.Assert.assertNull;
15
16 import java.io.IOException;
17 import java.nio.file.Files;
18 import java.nio.file.Path;
19
20 import org.junit.After;
21 import org.junit.Before;
22 import org.junit.Test;
23
24 public class SimpleLruCacheTest {
25
26 private Path trash;
27
28 private SimpleLruCache<String, String> cache;
29
30
31 @Before
32 public void setup() throws IOException {
33 trash = Files.createTempDirectory("tmp_");
34 cache = new SimpleLruCache<>(100, 0.2f);
35 }
36
37 @Before
38 @After
39 public void tearDown() throws Exception {
40 FileUtils.delete(trash.toFile(),
41 FileUtils.RECURSIVE | FileUtils.SKIP_MISSING);
42 }
43
44 @Test
45 public void testPutGet() {
46 cache.put("a", "A");
47 cache.put("z", "Z");
48 assertEquals("A", cache.get("a"));
49 assertEquals("Z", cache.get("z"));
50 }
51
52 @Test(expected = IllegalArgumentException.class)
53 public void testPurgeFactorTooLarge() {
54 cache.configure(5, 1.01f);
55 }
56
57 @Test(expected = IllegalArgumentException.class)
58 public void testPurgeFactorTooLarge2() {
59 cache.configure(5, 100);
60 }
61
62 @Test(expected = IllegalArgumentException.class)
63 public void testPurgeFactorTooSmall() {
64 cache.configure(5, 0);
65 }
66
67 @Test(expected = IllegalArgumentException.class)
68 public void testPurgeFactorTooSmall2() {
69 cache.configure(5, -100);
70 }
71
72 @Test
73 public void testGetMissing() {
74 assertEquals(null, cache.get("a"));
75 }
76
77 @Test
78 public void testPurge() {
79 for (int i = 0; i < 101; i++) {
80 cache.put("a" + i, "a" + i);
81 }
82 assertEquals(80, cache.size());
83 assertNull(cache.get("a0"));
84 assertNull(cache.get("a20"));
85 assertNotNull(cache.get("a21"));
86 assertNotNull(cache.get("a99"));
87 }
88
89 @Test
90 public void testConfigure() {
91 for (int i = 0; i < 100; i++) {
92 cache.put("a" + i, "a" + i);
93 }
94 assertEquals(100, cache.size());
95 cache.configure(10, 0.3f);
96 assertEquals(7, cache.size());
97 assertNull(cache.get("a0"));
98 assertNull(cache.get("a92"));
99 assertNotNull(cache.get("a93"));
100 assertNotNull(cache.get("a99"));
101 }
102 }