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.logging;
17  
18  import static org.junit.jupiter.api.Assertions.assertEquals;
19  
20  import java.io.Reader;
21  
22  import org.apache.ibatis.io.Resources;
23  import org.apache.ibatis.logging.commons.JakartaCommonsLoggingImpl;
24  import org.apache.ibatis.logging.jdk14.Jdk14LoggingImpl;
25  import org.apache.ibatis.logging.log4j.Log4jImpl;
26  import org.apache.ibatis.logging.log4j2.Log4j2Impl;
27  import org.apache.ibatis.logging.nologging.NoLoggingImpl;
28  import org.apache.ibatis.logging.slf4j.Slf4jImpl;
29  import org.apache.ibatis.logging.stdout.StdOutImpl;
30  import org.apache.ibatis.session.SqlSessionFactoryBuilder;
31  import org.junit.jupiter.api.AfterAll;
32  import org.junit.jupiter.api.Test;
33  
34  class LogFactoryTest {
35  
36    @AfterAll
37    static void restore() {
38      LogFactory.useSlf4jLogging();
39    }
40  
41    @Test
42    void shouldUseCommonsLogging() {
43      LogFactory.useCommonsLogging();
44      Log log = LogFactory.getLog(Object.class);
45      logSomething(log);
46      assertEquals(log.getClass().getName(), JakartaCommonsLoggingImpl.class.getName());
47    }
48  
49    @Test
50    void shouldUseLog4J() {
51      LogFactory.useLog4JLogging();
52      Log log = LogFactory.getLog(Object.class);
53      logSomething(log);
54      assertEquals(log.getClass().getName(), Log4jImpl.class.getName());
55    }
56  
57    @Test
58    void shouldUseLog4J2() {
59      LogFactory.useLog4J2Logging();
60      Log log = LogFactory.getLog(Object.class);
61      logSomething(log);
62      assertEquals(log.getClass().getName(), Log4j2Impl.class.getName());
63    }
64  
65    @Test
66    void shouldUseJdKLogging() {
67      LogFactory.useJdkLogging();
68      Log log = LogFactory.getLog(Object.class);
69      logSomething(log);
70      assertEquals(log.getClass().getName(), Jdk14LoggingImpl.class.getName());
71    }
72  
73    @Test
74    void shouldUseSlf4j() {
75      LogFactory.useSlf4jLogging();
76      Log log = LogFactory.getLog(Object.class);
77      logSomething(log);
78      assertEquals(log.getClass().getName(), Slf4jImpl.class.getName());
79    }
80  
81    @Test
82    void shouldUseStdOut() {
83      LogFactory.useStdOutLogging();
84      Log log = LogFactory.getLog(Object.class);
85      logSomething(log);
86      assertEquals(log.getClass().getName(), StdOutImpl.class.getName());
87    }
88  
89    @Test
90    void shouldUseNoLogging() {
91      LogFactory.useNoLogging();
92      Log log = LogFactory.getLog(Object.class);
93      logSomething(log);
94      assertEquals(log.getClass().getName(), NoLoggingImpl.class.getName());
95    }
96  
97    @Test
98    void shouldReadLogImplFromSettings() throws Exception {
99      try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/logging/mybatis-config.xml")) {
100       new SqlSessionFactoryBuilder().build(reader);
101     }
102 
103     Log log = LogFactory.getLog(Object.class);
104     log.debug("Debug message.");
105     assertEquals(log.getClass().getName(), NoLoggingImpl.class.getName());
106   }
107 
108   private void logSomething(Log log) {
109     log.warn("Warning message.");
110     log.debug("Debug message.");
111     log.error("Error message.");
112     log.error("Error with Exception.", new Exception("Test exception."));
113   }
114 
115 }