SimpleLogRecord extends AbstractLogRecord implements SimpleLogHandler { public static SimpleLogRecord create(LogData data) { return new SimpleLogRecord(data); } private  SimpleLogRecord(LogData data); private  SimpleLogRecord(RuntimeException error, LogData data); static SimpleLogRecord create(LogData data); static SimpleLogRecord error(RuntimeException error, LogData data); @Override void handleFormattedLogMessage(Level level, String message, @NullableDecl Throwable thrown);  }
@Test public void testWithArguments() { LogData data = FakeLogData.withBraceStyle("Answer={0}", 42).setLevel(Level.FINE); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getLevel()).isEqualTo(Level.FINE); assertThat(record.getMessage()).isEqualTo("Answer=42"); assertThat(record.getParameters()).isNull(); }
@Test public void testWithPrintfFormatting() { LogData data = FakeLogData.withPrintfStyle("Hex=%#08x, Int=%1$d", 0xC0DE); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getLevel()).isEqualTo(Level.INFO); assertThat(record.getMessage()).isEqualTo("Hex=0x00c0de, Int=49374"); assertThat(record.getParameters()).isNull(); }
@Test public void testWithThrown() { Throwable cause = new Throwable("Goodbye World"); LogData data = FakeLogData.withPrintfStyle("Hello World") .addMetadata(LogContext.Key.LOG_CAUSE, cause); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getThrown()).isSameInstanceAs(cause); }
@Test public void testWithArgumentsAndMetadata() { LogData data = FakeLogData.withPrintfStyle("Foo='%s'", "bar") .setTimestampNanos(123456789000L) .addMetadata(COUNT_KEY, 23) .addMetadata(ID_KEY, "test ID"); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getLevel()).isEqualTo(Level.INFO); assertThat(record.getMessage()).isEqualTo("Foo='bar' [CONTEXT count=23 id=\"test ID\" ]"); assertThat(record.getParameters()).isNull(); record.setMillis(123456789); assertThat(record.toString()) .isEqualTo( "SimpleLogRecord {\n" + " message: Foo='bar' [CONTEXT count=23 id=\"test ID\" ]\n" + " arguments: <none>\n" + " original message: Foo='%s'\n" + " original arguments:\n" + " bar\n" + " metadata:\n" + " count: 23\n" + " id: test ID\n" + " level: INFO\n" + " timestamp (nanos): 123456789000\n" + " class: com.google.FakeClass\n" + " method: fakeMethod\n" + " line number: 123\n" + "}"); }
@Test public void testNullArgs() { LogData data = FakeLogData.withPrintfStyle("value=%s", new Object[] { null }); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getMessage()).isEqualTo("value=null"); }
@Test public void testMissingArgs() { LogData data = FakeLogData.withPrintfStyle("foo=%s, bar=%s", "FOO"); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getMessage()).isEqualTo("foo=FOO, bar=[ERROR: MISSING LOG ARGUMENT]"); }
@Test public void testUnusedArgs() { LogData data = FakeLogData.withPrintfStyle("%2$s %s %<s %s", "a", "b", "c", "d"); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getMessage()).isEqualTo("b a a b [ERROR: UNUSED LOG ARGUMENTS]"); }
@Test public void testTrailingArgsAbove32AreCaught() { LogData data = FakeLogData.withPrintfStyle( "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s " + "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s", new Object[33]); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getMessage()).contains("[ERROR: UNUSED LOG ARGUMENTS]"); }
@Test public void testGapsInArgsUpTo32AreCaught() { LogData data = FakeLogData.withPrintfStyle( "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s " + "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %33$s", new Object[33]); try { SimpleLogRecord.create(data); fail("expected ParseException"); } catch (ParseException expected) { assertThat(expected.getMessage()).contains("unreferenced arguments [first missing index=31]"); } }
@Test public void testGapsInArgsAbove32AreIgnored() { Object[] args = new Object[34]; args[33] = "UNUSED"; LogData data = FakeLogData.withPrintfStyle( "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s " + "%s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %s %34$s", new Object[34]); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getMessage()).doesNotContain("UNUSED"); }
@Test public void testToString_b31405251() { LogData data = FakeLogData.withPrintfStyle("Answer=%d", 42); String toString = SimpleLogRecord.create(data).toString(); assertThat(toString).contains(" message: Answer=42"); assertThat(toString).contains(" arguments: <none>"); assertThat(toString).contains(" original message: Answer=%d"); data = FakeLogData.of("Literal String"); toString = SimpleLogRecord.create(data).toString(); assertThat(toString).contains(" message: Literal String"); assertThat(toString).contains(" arguments: <none>"); assertThat(toString).contains(" original message: Literal String"); }
@Test public void testLiteral() { LogData data = FakeLogData.of("literal message").setLevel(Level.WARNING); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getLevel()).isEqualTo(Level.WARNING); assertThat(record.getMessage()).isEqualTo("literal message"); assertThat(record.getParameters()).isNull(); assertThat(record.getLoggerName()).isEqualTo(data.getLoggerName()); assertThat(record.getSourceClassName()).isEqualTo(data.getLogSite().getClassName()); assertThat(record.getSourceMethodName()).isEqualTo(data.getLogSite().getMethodName()); }
@Test public void testLiteralNull() { LogData data = FakeLogData.of(null).setLevel(Level.WARNING); LogRecord record = SimpleLogRecord.create(data); assertThat(record.getLevel()).isEqualTo(Level.WARNING); assertThat(record.getMessage()).isEqualTo("null"); assertThat(record.getParameters()).isNull(); }