Skip to content

Commit 1504459

Browse files
committed
Improve tests.
1 parent b14a177 commit 1504459

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

com.io7m.seltzer.tests/src/main/java/com/io7m/seltzer/tests/SSLoggingTest.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import java.util.stream.Stream;
3333

3434
import static org.junit.jupiter.api.Assertions.assertEquals;
35+
import static org.junit.jupiter.api.Assertions.assertTrue;
3536

3637
public final class SSLoggingTest
3738
{
@@ -172,6 +173,7 @@ public Stream<DynamicTest> testLoggingWithException()
172173

173174
final var event = logger.events.remove();
174175
assertEquals(level, event.level);
176+
assertTrue(event.messagePattern.endsWith("CODE=ErrorCodeName REMEDIATING=Some action. {}: "));
175177
System.out.println(event);
176178
});
177179
});
@@ -209,6 +211,79 @@ public Stream<DynamicTest> testLoggingWithExceptionStyle()
209211

210212
final var event = logger.events.remove();
211213
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. {}"));
212287
System.out.println(event);
213288
});
214289
});
@@ -243,6 +318,7 @@ public Stream<DynamicTest> testLoggingWithoutException()
243318

244319
final var event = logger.events.remove();
245320
assertEquals(level, event.level);
321+
assertTrue(event.messagePattern.endsWith("ErrorCode=ErrorCodeName RemediatingAction=Some action. {}"));
246322
System.out.println(event);
247323
});
248324
});

0 commit comments

Comments
 (0)