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.assertNotNull;
20  import static org.junit.jupiter.api.Assertions.assertTrue;
21  
22  import java.io.File;
23  import java.io.IOException;
24  import java.io.InputStream;
25  import java.io.Reader;
26  import java.net.URL;
27  import java.nio.charset.Charset;
28  import java.util.Properties;
29  
30  import org.apache.ibatis.BaseDataTest;
31  import org.junit.jupiter.api.Assertions;
32  import org.junit.jupiter.api.Test;
33  
34  class ResourcesTest extends BaseDataTest {
35  
36    private static final ClassLoader CLASS_LOADER = ResourcesTest.class.getClassLoader();
37  
38    @Test
39    void shouldGetUrlForResource() throws Exception {
40      URL url = Resources.getResourceURL(JPETSTORE_PROPERTIES);
41      assertTrue(url.toString().endsWith("jpetstore/jpetstore-hsqldb.properties"));
42    }
43  
44    @Test
45    void shouldGetUrlAsProperties() throws Exception {
46      URL url = Resources.getResourceURL(CLASS_LOADER, JPETSTORE_PROPERTIES);
47      Properties props = Resources.getUrlAsProperties(url.toString());
48      assertNotNull(props.getProperty("driver"));
49    }
50  
51    @Test
52    void shouldGetResourceAsProperties() throws Exception {
53      Properties props = Resources.getResourceAsProperties(CLASS_LOADER, JPETSTORE_PROPERTIES);
54      assertNotNull(props.getProperty("driver"));
55    }
56  
57    @Test
58    void shouldGetUrlAsStream() throws Exception {
59      URL url = Resources.getResourceURL(CLASS_LOADER, JPETSTORE_PROPERTIES);
60      try (InputStream in = Resources.getUrlAsStream(url.toString())) {
61        assertNotNull(in);
62      }
63    }
64  
65    @Test
66    void shouldGetUrlAsReader() throws Exception {
67      URL url = Resources.getResourceURL(CLASS_LOADER, JPETSTORE_PROPERTIES);
68      try (Reader in = Resources.getUrlAsReader(url.toString())) {
69        assertNotNull(in);
70      }
71    }
72  
73    @Test
74    void shouldGetResourceAsStream() throws Exception {
75      try (InputStream in = Resources.getResourceAsStream(CLASS_LOADER, JPETSTORE_PROPERTIES)) {
76        assertNotNull(in);
77      }
78    }
79  
80    @Test
81    void shouldGetResourceAsReader() throws Exception {
82      try (Reader in = Resources.getResourceAsReader(CLASS_LOADER, JPETSTORE_PROPERTIES)) {
83        assertNotNull(in);
84      }
85    }
86  
87    @Test
88    void shouldGetResourceAsFile() throws Exception {
89      File file = Resources.getResourceAsFile(JPETSTORE_PROPERTIES);
90      assertTrue(file.getAbsolutePath().replace('\\', '/').endsWith("jpetstore/jpetstore-hsqldb.properties"));
91    }
92  
93    @Test
94    void shouldGetResourceAsFileWithClassloader() throws Exception {
95      File file = Resources.getResourceAsFile(CLASS_LOADER, JPETSTORE_PROPERTIES);
96      assertTrue(file.getAbsolutePath().replace('\\', '/').endsWith("jpetstore/jpetstore-hsqldb.properties"));
97    }
98  
99    @Test
100   void shouldGetResourceAsPropertiesWithOutClassloader() throws Exception {
101     Properties file = Resources.getResourceAsProperties(JPETSTORE_PROPERTIES);
102     assertNotNull(file);
103   }
104 
105   @Test
106   void shouldGetResourceAsPropertiesWithClassloader() throws Exception {
107     Properties file = Resources.getResourceAsProperties(CLASS_LOADER, JPETSTORE_PROPERTIES);
108     assertNotNull(file);
109   }
110 
111   @Test
112   void shouldAllowDefaultClassLoaderToBeSet() {
113     Resources.setDefaultClassLoader(this.getClass().getClassLoader());
114     assertEquals(this.getClass().getClassLoader(), Resources.getDefaultClassLoader());
115   }
116 
117   @Test
118   void shouldAllowDefaultCharsetToBeSet() {
119     Resources.setCharset(Charset.defaultCharset());
120     assertEquals(Charset.defaultCharset(), Resources.getCharset());
121   }
122 
123   @Test
124   void shouldGetClassForName() throws Exception {
125     Class<?> clazz = Resources.classForName(ResourcesTest.class.getName());
126     assertNotNull(clazz);
127   }
128 
129   @Test
130   void shouldNotFindThisClass() {
131     Assertions.assertThrows(ClassNotFoundException.class,
132         () -> Resources.classForName("some.random.class.that.does.not.Exist"));
133   }
134 
135   @Test
136   void shouldGetReader() throws IOException {
137 
138     // save the value
139     Charset charset = Resources.getCharset();
140 
141     // charset
142     Resources.setCharset(Charset.forName("US-ASCII"));
143     assertNotNull(Resources.getResourceAsReader(JPETSTORE_PROPERTIES));
144 
145     // no charset
146     Resources.setCharset(null);
147     assertNotNull(Resources.getResourceAsReader(JPETSTORE_PROPERTIES));
148 
149     // clean up
150     Resources.setCharset(charset);
151 
152   }
153 
154   @Test
155   void shouldGetReaderWithClassLoader() throws IOException {
156 
157     // save the value
158     Charset charset = Resources.getCharset();
159 
160     // charset
161     Resources.setCharset(Charset.forName("US-ASCII"));
162     assertNotNull(Resources.getResourceAsReader(getClass().getClassLoader(), JPETSTORE_PROPERTIES));
163 
164     // no charset
165     Resources.setCharset(null);
166     assertNotNull(Resources.getResourceAsReader(getClass().getClassLoader(), JPETSTORE_PROPERTIES));
167 
168     // clean up
169     Resources.setCharset(charset);
170 
171   }
172 
173 }