From 2e7f95b3234e16db1915ed9678456494659fd86e Mon Sep 17 00:00:00 2001 From: DmitriyMX Date: Thu, 7 Oct 2021 14:52:29 +0300 Subject: [PATCH] test: LoggerFormatter --- .../java/ghast/logger/LoggerFormatter.java | 4 - .../ghast/logger/LoggerFormatterTest.java | 84 +++++++++++++++++++ 2 files changed, 84 insertions(+), 4 deletions(-) create mode 100644 src/test/java/ghast/logger/LoggerFormatterTest.java diff --git a/src/main/java/ghast/logger/LoggerFormatter.java b/src/main/java/ghast/logger/LoggerFormatter.java index 759498f..08ad31f 100644 --- a/src/main/java/ghast/logger/LoggerFormatter.java +++ b/src/main/java/ghast/logger/LoggerFormatter.java @@ -43,10 +43,6 @@ public final class LoggerFormatter { } private static Object[] trimmedCopy(final Object[] argArray) { - if (argArray == null || argArray.length == 0) { - throw new IllegalStateException("non-sensical empty or null argument array"); - } - int trimmedLen = argArray.length - 1; Object[] trimmed = new Object[trimmedLen]; diff --git a/src/test/java/ghast/logger/LoggerFormatterTest.java b/src/test/java/ghast/logger/LoggerFormatterTest.java new file mode 100644 index 0000000..60fe5c7 --- /dev/null +++ b/src/test/java/ghast/logger/LoggerFormatterTest.java @@ -0,0 +1,84 @@ +package ghast.logger; + +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class LoggerFormatterTest { + + @Test + void arrayFormatNullPattern() { + Exception exception = new Exception("oops!"); + FormattingPair formattingPair = LoggerFormatter.arrayFormat(null, null, exception); + + assertNotNull(formattingPair); + assertNull(formattingPair.getMessage()); + assertNotNull(formattingPair.getThrowable()); + assertEquals(exception, formattingPair.getThrowable()); + + formattingPair = LoggerFormatter.arrayFormat(null, null, null); + assertNotNull(formattingPair); + assertNull(formattingPair.getMessage()); + assertNull(formattingPair.getThrowable()); + } + + @Test + void arrayFormatNullArgs() { + FormattingPair formattingPair = LoggerFormatter.arrayFormat("some pattern", null, null); + + assertNotNull(formattingPair); + assertNotNull(formattingPair.getMessage()); + assertEquals("some pattern", formattingPair.getMessage()); + assertNull(formattingPair.getThrowable()); + + formattingPair = LoggerFormatter.arrayFormat("some pattern {}", null, null); + assertNotNull(formattingPair); + assertNotNull(formattingPair.getMessage()); + assertEquals("some pattern {}", formattingPair.getMessage()); + assertNull(formattingPair.getThrowable()); + } + + @Test + void arrayFormat() { + Exception exception = new Exception("oops!"); + FormattingPair formattingPair = LoggerFormatter.arrayFormat("some pattern {}", new Object[]{"item-1"}, exception); + + assertNotNull(formattingPair); + assertNotNull(formattingPair.getMessage()); + assertEquals("some pattern item-1", formattingPair.getMessage()); + assertNotNull(formattingPair.getThrowable()); + assertEquals(exception, formattingPair.getThrowable()); + } + + @Test + void arrayFormatWithoutThrowable() { + FormattingPair formattingPair = LoggerFormatter.arrayFormat("Arg1: {}", new Object[]{ "item-1" }); + + assertNotNull(formattingPair); + assertNotNull(formattingPair.getMessage()); + assertEquals("Arg1: item-1", formattingPair.getMessage()); + assertNull(formattingPair.getThrowable()); + } + + @Test + void arrayFormatWithoutThrowableNullArgs() { + FormattingPair formattingPair = LoggerFormatter.arrayFormat("Arg1: {}", null); + + assertNotNull(formattingPair); + assertNotNull(formattingPair.getMessage()); + assertEquals("Arg1: {}", formattingPair.getMessage()); + assertNull(formattingPair.getThrowable()); + } + + @Test + void arrayFormatThrowableInArgs() { + Exception exception = new Exception("oops!"); + FormattingPair formattingPair = LoggerFormatter.arrayFormat("Arg1: {}", new Object[]{ "item-1", exception }); + + assertNotNull(formattingPair); + assertNotNull(formattingPair.getMessage()); + assertEquals("Arg1: item-1", formattingPair.getMessage()); + assertNotNull(formattingPair.getThrowable()); + assertEquals(exception, formattingPair.getThrowable()); + } +} \ No newline at end of file