First-class support for printf-style format strings in LogMessage

LogMessage is an abstract class now: with internal subclasses for Supplier bindings as well as printf-style format strings with a variable number of arguments (some fixed for efficiency, varargs array as fallback), created through corresponding static factory methods.

Closes gh-22726
This commit is contained in:
Juergen Hoeller
2019-04-04 01:52:51 +02:00
parent 5616eb2e8e
commit 9080ae24f9
3 changed files with 263 additions and 29 deletions

View File

@@ -27,9 +27,44 @@ import static org.junit.Assert.*;
public class LogSupportTests {
@Test
public void testLogMessage() {
LogMessage msg = new LogMessage(() -> new StringBuilder("a"));
assertEquals("a", msg.toString());
public void testLogMessageWithSupplier() {
LogMessage msg = LogMessage.lazy(() -> new StringBuilder("a").append(" b"));
assertEquals("a b", msg.toString());
assertSame(msg.toString(), msg.toString());
}
@Test
public void testLogMessageWithFormat1() {
LogMessage msg = LogMessage.format("a %s", "b");
assertEquals("a b", msg.toString());
assertSame(msg.toString(), msg.toString());
}
@Test
public void testLogMessageWithFormat2() {
LogMessage msg = LogMessage.format("a %s %s", "b", "c");
assertEquals("a b c", msg.toString());
assertSame(msg.toString(), msg.toString());
}
@Test
public void testLogMessageWithFormat3() {
LogMessage msg = LogMessage.format("a %s %s %s", "b", "c", "d");
assertEquals("a b c d", msg.toString());
assertSame(msg.toString(), msg.toString());
}
@Test
public void testLogMessageWithFormat4() {
LogMessage msg = LogMessage.format("a %s %s %s %s", "b", "c", "d", "e");
assertEquals("a b c d e", msg.toString());
assertSame(msg.toString(), msg.toString());
}
@Test
public void testLogMessageWithFormatX() {
LogMessage msg = LogMessage.format("a %s %s %s %s %s", "b", "c", "d", "e", "f");
assertEquals("a b c d e f", msg.toString());
assertSame(msg.toString(), msg.toString());
}