View Javadoc
1   /*
2    *    Copyright 2009-2022 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.assertNotNull;
19  import static org.junit.jupiter.api.Assertions.assertNull;
20  
21  import org.apache.ibatis.BaseDataTest;
22  import org.junit.jupiter.api.Assertions;
23  import org.junit.jupiter.api.BeforeEach;
24  import org.junit.jupiter.api.Test;
25  
26  class ClassLoaderWrapperTest extends BaseDataTest {
27  
28    private ClassLoaderWrapper wrapper;
29    private ClassLoader loader;
30    private final String RESOURCE_NOT_FOUND = "some_resource_that_does_not_exist.properties";
31    private final String CLASS_NOT_FOUND = "some.random.class.that.does.not.Exist";
32    private final String CLASS_FOUND = "java.lang.Object";
33  
34    @BeforeEach
35    void beforeClassLoaderWrapperTest() {
36      wrapper = new ClassLoaderWrapper();
37      loader = getClass().getClassLoader();
38    }
39  
40    @Test
41    void classForName() throws ClassNotFoundException {
42      assertNotNull(wrapper.classForName(CLASS_FOUND));
43    }
44  
45    @Test
46    void classForNameNotFound() {
47      Assertions.assertThrows(ClassNotFoundException.class, () -> assertNotNull(wrapper.classForName(CLASS_NOT_FOUND)));
48    }
49  
50    @Test
51    void classForNameWithClassLoader() throws ClassNotFoundException {
52      assertNotNull(wrapper.classForName(CLASS_FOUND, loader));
53    }
54  
55    @Test
56    void getResourceAsURL() {
57      assertNotNull(wrapper.getResourceAsURL(JPETSTORE_PROPERTIES));
58    }
59  
60    @Test
61    void getResourceAsURLNotFound() {
62      assertNull(wrapper.getResourceAsURL(RESOURCE_NOT_FOUND));
63    }
64  
65    @Test
66    void getResourceAsURLWithClassLoader() {
67      assertNotNull(wrapper.getResourceAsURL(JPETSTORE_PROPERTIES, loader));
68    }
69  
70    @Test
71    void getResourceAsStream() {
72      assertNotNull(wrapper.getResourceAsStream(JPETSTORE_PROPERTIES));
73    }
74  
75    @Test
76    void getResourceAsStreamNotFound() {
77      assertNull(wrapper.getResourceAsStream(RESOURCE_NOT_FOUND));
78    }
79  
80    @Test
81    void getResourceAsStreamWithClassLoader() {
82      assertNotNull(wrapper.getResourceAsStream(JPETSTORE_PROPERTIES, loader));
83    }
84  
85  }