|
32 | 32 | import java.util.stream.Stream; |
33 | 33 |
|
34 | 34 | import static org.junit.jupiter.api.Assertions.assertEquals; |
| 35 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
35 | 36 |
|
36 | 37 | public final class SSLoggingTest |
37 | 38 | { |
@@ -172,6 +173,7 @@ public Stream<DynamicTest> testLoggingWithException() |
172 | 173 |
|
173 | 174 | final var event = logger.events.remove(); |
174 | 175 | assertEquals(level, event.level); |
| 176 | + assertTrue(event.messagePattern.endsWith("CODE=ErrorCodeName REMEDIATING=Some action. {}: ")); |
175 | 177 | System.out.println(event); |
176 | 178 | }); |
177 | 179 | }); |
@@ -209,6 +211,79 @@ public Stream<DynamicTest> testLoggingWithExceptionStyle() |
209 | 211 |
|
210 | 212 | final var event = logger.events.remove(); |
211 | 213 | assertEquals(level, event.level); |
| 214 | + assertTrue(event.messagePattern.endsWith("CODE=ErrorCodeName REMEDIATING=Some action. {}")); |
| 215 | + System.out.println(event); |
| 216 | + }); |
| 217 | + }); |
| 218 | + } |
| 219 | + |
| 220 | + @TestFactory |
| 221 | + public Stream<DynamicTest> testLoggingWithStyle() |
| 222 | + { |
| 223 | + final var logger = |
| 224 | + new CapturingLogger(); |
| 225 | + |
| 226 | + return Stream.of(Level.values()) |
| 227 | + .map(level -> { |
| 228 | + return DynamicTest.dynamicTest( |
| 229 | + "testLogging_%s".formatted(level), |
| 230 | + () -> { |
| 231 | + SSLogging.logMDCWithStyle( |
| 232 | + logger, |
| 233 | + level, |
| 234 | + SSLogging.MessageStyle.STYLE_MESSAGE_ONLY, |
| 235 | + new SStructuredError<>( |
| 236 | + "ErrorCodeName", |
| 237 | + "A message.", |
| 238 | + Map.ofEntries( |
| 239 | + Map.entry("a", "x"), |
| 240 | + Map.entry("b", "y"), |
| 241 | + Map.entry("c", "z") |
| 242 | + ), |
| 243 | + Optional.of("Some action."), |
| 244 | + Optional.of(new IOException("Printer on fire.")) |
| 245 | + ) |
| 246 | + ); |
| 247 | + |
| 248 | + final var event = logger.events.remove(); |
| 249 | + assertEquals(level, event.level); |
| 250 | + assertTrue(event.messagePattern.endsWith("ErrorCode=ErrorCodeName RemediatingAction=Some action. {}")); |
| 251 | + System.out.println(event); |
| 252 | + }); |
| 253 | + }); |
| 254 | + } |
| 255 | + |
| 256 | + @TestFactory |
| 257 | + public Stream<DynamicTest> testLoggingWithStyleWithoutException() |
| 258 | + { |
| 259 | + final var logger = |
| 260 | + new CapturingLogger(); |
| 261 | + |
| 262 | + return Stream.of(Level.values()) |
| 263 | + .map(level -> { |
| 264 | + return DynamicTest.dynamicTest( |
| 265 | + "testLogging_%s".formatted(level), |
| 266 | + () -> { |
| 267 | + SSLogging.logMDCWithStyle( |
| 268 | + logger, |
| 269 | + level, |
| 270 | + SSLogging.MessageStyle.STYLE_MESSAGE_ONLY, |
| 271 | + new SStructuredError<>( |
| 272 | + "ErrorCodeName", |
| 273 | + "A message.", |
| 274 | + Map.ofEntries( |
| 275 | + Map.entry("a", "x"), |
| 276 | + Map.entry("b", "y"), |
| 277 | + Map.entry("c", "z") |
| 278 | + ), |
| 279 | + Optional.of("Some action."), |
| 280 | + Optional.empty() |
| 281 | + ) |
| 282 | + ); |
| 283 | + |
| 284 | + final var event = logger.events.remove(); |
| 285 | + assertEquals(level, event.level); |
| 286 | + assertTrue(event.messagePattern.endsWith("ErrorCode=ErrorCodeName RemediatingAction=Some action. {}")); |
212 | 287 | System.out.println(event); |
213 | 288 | }); |
214 | 289 | }); |
@@ -243,6 +318,7 @@ public Stream<DynamicTest> testLoggingWithoutException() |
243 | 318 |
|
244 | 319 | final var event = logger.events.remove(); |
245 | 320 | assertEquals(level, event.level); |
| 321 | + assertTrue(event.messagePattern.endsWith("ErrorCode=ErrorCodeName RemediatingAction=Some action. {}")); |
246 | 322 | System.out.println(event); |
247 | 323 | }); |
248 | 324 | }); |
|
0 commit comments