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
35
36
37
38 class ClassPathRepositoryTest {
39
40 @Test
41 void testClassPath() throws IOException {
42 try (ClassPath classPath = new ClassPath("")) {
43 final ClassPathRepository repository = new ClassPathRepository(classPath);
44 try (ClassPath repoCp = repository.getClassPath()) {
45 assertEquals(classPath, repoCp);
46 }
47 }
48 }
49
50 @Test
51 void testClassPathRepository() throws ClassNotFoundException, IOException {
52 try (ClassPath classPath = new ClassPath("")) {
53 verifyCaching(new ClassPathRepository(classPath));
54 }
55 }
56
57 @Test
58 void testClassWithoutPackage() throws IOException {
59 try (ClassPath classPath = new ClassPath("")) {
60 final ClassPathRepository repository = new ClassPathRepository(classPath);
61 assertThrows(ClassNotFoundException.class, () -> repository.loadClass("ClassXYZ"));
62 }
63 }
64
65 @Test
66 void testEmptyInput() throws IOException {
67 try (ClassPath classPath = new ClassPath("")) {
68 final ClassPathRepository repository = new ClassPathRepository(classPath);
69 assertThrows(IllegalArgumentException.class, () -> repository.loadClass(""));
70 }
71 }
72
73 @Test
74 void testLruCacheClassPathRepository() throws ClassNotFoundException, IOException {
75 try (ClassPath classPath = new ClassPath("")) {
76 verifyCaching(new LruCacheClassPathRepository(classPath, 10));
77 }
78 }
79
80 @Test
81 void testMemorySensitiveClassPathRepository() throws ClassNotFoundException, IOException {
82 try (ClassPath classPath = new ClassPath("")) {
83 verifyCaching(new MemorySensitiveClassPathRepository(classPath));
84 }
85 }
86
87 @Test
88 void testNoClassNotFound() throws IOException {
89 try (ClassPath classPath = new ClassPath("")) {
90 final ClassPathRepository repository = new ClassPathRepository(classPath);
91 assertThrows(ClassNotFoundException.class, () -> repository.loadClass("no.such.Class"));
92 }
93 }
94
95 @Test
96 void testNullInput() throws IOException {
97 try (ClassPath classPath = new ClassPath("")) {
98 final ClassPathRepository repository = new ClassPathRepository(classPath);
99 assertThrows(IllegalArgumentException.class, () -> repository.loadClass((String) null));
100 }
101 }
102
103 private void verifyCaching(final AbstractClassPathRepository repository) throws ClassNotFoundException {
104
105 final JavaClass class1 = repository.loadClass("java.lang.String");
106 assertNotNull(class1);
107 final JavaClass class2 = repository.loadClass("java/lang/Long");
108 assertNotNull(class2);
109
110
111 assertEquals(class1, repository.findClass("java.lang.String"));
112 assertEquals(class2, repository.findClass("java.lang.Long"));
113
114
115 repository.removeClass(class1);
116 assertNull(repository.findClass("java.lang.String"));
117
118
119 repository.clear();
120 assertNull(repository.findClass("java.lang.Long"));
121 }
122 }