Skip to content

Commit bb1bf8c

Browse files
authored
Merge pull request #1568 from peterchenhdu/master
add unit tests for ResolverUtil.
2 parents 08bcdb8 + 30185e3 commit bb1bf8c

File tree

1 file changed

+151
-0
lines changed

1 file changed

+151
-0
lines changed
Lines changed: 151 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,151 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.apache.ibatis.io;
17+
18+
import static org.junit.jupiter.api.Assertions.*;
19+
20+
import java.security.AccessController;
21+
import java.security.PrivilegedAction;
22+
import java.util.Set;
23+
24+
import org.apache.ibatis.annotations.CacheNamespace;
25+
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.Test;
27+
28+
/**
29+
* Unit tests for {@link ResolverUtil}.
30+
*
31+
* @author Pi Chen
32+
* @since 3.5.2
33+
*/
34+
35+
class ResolverUtilTest {
36+
private static ClassLoader currentContextClassLoader;
37+
38+
@BeforeAll
39+
static void setUp() {
40+
currentContextClassLoader = Thread.currentThread().getContextClassLoader();
41+
}
42+
43+
@Test
44+
void getClasses() {
45+
assertEquals(new ResolverUtil<>().getClasses().size(), 0);
46+
}
47+
48+
@Test
49+
void getClassLoader() {
50+
assertEquals(new ResolverUtil<>().getClassLoader(), currentContextClassLoader);
51+
}
52+
53+
@Test
54+
void setClassLoader() {
55+
ResolverUtil resolverUtil = new ResolverUtil();
56+
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
57+
resolverUtil.setClassLoader(new ClassLoader() {
58+
});
59+
return null;
60+
});
61+
assertNotEquals(resolverUtil.getClassLoader(), currentContextClassLoader);
62+
}
63+
64+
@Test
65+
void findImplementationsWithNullPackageName() {
66+
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
67+
resolverUtil.findImplementations(VFS.class, null);
68+
assertEquals(resolverUtil.getClasses().size(), 0);
69+
}
70+
71+
@Test
72+
void findImplementations() {
73+
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
74+
resolverUtil.findImplementations(VFS.class, "org.apache.ibatis.io");
75+
Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
76+
//org.apache.ibatis.io.VFS
77+
//org.apache.ibatis.io.DefaultVFS
78+
//org.apache.ibatis.io.JBoss6VFS
79+
assertEquals(classSets.size(), 3); //fail if add a new VFS implementation in this package!!!
80+
classSets.forEach(c -> assertTrue(VFS.class.isAssignableFrom(c)));
81+
}
82+
83+
@Test
84+
void findAnnotatedWithNullPackageName() {
85+
ResolverUtil<Object> resolverUtil = new ResolverUtil<>();
86+
resolverUtil.findAnnotated(CacheNamespace.class, null);
87+
assertEquals(resolverUtil.getClasses().size(), 0);
88+
}
89+
90+
@Test
91+
void findAnnotated() {
92+
ResolverUtil<Object> resolverUtil = new ResolverUtil<>();
93+
resolverUtil.findAnnotated(CacheNamespace.class, this.getClass().getPackage().getName());
94+
Set<Class<?>> classSets = resolverUtil.getClasses();
95+
//org.apache.ibatis.io.ResolverUtilTest.TestMapper
96+
assertEquals(classSets.size(), 1);
97+
classSets.forEach(c -> assertNotNull(c.getAnnotation(CacheNamespace.class)));
98+
}
99+
100+
@Test
101+
void find() {
102+
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
103+
resolverUtil.find(new ResolverUtil.IsA(VFS.class), "org.apache.ibatis.io");
104+
Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
105+
//org.apache.ibatis.io.VFS
106+
//org.apache.ibatis.io.DefaultVFS
107+
//org.apache.ibatis.io.JBoss6VFS
108+
assertEquals(classSets.size(), 3);
109+
classSets.forEach(c -> assertTrue(VFS.class.isAssignableFrom(c)));
110+
}
111+
112+
@Test
113+
void getPackagePath() {
114+
ResolverUtil resolverUtil = new ResolverUtil();
115+
assertNull(resolverUtil.getPackagePath(null));
116+
assertEquals(resolverUtil.getPackagePath("org.apache.ibatis.io"), "org/apache/ibatis/io");
117+
}
118+
119+
@Test
120+
void addIfMatching() {
121+
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
122+
resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/DefaultVFS.class");
123+
resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/VFS.class");
124+
Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
125+
assertEquals(classSets.size(), 2);
126+
classSets.forEach(c -> assertTrue(VFS.class.isAssignableFrom(c)));
127+
}
128+
129+
@Test
130+
void addIfNotMatching() {
131+
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
132+
resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/Xxx.class");
133+
assertEquals(resolverUtil.getClasses().size(), 0);
134+
}
135+
136+
@Test
137+
void testToString() {
138+
ResolverUtil.IsA isa = new ResolverUtil.IsA(VFS.class);
139+
assertTrue(isa.toString().contains(VFS.class.getSimpleName()));
140+
141+
ResolverUtil.AnnotatedWith annotatedWith = new ResolverUtil.AnnotatedWith(CacheNamespace.class);
142+
assertTrue(annotatedWith.toString().contains("@" + CacheNamespace.class.getSimpleName()));
143+
}
144+
145+
146+
@CacheNamespace(readWrite = false)
147+
private interface TestMapper {
148+
//test ResolverUtil.findAnnotated method
149+
}
150+
151+
}

0 commit comments

Comments
 (0)