1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.bcel.util;
18
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertNotNull;
21 import static org.junit.jupiter.api.Assertions.assertNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23
24 import java.io.IOException;
25
26 import org.apache.bcel.classfile.JavaClass;
27 import org.junit.jupiter.api.Test;
28
29
30
31
32
33
34
35
36 public class ClassPathRepositoryTestCase {
37
38 @Test
39 public void testClassPath() throws IOException {
40 try (final ClassPath classPath = new ClassPath("")) {
41 final ClassPathRepository repository = new ClassPathRepository(classPath);
42 try (final ClassPath repoCp = repository.getClassPath()) {
43 assertEquals(classPath, repoCp);
44 }
45 }
46 }
47
48 @Test
49 public void testClassPathRepository() throws ClassNotFoundException, IOException {
50 try (final ClassPath classPath = new ClassPath("")) {
51 verifyCaching(new ClassPathRepository(classPath));
52 }
53 }
54
55 @Test
56 public void testClassWithoutPackage() throws IOException {
57 try (final ClassPath classPath = new ClassPath("")) {
58 final ClassPathRepository repository = new ClassPathRepository(classPath);
59 assertThrows(ClassNotFoundException.class, () -> repository.loadClass("ClassXYZ"));
60 }
61 }
62
63 @Test
64 public void testEmptyInput() throws IOException {
65 try (final ClassPath classPath = new ClassPath("")) {
66 final ClassPathRepository repository = new ClassPathRepository(classPath);
67 assertThrows(IllegalArgumentException.class, () -> repository.loadClass(""));
68 }
69 }
70
71 @Test
72 public void testLruCacheClassPathRepository() throws ClassNotFoundException, IOException {
73 try (final ClassPath classPath = new ClassPath("")) {
74 verifyCaching(new LruCacheClassPathRepository(classPath, 10));
75 }
76 }
77
78 @Test
79 public void testMemorySensitiveClassPathRepository() throws ClassNotFoundException, IOException {
80 try (final ClassPath classPath = new ClassPath("")) {
81 verifyCaching(new MemorySensitiveClassPathRepository(classPath));
82 }
83 }
84
85 @Test
86 public void testNoClassNotFound() throws IOException {
87 try (final ClassPath classPath = new ClassPath("")) {
88 final ClassPathRepository repository = new ClassPathRepository(classPath);
89 assertThrows(ClassNotFoundException.class, () -> repository.loadClass("no.such.Class"));
90 }
91 }
92
93 @Test
94 public void testNullInput() throws IOException {
95 try (final ClassPath classPath = new ClassPath("")) {
96 final ClassPathRepository repository = new ClassPathRepository(classPath);
97 assertThrows(IllegalArgumentException.class, () -> repository.loadClass((String) null));
98 }
99 }
100
101 private void verifyCaching(final AbstractClassPathRepository repository) throws ClassNotFoundException {
102
103 final JavaClass class1 = repository.loadClass("java.lang.String");
104 assertNotNull(class1);
105 final JavaClass class2 = repository.loadClass("java/lang/Long");
106 assertNotNull(class2);
107
108
109 assertEquals(class1, repository.findClass("java.lang.String"));
110 assertEquals(class2, repository.findClass("java.lang.Long"));
111
112
113 repository.removeClass(class1);
114 assertNull(repository.findClass("java.lang.String"));
115
116
117 repository.clear();
118 assertNull(repository.findClass("java.lang.Long"));
119 }
120 }