1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.apache.ibatis.io;
17
18 import static org.junit.jupiter.api.Assertions.assertEquals;
19 import static org.junit.jupiter.api.Assertions.assertNotEquals;
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.assertTrue;
23
24 import java.security.AccessController;
25 import java.security.PrivilegedAction;
26 import java.util.Set;
27
28 import org.apache.ibatis.annotations.CacheNamespace;
29 import org.junit.jupiter.api.BeforeAll;
30 import org.junit.jupiter.api.Test;
31
32
33
34
35
36
37
38
39
40 class ResolverUtilTest {
41 private static ClassLoader currentContextClassLoader;
42
43 @BeforeAll
44 static void setUp() {
45 currentContextClassLoader = Thread.currentThread().getContextClassLoader();
46 }
47
48 @Test
49 void getClasses() {
50 assertEquals(new ResolverUtil<>().getClasses().size(), 0);
51 }
52
53 @Test
54 void getClassLoader() {
55 assertEquals(new ResolverUtil<>().getClassLoader(), currentContextClassLoader);
56 }
57
58 @Test
59 void setClassLoader() {
60 ResolverUtil resolverUtil = new ResolverUtil();
61 AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
62 resolverUtil.setClassLoader(new ClassLoader() {
63 });
64 return null;
65 });
66 assertNotEquals(resolverUtil.getClassLoader(), currentContextClassLoader);
67 }
68
69 @Test
70 void findImplementationsWithNullPackageName() {
71 ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
72 resolverUtil.findImplementations(VFS.class, (String[]) null);
73 assertEquals(resolverUtil.getClasses().size(), 0);
74 }
75
76 @Test
77 void findImplementations() {
78 ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
79 resolverUtil.findImplementations(VFS.class, "org.apache.ibatis.io");
80 Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
81
82
83
84 assertEquals(classSets.size(), 3);
85 classSets.forEach(c -> assertTrue(VFS.class.isAssignableFrom(c)));
86 }
87
88 @Test
89 void findAnnotatedWithNullPackageName() {
90 ResolverUtil<Object> resolverUtil = new ResolverUtil<>();
91 resolverUtil.findAnnotated(CacheNamespace.class, (String[]) null);
92 assertEquals(resolverUtil.getClasses().size(), 0);
93 }
94
95 @Test
96 void findAnnotated() {
97 ResolverUtil<Object> resolverUtil = new ResolverUtil<>();
98 resolverUtil.findAnnotated(CacheNamespace.class, this.getClass().getPackage().getName());
99 Set<Class<?>> classSets = resolverUtil.getClasses();
100
101 assertEquals(classSets.size(), 1);
102 classSets.forEach(c -> assertNotNull(c.getAnnotation(CacheNamespace.class)));
103 }
104
105 @Test
106 void find() {
107 ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
108 resolverUtil.find(new ResolverUtil.IsA(VFS.class), "org.apache.ibatis.io");
109 Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
110
111
112
113 assertEquals(classSets.size(), 3);
114 classSets.forEach(c -> assertTrue(VFS.class.isAssignableFrom(c)));
115 }
116
117 @Test
118 void getPackagePath() {
119 ResolverUtil resolverUtil = new ResolverUtil();
120 assertNull(resolverUtil.getPackagePath(null));
121 assertEquals(resolverUtil.getPackagePath("org.apache.ibatis.io"), "org/apache/ibatis/io");
122 }
123
124 @Test
125 void addIfMatching() {
126 ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
127 resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/DefaultVFS.class");
128 resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/VFS.class");
129 Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
130 assertEquals(classSets.size(), 2);
131 classSets.forEach(c -> assertTrue(VFS.class.isAssignableFrom(c)));
132 }
133
134 @Test
135 void addIfNotMatching() {
136 ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
137 resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/Xxx.class");
138 assertEquals(resolverUtil.getClasses().size(), 0);
139 }
140
141 @Test
142 void testToString() {
143 ResolverUtil.IsA isa = new ResolverUtil.IsA(VFS.class);
144 assertTrue(isa.toString().contains(VFS.class.getSimpleName()));
145
146 ResolverUtil.AnnotatedWith annotatedWith = new ResolverUtil.AnnotatedWith(CacheNamespace.class);
147 assertTrue(annotatedWith.toString().contains("@" + CacheNamespace.class.getSimpleName()));
148 }
149
150 @CacheNamespace(readWrite = false)
151 private interface TestMapper {
152
153 }
154
155 }