1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.bcel.util;
20
21 import static org.junit.jupiter.api.Assertions.assertEquals;
22 import static org.junit.jupiter.api.Assertions.assertNotNull;
23 import static org.junit.jupiter.api.Assertions.assertNull;
24 import static org.junit.jupiter.api.Assertions.assertThrows;
25
26 import java.io.IOException;
27
28 import org.apache.bcel.classfile.JavaClass;
29 import org.junit.jupiter.api.Test;
30
31
32
33
34 class LruCacheClassPathRepositoryTest {
35
36 @Test
37 void testCacheEviction() throws ClassNotFoundException, IOException {
38 try (ClassPath classPath = new ClassPath("")) {
39 final LruCacheClassPathRepository repository = new LruCacheClassPathRepository(classPath, 2);
40 final JavaClass class1 = repository.loadClass("java.lang.String");
41 assertNotNull(class1);
42 final JavaClass class2 = repository.loadClass("java.lang.Long");
43 assertNotNull(class2);
44 final JavaClass class3 = repository.loadClass("java.lang.Integer");
45 assertNotNull(class3);
46
47 assertNull(repository.findClass("java.lang.String"));
48 final JavaClass cachedClass2 = repository.findClass("java.lang.Long");
49 assertEquals(class2, cachedClass2);
50 }
51 }
52
53 @Test
54 void testLeastRecentlyUsedEviction() throws ClassNotFoundException, IOException {
55 try (ClassPath classPath = new ClassPath("")) {
56 final LruCacheClassPathRepository repository = new LruCacheClassPathRepository(classPath, 2);
57 final JavaClass class1 = repository.loadClass("java.lang.String");
58 assertNotNull(class1);
59 final JavaClass class2 = repository.loadClass("java.lang.Long");
60 assertNotNull(class2);
61 repository.findClass("java.lang.String");
62 final JavaClass class3 = repository.loadClass("java.lang.Integer");
63 assertNotNull(class3);
64
65 assertNull(repository.findClass("java.lang.Long"));
66 final JavaClass cachedClass1 = repository.findClass("java.lang.String");
67 assertEquals(class1, cachedClass1);
68 }
69 }
70
71 @Test
72 void testZeroCacheSize() throws IOException {
73 try (ClassPath classPath = new ClassPath("")) {
74 assertThrows(IllegalArgumentException.class, () -> new LruCacheClassPathRepository(classPath, 0));
75 }
76 }
77 }