下面列出了org.apache.logging.log4j.util.Strings#repeat ( ) 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
@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);
}
@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);
}
@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);
}
@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);
}
@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();
});
}