2011-05-22 16:53:21 +00:00
|
|
|
|
// ReSharper disable RedundantUsingDirective
|
|
|
|
|
using System;
|
2011-05-19 03:55:35 +00:00
|
|
|
|
using System.Collections.Generic;
|
2011-06-18 02:00:44 +00:00
|
|
|
|
using System.Linq;
|
2011-05-19 03:55:35 +00:00
|
|
|
|
using NLog;
|
|
|
|
|
using NLog.Targets;
|
2011-06-02 21:06:46 +00:00
|
|
|
|
using NUnit.Framework;
|
2011-05-19 03:55:35 +00:00
|
|
|
|
|
|
|
|
|
namespace NzbDrone.Core.Test.Framework
|
|
|
|
|
{
|
|
|
|
|
public class ExceptionVerification : Target
|
|
|
|
|
{
|
|
|
|
|
private static List<LogEventInfo> _logs = new List<LogEventInfo>();
|
2011-10-17 02:42:11 +00:00
|
|
|
|
private static List<Type> _inconclusive = new List<Type>();
|
2011-05-19 03:55:35 +00:00
|
|
|
|
|
|
|
|
|
protected override void Write(LogEventInfo logEvent)
|
|
|
|
|
{
|
|
|
|
|
if (logEvent.Level >= LogLevel.Warn)
|
|
|
|
|
{
|
|
|
|
|
_logs.Add(logEvent);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void Reset()
|
|
|
|
|
{
|
|
|
|
|
_logs = new List<LogEventInfo>();
|
2011-10-17 02:42:11 +00:00
|
|
|
|
_inconclusive = new List<Type>();
|
2011-05-19 03:55:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-06-02 21:06:46 +00:00
|
|
|
|
internal static void AssertNoUnexcpectedLogs()
|
2011-05-19 03:55:35 +00:00
|
|
|
|
{
|
2011-05-27 03:04:36 +00:00
|
|
|
|
ExcpectedFatals(0);
|
|
|
|
|
ExcpectedErrors(0);
|
|
|
|
|
ExcpectedWarns(0);
|
2011-05-19 03:55:35 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private static string GetLogsString(IEnumerable<LogEventInfo> logs)
|
|
|
|
|
{
|
|
|
|
|
string errors = "";
|
|
|
|
|
foreach (var log in logs)
|
|
|
|
|
{
|
|
|
|
|
string exception = "";
|
|
|
|
|
if (log.Exception != null)
|
|
|
|
|
{
|
2011-05-27 03:04:36 +00:00
|
|
|
|
exception = log.Exception.Message;
|
2011-05-19 03:55:35 +00:00
|
|
|
|
}
|
2011-05-27 03:04:36 +00:00
|
|
|
|
errors += Environment.NewLine + String.Format("[{0}] {1}: {2} [{3}]", log.Level, log.LoggerName, log.FormattedMessage, exception);
|
2011-05-19 03:55:35 +00:00
|
|
|
|
}
|
|
|
|
|
return errors;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void ExcpectedErrors(int count)
|
|
|
|
|
{
|
|
|
|
|
Excpected(LogLevel.Error, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void ExcpectedFatals(int count)
|
|
|
|
|
{
|
|
|
|
|
Excpected(LogLevel.Fatal, count);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void ExcpectedWarns(int count)
|
|
|
|
|
{
|
|
|
|
|
Excpected(LogLevel.Warn, count);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-20 04:18:51 +00:00
|
|
|
|
internal static void IgnoreWarns()
|
|
|
|
|
{
|
|
|
|
|
Ignore(LogLevel.Warn);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
internal static void IgnoreErrors()
|
|
|
|
|
{
|
|
|
|
|
Ignore(LogLevel.Error);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 02:42:11 +00:00
|
|
|
|
internal static void MarkForInconclusive(Type exception)
|
|
|
|
|
{
|
|
|
|
|
_inconclusive.Add(exception);
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-19 03:55:35 +00:00
|
|
|
|
private static void Excpected(LogLevel level, int count)
|
|
|
|
|
{
|
2011-10-17 03:02:20 +00:00
|
|
|
|
var inconclusiveLogs = _logs.Where(l => _inconclusive.Any(c => c == l.Exception.GetType())).ToList();
|
2011-10-17 02:42:11 +00:00
|
|
|
|
|
2011-10-17 03:02:20 +00:00
|
|
|
|
var levelLogs = _logs.Except(inconclusiveLogs).Where(l => l.Level == level).ToList();
|
2011-05-19 03:55:35 +00:00
|
|
|
|
|
|
|
|
|
if (levelLogs.Count != count)
|
|
|
|
|
{
|
2011-05-27 03:04:36 +00:00
|
|
|
|
|
2011-05-19 03:55:35 +00:00
|
|
|
|
var message = String.Format("{0} {1}(s) were expected but {2} were logged.\n\r{3}",
|
2011-05-27 03:04:36 +00:00
|
|
|
|
count, level, levelLogs.Count, GetLogsString(levelLogs));
|
|
|
|
|
|
2011-10-17 02:42:11 +00:00
|
|
|
|
message = "********************************************************************************************************************************\n\r"
|
2011-05-27 03:04:36 +00:00
|
|
|
|
+ message +
|
|
|
|
|
"\n\r********************************************************************************************************************************";
|
2011-05-19 03:55:35 +00:00
|
|
|
|
|
|
|
|
|
Assert.Fail(message);
|
|
|
|
|
}
|
|
|
|
|
|
2011-10-17 03:02:20 +00:00
|
|
|
|
if (inconclusiveLogs.Count != 0)
|
2011-10-17 02:42:11 +00:00
|
|
|
|
{
|
2011-10-17 03:02:20 +00:00
|
|
|
|
Assert.Inconclusive(GetLogsString(inconclusiveLogs));
|
2011-10-17 02:42:11 +00:00
|
|
|
|
}
|
|
|
|
|
|
2011-05-19 03:55:35 +00:00
|
|
|
|
levelLogs.ForEach(c => _logs.Remove(c));
|
|
|
|
|
}
|
2011-05-20 04:18:51 +00:00
|
|
|
|
|
|
|
|
|
private static void Ignore(LogLevel level)
|
|
|
|
|
{
|
|
|
|
|
var levelLogs = _logs.Where(l => l.Level == level).ToList();
|
|
|
|
|
levelLogs.ForEach(c => _logs.Remove(c));
|
|
|
|
|
}
|
2011-05-19 03:55:35 +00:00
|
|
|
|
}
|
|
|
|
|
}
|