diff --git a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/LogMessageIdFormat.java b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/LogMessageIdFormat.java index 161770836..3116ed936 100644 --- a/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/LogMessageIdFormat.java +++ b/openhtmltopdf-core/src/main/java/com/openhtmltopdf/util/LogMessageIdFormat.java @@ -5,7 +5,7 @@ class LogMessageIdFormat { - private final Object PLACEHOLDER = new Object(); + private static final Object PLACEHOLDER = new Object(); private final List tokens; @@ -34,12 +34,13 @@ private List prepareFormatter(String messageFormat) { String formatMessage(Object[] args) { StringBuilder sb = new StringBuilder(); + int argsLength = args == null ? 0 : args.length; int size = tokens.size(); int argsUse = 0; for (int i = 0; i < size; i++) { Object f = tokens.get(i); if (f == PLACEHOLDER) { - Object argument = args[argsUse]; + Object argument = argsUse < argsLength ? args[argsUse] : ""; sb.append(argument); argsUse++; } else { diff --git a/openhtmltopdf-core/src/test/java/com/openhtmltopdf/util/LogMessageIdFormatTest.java b/openhtmltopdf-core/src/test/java/com/openhtmltopdf/util/LogMessageIdFormatTest.java new file mode 100644 index 000000000..584292ea2 --- /dev/null +++ b/openhtmltopdf-core/src/test/java/com/openhtmltopdf/util/LogMessageIdFormatTest.java @@ -0,0 +1,21 @@ +package com.openhtmltopdf.util; + +import org.junit.Assert; +import org.junit.Test; + +public class LogMessageIdFormatTest { + + @Test + public void testFormats() { + Assert.assertEquals("", new LogMessageIdFormat("").formatMessage(new Integer[]{1,2,3})); + Assert.assertEquals("abcd", new LogMessageIdFormat("abcd").formatMessage(null)); + Assert.assertEquals("1a2b3", new LogMessageIdFormat("1{}2{}3").formatMessage(new String[]{"a", "b"})); + Assert.assertEquals("123", new LogMessageIdFormat("{}{}{}").formatMessage(new Integer[]{1,2,3})); + Assert.assertEquals("A1a2b3B", new LogMessageIdFormat("{}1{}2{}3{}").formatMessage(new String[]{"A", "a", "b", "B"})); + + Assert.assertEquals("A123", new LogMessageIdFormat("{}1{}2{}3{}").formatMessage(new String[]{"A"})); + + Assert.assertEquals("", new LogMessageIdFormat("{}{}{}").formatMessage(new Integer[]{})); + Assert.assertEquals("", new LogMessageIdFormat("{}{}{}").formatMessage(null)); + } +}