View Javadoc
1   /*
2    *    Copyright 2009-2023 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    *       https://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.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   * Unit tests for {@link ResolverUtil}.
34   *
35   * @author Pi Chen
36   *
37   * @since 3.5.2
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      // org.apache.ibatis.io.VFS
82      // org.apache.ibatis.io.DefaultVFS
83      // org.apache.ibatis.io.JBoss6VFS
84      assertEquals(classSets.size(), 3); // fail if add a new VFS implementation in this package!!!
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     // org.apache.ibatis.io.ResolverUtilTest.TestMapper
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     // org.apache.ibatis.io.VFS
111     // org.apache.ibatis.io.DefaultVFS
112     // org.apache.ibatis.io.JBoss6VFS
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     // test ResolverUtil.findAnnotated method
153   }
154 
155 }