forked from mattbertolini/liquibase-slf4j
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSlf4jLoggerTest.java
298 lines (253 loc) · 9.79 KB
/
Slf4jLoggerTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*
* Copyright (c) 2012-2020 Matt Bertolini
*
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
* documentation files (the "Software"), to deal in the Software without restriction, including without limitation the
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
* Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS
* OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
* OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
package com.mattbertolini.liquibase.logging.slf4j;
import liquibase.logging.LogMessageFilter;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import java.util.logging.Level;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
* @author Matt Bertolini
*/
class Slf4jLoggerTest {
private Logger delegate;
private LogMessageFilter filter;
private Slf4jLogger logger;
@BeforeEach
void setUp() {
delegate = mock(Logger.class);
filter = mock(LogMessageFilter.class);
logger = new Slf4jLogger(delegate, filter);
}
// log method
@Test
void logWithSevereLevel() {
String message = "severe test";
logger.log(Level.SEVERE, message, null);
verify(delegate).error(message, (Throwable) null);
}
@Test
void logWithWarningLevel() {
String message = "warning test";
logger.log(Level.WARNING, message, null);
verify(delegate).warn(message, (Throwable) null);
}
@Test
void logWithInfoLevel() {
String message = "info test";
logger.log(Level.INFO, message, null);
verify(delegate).info(message, (Throwable) null);
}
@Test
void logWithConfigLevel() {
String message = "config test";
logger.log(Level.CONFIG, message, null);
verify(delegate).info(message, (Throwable) null);
}
@Test
void logWithFineLevel() {
String message = "fine test";
logger.log(Level.FINE, message, null);
verify(delegate).debug(message, (Throwable) null);
}
@Test
void logWithFinestLevel() {
String message = "finest test";
logger.log(Level.FINEST, message, null);
verify(delegate).trace(message, (Throwable) null);
}
// Severe level
@Test
void logsSevereWithMessage() {
String expected = "This is a severe test.";
when(delegate.isErrorEnabled()).thenReturn(true);
logger.severe(expected);
verify(delegate).error(expected);
}
@Test
void logsSevereWithMessageAndThrowable() {
RuntimeException exception = new RuntimeException("Exception");
String expected = "This is a severe test.";
when(delegate.isErrorEnabled()).thenReturn(true);
logger.severe(expected, exception);
verify(delegate).error(expected, exception);
}
@Test
void doesNotLogSevereWhenLogLevelDisabled() {
when(delegate.isErrorEnabled()).thenReturn(false);
logger.severe("do not log");
verify(delegate, never()).error(anyString());
}
@Test
void doesNotLogSevereWithThrowableWhenLogLevelDisabled() {
RuntimeException exception = new RuntimeException("Exception");
when(delegate.isErrorEnabled()).thenReturn(false);
logger.severe("do not log", exception);
verify(delegate, never()).error(anyString(), any(Throwable.class));
}
// Warning level
@Test
void logsWarningWithMessage() {
String expected = "This is a warning test.";
when(delegate.isWarnEnabled()).thenReturn(true);
logger.warning(expected);
verify(delegate).warn(expected);
}
@Test
void logsWarningWithMessageAndThrowable() {
RuntimeException exception = new RuntimeException("Exception");
String expected = "This is a warning test.";
when(delegate.isWarnEnabled()).thenReturn(true);
logger.warning(expected, exception);
verify(delegate).warn(expected, exception);
}
@Test
void doesNotLogWarningWhenLogLevelDisabled() {
when(delegate.isWarnEnabled()).thenReturn(false);
logger.warning("do not log");
verify(delegate, never()).warn(anyString());
}
@Test
void doesNotLogWarningWithThrowableWhenLogLevelDisabled() {
RuntimeException exception = new RuntimeException("Exception");
when(delegate.isWarnEnabled()).thenReturn(false);
logger.warning("do not log", exception);
verify(delegate, never()).warn(anyString(), any(Throwable.class));
}
// Info level
@Test
void logsInfoWithMessage() {
String expected = "This is a info test.";
when(delegate.isInfoEnabled()).thenReturn(true);
logger.info(expected);
verify(delegate).info(expected);
}
@Test
void logsInfoWithMessageAndThrowable() {
RuntimeException exception = new RuntimeException("Exception");
String expected = "This is a info test.";
when(delegate.isInfoEnabled()).thenReturn(true);
logger.info(expected, exception);
verify(delegate).info(expected, exception);
}
@Test
void doesNotLogInfoWhenLogLevelDisabled() {
when(delegate.isInfoEnabled()).thenReturn(false);
logger.info("do not log");
verify(delegate, never()).info(anyString());
}
@Test
void doesNotLogInfoWithThrowableWhenLogLevelDisabled() {
RuntimeException exception = new RuntimeException("Exception");
when(delegate.isInfoEnabled()).thenReturn(false);
logger.info("do not log", exception);
verify(delegate, never()).info(anyString(), any(Throwable.class));
}
// Config level
@Test
void logsConfigWithMessage() {
String expected = "This is a config test.";
when(delegate.isInfoEnabled()).thenReturn(true);
logger.config(expected);
verify(delegate).info(expected);
}
@Test
void logsConfigWithMessageAndThrowable() {
RuntimeException exception = new RuntimeException("Exception");
String expected = "This is a config test.";
when(delegate.isInfoEnabled()).thenReturn(true);
logger.config(expected, exception);
verify(delegate).info(expected, exception);
}
@Test
void doesNotLogConfigWhenLogLevelDisabled() {
when(delegate.isInfoEnabled()).thenReturn(false);
logger.config("do not log");
verify(delegate, never()).info(anyString());
}
@Test
void doesNotLogConfigWithThrowableWhenLogLevelDisabled() {
RuntimeException exception = new RuntimeException("Exception");
when(delegate.isInfoEnabled()).thenReturn(false);
logger.config("do not log", exception);
verify(delegate, never()).info(anyString(), any(Throwable.class));
}
// Fine level
@Test
void logsFineWithMessage() {
String expected = "This is a fine test.";
when(delegate.isDebugEnabled()).thenReturn(true);
logger.fine(expected);
verify(delegate).debug(expected);
}
@Test
void logsFineWithMessageAndThrowable() {
RuntimeException exception = new RuntimeException("Exception");
String expected = "This is a fine test.";
when(delegate.isDebugEnabled()).thenReturn(true);
logger.fine(expected, exception);
verify(delegate).debug(expected, exception);
}
@Test
void doesNotLogFineWhenLogLevelDisabled() {
when(delegate.isDebugEnabled()).thenReturn(false);
logger.fine("do not log");
verify(delegate, never()).debug(anyString());
}
@Test
void doesNotLogFineWithThrowableWhenLogLevelDisabled() {
RuntimeException exception = new RuntimeException("Exception");
when(delegate.isDebugEnabled()).thenReturn(false);
logger.fine("do not log", exception);
verify(delegate, never()).debug(anyString(), any(Throwable.class));
}
// Debug level (deprecated). Delegates to fine
@Test
void logsDebugWithMessage() {
String expected = "This is a debug test.";
when(delegate.isDebugEnabled()).thenReturn(true);
logger.debug(expected);
verify(delegate).debug(expected);
}
@Test
void logsDebugWithMessageAndThrowable() {
RuntimeException exception = new RuntimeException("Exception");
String expected = "This is a debug test.";
when(delegate.isDebugEnabled()).thenReturn(true);
logger.debug(expected, exception);
verify(delegate).debug(expected, exception);
}
@Test
void doesNotLogDebugWhenLogLevelDisabled() {
when(delegate.isDebugEnabled()).thenReturn(false);
logger.debug("do not log");
verify(delegate, never()).debug(anyString());
}
@Test
void doesNotLogDebugWithThrowableWhenLogLevelDisabled() {
RuntimeException exception = new RuntimeException("Exception");
when(delegate.isDebugEnabled()).thenReturn(false);
logger.debug("do not log", exception);
verify(delegate, never()).debug(anyString(), any(Throwable.class));
}
}