| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| MockLogHandler |
|
| 0.0;0 |
| 1 | package biz.xsoftware.mock; | |
| 2 | ||
| 3 | import java.util.ArrayList; | |
| 4 | import java.util.Collections; | |
| 5 | import java.util.List; | |
| 6 | import java.util.logging.Handler; | |
| 7 | import java.util.logging.Level; | |
| 8 | import java.util.logging.LogRecord; | |
| 9 | import java.util.logging.Logger; | |
| 10 | ||
| 11 | /** | |
| 12 | * Used to aid in the testing of generated log messages. This class extends a | |
| 13 | * logging {@link Handler} in order to capture log messages. These log messages | |
| 14 | * can then be checked against to verify required log messages are indeed | |
| 15 | * logged. | |
| 16 | * | |
| 17 | * For example: | |
| 18 | * | |
| 19 | * <pre> | |
| 20 | * public class MyLogObject { | |
| 21 | * private static final Logger log = Logger.getLogger(MyLogObject.class.getName()); | |
| 22 | * | |
| 23 | * public void runWarningLog(String msg) { | |
| 24 | * log.warning(msg); | |
| 25 | * } | |
| 26 | * } | |
| 27 | * | |
| 28 | * public class TestMyLogObject { | |
| 29 | * MyLogObject logObj = new MyLogObject(); | |
| 30 | * | |
| 31 | * MockLogHandler mockLogHandler = new MockLogHandler(MyLogObject.class.getName()); | |
| 32 | * | |
| 33 | * @Test | |
| 34 | * public void testLogging() { | |
| 35 | * // verify the log handler starts out with zero log records | |
| 36 | * assertEquals(0, mockLogHandler.getLogRecords().size()); | |
| 37 | * // send a log message | |
| 38 | * logObj.runWarningLog("test one"); | |
| 39 | * // verify the log handler now has one log message | |
| 40 | * assertEquals(1, mockLogHandler.getLogRecords().size()); | |
| 41 | * } | |
| 42 | * } | |
| 43 | * </pre> | |
| 44 | * | |
| 45 | * | |
| 46 | * @author Brian Freeman | |
| 47 | * @since Mar 23, 2007 | |
| 48 | */ | |
| 49 | public class MockLogHandler extends Handler { | |
| 50 | ||
| 51 | 1 | public static final Level DEFAULT_LOG_LEVEL = Level.FINE; |
| 52 | ||
| 53 | private Level oldLevel; | |
| 54 | ||
| 55 | private Logger logger; | |
| 56 | ||
| 57 | 10 | private final List<LogRecord> recordList = Collections.synchronizedList(new ArrayList<LogRecord>()); |
| 58 | ||
| 59 | /** | |
| 60 | * Creates an instance of this object. Retrieves the logger for the given | |
| 61 | * class name and adds the instance to the logger's list of | |
| 62 | * <code>Handler</code>s. It will also set the log level if you're trying | |
| 63 | * to verify a specific Level | |
| 64 | * | |
| 65 | * @param classToMonitor This is the full (package name included) class | |
| 66 | * name. It can probably be just a package name too | |
| 67 | * | |
| 68 | * @param logLevel The log level to be monitored | |
| 69 | */ | |
| 70 | 10 | public MockLogHandler(String classToMonitor, Level logLevel) { |
| 71 | 10 | logger = Logger.getLogger(classToMonitor); |
| 72 | 10 | oldLevel = logger.getLevel(); |
| 73 | 10 | logger.setLevel(logLevel); |
| 74 | 10 | logger.addHandler(this); |
| 75 | ||
| 76 | 10 | setLevel(logLevel); |
| 77 | 10 | } |
| 78 | ||
| 79 | /** | |
| 80 | * Helper constructor that just sets the Level to FINE | |
| 81 | * | |
| 82 | * @param classToMonitor | |
| 83 | */ | |
| 84 | public MockLogHandler(String classToMonitor) { | |
| 85 | 10 | this(classToMonitor, DEFAULT_LOG_LEVEL); |
| 86 | 10 | } |
| 87 | ||
| 88 | /* | |
| 89 | * (non-Javadoc) | |
| 90 | * | |
| 91 | * @see java.util.logging.Handler#close() | |
| 92 | */ | |
| 93 | @Override | |
| 94 | public void close() { | |
| 95 | 11 | if (oldLevel != null) { |
| 96 | 10 | logger.setLevel(oldLevel); |
| 97 | 10 | setLevel(oldLevel); |
| 98 | } | |
| 99 | ||
| 100 | 11 | logger.removeHandler(this); |
| 101 | 11 | } |
| 102 | ||
| 103 | /** | |
| 104 | * Clears the list of log records | |
| 105 | * | |
| 106 | * @see java.util.logging.Handler#flush() | |
| 107 | */ | |
| 108 | @Override | |
| 109 | public void flush() { | |
| 110 | 1 | recordList.clear(); |
| 111 | 1 | } |
| 112 | ||
| 113 | /* | |
| 114 | * (non-Javadoc) | |
| 115 | * | |
| 116 | * @see java.util.logging.Handler#publish(java.util.logging.LogRecord) | |
| 117 | */ | |
| 118 | @Override | |
| 119 | public void publish(LogRecord record) { | |
| 120 | 19 | recordList.add(record); |
| 121 | 19 | } |
| 122 | ||
| 123 | /** | |
| 124 | * Looks at all the messages received so far to see if one equals the given | |
| 125 | * String. Looks for a match not worrying about the log level | |
| 126 | * | |
| 127 | * @param message The exact String to match against | |
| 128 | * | |
| 129 | * @return true if a match is found | |
| 130 | */ | |
| 131 | public boolean logMessageReceivedThatEquals(String message) { | |
| 132 | 2 | for (LogRecord rec : recordList) { |
| 133 | 2 | if (message.equals(rec.getMessage())) |
| 134 | 1 | return true; |
| 135 | 1 | } |
| 136 | ||
| 137 | // no match found | |
| 138 | 1 | return false; |
| 139 | } | |
| 140 | ||
| 141 | /** | |
| 142 | * Similar to the {@link #logMessageReceivedThatContains(String)}, this | |
| 143 | * method will search for an exact string match. However, this one will also | |
| 144 | * verify that the log level matches the given level | |
| 145 | * | |
| 146 | * @param message The exact String to match | |
| 147 | * | |
| 148 | * @param level The log level to compare against | |
| 149 | * | |
| 150 | * @return True if the given message matches one of the log messages and the | |
| 151 | * log Level is the same | |
| 152 | */ | |
| 153 | public boolean logMessageReceivedThatEquals(String message, Level level) { | |
| 154 | 3 | for (LogRecord rec : recordList) { |
| 155 | 3 | if (message.equals(rec.getMessage()) && level.equals(rec.getLevel())) { |
| 156 | 1 | return true; |
| 157 | } | |
| 158 | 2 | } |
| 159 | ||
| 160 | // no match found | |
| 161 | 2 | return false; |
| 162 | } | |
| 163 | ||
| 164 | /** | |
| 165 | * Looks at all the messages received so far to see if one equals the given | |
| 166 | * String. Looks for a match not worrying about the log level | |
| 167 | * | |
| 168 | * @param message The exact String to match against | |
| 169 | * | |
| 170 | * @return true if a match is found | |
| 171 | */ | |
| 172 | public boolean logMessageReceivedThatContains(String message) { | |
| 173 | 1 | for (LogRecord rec : recordList) { |
| 174 | 1 | if (rec.getMessage().contains(message)) |
| 175 | 1 | return true; |
| 176 | 0 | } |
| 177 | ||
| 178 | // no match found | |
| 179 | 0 | return false; |
| 180 | } | |
| 181 | ||
| 182 | /** | |
| 183 | * Similar to the {@link #logMessageReceivedThatContains(String)}, this | |
| 184 | * method will search for an exact string match. However, this one will also | |
| 185 | * verify that the log level matches the given level | |
| 186 | * | |
| 187 | * @param message The exact String to match | |
| 188 | * | |
| 189 | * @param level The log level to compare against | |
| 190 | * | |
| 191 | * @return True if the given message matches one of the log messages and the | |
| 192 | * log Level is the same | |
| 193 | */ | |
| 194 | public boolean logMessageReceivedThatContains(String message, Level level) { | |
| 195 | 2 | for (LogRecord rec : recordList) { |
| 196 | 2 | if (rec.getMessage().contains(message) && level.equals(rec.getLevel())) { |
| 197 | 1 | return true; |
| 198 | } | |
| 199 | 1 | } |
| 200 | ||
| 201 | // no match found | |
| 202 | 1 | return false; |
| 203 | } | |
| 204 | ||
| 205 | /** | |
| 206 | * Searches the <code>List</code> of <code>LogRecords</code> for an | |
| 207 | * exact match using the <code>LogRecord.getMessage()</code> method. If a | |
| 208 | * match is found it is added to the <code>List</code> that is returned | |
| 209 | * | |
| 210 | * @param message The message String to match with | |
| 211 | * | |
| 212 | * @return The list of matching <code>LogRecord</code>s or an empty list | |
| 213 | * | |
| 214 | */ | |
| 215 | public List<LogRecord> getLogRecordsThatEqual(String message) { | |
| 216 | 1 | ArrayList<LogRecord> result = new ArrayList<LogRecord>(); |
| 217 | ||
| 218 | 1 | for (LogRecord rec : recordList) { |
| 219 | 1 | if (message.equals(rec.getMessage())) { |
| 220 | 1 | result.add(rec); |
| 221 | } | |
| 222 | 1 | } |
| 223 | ||
| 224 | 1 | return result; |
| 225 | } | |
| 226 | ||
| 227 | /** | |
| 228 | * Returns a <code>List</code> of <code>LogRecords</code> that have a | |
| 229 | * message that contains the given message. The | |
| 230 | * <code>String.contains()</code> method will be used. | |
| 231 | * | |
| 232 | * @param message the String to search for in the list of | |
| 233 | * <code>LogRecords</code> | |
| 234 | */ | |
| 235 | public List<LogRecord> getLogRecordsThatContain(String message) { | |
| 236 | 1 | ArrayList<LogRecord> result = new ArrayList<LogRecord>(); |
| 237 | ||
| 238 | 1 | for (LogRecord rec : recordList) { |
| 239 | 3 | if (rec.getMessage().contains(message)) { |
| 240 | 1 | result.add(rec); |
| 241 | } | |
| 242 | 3 | } |
| 243 | ||
| 244 | 1 | return result; |
| 245 | } | |
| 246 | ||
| 247 | /** | |
| 248 | * Returns a <code>List</code> of <code>LogRecords</code> that have a | |
| 249 | * message that contains the given message. The | |
| 250 | * <code>String.contains()</code> method will be used. | |
| 251 | * | |
| 252 | * @param message the String to search for in the list of | |
| 253 | * <code>LogRecords</code> | |
| 254 | */ | |
| 255 | public List<LogRecord> getLogRecordsThatContain(String message, Level level) { | |
| 256 | 1 | ArrayList<LogRecord> result = new ArrayList<LogRecord>(); |
| 257 | ||
| 258 | 1 | for (LogRecord rec : recordList) { |
| 259 | 3 | if (level.equals(rec.getLevel()) && rec.getMessage().contains(message)) |
| 260 | 1 | result.add(rec); |
| 261 | 3 | } |
| 262 | ||
| 263 | 1 | return result; |
| 264 | } | |
| 265 | ||
| 266 | /** | |
| 267 | * Returns all of the saved <code>LogRecords</code> | |
| 268 | */ | |
| 269 | public List<LogRecord> getLogRecords() { | |
| 270 | 11 | return recordList; |
| 271 | } | |
| 272 | ||
| 273 | } |