2017-10-10 15:44:04 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2017-10-10 15:44:04 +02:00
|
|
|
|
*
|
|
|
|
|
* 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/.
|
|
|
|
|
*/
|
|
|
|
|
|
2017-10-11 07:38:25 +02:00
|
|
|
|
using System;
|
2017-10-10 15:44:04 +02:00
|
|
|
|
using Microsoft.VisualStudio.TestTools.UnitTesting;
|
|
|
|
|
using Moq;
|
2019-08-30 09:55:26 +02:00
|
|
|
|
using SafeExamBrowser.Logging.Contracts;
|
2019-09-06 09:39:28 +02:00
|
|
|
|
using SafeExamBrowser.Settings.Logging;
|
2017-10-10 15:44:04 +02:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Logging.UnitTests
|
2017-10-10 15:44:04 +02:00
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class ModuleLoggerTests
|
|
|
|
|
{
|
2018-08-31 15:29:36 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCorrectlyClone()
|
|
|
|
|
{
|
|
|
|
|
var loggerMock = new Mock<ILogger>();
|
|
|
|
|
var sut = new ModuleLogger(loggerMock.Object, nameof(ModuleLoggerTests));
|
|
|
|
|
var clone = sut.CloneFor("blubb");
|
|
|
|
|
|
|
|
|
|
sut.Debug("Debug");
|
|
|
|
|
clone.Debug("Debug");
|
|
|
|
|
|
|
|
|
|
loggerMock.Verify(l => l.Debug($"[{nameof(ModuleLoggerTests)}] Debug"), Times.Once);
|
|
|
|
|
loggerMock.Verify(l => l.Debug($"[blubb] Debug"), Times.Once);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 15:44:04 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustCorrectlyForwardCalls()
|
|
|
|
|
{
|
2017-10-11 07:38:25 +02:00
|
|
|
|
var exception = new Exception();
|
2017-10-10 15:44:04 +02:00
|
|
|
|
var loggerMock = new Mock<ILogger>();
|
|
|
|
|
var logObserverMock = new Mock<ILogObserver>();
|
|
|
|
|
var logText = new LogText("Log text");
|
2018-08-31 15:29:36 +02:00
|
|
|
|
var sut = new ModuleLogger(loggerMock.Object, nameof(ModuleLoggerTests));
|
2017-10-10 15:44:04 +02:00
|
|
|
|
|
2019-04-04 16:06:05 +02:00
|
|
|
|
loggerMock.SetupGet(l => l.LogLevel).Returns(LogLevel.Error);
|
|
|
|
|
|
|
|
|
|
sut.LogLevel = LogLevel.Debug;
|
2018-03-14 15:27:11 +01:00
|
|
|
|
sut.Debug("Debug");
|
2017-10-10 15:44:04 +02:00
|
|
|
|
sut.Info("Info");
|
|
|
|
|
sut.Warn("Warning");
|
|
|
|
|
sut.Error("Error");
|
2017-10-11 07:38:25 +02:00
|
|
|
|
sut.Error("Error", exception);
|
2017-10-10 15:44:04 +02:00
|
|
|
|
sut.Log("Raw text");
|
|
|
|
|
sut.Subscribe(logObserverMock.Object);
|
|
|
|
|
sut.Unsubscribe(logObserverMock.Object);
|
|
|
|
|
sut.GetLog();
|
|
|
|
|
|
2019-04-04 16:06:05 +02:00
|
|
|
|
loggerMock.VerifySet(l => l.LogLevel = LogLevel.Debug, Times.Once);
|
2018-03-14 15:27:11 +01:00
|
|
|
|
loggerMock.Verify(l => l.Debug($"[{nameof(ModuleLoggerTests)}] Debug"), Times.Once);
|
2017-10-10 15:44:04 +02:00
|
|
|
|
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);
|
2017-10-11 07:38:25 +02:00
|
|
|
|
loggerMock.Verify(l => l.Error($"[{nameof(ModuleLoggerTests)}] Error", exception), Times.Once);
|
2017-10-10 15:44:04 +02:00
|
|
|
|
loggerMock.Verify(l => l.Log("Raw text"), 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);
|
2019-04-04 16:06:05 +02:00
|
|
|
|
|
|
|
|
|
Assert.AreEqual(LogLevel.Error, sut.LogLevel);
|
2017-10-10 15:44:04 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|