SimpleLog/SimpleLog.Tests/DefaultLoggerTest.cs

128 lines
4.1 KiB
C#
Raw Permalink Normal View History

2009-04-12 02:31:35 +00:00
using System.Collections.Generic;
using NUnit.Framework;
using NUnit.Mocks;
using NUnit.Framework.SyntaxHelpers;
using SimpleLog.Framework;
2009-04-12 02:31:35 +00:00
namespace SimpleLog.Tests {
[TestFixture]
public class DefaultLoggerTest {
private DefaultLogger Logger;
private DynamicMock Handler;
2009-04-19 22:10:13 +00:00
private string TempFile;
2009-04-12 02:31:35 +00:00
[SetUp]
2009-04-19 22:10:13 +00:00
public void SetUp() {
this.Logger = new DefaultLogger();
2009-04-12 02:31:35 +00:00
this.Logger.MessageHandler = new DefaultMessageHandler();
this.Handler = new DynamicMock(typeof(ILogHandler));
2009-04-19 22:10:13 +00:00
this.TempFile = null;
2009-04-12 02:31:35 +00:00
}
[TearDown]
2009-04-19 22:10:13 +00:00
public void TearDown() {
2009-04-12 02:31:35 +00:00
this.Logger.UnregisterAllLogHandlers();
this.Logger.Enabled = true;
this.Logger.MessageHandler = new DefaultMessageHandler();
2009-04-19 22:10:13 +00:00
if (this.TempFile != null && System.IO.File.Exists(this.TempFile)) {
System.IO.File.Delete(this.TempFile);
}
2009-04-12 02:31:35 +00:00
}
[Test]
public void TestRegisterAndUnregisterLogHandler() {
this.Handler.ExpectAndReturn("GetType", typeof(ILogHandler));
ILogHandler handler = (ILogHandler)this.Handler.MockInstance;
this.Logger.RegisterLogHandler(handler);
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(1));
Assert.That(this.Logger.UnregisterLogHandlerType("SimpleLog.Framework.ILogHandler"), Is.EqualTo(1));
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(0));
2009-04-12 02:31:35 +00:00
this.Logger.RegisterLogHandler(handler);
Assert.That(this.Logger.LogHandlerCount, Is.EqualTo(1));
2009-04-12 02:31:35 +00:00
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);
2009-04-19 22:10:13 +00:00
//h4x0rz!
this.Handler.ExpectAndReturn("get_LogLevel", LogLevel.None);
2009-04-12 02:31:35 +00:00
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);
2009-04-19 22:10:13 +00:00
//h4x0rz!
this.Handler.ExpectAndReturn("get_LogLevel", LogLevel.None);
2009-04-12 02:31:35 +00:00
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);
2009-04-19 22:10:13 +00:00
//h4x0rz!
this.Handler.ExpectAndReturn("get_LogLevel", LogLevel.None);
2009-04-12 02:31:35 +00:00
Assert.That(this.Logger.Log("yay", LogLevel.Debug));
this.Handler.Verify();
msgHandler.Verify();
}
2009-04-19 22:10:13 +00:00
[Test]
public void TestLoadConfig() {
this.TempFile = System.IO.Path.GetTempFileName();
}
2009-04-12 02:31:35 +00:00
}
}