2013-03-28 15:07:09 -07:00
|
|
|
|
2011-10-23 22:54:09 -07:00
|
|
|
|
2011-05-22 09:53:21 -07:00
|
|
|
using System;
|
2011-05-18 20:55:35 -07:00
|
|
|
using System.Collections.Generic;
|
2011-06-17 19:00:44 -07:00
|
|
|
using System.Linq;
|
2011-05-18 20:55:35 -07:00
|
|
|
using NLog;
|
|
|
|
using NLog.Targets;
|
2011-06-02 14:06:46 -07:00
|
|
|
using NUnit.Framework;
|
2011-05-18 20:55:35 -07:00
|
|
|
|
2011-10-23 22:54:09 -07:00
|
|
|
namespace NzbDrone.Test.Common
|
2011-05-18 20:55:35 -07:00
|
|
|
{
|
|
|
|
public class ExceptionVerification : Target
|
|
|
|
{
|
|
|
|
private static List<LogEventInfo> _logs = new List<LogEventInfo>();
|
|
|
|
|
|
|
|
protected override void Write(LogEventInfo logEvent)
|
|
|
|
{
|
|
|
|
if (logEvent.Level >= LogLevel.Warn)
|
|
|
|
{
|
|
|
|
_logs.Add(logEvent);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-10-23 22:54:09 -07:00
|
|
|
public static void Reset()
|
2011-05-18 20:55:35 -07:00
|
|
|
{
|
|
|
|
_logs = new List<LogEventInfo>();
|
|
|
|
}
|
|
|
|
|
2011-10-23 22:54:09 -07:00
|
|
|
public static void AssertNoUnexcpectedLogs()
|
2011-05-18 20:55:35 -07:00
|
|
|
{
|
2011-12-19 16:58:26 -08:00
|
|
|
ExpectedFatals(0);
|
|
|
|
ExpectedErrors(0);
|
|
|
|
ExpectedWarns(0);
|
2011-05-18 20:55:35 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
private static string GetLogsString(IEnumerable<LogEventInfo> logs)
|
|
|
|
{
|
|
|
|
string errors = "";
|
|
|
|
foreach (var log in logs)
|
|
|
|
{
|
|
|
|
string exception = "";
|
|
|
|
if (log.Exception != null)
|
|
|
|
{
|
2011-11-20 19:58:32 -08:00
|
|
|
exception = String.Format("[{0}: {1}]", log.Exception.GetType(), log.Exception.Message);
|
2011-05-18 20:55:35 -07:00
|
|
|
}
|
2011-11-06 22:26:21 -08:00
|
|
|
|
|
|
|
errors += Environment.NewLine + String.Format("[{0}] {1}: {2} {3}", log.Level, log.LoggerName, log.FormattedMessage, exception);
|
2011-05-18 20:55:35 -07:00
|
|
|
}
|
|
|
|
return errors;
|
|
|
|
}
|
|
|
|
|
2011-12-19 16:58:26 -08:00
|
|
|
public static void ExpectedErrors(int count)
|
2011-05-18 20:55:35 -07:00
|
|
|
{
|
2012-03-06 23:34:26 -08:00
|
|
|
Expected(LogLevel.Error, count);
|
2011-05-18 20:55:35 -07:00
|
|
|
}
|
|
|
|
|
2011-12-19 16:58:26 -08:00
|
|
|
public static void ExpectedFatals(int count)
|
2011-05-18 20:55:35 -07:00
|
|
|
{
|
2012-03-06 23:34:26 -08:00
|
|
|
Expected(LogLevel.Fatal, count);
|
2011-05-18 20:55:35 -07:00
|
|
|
}
|
|
|
|
|
2011-12-19 16:58:26 -08:00
|
|
|
public static void ExpectedWarns(int count)
|
2011-05-18 20:55:35 -07:00
|
|
|
{
|
2012-03-06 23:34:26 -08:00
|
|
|
Expected(LogLevel.Warn, count);
|
2011-05-18 20:55:35 -07:00
|
|
|
}
|
|
|
|
|
2011-10-23 22:54:09 -07:00
|
|
|
public static void IgnoreWarns()
|
2011-05-19 21:18:51 -07:00
|
|
|
{
|
|
|
|
Ignore(LogLevel.Warn);
|
|
|
|
}
|
|
|
|
|
2011-10-23 22:54:09 -07:00
|
|
|
public static void IgnoreErrors()
|
2011-05-19 21:18:51 -07:00
|
|
|
{
|
|
|
|
Ignore(LogLevel.Error);
|
|
|
|
}
|
|
|
|
|
2011-10-23 22:54:09 -07:00
|
|
|
public static void MarkInconclusive(Type exception)
|
2011-10-16 19:42:11 -07:00
|
|
|
{
|
2011-11-20 19:58:32 -08:00
|
|
|
var inconclusiveLogs = _logs.Where(l => l.Exception != null && l.Exception.GetType() == exception).ToList();
|
2011-10-16 20:32:57 -07:00
|
|
|
|
2012-02-25 11:57:56 -08:00
|
|
|
if (inconclusiveLogs.Any())
|
2011-10-16 20:32:57 -07:00
|
|
|
{
|
|
|
|
inconclusiveLogs.ForEach(c => _logs.Remove(c));
|
|
|
|
Assert.Inconclusive(GetLogsString(inconclusiveLogs));
|
|
|
|
}
|
2011-10-16 19:42:11 -07:00
|
|
|
}
|
|
|
|
|
2012-01-22 19:01:16 -08:00
|
|
|
public static void MarkInconclusive(string text)
|
|
|
|
{
|
2012-02-25 11:57:56 -08:00
|
|
|
var inconclusiveLogs = _logs.Where(l => l.FormattedMessage.ToLower().Contains(text.ToLower())).ToList();
|
2012-01-22 19:01:16 -08:00
|
|
|
|
2012-02-25 11:57:56 -08:00
|
|
|
if (inconclusiveLogs.Any())
|
2012-01-22 19:01:16 -08:00
|
|
|
{
|
|
|
|
inconclusiveLogs.ForEach(c => _logs.Remove(c));
|
|
|
|
Assert.Inconclusive(GetLogsString(inconclusiveLogs));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-06 23:34:26 -08:00
|
|
|
private static void Expected(LogLevel level, int count)
|
2011-05-18 20:55:35 -07:00
|
|
|
{
|
2011-10-16 20:32:57 -07:00
|
|
|
var levelLogs = _logs.Where(l => l.Level == level).ToList();
|
2011-05-18 20:55:35 -07:00
|
|
|
|
|
|
|
if (levelLogs.Count != count)
|
|
|
|
{
|
2011-05-26 20:04:36 -07:00
|
|
|
|
2011-05-18 20:55:35 -07:00
|
|
|
var message = String.Format("{0} {1}(s) were expected but {2} were logged.\n\r{3}",
|
2011-05-26 20:04:36 -07:00
|
|
|
count, level, levelLogs.Count, GetLogsString(levelLogs));
|
|
|
|
|
2011-11-06 22:26:21 -08:00
|
|
|
message = "\n\r****************************************************************************************\n\r"
|
2011-05-26 20:04:36 -07:00
|
|
|
+ message +
|
2011-11-06 22:26:21 -08:00
|
|
|
"\n\r****************************************************************************************";
|
2011-05-18 20:55:35 -07:00
|
|
|
|
|
|
|
Assert.Fail(message);
|
|
|
|
}
|
|
|
|
|
|
|
|
levelLogs.ForEach(c => _logs.Remove(c));
|
|
|
|
}
|
2011-05-19 21:18:51 -07:00
|
|
|
|
|
|
|
private static void Ignore(LogLevel level)
|
|
|
|
{
|
|
|
|
var levelLogs = _logs.Where(l => l.Level == level).ToList();
|
|
|
|
levelLogs.ForEach(c => _logs.Remove(c));
|
|
|
|
}
|
2011-05-18 20:55:35 -07:00
|
|
|
}
|
|
|
|
}
|