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>
|
||||||
<ItemGroup>
|
<ItemGroup>
|
||||||
<Compile Include="ConsoleLogHandlerTest.cs" />
|
<Compile Include="ConsoleLogHandlerTest.cs" />
|
||||||
|
<Compile Include="DefaultLoggerTest.cs" />
|
||||||
<Compile Include="DefaultMessageHandlerTest.cs" />
|
<Compile Include="DefaultMessageHandlerTest.cs" />
|
||||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||||
</ItemGroup>
|
</ItemGroup>
|
||||||
|
@ -40,7 +40,7 @@ namespace SimpleLog {
|
|||||||
IMessageHandler messageHandler;
|
IMessageHandler messageHandler;
|
||||||
foreach (ILogHandler handler in this.LogHandlers) {
|
foreach (ILogHandler handler in this.LogHandlers) {
|
||||||
allowedLevel = handler.LogLevel ?? (LogLevel)this.LogLevel;
|
allowedLevel = handler.LogLevel ?? (LogLevel)this.LogLevel;
|
||||||
if (level <= allowedLevel) {
|
if (level >= allowedLevel) {
|
||||||
messageHandler = handler.MessageHandler ?? this.MessageHandler;
|
messageHandler = handler.MessageHandler ?? this.MessageHandler;
|
||||||
messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat;
|
messageHandler.DateFormat = messageHandler.DateFormat ?? this.DateFormat;
|
||||||
|
|
||||||
@ -59,24 +59,18 @@ namespace SimpleLog {
|
|||||||
this.LogHandlers.Add(handler);
|
this.LogHandlers.Add(handler);
|
||||||
}
|
}
|
||||||
|
|
||||||
public bool UnregisterLogHandler(ILogHandler handler) {
|
public int UnregisterLogHandlerType(string qualifiedClassName) {
|
||||||
if (this.LogHandlers.Contains(handler)) {
|
return this.LogHandlers.RemoveAll(
|
||||||
this.LogHandlers.Remove(handler);
|
delegate(ILogHandler handler) {
|
||||||
handler.GracefulShutDown();
|
//Console.WriteLine(handler.GetType().ToString() == qualifiedClassName);
|
||||||
return true;
|
return (handler.GetType().ToString() == qualifiedClassName);
|
||||||
}
|
}
|
||||||
|
);
|
||||||
return false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public int UnregisterLogHandlerType(string assemblyName) {
|
public int UnregisterAllLogHandlers() {
|
||||||
int count = 0;
|
int count = this.LogHandlerCount;
|
||||||
foreach (ILogHandler handler in this.LogHandlers) {
|
this.LogHandlers.RemoveRange(0, count);
|
||||||
if (handler.GetType().AssemblyQualifiedName == assemblyName) {
|
|
||||||
count += this.UnregisterLogHandler(handler) ? 1 : 0;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -118,5 +112,14 @@ namespace SimpleLog {
|
|||||||
}
|
}
|
||||||
#endregion
|
#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
|
/// Unregisters the given log handler
|
||||||
/// </summary>
|
/// </summary>
|
||||||
/// <returns>TRUE if successfully unregistered, FALSE if not</returns>
|
/// <returns>TRUE if successfully unregistered, FALSE if not</returns>
|
||||||
bool UnregisterLogHandler(ILogHandler handler);
|
//bool UnregisterLogHandler(ILogHandler handler);
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Removes all log handlers that match the given assembly name
|
/// Removes all log handlers that match the given assembly name
|
||||||
@ -19,6 +19,12 @@
|
|||||||
/// <returns>Number of log handlers that were unregistered</returns>
|
/// <returns>Number of log handlers that were unregistered</returns>
|
||||||
int UnregisterLogHandlerType(string assemblyName);
|
int UnregisterLogHandlerType(string assemblyName);
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Unregisters all registered log handlers
|
||||||
|
/// </summary>
|
||||||
|
/// <returns>Number of log handlers that were unregistered</returns>
|
||||||
|
int UnregisterAllLogHandlers();
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Gets or sets the log level
|
/// Gets or sets the log level
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
Loading…
Reference in New Issue
Block a user