org.xwiki.test
Class LogRule

java.lang.Object
  extended by org.xwiki.test.LogRule
All Implemented Interfaces:
org.junit.rules.TestRule

public class LogRule
extends Object
implements org.junit.rules.TestRule

Allow capturing Logs output in the Class under test during unit testing. This is useful for two reasons:

This code was inspired by code written by Christian Baranowski in a blog post.

Example usage:

  @Rule public LogRule logRule = new LogRule() {{
      record(LogLevel.WARN);
      recordLoggingForType(RestrictParseLocationEventHandler.class);
  }};
 

Since:
4.2RC1
Version:
$Id: 8f56c07f8560fc0eab2a4a26cf82bc284e7179f6 $

Nested Class Summary
static class LogRule.LogLevel
          Helper class to represent Logging levels to capture.
 class LogRule.LogStatement
          The actual code that executes our capturing logic before the test runs and removes it after it has run.
 
Constructor Summary
LogRule()
           
 
Method Summary
 org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement, org.junit.runner.Description description)
           
 boolean contains(String logOutput)
           
 String getMessage(int position)
           
 void record(LogRule.LogLevel level)
           
 void recordLoggingForType(Class<?> type)
           
 int size()
           
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

LogRule

public LogRule()
Method Detail

apply

public org.junit.runners.model.Statement apply(org.junit.runners.model.Statement statement,
                                               org.junit.runner.Description description)
Specified by:
apply in interface org.junit.rules.TestRule

record

public void record(LogRule.LogLevel level)
Parameters:
level - the log level to capture

recordLoggingForType

public void recordLoggingForType(Class<?> type)
Parameters:
type - the logging class type for which to capture logs

contains

public boolean contains(String logOutput)
Parameters:
logOutput - the log output to match in the captured data
Returns:
true if the passed text was logged or false otherwise

getMessage

public String getMessage(int position)
Parameters:
position - the message number in the list of captured logs
Returns:
the message at the specified position

size

public int size()
Returns:
the number of log messages that have been captured


Copyright © 2004–2014 XWiki. All rights reserved.