LogcatWriter implements Writer { @Override public void write(final LogEntry logEntry) { String tag = renderTag(logEntry); String message = renderMessage(logEntry); switch (logEntry.getLevel()) { case TRACE: Log.println(Log.VERBOSE, tag, message); break; case DEBUG: Log.println(Log.DEBUG, tag, message); break; case INFO: Log.println(Log.INFO, tag, message); break; case WARN: Log.println(Log.WARN, tag, message); break; case ERROR: Log.println(Log.ERROR, tag, message); break; default: InternalLogger.log(Level.ERROR, "Unexpected logging level: " + logEntry.getLevel()); break; } } LogcatWriter();  LogcatWriter(final Map<String, String> properties); @Override Collection<LogEntryValue> getRequiredLogEntryValues(); @Override void write(final LogEntry logEntry); @Override void flush(); @Override void close();  }
@Test public void trace() { LogcatWriter writer = new LogcatWriter(doubletonMap("tagname", "{class-name}", "format", "{message}")); writer.write(LogEntryBuilder.empty().level(Level.TRACE).className("MyClass").message("Hello World!").create()); assertThat(levelCaptor.getAllValues()).containsOnly(Log.VERBOSE); assertThat(tagCaptor.getAllValues()).containsOnly("MyClass"); assertThat(messageCaptor.getAllValues()).containsOnly("Hello World!"); }
@Test public void debug() { LogcatWriter writer = new LogcatWriter(doubletonMap("tagname", "{class-name}", "format", "{message}")); writer.write(LogEntryBuilder.empty().level(Level.DEBUG).className("MyClass").message("Hello World!").create()); assertThat(levelCaptor.getAllValues()).containsOnly(Log.DEBUG); assertThat(tagCaptor.getAllValues()).containsOnly("MyClass"); assertThat(messageCaptor.getAllValues()).containsOnly("Hello World!"); }
@Test public void info() { LogcatWriter writer = new LogcatWriter(doubletonMap("tagname", "{class-name}", "format", "{message}")); writer.write(LogEntryBuilder.empty().level(Level.INFO).className("MyClass").message("Hello World!").create()); assertThat(levelCaptor.getAllValues()).containsOnly(Log.INFO); assertThat(tagCaptor.getAllValues()).containsOnly("MyClass"); assertThat(messageCaptor.getAllValues()).containsOnly("Hello World!"); }
@Test public void warning() { LogcatWriter writer = new LogcatWriter(doubletonMap("tagname", "{class-name}", "format", "{message}")); writer.write(LogEntryBuilder.empty().level(Level.WARN).className("MyClass").message("Hello World!").create()); assertThat(levelCaptor.getAllValues()).containsOnly(Log.WARN); assertThat(tagCaptor.getAllValues()).containsOnly("MyClass"); assertThat(messageCaptor.getAllValues()).containsOnly("Hello World!"); }
@Test public void error() { LogcatWriter writer = new LogcatWriter(doubletonMap("tagname", "{class-name}", "format", "{message}")); writer.write(LogEntryBuilder.empty().level(Level.ERROR).className("MyClass").message("Hello World!").create()); assertThat(levelCaptor.getAllValues()).containsOnly(Log.ERROR); assertThat(tagCaptor.getAllValues()).containsOnly("MyClass"); assertThat(messageCaptor.getAllValues()).containsOnly("Hello World!"); }
@Test public void trimTag() { LogcatWriter writer = new LogcatWriter(doubletonMap("tagname", "ABCDEFGHIJKLMNOPQRSTUVWXYZ", "format", "{message}")); writer.write(LogEntryBuilder.empty().level(Level.INFO).message("Hello World!").create()); assertThat(tagCaptor.getAllValues()).containsOnly("ABCDEFGHIJKLMNOPQRST..."); assertThat(messageCaptor.getAllValues()).containsOnly("Hello World!"); }
@Test public void writingThread() { LogcatWriter writer = new LogcatWriter(tripletonMap("tagname", "{class-name}", "format", "{message}", "writingthread", "true")); writer.write(LogEntryBuilder.empty().level(Level.INFO).className("FirstClass").message("Hello World!").create()); writer.write(LogEntryBuilder.empty().level(Level.INFO).className("SecondClass").message("Hello Universe!").create()); assertThat(tagCaptor.getAllValues()).containsOnly("FirstClass", "SecondClass"); assertThat(messageCaptor.getAllValues()).containsOnly("Hello World!", "Hello Universe!"); }
@Test public void illegalSeverityLevel() { LogcatWriter writer = new LogcatWriter(doubletonMap("tagname", "STATIC", "format", "{message}")); writer.write(LogEntryBuilder.empty().level(Level.OFF).message("Hello World!").create()); assertThat(tagCaptor.getAllValues()).isEmpty(); assertThat(messageCaptor.getAllValues()).isEmpty(); assertThat(systemStream.consumeErrorOutput()).containsOnlyOnce("ERROR").containsOnlyOnce("OFF"); }