Skip to content

Commit 7319722

Browse files
committed
add unit tests for ResolverUtil.
1 parent ab0395c commit 7319722

File tree

1 file changed

+129
-0
lines changed

1 file changed

+129
-0
lines changed
Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
/**
2+
* Copyright 2009-2019 the original author or authors.
3+
* <p>
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+
* <p>
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
* <p>
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 org.apache.ibatis.annotations.CacheNamespace;
19+
import org.junit.Assert;
20+
import org.junit.Before;
21+
import org.junit.Test;
22+
23+
import java.util.Set;
24+
25+
/**
26+
* Unit tests for {@link ResolverUtil}.
27+
*
28+
* @author Pi Chen
29+
* @since 3.5.2
30+
*/
31+
32+
public class ResolverUtilTest {
33+
private ClassLoader currentContextClassLoader;
34+
35+
@Before
36+
public void setUp() throws Exception {
37+
currentContextClassLoader = Thread.currentThread().getContextClassLoader();
38+
}
39+
40+
@Test
41+
public void getClasses() {
42+
Assert.assertEquals(new ResolverUtil<>().getClasses().size(), 0);
43+
}
44+
45+
@Test
46+
public void getClassLoader() {
47+
Assert.assertEquals(new ResolverUtil<>().getClassLoader(), currentContextClassLoader);
48+
}
49+
50+
@Test
51+
public void setClassLoader() {
52+
ResolverUtil resolverUtil = new ResolverUtil();
53+
resolverUtil.setClassLoader(new ClassLoader() {
54+
// do nothing...
55+
});
56+
Assert.assertNotEquals(resolverUtil.getClassLoader(), currentContextClassLoader);
57+
}
58+
59+
@Test
60+
public void findImplementationsWithNullPackageName() {
61+
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
62+
resolverUtil.findImplementations(VFS.class, null);
63+
Assert.assertEquals(resolverUtil.getClasses().size(), 0);
64+
}
65+
66+
@Test
67+
public void findImplementations() {
68+
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
69+
resolverUtil.findImplementations(VFS.class, "org.apache.ibatis.io");
70+
Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
71+
classSets.forEach(c -> Assert.assertTrue(VFS.class.isAssignableFrom(c)));
72+
}
73+
74+
@Test
75+
public void findAnnotatedWithNullPackageName() {
76+
ResolverUtil<Object> resolverUtil = new ResolverUtil<>();
77+
resolverUtil.findAnnotated(CacheNamespace.class, null);
78+
Assert.assertEquals(resolverUtil.getClasses().size(), 0);
79+
}
80+
81+
@Test
82+
public void findAnnotated() {
83+
ResolverUtil<Object> resolverUtil = new ResolverUtil<>();
84+
resolverUtil.findAnnotated(CacheNamespace.class, "org.apache.ibatis.binding");
85+
Set<Class<?>> classSets = resolverUtil.getClasses();
86+
classSets.forEach(c -> Assert.assertNotNull(c.getAnnotation(CacheNamespace.class)));
87+
}
88+
89+
@Test
90+
public void find() {
91+
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
92+
resolverUtil.find(new ResolverUtil.IsA(VFS.class), "org.apache.ibatis.io");
93+
Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
94+
classSets.forEach(c -> Assert.assertTrue(VFS.class.isAssignableFrom(c)));
95+
}
96+
97+
@Test
98+
public void getPackagePath() {
99+
ResolverUtil resolverUtil = new ResolverUtil();
100+
Assert.assertNull(resolverUtil.getPackagePath(null));
101+
Assert.assertEquals(resolverUtil.getPackagePath("org.apache.ibatis.io"), "org/apache/ibatis/io");
102+
}
103+
104+
@Test
105+
public void addIfMatching() {
106+
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
107+
resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/DefaultVFS.class");
108+
resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/VFS.class");
109+
Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
110+
classSets.forEach(c -> Assert.assertTrue(VFS.class.isAssignableFrom(c)));
111+
}
112+
113+
@Test
114+
public void addIfNotMatching() {
115+
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
116+
resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/Xxx.class");
117+
Assert.assertEquals(resolverUtil.getClasses().size(), 0);
118+
}
119+
120+
@Test
121+
public void testToString() {
122+
ResolverUtil.IsA isa = new ResolverUtil.IsA(VFS.class);
123+
Assert.assertTrue(isa.toString().contains(VFS.class.getSimpleName()));
124+
125+
ResolverUtil.AnnotatedWith annotatedWith = new ResolverUtil.AnnotatedWith(CacheNamespace.class);
126+
Assert.assertTrue(annotatedWith.toString().contains("@" + CacheNamespace.class.getSimpleName()));
127+
}
128+
129+
}

0 commit comments

Comments
 (0)