Skip to content

Commit 30185e3

Browse files
committed
upgrade to JUnit 5.
1 parent 7319722 commit 30185e3

File tree

1 file changed

+72
-50
lines changed

1 file changed

+72
-50
lines changed
Lines changed: 72 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -1,129 +1,151 @@
11
/**
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.
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.
1515
*/
1616
package org.apache.ibatis.io;
1717

18-
import org.apache.ibatis.annotations.CacheNamespace;
19-
import org.junit.Assert;
20-
import org.junit.Before;
21-
import org.junit.Test;
18+
import static org.junit.jupiter.api.Assertions.*;
2219

20+
import java.security.AccessController;
21+
import java.security.PrivilegedAction;
2322
import java.util.Set;
2423

24+
import org.apache.ibatis.annotations.CacheNamespace;
25+
import org.junit.jupiter.api.BeforeAll;
26+
import org.junit.jupiter.api.Test;
27+
2528
/**
2629
* Unit tests for {@link ResolverUtil}.
2730
*
2831
* @author Pi Chen
2932
* @since 3.5.2
3033
*/
3134

32-
public class ResolverUtilTest {
33-
private ClassLoader currentContextClassLoader;
35+
class ResolverUtilTest {
36+
private static ClassLoader currentContextClassLoader;
3437

35-
@Before
36-
public void setUp() throws Exception {
38+
@BeforeAll
39+
static void setUp() {
3740
currentContextClassLoader = Thread.currentThread().getContextClassLoader();
3841
}
3942

4043
@Test
41-
public void getClasses() {
42-
Assert.assertEquals(new ResolverUtil<>().getClasses().size(), 0);
44+
void getClasses() {
45+
assertEquals(new ResolverUtil<>().getClasses().size(), 0);
4346
}
4447

4548
@Test
46-
public void getClassLoader() {
47-
Assert.assertEquals(new ResolverUtil<>().getClassLoader(), currentContextClassLoader);
49+
void getClassLoader() {
50+
assertEquals(new ResolverUtil<>().getClassLoader(), currentContextClassLoader);
4851
}
4952

5053
@Test
51-
public void setClassLoader() {
54+
void setClassLoader() {
5255
ResolverUtil resolverUtil = new ResolverUtil();
53-
resolverUtil.setClassLoader(new ClassLoader() {
54-
// do nothing...
56+
AccessController.doPrivileged((PrivilegedAction<Void>) () -> {
57+
resolverUtil.setClassLoader(new ClassLoader() {
58+
});
59+
return null;
5560
});
56-
Assert.assertNotEquals(resolverUtil.getClassLoader(), currentContextClassLoader);
61+
assertNotEquals(resolverUtil.getClassLoader(), currentContextClassLoader);
5762
}
5863

5964
@Test
60-
public void findImplementationsWithNullPackageName() {
65+
void findImplementationsWithNullPackageName() {
6166
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
6267
resolverUtil.findImplementations(VFS.class, null);
63-
Assert.assertEquals(resolverUtil.getClasses().size(), 0);
68+
assertEquals(resolverUtil.getClasses().size(), 0);
6469
}
6570

6671
@Test
67-
public void findImplementations() {
72+
void findImplementations() {
6873
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
6974
resolverUtil.findImplementations(VFS.class, "org.apache.ibatis.io");
7075
Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
71-
classSets.forEach(c -> Assert.assertTrue(VFS.class.isAssignableFrom(c)));
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)));
7281
}
7382

7483
@Test
75-
public void findAnnotatedWithNullPackageName() {
84+
void findAnnotatedWithNullPackageName() {
7685
ResolverUtil<Object> resolverUtil = new ResolverUtil<>();
7786
resolverUtil.findAnnotated(CacheNamespace.class, null);
78-
Assert.assertEquals(resolverUtil.getClasses().size(), 0);
87+
assertEquals(resolverUtil.getClasses().size(), 0);
7988
}
8089

8190
@Test
82-
public void findAnnotated() {
91+
void findAnnotated() {
8392
ResolverUtil<Object> resolverUtil = new ResolverUtil<>();
84-
resolverUtil.findAnnotated(CacheNamespace.class, "org.apache.ibatis.binding");
93+
resolverUtil.findAnnotated(CacheNamespace.class, this.getClass().getPackage().getName());
8594
Set<Class<?>> classSets = resolverUtil.getClasses();
86-
classSets.forEach(c -> Assert.assertNotNull(c.getAnnotation(CacheNamespace.class)));
95+
//org.apache.ibatis.io.ResolverUtilTest.TestMapper
96+
assertEquals(classSets.size(), 1);
97+
classSets.forEach(c -> assertNotNull(c.getAnnotation(CacheNamespace.class)));
8798
}
8899

89100
@Test
90-
public void find() {
101+
void find() {
91102
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
92103
resolverUtil.find(new ResolverUtil.IsA(VFS.class), "org.apache.ibatis.io");
93104
Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
94-
classSets.forEach(c -> Assert.assertTrue(VFS.class.isAssignableFrom(c)));
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)));
95110
}
96111

97112
@Test
98-
public void getPackagePath() {
113+
void getPackagePath() {
99114
ResolverUtil resolverUtil = new ResolverUtil();
100-
Assert.assertNull(resolverUtil.getPackagePath(null));
101-
Assert.assertEquals(resolverUtil.getPackagePath("org.apache.ibatis.io"), "org/apache/ibatis/io");
115+
assertNull(resolverUtil.getPackagePath(null));
116+
assertEquals(resolverUtil.getPackagePath("org.apache.ibatis.io"), "org/apache/ibatis/io");
102117
}
103118

104119
@Test
105-
public void addIfMatching() {
120+
void addIfMatching() {
106121
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
107122
resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/DefaultVFS.class");
108123
resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/VFS.class");
109124
Set<Class<? extends VFS>> classSets = resolverUtil.getClasses();
110-
classSets.forEach(c -> Assert.assertTrue(VFS.class.isAssignableFrom(c)));
125+
assertEquals(classSets.size(), 2);
126+
classSets.forEach(c -> assertTrue(VFS.class.isAssignableFrom(c)));
111127
}
112128

113129
@Test
114-
public void addIfNotMatching() {
130+
void addIfNotMatching() {
115131
ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
116132
resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/Xxx.class");
117-
Assert.assertEquals(resolverUtil.getClasses().size(), 0);
133+
assertEquals(resolverUtil.getClasses().size(), 0);
118134
}
119135

120136
@Test
121-
public void testToString() {
137+
void testToString() {
122138
ResolverUtil.IsA isa = new ResolverUtil.IsA(VFS.class);
123-
Assert.assertTrue(isa.toString().contains(VFS.class.getSimpleName()));
139+
assertTrue(isa.toString().contains(VFS.class.getSimpleName()));
124140

125141
ResolverUtil.AnnotatedWith annotatedWith = new ResolverUtil.AnnotatedWith(CacheNamespace.class);
126-
Assert.assertTrue(annotatedWith.toString().contains("@" + CacheNamespace.class.getSimpleName()));
142+
assertTrue(annotatedWith.toString().contains("@" + CacheNamespace.class.getSimpleName()));
143+
}
144+
145+
146+
@CacheNamespace(readWrite = false)
147+
private interface TestMapper {
148+
//test ResolverUtil.findAnnotated method
127149
}
128150

129151
}

0 commit comments

Comments
 (0)