1
2
3
4
5
6
7
8
9
10
11
12
13
14
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.Test;
32
33 class LogFactoryTest {
34
35 @Test
36 void shouldUseCommonsLogging() {
37 LogFactory.useCommonsLogging();
38 Log log = LogFactory.getLog(Object.class);
39 logSomething(log);
40 assertEquals(log.getClass().getName(), JakartaCommonsLoggingImpl.class.getName());
41 }
42
43 @Test
44 void shouldUseLog4J() {
45 LogFactory.useLog4JLogging();
46 Log log = LogFactory.getLog(Object.class);
47 logSomething(log);
48 assertEquals(log.getClass().getName(), Log4jImpl.class.getName());
49 }
50
51 @Test
52 void shouldUseLog4J2() {
53 LogFactory.useLog4J2Logging();
54 Log log = LogFactory.getLog(Object.class);
55 logSomething(log);
56 assertEquals(log.getClass().getName(), Log4j2Impl.class.getName());
57 }
58
59 @Test
60 void shouldUseJdKLogging() {
61 LogFactory.useJdkLogging();
62 Log log = LogFactory.getLog(Object.class);
63 logSomething(log);
64 assertEquals(log.getClass().getName(), Jdk14LoggingImpl.class.getName());
65 }
66
67 @Test
68 void shouldUseSlf4j() {
69 LogFactory.useSlf4jLogging();
70 Log log = LogFactory.getLog(Object.class);
71 logSomething(log);
72 assertEquals(log.getClass().getName(), Slf4jImpl.class.getName());
73 }
74
75 @Test
76 void shouldUseStdOut() {
77 LogFactory.useStdOutLogging();
78 Log log = LogFactory.getLog(Object.class);
79 logSomething(log);
80 assertEquals(log.getClass().getName(), StdOutImpl.class.getName());
81 }
82
83 @Test
84 void shouldUseNoLogging() {
85 LogFactory.useNoLogging();
86 Log log = LogFactory.getLog(Object.class);
87 logSomething(log);
88 assertEquals(log.getClass().getName(), NoLoggingImpl.class.getName());
89 }
90
91 @Test
92 void shouldReadLogImplFromSettings() throws Exception {
93 try (Reader reader = Resources.getResourceAsReader("org/apache/ibatis/logging/mybatis-config.xml")) {
94 new SqlSessionFactoryBuilder().build(reader);
95 }
96
97 Log log = LogFactory.getLog(Object.class);
98 log.debug("Debug message.");
99 assertEquals(log.getClass().getName(), NoLoggingImpl.class.getName());
100 }
101
102 private void logSomething(Log log) {
103 log.warn("Warning message.");
104 log.debug("Debug message.");
105 log.error("Error message.");
106 log.error("Error with Exception.", new Exception("Test exception."));
107 }
108
109 }