default logger unit test
This commit is contained in:
parent
a9114a8f2d
commit
15e77c1574
110
SimpleLog.Tests/DefaultLoggerTest.cs
Normal file
110
SimpleLog.Tests/DefaultLoggerTest.cs
Normal file
@ -0,0 +1,110 @@
|
||||
using System.Collections.Generic;
|
||||
using NUnit.Framework;
|
||||
using NUnit.Mocks;
|
||||
using NUnit.Framework.SyntaxHelpers;
|
||||
|
||||
namespace SimpleLog.Tests {
|
||||
[TestFixture]
|
||||
public class DefaultLoggerTest {
|
||||
|
||||
private DefaultLogger Logger;
|
||||
private DynamicMock Handler;
|
||||
|
||||
[SetUp]
|
||||
public void Init() {
|
||||
this.Logger = DefaultLogger.Instance;
|
||||
this.Logger.Enabled = true;
|
||||
this.Logger.MessageHandler = new DefaultMessageHandler();
|
||||
this.Handler = new DynamicMock(typeof(ILogHandler));
|
||||
|
||||
}
|
||||
|
||||
[TearDown]
|
||||
public void DeInit() {
|
||||
this.Logger.UnregisterAllLogHandlers();
|
||||
this.Logger.Enabled = true;
|
||||
this.Logger.MessageHandler = new DefaultMessageHandler();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestRegisterAndUnregisterLogHandler() {
|
||||
int oldCount = this.Logger.LogHandlerCount;
|
||||
this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler));
|
||||
|
||||
|
||||
ILogHandler handler = (ILogHandler)this.Handler.MockInstance;
|
||||
|
||||
this.Logger.RegisterLogHandler(handler);
|
||||
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(oldCount + 1));
|
||||
|
||||
Assert.That(this.Logger.UnregisterLogHandlerType("SimpleLog.ILogHandler"), Is.EqualTo(1));
|
||||
|
||||
this.Logger.RegisterLogHandler(handler);
|
||||
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(oldCount + 1));
|
||||
|
||||
this.Logger.UnregisterAllLogHandlers();
|
||||
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(0));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestLogWhenDisabled() {
|
||||
this.Logger.Enabled = false;
|
||||
Assert.That(this.Logger.Log("yay", LogLevel.Debug));
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestLogWithHighLogLevel() {
|
||||
DynamicMock msgHandler = new DynamicMock(typeof(IMessageHandler));
|
||||
msgHandler.ExpectAndReturn("ConvertMessageToString", "foo", "yay");
|
||||
msgHandler.ExpectAndReturn("FormatLogMessage", "foobar", "foo", LogLevel.Warning);
|
||||
|
||||
//NUnit mock objects suck, so setting this property on DefaultLogger
|
||||
//and assuming handler.MessageHandler returns null is the best I can do
|
||||
this.Logger.MessageHandler = (IMessageHandler)msgHandler.MockInstance;
|
||||
|
||||
this.Handler.ExpectAndReturn("Log", true, "foobar", LogLevel.Warning);
|
||||
|
||||
this.Logger.RegisterLogHandler((ILogHandler)this.Handler.MockInstance);
|
||||
|
||||
Assert.That(this.Logger.Log("yay", LogLevel.Warning));
|
||||
this.Handler.Verify();
|
||||
msgHandler.Verify();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestLogWithHighLogLevelFails() {
|
||||
DynamicMock msgHandler = new DynamicMock(typeof(IMessageHandler));
|
||||
msgHandler.ExpectAndReturn("ConvertMessageToString", "foo", "yay");
|
||||
msgHandler.ExpectAndReturn("FormatLogMessage", "foobar", "foo", LogLevel.Warning);
|
||||
|
||||
//NUnit mock objects suck, so setting this property on DefaultLogger
|
||||
//and assuming handler.MessageHandler returns null is the best I can do
|
||||
this.Logger.MessageHandler = (IMessageHandler)msgHandler.MockInstance;
|
||||
|
||||
this.Handler.ExpectAndReturn("Log", false, "foobar", LogLevel.Warning);
|
||||
|
||||
this.Logger.RegisterLogHandler((ILogHandler)this.Handler.MockInstance);
|
||||
|
||||
Assert.That(!this.Logger.Log("yay", LogLevel.Warning));
|
||||
this.Handler.Verify();
|
||||
msgHandler.Verify();
|
||||
}
|
||||
|
||||
[Test]
|
||||
public void TestLogWithLowLogLevel() {
|
||||
DynamicMock msgHandler = new DynamicMock(typeof(IMessageHandler));
|
||||
msgHandler.ExpectNoCall("ConvertMessageToString");
|
||||
msgHandler.ExpectNoCall("FormatLogMessage");
|
||||
this.Logger.MessageHandler = (IMessageHandler)msgHandler.MockInstance;
|
||||
|
||||
this.Handler.ExpectNoCall("Log");
|
||||
|
||||
this.Logger.RegisterLogHandler((ILogHandler)this.Handler.MockInstance);
|
||||
|
||||
Assert.That(this.Logger.Log("yay", LogLevel.Debug));
|
||||
this.Handler.Verify();
|
||||
msgHandler.Verify();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
@ -49,6 +49,7 @@
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
<Compile Include="ConsoleLogHandlerTest.cs" />
|
||||
<Compile Include="DefaultLoggerTest.cs" />
|
||||
<Compile Include="DefaultMessageHandlerTest.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
|
@ -40,7 +40,7 @@ namespace SimpleLog {
|
||||
IMessageHandler messageHandler;
|
||||
foreach (ILogHandler handler in this.LogHandlers) {
|
||||
allowedLevel = handler.LogLevel ?? (LogLevel)this.LogLevel;
|
||||
if (level <= allowedLevel) {
|
||||
if (level >= allowedLevel) {
|
||||
messageHandler = handler.MessageHandler ?? this.MessageHandler;
|
||||
messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat;
|
||||
|
||||
@ -59,24 +59,18 @@ namespace SimpleLog {
|
||||
this.LogHandlers.Add(handler);
|
||||
}
|
||||
|
||||
public bool UnregisterLogHandler(ILogHandler handler) {
|
||||
if (this.LogHandlers.Contains(handler)) {
|
||||
this.LogHandlers.Remove(handler);
|
||||
handler.GracefulShutDown();
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
public int UnregisterLogHandlerType(string qualifiedClassName) {
|
||||
return this.LogHandlers.RemoveAll(
|
||||
delegate(ILogHandler handler) {
|
||||
//Console.WriteLine(handler.GetType().ToString() == qualifiedClassName);
|
||||
return (handler.GetType().ToString() == qualifiedClassName);
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
public int UnregisterLogHandlerType(string assemblyName) {
|
||||
int count = 0;
|
||||
foreach (ILogHandler handler in this.LogHandlers) {
|
||||
if (handler.GetType().AssemblyQualifiedName == assemblyName) {
|
||||
count += this.UnregisterLogHandler(handler) ? 1 : 0;
|
||||
}
|
||||
}
|
||||
|
||||
public int UnregisterAllLogHandlers() {
|
||||
int count = this.LogHandlerCount;
|
||||
this.LogHandlers.RemoveRange(0, count);
|
||||
return count;
|
||||
}
|
||||
|
||||
@ -118,5 +112,14 @@ namespace SimpleLog {
|
||||
}
|
||||
#endregion
|
||||
|
||||
/// <summary>
|
||||
/// Gets the number of registered log handlers
|
||||
/// </summary>
|
||||
public int LogHandlerCount {
|
||||
get {
|
||||
return this.LogHandlers.Count;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
@ -11,7 +11,7 @@
|
||||
/// Unregisters the given log handler
|
||||
/// </summary>
|
||||
/// <returns>TRUE if successfully unregistered, FALSE if not</returns>
|
||||
bool UnregisterLogHandler(ILogHandler handler);
|
||||
//bool UnregisterLogHandler(ILogHandler handler);
|
||||
|
||||
/// <summary>
|
||||
/// Removes all log handlers that match the given assembly name
|
||||
@ -19,6 +19,12 @@
|
||||
/// <returns>Number of log handlers that were unregistered</returns>
|
||||
int UnregisterLogHandlerType(string assemblyName);
|
||||
|
||||
/// <summary>
|
||||
/// Unregisters all registered log handlers
|
||||
/// </summary>
|
||||
/// <returns>Number of log handlers that were unregistered</returns>
|
||||
int UnregisterAllLogHandlers();
|
||||
|
||||
/// <summary>
|
||||
/// Gets or sets the log level
|
||||
/// </summary>
|
||||
|
Loading…
Reference in New Issue
Block a user