|
1 | 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. |
| 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 | 15 | */
|
16 | 16 | package org.apache.ibatis.io;
|
17 | 17 |
|
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.*; |
22 | 19 |
|
| 20 | +import java.security.AccessController; |
| 21 | +import java.security.PrivilegedAction; |
23 | 22 | import java.util.Set;
|
24 | 23 |
|
| 24 | +import org.apache.ibatis.annotations.CacheNamespace; |
| 25 | +import org.junit.jupiter.api.BeforeAll; |
| 26 | +import org.junit.jupiter.api.Test; |
| 27 | + |
25 | 28 | /**
|
26 | 29 | * Unit tests for {@link ResolverUtil}.
|
27 | 30 | *
|
28 | 31 | * @author Pi Chen
|
29 | 32 | * @since 3.5.2
|
30 | 33 | */
|
31 | 34 |
|
32 |
| -public class ResolverUtilTest { |
33 |
| - private ClassLoader currentContextClassLoader; |
| 35 | +class ResolverUtilTest { |
| 36 | + private static ClassLoader currentContextClassLoader; |
34 | 37 |
|
35 |
| - @Before |
36 |
| - public void setUp() throws Exception { |
| 38 | + @BeforeAll |
| 39 | + static void setUp() { |
37 | 40 | currentContextClassLoader = Thread.currentThread().getContextClassLoader();
|
38 | 41 | }
|
39 | 42 |
|
40 | 43 | @Test
|
41 |
| - public void getClasses() { |
42 |
| - Assert.assertEquals(new ResolverUtil<>().getClasses().size(), 0); |
| 44 | + void getClasses() { |
| 45 | + assertEquals(new ResolverUtil<>().getClasses().size(), 0); |
43 | 46 | }
|
44 | 47 |
|
45 | 48 | @Test
|
46 |
| - public void getClassLoader() { |
47 |
| - Assert.assertEquals(new ResolverUtil<>().getClassLoader(), currentContextClassLoader); |
| 49 | + void getClassLoader() { |
| 50 | + assertEquals(new ResolverUtil<>().getClassLoader(), currentContextClassLoader); |
48 | 51 | }
|
49 | 52 |
|
50 | 53 | @Test
|
51 |
| - public void setClassLoader() { |
| 54 | + void setClassLoader() { |
52 | 55 | 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; |
55 | 60 | });
|
56 |
| - Assert.assertNotEquals(resolverUtil.getClassLoader(), currentContextClassLoader); |
| 61 | + assertNotEquals(resolverUtil.getClassLoader(), currentContextClassLoader); |
57 | 62 | }
|
58 | 63 |
|
59 | 64 | @Test
|
60 |
| - public void findImplementationsWithNullPackageName() { |
| 65 | + void findImplementationsWithNullPackageName() { |
61 | 66 | ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
|
62 | 67 | resolverUtil.findImplementations(VFS.class, null);
|
63 |
| - Assert.assertEquals(resolverUtil.getClasses().size(), 0); |
| 68 | + assertEquals(resolverUtil.getClasses().size(), 0); |
64 | 69 | }
|
65 | 70 |
|
66 | 71 | @Test
|
67 |
| - public void findImplementations() { |
| 72 | + void findImplementations() { |
68 | 73 | ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
|
69 | 74 | resolverUtil.findImplementations(VFS.class, "org.apache.ibatis.io");
|
70 | 75 | 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))); |
72 | 81 | }
|
73 | 82 |
|
74 | 83 | @Test
|
75 |
| - public void findAnnotatedWithNullPackageName() { |
| 84 | + void findAnnotatedWithNullPackageName() { |
76 | 85 | ResolverUtil<Object> resolverUtil = new ResolverUtil<>();
|
77 | 86 | resolverUtil.findAnnotated(CacheNamespace.class, null);
|
78 |
| - Assert.assertEquals(resolverUtil.getClasses().size(), 0); |
| 87 | + assertEquals(resolverUtil.getClasses().size(), 0); |
79 | 88 | }
|
80 | 89 |
|
81 | 90 | @Test
|
82 |
| - public void findAnnotated() { |
| 91 | + void findAnnotated() { |
83 | 92 | ResolverUtil<Object> resolverUtil = new ResolverUtil<>();
|
84 |
| - resolverUtil.findAnnotated(CacheNamespace.class, "org.apache.ibatis.binding"); |
| 93 | + resolverUtil.findAnnotated(CacheNamespace.class, this.getClass().getPackage().getName()); |
85 | 94 | 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))); |
87 | 98 | }
|
88 | 99 |
|
89 | 100 | @Test
|
90 |
| - public void find() { |
| 101 | + void find() { |
91 | 102 | ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
|
92 | 103 | resolverUtil.find(new ResolverUtil.IsA(VFS.class), "org.apache.ibatis.io");
|
93 | 104 | 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))); |
95 | 110 | }
|
96 | 111 |
|
97 | 112 | @Test
|
98 |
| - public void getPackagePath() { |
| 113 | + void getPackagePath() { |
99 | 114 | 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"); |
102 | 117 | }
|
103 | 118 |
|
104 | 119 | @Test
|
105 |
| - public void addIfMatching() { |
| 120 | + void addIfMatching() { |
106 | 121 | ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
|
107 | 122 | resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/DefaultVFS.class");
|
108 | 123 | resolverUtil.addIfMatching(new ResolverUtil.IsA(VFS.class), "org/apache/ibatis/io/VFS.class");
|
109 | 124 | 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))); |
111 | 127 | }
|
112 | 128 |
|
113 | 129 | @Test
|
114 |
| - public void addIfNotMatching() { |
| 130 | + void addIfNotMatching() { |
115 | 131 | ResolverUtil<VFS> resolverUtil = new ResolverUtil<>();
|
116 | 132 | 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); |
118 | 134 | }
|
119 | 135 |
|
120 | 136 | @Test
|
121 |
| - public void testToString() { |
| 137 | + void testToString() { |
122 | 138 | 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())); |
124 | 140 |
|
125 | 141 | 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 |
127 | 149 | }
|
128 | 150 |
|
129 | 151 | }
|
0 commit comments