AdvancedMessageFormatter extends AbstractMessageFormatter { @Override public String format(final String message, final Object[] arguments) { int length = message.length(); StringBuilder builder = new StringBuilder(length + ADDITIONAL_STRING_BUILDER_CAPACITY); StringBuilder buffer = new StringBuilder(length + ADDITIONAL_STRING_BUILDER_CAPACITY); StringBuilder current = builder; int argumentIndex = 0; int openingTickIndex = -1; int openingCurlyBrackets = 0; for (int index = 0; index < length; ++index) { char character = message.charAt(index); if (escape && character == '\'' && index + 1 < length && openingCurlyBrackets == 0) { if (message.charAt(index + 1) == '\'') { current.append('\''); index += 1; } else { openingTickIndex = openingTickIndex < 0 ? index : -1; } } else if (character == '{' && index + 1 < length && argumentIndex < arguments.length && openingTickIndex < 0) { if (openingCurlyBrackets++ == 0) { current = buffer; } else { current.append(character); } } else if (character == '}' && openingCurlyBrackets > 0 && openingTickIndex < 0) { if (--openingCurlyBrackets == 0) { Object argument = resolve(arguments[argumentIndex++]); if (buffer.length() == 0) { builder.append(argument); } else { builder.append(format(buffer.toString(), argument)); buffer.setLength(0); } buffer.setLength(0); current = builder; } else { current.append(character); } } else { current.append(character); } } if (buffer.length() > 0) { builder.append('{'); builder.append(buffer); } if (openingTickIndex >= 0) { builder.insert(openingTickIndex, '\''); } return builder.toString(); } AdvancedMessageFormatter(final Locale locale, final boolean escape); @Override String format(final String message, final Object[] arguments);  }
@Test public void withoutPlaceholders() { assertThat(format("Hello World!")).isEqualTo("Hello World!"); }
@Test public void onlyPlaceholder() { assertThat(format("{}", 42)).isEqualTo("42"); }
@Test public void singlePlaceholder() { assertThat(format("Hello {}!", "tinylog")).isEqualTo("Hello tinylog!"); }
@Test public void multiplePlaceholders() { assertThat(format("{} + {} = {}", 1, 2, 3)).isEqualTo("1 + 2 = 3"); }
@Test public void lazyArgumentSupplier() { Supplier<Integer> supplier = () -> 42; assertThat(format("It is {}", supplier)).isEqualTo("It is 42"); }
@Test public void choiceFormat() { assertThat(format("{0#zero|1#one|1<multiple}", 0)).isEqualTo("zero"); assertThat(format("{0#zero|1#one|1<multiple}", 1)).isEqualTo("one"); assertThat(format("{0#zero|1#one|1<multiple}", 2)).isEqualTo("multiple"); }
@Test public void numberFormat() { assertThat(format(Locale.US, "{0.00}", 1)).isEqualTo("1.00"); assertThat(format(Locale.GERMANY, "{0.00}", 1)).isEqualTo("1,00"); }
@Test public void choiceAndNumberFormat() { assertThat(format("{0#zero|1#one|1<{000}}", 0)).isEqualTo("zero"); assertThat(format("{0#zero|1#one|1<{000}}", 42)).isEqualTo("042"); }
@Test public void tooManyArguments() { assertThat(format("Hello {}!", "tinylog", "world")).isEqualTo("Hello tinylog!"); }
@Test public void tooFewArguments() { assertThat(format("Hello {}!")).isEqualTo("Hello {}!"); }
@Test public void missingOpeningCurlyBracket() { assertThat(format("} Hello", "tinylog")).isEqualTo("} Hello"); assertThat(format("Hello }", "tinylog")).isEqualTo("Hello }"); }
@Test public void missingClosingCurlyBracket() { assertThat(format("{ Hello", "tinylog")).isEqualTo("{ Hello"); assertThat(format("Hello {", "tinylog")).isEqualTo("Hello {"); }
@Test public void ignoreEscapedPlaceholders() { assumeThat(escape).isTrue(); assertThat(format("'{foo}' {}", "bar")).isEqualTo("{foo} bar"); }
@Test public void resolvePlaceholderWithinTicks() { assumeThat(escape).isFalse(); assertThat(format("'{}'", "foo")).isEqualTo("'foo'"); }
@Test public void convertDoubleTicks() { assumeThat(escape).isTrue(); assertThat(format("this <''> is a single tick")).isEqualTo("this <'> is a single tick"); }
@Test public void keepDoubleTicks() { assumeThat(escape).isFalse(); assertThat(format("these <''> are double ticks")).isEqualTo("these <''> are double ticks"); }
@Test public void keepSingleTicks() { assertThat(format("this <'> is a single tick")).isEqualTo("this <'> is a single tick"); }
@Test public void illegalChoiceFormat() { assertThat(format("Test {#|}!", 42)).isEqualTo("Test 42!"); assertThat(systemStream.consumeErrorOutput()).containsOnlyOnce("WARN").containsOnlyOnce("#|"); }
@Test public void illegalNumberFormat() { assertThat(format("Test {#..#}!", 42)).isEqualTo("Test 42!"); assertThat(systemStream.consumeErrorOutput()).containsOnlyOnce("WARN").containsOnlyOnce("#..#"); }