|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||
java.lang.Objectjava.util.logging.Handler
biz.xsoftware.mock.MockLogHandler
public class MockLogHandler
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());
}
}
| 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 |
|---|
public static final Level DEFAULT_LOG_LEVEL
| Constructor Detail |
|---|
public MockLogHandler(String classToMonitor,
Level logLevel)
Handlers. It will also set the log level if you're trying
to verify a specific Level
classToMonitor - This is the full (package name included) class
name. It can probably be just a package name toologLevel - The log level to be monitoredpublic MockLogHandler(String classToMonitor)
classToMonitor - | Method Detail |
|---|
public void close()
close in class Handlerpublic void flush()
flush in class HandlerHandler.flush()public void publish(LogRecord record)
publish in class Handlerpublic boolean logMessageReceivedThatEquals(String message)
message - The exact String to match against
public boolean logMessageReceivedThatEquals(String message,
Level level)
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
message - The exact String to matchlevel - The log level to compare against
public boolean logMessageReceivedThatContains(String message)
message - The exact String to match against
public boolean logMessageReceivedThatContains(String message,
Level level)
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
message - The exact String to matchlevel - The log level to compare against
public List<LogRecord> getLogRecordsThatEqual(String message)
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
message - The message String to match with
LogRecords or an empty listpublic List<LogRecord> getLogRecordsThatContain(String message)
List of LogRecords that have a
message that contains the given message. The
String.contains() method will be used.
message - the String to search for in the list of
LogRecords
public List<LogRecord> getLogRecordsThatContain(String message,
Level level)
List of LogRecords that have a
message that contains the given message. The
String.contains() method will be used.
message - the String to search for in the list of
LogRecordspublic List<LogRecord> getLogRecords()
LogRecords
|
||||||||||
| PREV CLASS NEXT CLASS | FRAMES NO FRAMES | |||||||||
| SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD | |||||||||