biz.xsoftware.mock
Class MockLogHandler

java.lang.Object
  extended by java.util.logging.Handler
      extended by biz.xsoftware.mock.MockLogHandler

public class MockLogHandler
extends Handler

Used to aid in the testing of generated log messages. This class extends a logging Handler in order to capture log messages. These log messages can then be checked against to verify required log messages are indeed logged. For example:

 public class MyLogObject {
     private static final Logger log = Logger.getLogger(MyLogObject.class.getName());
 
     public void runWarningLog(String msg) {
         log.warning(msg);
     }
 }
 
 public class TestMyLogObject {
     MyLogObject logObj = new MyLogObject();
 
     MockLogHandler mockLogHandler = new MockLogHandler(MyLogObject.class.getName());
 
     @Test
     public void testLogging() {
         // verify the log handler starts out with zero log records
         assertEquals(0, mockLogHandler.getLogRecords().size());
         // send a log message
         logObj.runWarningLog("test one");
         // verify the log handler now has one log message
         assertEquals(1, mockLogHandler.getLogRecords().size());
     }
 }
 

Since:
Mar 23, 2007
Author:
Brian Freeman

Field Summary
static Level DEFAULT_LOG_LEVEL
           
 
Constructor Summary
MockLogHandler(String classToMonitor)
          Helper constructor that just sets the Level to FINE
MockLogHandler(String classToMonitor, Level logLevel)
          Creates an instance of this object.
 
Method Summary
 void close()
           
 void flush()
          Clears the list of log records
 List<LogRecord> getLogRecords()
          Returns all of the saved LogRecords
 List<LogRecord> getLogRecordsThatContain(String message)
          Returns a List of LogRecords that have a message that contains the given message.
 List<LogRecord> getLogRecordsThatContain(String message, Level level)
          Returns a List of LogRecords that have a message that contains the given message.
 List<LogRecord> getLogRecordsThatEqual(String message)
          Searches the List of LogRecords for an exact match using the LogRecord.getMessage() method.
 boolean logMessageReceivedThatContains(String message)
          Looks at all the messages received so far to see if one equals the given String.
 boolean logMessageReceivedThatContains(String message, Level level)
          Similar to the logMessageReceivedThatContains(String), this method will search for an exact string match.
 boolean logMessageReceivedThatEquals(String message)
          Looks at all the messages received so far to see if one equals the given String.
 boolean logMessageReceivedThatEquals(String message, Level level)
          Similar to the logMessageReceivedThatContains(String), this method will search for an exact string match.
 void publish(LogRecord record)
           
 
Methods inherited from class java.util.logging.Handler
getEncoding, getErrorManager, getFilter, getFormatter, getLevel, isLoggable, setEncoding, setErrorManager, setFilter, setFormatter, setLevel
 
Methods inherited from class java.lang.Object
equals, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Field Detail

DEFAULT_LOG_LEVEL

public static final Level DEFAULT_LOG_LEVEL
Constructor Detail

MockLogHandler

public MockLogHandler(String classToMonitor,
                      Level logLevel)
Creates an instance of this object. Retrieves the logger for the given class name and adds the instance to the logger's list of Handlers. It will also set the log level if you're trying to verify a specific Level

Parameters:
classToMonitor - This is the full (package name included) class name. It can probably be just a package name too
logLevel - The log level to be monitored

MockLogHandler

public MockLogHandler(String classToMonitor)
Helper constructor that just sets the Level to FINE

Parameters:
classToMonitor -
Method Detail

close

public void close()
Specified by:
close in class Handler

flush

public void flush()
Clears the list of log records

Specified by:
flush in class Handler
See Also:
Handler.flush()

publish

public void publish(LogRecord record)
Specified by:
publish in class Handler

logMessageReceivedThatEquals

public boolean logMessageReceivedThatEquals(String message)
Looks at all the messages received so far to see if one equals the given String. Looks for a match not worrying about the log level

Parameters:
message - The exact String to match against
Returns:
true if a match is found

logMessageReceivedThatEquals

public boolean logMessageReceivedThatEquals(String message,
                                            Level level)
Similar to the logMessageReceivedThatContains(String), this method will search for an exact string match. However, this one will also verify that the log level matches the given level

Parameters:
message - The exact String to match
level - The log level to compare against
Returns:
True if the given message matches one of the log messages and the log Level is the same

logMessageReceivedThatContains

public boolean logMessageReceivedThatContains(String message)
Looks at all the messages received so far to see if one equals the given String. Looks for a match not worrying about the log level

Parameters:
message - The exact String to match against
Returns:
true if a match is found

logMessageReceivedThatContains

public boolean logMessageReceivedThatContains(String message,
                                              Level level)
Similar to the logMessageReceivedThatContains(String), this method will search for an exact string match. However, this one will also verify that the log level matches the given level

Parameters:
message - The exact String to match
level - The log level to compare against
Returns:
True if the given message matches one of the log messages and the log Level is the same

getLogRecordsThatEqual

public List<LogRecord> getLogRecordsThatEqual(String message)
Searches the List of LogRecords for an exact match using the LogRecord.getMessage() method. If a match is found it is added to the List that is returned

Parameters:
message - The message String to match with
Returns:
The list of matching LogRecords or an empty list

getLogRecordsThatContain

public List<LogRecord> getLogRecordsThatContain(String message)
Returns a List of LogRecords that have a message that contains the given message. The String.contains() method will be used.

Parameters:
message - the String to search for in the list of LogRecords

getLogRecordsThatContain

public List<LogRecord> getLogRecordsThatContain(String message,
                                                Level level)
Returns a List of LogRecords that have a message that contains the given message. The String.contains() method will be used.

Parameters:
message - the String to search for in the list of LogRecords

getLogRecords

public List<LogRecord> getLogRecords()
Returns all of the saved LogRecords