Java Code Examples for org.apache.logging.log4j.util.Strings#repeat()

The following examples show how to use org.apache.logging.log4j.util.Strings#repeat() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: JsonWriterTest.java    From logging-log4j2 with Apache License 2.0 6 votes vote down vote up
@Test
public void test_writeString_formattable_excessive_string() {
    final int maxStringLength = WRITER.getMaxStringLength();
    final String excessiveString = Strings.repeat("x", maxStringLength) + 'y';
    final String expectedJson = '"' +
            excessiveString.substring(0, maxStringLength) +
            WRITER.getTruncatedStringSuffix() +
            '"';
    @SuppressWarnings("Convert2Lambda")
    final String actualJson = WRITER.use(() ->
            WRITER.writeString(new StringBuilderFormattable() {
                @Override
                public void formatTo(StringBuilder stringBuilder) {
                    stringBuilder.append(excessiveString);
                }
            }));
    Assertions.assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example 2
Source File: JsonWriterTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void test_writeString_emitter_excessive_string() {
    final int maxStringLength = WRITER.getMaxStringLength();
    final String excessiveString = Strings.repeat("x", maxStringLength) + 'y';
    final String expectedJson = '"' +
            excessiveString.substring(0, maxStringLength) +
            WRITER.getTruncatedStringSuffix() +
            '"';
    final BiConsumer<StringBuilder, String> emitter = StringBuilder::append;
    final String actualJson =
            WRITER.use(() -> WRITER.writeString(emitter, excessiveString));
    Assertions.assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example 3
Source File: JsonWriterTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void test_writeString_excessive_seq() {
    final CharSequence seq = Strings.repeat("x", WRITER.getMaxStringLength()) + 'y';
    final String expectedJson = "\"" +
            Strings.repeat("x", WRITER.getMaxStringLength()) +
            WRITER.getTruncatedStringSuffix() +
            '"';
    final String actualJson = WRITER.use(() -> WRITER.writeString(seq));
    Assertions.assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example 4
Source File: JsonWriterTest.java    From logging-log4j2 with Apache License 2.0 5 votes vote down vote up
@Test
public void test_writeString_excessive_buffer() {
    final char[] buffer =
            (Strings.repeat("x", WRITER.getMaxStringLength()) + 'y')
                    .toCharArray();
    final String expectedJson = "\"" +
            Strings.repeat("x", WRITER.getMaxStringLength()) +
            WRITER.getTruncatedStringSuffix() +
            '"';
    final String actualJson = WRITER.use(() -> WRITER.writeString(buffer));
    Assertions.assertThat(actualJson).isEqualTo(expectedJson);
}
 
Example 5
Source File: JsonTemplateLayoutTest.java    From logging-log4j2 with Apache License 2.0 4 votes vote down vote up
@Test
public void test_maxStringLength() {

    // Create the log event.
    final int maxStringLength = 30;
    final String excessiveMessageString = Strings.repeat("m", maxStringLength) + 'M';
    final SimpleMessage message = new SimpleMessage(excessiveMessageString);
    final Throwable thrown = new RuntimeException();
    final LogEvent logEvent = Log4jLogEvent
            .newBuilder()
            .setLoggerName(LOGGER_NAME)
            .setLevel(Level.INFO)
            .setMessage(message)
            .setThrown(thrown)
            .build();

    // Create the event template node with map values.
    final String messageKey = "message";
    final String excessiveKey = Strings.repeat("k", maxStringLength) + 'K';
    final String excessiveValue = Strings.repeat("v", maxStringLength) + 'V';
    final String nullValueKey = "nullValueKey";
    final String eventTemplate = writeJson(Map(
            messageKey, Map("$resolver", "message"),
            excessiveKey, excessiveValue,
            nullValueKey, Map(
                    "$resolver", "exception",
                    "field", "message")));

    // Create the layout.
    final JsonTemplateLayout layout = JsonTemplateLayout
            .newBuilder()
            .setConfiguration(CONFIGURATION)
            .setEventTemplate(eventTemplate)
            .setMaxStringLength(maxStringLength)
            .build();

    // Check serialized event.
    usingSerializedLogEventAccessor(layout, logEvent, accessor -> {
        final String truncatedStringSuffix =
                JsonTemplateLayoutDefaults.getTruncatedStringSuffix();
        final String truncatedMessageString =
                excessiveMessageString.substring(0, maxStringLength) +
                        truncatedStringSuffix;
        assertThat(accessor.getString(messageKey)).isEqualTo(truncatedMessageString);
        final String truncatedKey =
                excessiveKey.substring(0, maxStringLength) +
                        truncatedStringSuffix;
        final String truncatedValue =
                excessiveValue.substring(0, maxStringLength) +
                        truncatedStringSuffix;
        assertThat(accessor.getString(truncatedKey)).isEqualTo(truncatedValue);
        assertThat(accessor.getString(nullValueKey)).isNull();
    });

}