com.google.common.testing
Class TestLogHandler

java.lang.Object
  extended by java.util.logging.Handler
      extended by com.google.common.testing.TestLogHandler

public class TestLogHandler
extends java.util.logging.Handler

Tests may use this to intercept messages that are logged by the code under test. Example:

   TestLogHandler handler;

   protected void setUp() throws Exception {
     super.setUp();
     handler = new TestLogHandler();
     SomeClass.logger.addHandler(handler);
     addRequiredTearDown(new TearDown() {
       public void tearDown() throws Exception {
         SomeClass.logger.removeHandler(handler);
       }
     });
   }

   public void test() {
     SomeClass.foo();
     LogRecord firstRecord = handler.getStoredLogRecords().get(0);
     assertEquals("some message", firstRecord.getMessage());
   }
 
You can see more usage examples in TestLogHandlerTest.

Author:
kevinb

Field Summary
private  java.util.List<java.util.logging.LogRecord> list
          We will keep a private list of all logged records
private  java.util.List<java.util.logging.LogRecord> storedLogRecords
          And also the same list in an unmodifiable view (for the getter)
 
Constructor Summary
TestLogHandler()
           
 
Method Summary
 void clear()
           
 void close()
           
 void flush()
           
 java.util.List<java.util.logging.LogRecord> getStoredLogRecords()
          Fetch the list of logged records
 void publish(java.util.logging.LogRecord record)
          Adds the most recently logged record to our list.
 
Methods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, isLoggable, reportError, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

list

private final java.util.List<java.util.logging.LogRecord> list
We will keep a private list of all logged records


storedLogRecords

private final java.util.List<java.util.logging.LogRecord> storedLogRecords
And also the same list in an unmodifiable view (for the getter)

Constructor Detail

TestLogHandler

public TestLogHandler()
Method Detail

publish

public void publish(java.util.logging.LogRecord record)
Adds the most recently logged record to our list.

Specified by:
publish in class java.util.logging.Handler

flush

public void flush()
Specified by:
flush in class java.util.logging.Handler

close

public void close()
Specified by:
close in class java.util.logging.Handler

clear

public void clear()

getStoredLogRecords

public java.util.List<java.util.logging.LogRecord> getStoredLogRecords()
Fetch the list of logged records

Returns:
unmodifiablie LogRecord list of all logged records


Copyright © 2011 Google. All Rights Reserved.