SEBWIN-183: Extended unit testing for logging components (and fixed issues encountered while doing so).
This commit is contained in:
parent
135fbd76ec
commit
f952904cf0
9 changed files with 170 additions and 4 deletions
|
@ -16,37 +16,44 @@ namespace SafeExamBrowser.Contracts.Logging
|
|||
/// <summary>
|
||||
/// Logs the given message with severity <b>INFO</b>.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException" />
|
||||
void Info(string message);
|
||||
|
||||
/// <summary>
|
||||
/// Logs the given message with severity <b>WARNING</b>.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException" />
|
||||
void Warn(string message);
|
||||
|
||||
/// <summary>
|
||||
/// Logs the given message with severity <b>ERROR</b>.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException" />
|
||||
void Error(string message);
|
||||
|
||||
/// <summary>
|
||||
/// Logs the given message with severity <b>ERROR</b> and includes information about
|
||||
/// the specified exception (i.e. type, message and stacktrace).
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException" />
|
||||
void Error(string message, Exception exception);
|
||||
|
||||
/// <summary>
|
||||
/// Logs the given message as raw text.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException" />
|
||||
void Log(string message);
|
||||
|
||||
/// <summary>
|
||||
/// Appends the given content to the log.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException" />
|
||||
void Log(ILogContent content);
|
||||
|
||||
/// <summary>
|
||||
/// Suscribes an observer to the application log.
|
||||
/// </summary>
|
||||
/// <exception cref="ArgumentNullException" />
|
||||
void Subscribe(ILogObserver observer);
|
||||
|
||||
/// <summary>
|
||||
|
|
|
@ -0,0 +1,52 @@
|
|||
/*
|
||||
* Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET)
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using SafeExamBrowser.Contracts.Logging;
|
||||
using SafeExamBrowser.Core.Logging;
|
||||
|
||||
namespace SafeExamBrowser.Core.UnitTests.Logging
|
||||
{
|
||||
[TestClass]
|
||||
public class DefaultLogFormatterTests
|
||||
{
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(NotImplementedException))]
|
||||
public void MustReportNotYetImplementedLogContent()
|
||||
{
|
||||
var sut = new DefaultLogFormatter();
|
||||
|
||||
sut.Format(new NewLogContentType());
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MustReturnRawTextForLogMessage()
|
||||
{
|
||||
var sut = new DefaultLogFormatter();
|
||||
var entry = new LogText("Must return this text...");
|
||||
|
||||
var text = sut.Format(entry);
|
||||
|
||||
Assert.AreEqual(entry.Text, text);
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MustCorrectlyFormatLogMessage()
|
||||
{
|
||||
var sut = new DefaultLogFormatter();
|
||||
var date = new DateTime(2017, 10, 10, 15, 24, 38);
|
||||
var threadInfo = new ThreadInfo(1234, "ABC");
|
||||
var entry = new LogMessage(date, LogLevel.Warning, "Here's a warning message...", threadInfo);
|
||||
|
||||
var text = sut.Format(entry);
|
||||
|
||||
Assert.AreEqual($"2017-10-10 15:24:38.000 [1234: ABC] - WARNING: Here's a warning message...", text);
|
||||
}
|
||||
}
|
||||
}
|
|
@ -67,6 +67,19 @@ namespace SafeExamBrowser.Core.UnitTests.Logging
|
|||
}
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
public void MustNotAllowLoggingNull()
|
||||
{
|
||||
var sut = new Logger();
|
||||
|
||||
Assert.ThrowsException<ArgumentNullException>(() => sut.Info(null));
|
||||
Assert.ThrowsException<ArgumentNullException>(() => sut.Warn(null));
|
||||
Assert.ThrowsException<ArgumentNullException>(() => sut.Error(null));
|
||||
Assert.ThrowsException<ArgumentNullException>(() => sut.Error(null, null));
|
||||
Assert.ThrowsException<ArgumentNullException>(() => sut.Log((string) null));
|
||||
Assert.ThrowsException<ArgumentNullException>(() => sut.Log((ILogContent) null));
|
||||
}
|
||||
|
||||
[TestMethod]
|
||||
[ExpectedException(typeof(ArgumentNullException))]
|
||||
public void MustNotAllowNullObserver()
|
||||
|
|
46
SafeExamBrowser.Core.UnitTests/Logging/ModuleLoggerTests.cs
Normal file
46
SafeExamBrowser.Core.UnitTests/Logging/ModuleLoggerTests.cs
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET)
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
||||
using Moq;
|
||||
using SafeExamBrowser.Contracts.Logging;
|
||||
using SafeExamBrowser.Core.Logging;
|
||||
|
||||
namespace SafeExamBrowser.Core.UnitTests.Logging
|
||||
{
|
||||
[TestClass]
|
||||
public class ModuleLoggerTests
|
||||
{
|
||||
[TestMethod]
|
||||
public void MustCorrectlyForwardCalls()
|
||||
{
|
||||
var loggerMock = new Mock<ILogger>();
|
||||
var logObserverMock = new Mock<ILogObserver>();
|
||||
var logText = new LogText("Log text");
|
||||
var sut = new ModuleLogger(loggerMock.Object, typeof(ModuleLoggerTests));
|
||||
|
||||
sut.Info("Info");
|
||||
sut.Warn("Warning");
|
||||
sut.Error("Error");
|
||||
sut.Log("Raw text");
|
||||
sut.Log(logText);
|
||||
sut.Subscribe(logObserverMock.Object);
|
||||
sut.Unsubscribe(logObserverMock.Object);
|
||||
sut.GetLog();
|
||||
|
||||
loggerMock.Verify(l => l.Info($"[{nameof(ModuleLoggerTests)}] Info"), Times.Once);
|
||||
loggerMock.Verify(l => l.Warn($"[{nameof(ModuleLoggerTests)}] Warning"), Times.Once);
|
||||
loggerMock.Verify(l => l.Error($"[{nameof(ModuleLoggerTests)}] Error"), Times.Once);
|
||||
loggerMock.Verify(l => l.Log("Raw text"), Times.Once);
|
||||
loggerMock.Verify(l => l.Log(logText), Times.Once);
|
||||
loggerMock.Verify(l => l.Subscribe(logObserverMock.Object), Times.Once);
|
||||
loggerMock.Verify(l => l.Unsubscribe(logObserverMock.Object), Times.Once);
|
||||
loggerMock.Verify(l => l.GetLog(), Times.Once);
|
||||
}
|
||||
}
|
||||
}
|
21
SafeExamBrowser.Core.UnitTests/Logging/NewLogContentType.cs
Normal file
21
SafeExamBrowser.Core.UnitTests/Logging/NewLogContentType.cs
Normal file
|
@ -0,0 +1,21 @@
|
|||
/*
|
||||
* Copyright (c) 2017 ETH Zürich, Educational Development and Technology (LET)
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
using System;
|
||||
using SafeExamBrowser.Contracts.Logging;
|
||||
|
||||
namespace SafeExamBrowser.Core.UnitTests.Logging
|
||||
{
|
||||
class NewLogContentType : ILogContent
|
||||
{
|
||||
public object Clone()
|
||||
{
|
||||
throw new NotImplementedException();
|
||||
}
|
||||
}
|
||||
}
|
|
@ -77,7 +77,10 @@
|
|||
<Compile Include="Behaviour\ShutdownControllerTests.cs" />
|
||||
<Compile Include="I18n\TextTests.cs" />
|
||||
<Compile Include="I18n\XmlTextResourceTests.cs" />
|
||||
<Compile Include="Logging\DefaultLogFormatterTests.cs" />
|
||||
<Compile Include="Logging\LoggerTests.cs" />
|
||||
<Compile Include="Logging\ModuleLoggerTests.cs" />
|
||||
<Compile Include="Logging\NewLogContentType.cs" />
|
||||
<Compile Include="Properties\AssemblyInfo.cs" />
|
||||
</ItemGroup>
|
||||
<ItemGroup>
|
||||
|
|
|
@ -9,9 +9,9 @@
|
|||
using System;
|
||||
using SafeExamBrowser.Contracts.Logging;
|
||||
|
||||
namespace SafeExamBrowser.Core.Entities
|
||||
namespace SafeExamBrowser.Core.Logging
|
||||
{
|
||||
class LogMessage : ILogMessage
|
||||
public class LogMessage : ILogMessage
|
||||
{
|
||||
public DateTime DateTime { get; private set; }
|
||||
public LogLevel Severity { get; private set; }
|
||||
|
|
|
@ -12,7 +12,6 @@ using System.Linq;
|
|||
using System.Text;
|
||||
using System.Threading;
|
||||
using SafeExamBrowser.Contracts.Logging;
|
||||
using SafeExamBrowser.Core.Entities;
|
||||
|
||||
namespace SafeExamBrowser.Core.Logging
|
||||
{
|
||||
|
@ -24,21 +23,46 @@ namespace SafeExamBrowser.Core.Logging
|
|||
|
||||
public void Info(string message)
|
||||
{
|
||||
if (message == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(message));
|
||||
}
|
||||
|
||||
Add(LogLevel.Info, message);
|
||||
}
|
||||
|
||||
public void Warn(string message)
|
||||
{
|
||||
if (message == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(message));
|
||||
}
|
||||
|
||||
Add(LogLevel.Warning, message);
|
||||
}
|
||||
|
||||
public void Error(string message)
|
||||
{
|
||||
if (message == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(message));
|
||||
}
|
||||
|
||||
Add(LogLevel.Error, message);
|
||||
}
|
||||
|
||||
public void Error(string message, Exception exception)
|
||||
{
|
||||
if (message == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(message));
|
||||
}
|
||||
|
||||
if (exception == null)
|
||||
{
|
||||
throw new ArgumentNullException(nameof(exception));
|
||||
}
|
||||
|
||||
var details = new StringBuilder();
|
||||
|
||||
details.AppendLine();
|
||||
|
|
|
@ -11,7 +11,7 @@ using SafeExamBrowser.Contracts.Logging;
|
|||
|
||||
namespace SafeExamBrowser.Core.Logging
|
||||
{
|
||||
class ThreadInfo : IThreadInfo
|
||||
public class ThreadInfo : IThreadInfo
|
||||
{
|
||||
public int Id { get; private set; }
|
||||
public string Name { get; private set; }
|
||||
|
|
Loading…
Reference in a new issue