2017-07-06 18:18:39 +02:00
|
|
|
|
/*
|
2024-03-05 18:37:42 +01:00
|
|
|
|
* Copyright (c) 2024 ETH Zürich, IT Services
|
2017-07-06 18:18:39 +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/.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Collections.Generic;
|
|
|
|
|
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-07-06 18:18:39 +02:00
|
|
|
|
|
2018-08-31 10:06:27 +02:00
|
|
|
|
namespace SafeExamBrowser.Logging.UnitTests
|
2017-07-06 18:18:39 +02:00
|
|
|
|
{
|
|
|
|
|
[TestClass]
|
|
|
|
|
public class LoggerTests
|
|
|
|
|
{
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustAddMessagesToLog()
|
|
|
|
|
{
|
|
|
|
|
var sut = new Logger();
|
2018-01-23 15:33:54 +01:00
|
|
|
|
var debug = "I'm a debug message";
|
2017-07-06 18:18:39 +02:00
|
|
|
|
var info = "I'm an info message";
|
|
|
|
|
var warn = "I'm a warning!";
|
|
|
|
|
var error = "I AM AN ERROR!!";
|
2017-10-11 07:38:25 +02:00
|
|
|
|
var exceptionMessage = "I'm an exception message";
|
|
|
|
|
var exception = new Exception(exceptionMessage);
|
|
|
|
|
var message = "I'm a simple text message";
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
sut.Debug(debug);
|
2017-07-06 18:18:39 +02:00
|
|
|
|
sut.Info(info);
|
|
|
|
|
sut.Warn(warn);
|
|
|
|
|
sut.Error(error);
|
2017-10-11 07:38:25 +02:00
|
|
|
|
sut.Error(error, exception);
|
|
|
|
|
sut.Log(message);
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
|
|
|
|
var log = sut.GetLog();
|
|
|
|
|
|
2019-01-23 08:12:15 +01:00
|
|
|
|
Assert.IsTrue(log.Count == 7);
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
Assert.IsTrue(debug.Equals((log[0] as ILogMessage).Message));
|
|
|
|
|
Assert.IsTrue((log[0] as ILogMessage).Severity == LogLevel.Debug);
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
Assert.IsTrue(info.Equals((log[1] as ILogMessage).Message));
|
|
|
|
|
Assert.IsTrue((log[1] as ILogMessage).Severity == LogLevel.Info);
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
Assert.IsTrue(warn.Equals((log[2] as ILogMessage).Message));
|
|
|
|
|
Assert.IsTrue((log[2] as ILogMessage).Severity == LogLevel.Warning);
|
2017-10-11 07:38:25 +02:00
|
|
|
|
|
|
|
|
|
Assert.IsTrue(error.Equals((log[3] as ILogMessage).Message));
|
|
|
|
|
Assert.IsTrue((log[3] as ILogMessage).Severity == LogLevel.Error);
|
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
Assert.IsTrue(error.Equals((log[4] as ILogMessage).Message));
|
|
|
|
|
Assert.IsTrue((log[4] as ILogMessage).Severity == LogLevel.Error);
|
|
|
|
|
Assert.IsTrue((log[5] as ILogText).Text.Contains(exceptionMessage));
|
2017-10-11 07:38:25 +02:00
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
Assert.IsTrue(message.Equals((log[6] as ILogText).Text));
|
2017-07-06 18:18:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 15:27:11 +01:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustAddInnerExceptionsToLog()
|
|
|
|
|
{
|
|
|
|
|
var sut = new Logger();
|
|
|
|
|
var outerMessage = "Some message for the outer exception";
|
|
|
|
|
var innerMessage = "BAAAAM! Inner one here.";
|
|
|
|
|
var innerInnerMessage = "Yikes, a null reference...";
|
|
|
|
|
var exception = new Exception(outerMessage, new ArgumentException(innerMessage, new NullReferenceException(innerInnerMessage)));
|
|
|
|
|
|
|
|
|
|
sut.Error("blubb", exception);
|
|
|
|
|
|
|
|
|
|
var log = sut.GetLog();
|
|
|
|
|
var logText = log[1] as ILogText;
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(2, log.Count);
|
|
|
|
|
Assert.IsTrue(logText.Text.Contains(outerMessage));
|
|
|
|
|
Assert.IsTrue(logText.Text.Contains(innerMessage));
|
|
|
|
|
Assert.IsTrue(logText.Text.Contains(innerInnerMessage));
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 18:18:39 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustReturnCopyOfLog()
|
|
|
|
|
{
|
|
|
|
|
var sut = new Logger();
|
2018-01-23 15:33:54 +01:00
|
|
|
|
var debug = "I'm a debug message";
|
2017-07-06 18:18:39 +02:00
|
|
|
|
var info = "I'm an info message";
|
|
|
|
|
var warn = "I'm a warning!";
|
|
|
|
|
var error = "I AM AN ERROR!!";
|
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
sut.Debug(debug);
|
2017-07-06 18:18:39 +02:00
|
|
|
|
sut.Info(info);
|
|
|
|
|
sut.Warn(warn);
|
|
|
|
|
sut.Error(error);
|
|
|
|
|
|
|
|
|
|
var log1 = sut.GetLog();
|
|
|
|
|
var log2 = sut.GetLog();
|
|
|
|
|
|
|
|
|
|
Assert.AreNotSame(log1, log2);
|
|
|
|
|
|
|
|
|
|
foreach (var message in log1)
|
|
|
|
|
{
|
|
|
|
|
Assert.AreNotSame(message, log2[log1.IndexOf(message)]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-10 15:44:04 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotAllowLoggingNull()
|
|
|
|
|
{
|
|
|
|
|
var sut = new Logger();
|
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
Assert.ThrowsException<ArgumentNullException>(() => sut.Debug(null));
|
2017-10-10 15:44:04 +02:00
|
|
|
|
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));
|
2017-10-11 07:38:25 +02:00
|
|
|
|
Assert.ThrowsException<ArgumentNullException>(() => sut.Error("Hello world!", null));
|
|
|
|
|
Assert.ThrowsException<ArgumentNullException>(() => sut.Error(null, new Exception()));
|
2017-10-10 15:44:04 +02:00
|
|
|
|
Assert.ThrowsException<ArgumentNullException>(() => sut.Log((string) null));
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 18:18:39 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
[ExpectedException(typeof(ArgumentNullException))]
|
|
|
|
|
public void MustNotAllowNullObserver()
|
|
|
|
|
{
|
|
|
|
|
var sut = new Logger();
|
|
|
|
|
|
|
|
|
|
sut.Subscribe(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotSubscribeSameObserverMultipleTimes()
|
|
|
|
|
{
|
|
|
|
|
var sut = new Logger();
|
|
|
|
|
var observer = new Mock<ILogObserver>();
|
|
|
|
|
|
|
|
|
|
observer.Setup(o => o.Notify(It.IsAny<ILogMessage>()));
|
|
|
|
|
|
|
|
|
|
sut.Subscribe(observer.Object);
|
|
|
|
|
sut.Subscribe(observer.Object);
|
|
|
|
|
sut.Subscribe(observer.Object);
|
|
|
|
|
|
|
|
|
|
sut.Info("Blubb");
|
|
|
|
|
|
|
|
|
|
observer.Verify(o => o.Notify(It.IsAny<ILogMessage>()), Times.Once());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustNotFailWhenRemovingNullObserver()
|
|
|
|
|
{
|
|
|
|
|
var sut = new Logger();
|
|
|
|
|
|
|
|
|
|
sut.Unsubscribe(null);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustSubscribeObserver()
|
|
|
|
|
{
|
|
|
|
|
var sut = new Logger();
|
|
|
|
|
var observer = new Mock<ILogObserver>();
|
|
|
|
|
var message = "Blubb";
|
2017-07-07 15:46:32 +02:00
|
|
|
|
var messages = new List<ILogContent>();
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
observer.Setup(o => o.Notify(It.IsAny<ILogContent>())).Callback<ILogContent>(m => messages.Add(m));
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
|
|
|
|
sut.Subscribe(observer.Object);
|
|
|
|
|
sut.Info(message);
|
|
|
|
|
sut.Warn(message);
|
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
observer.Verify(o => o.Notify(It.IsAny<ILogContent>()), Times.Exactly(2));
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
|
|
|
|
Assert.IsTrue(messages.Count == 2);
|
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
Assert.IsTrue((messages[0] as ILogMessage).Severity == LogLevel.Info);
|
|
|
|
|
Assert.IsTrue(message.Equals((messages[0] as ILogMessage).Message));
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
2017-07-26 14:36:20 +02:00
|
|
|
|
Assert.IsTrue((messages[1] as ILogMessage).Severity == LogLevel.Warning);
|
2017-07-07 15:46:32 +02:00
|
|
|
|
Assert.IsTrue(message.Equals((messages[1] as ILogMessage).Message));
|
2017-07-06 18:18:39 +02:00
|
|
|
|
}
|
|
|
|
|
|
2019-04-04 16:06:05 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustRespectLogLevel()
|
|
|
|
|
{
|
|
|
|
|
var sut = new Logger();
|
|
|
|
|
|
|
|
|
|
sut.LogLevel = LogLevel.Error;
|
|
|
|
|
sut.Debug("debug");
|
|
|
|
|
sut.Info("info");
|
|
|
|
|
sut.Warn("warn");
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(0, sut.GetLog().Count);
|
|
|
|
|
|
|
|
|
|
sut = new Logger();
|
|
|
|
|
sut.LogLevel = LogLevel.Warning;
|
|
|
|
|
sut.Debug("debug");
|
|
|
|
|
sut.Info("info");
|
|
|
|
|
sut.Warn("warn");
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(1, sut.GetLog().Count);
|
|
|
|
|
|
|
|
|
|
sut = new Logger();
|
|
|
|
|
sut.LogLevel = LogLevel.Info;
|
|
|
|
|
sut.Debug("debug");
|
|
|
|
|
sut.Info("info");
|
|
|
|
|
sut.Warn("warn");
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(2, sut.GetLog().Count);
|
|
|
|
|
|
|
|
|
|
sut = new Logger();
|
|
|
|
|
sut.LogLevel = LogLevel.Debug;
|
|
|
|
|
sut.Debug("debug");
|
|
|
|
|
sut.Info("info");
|
|
|
|
|
sut.Warn("warn");
|
|
|
|
|
|
|
|
|
|
Assert.AreEqual(3, sut.GetLog().Count);
|
|
|
|
|
}
|
|
|
|
|
|
2017-07-06 18:18:39 +02:00
|
|
|
|
[TestMethod]
|
|
|
|
|
public void MustUnsubscribeObserver()
|
|
|
|
|
{
|
|
|
|
|
var sut = new Logger();
|
|
|
|
|
var observer = new Mock<ILogObserver>();
|
|
|
|
|
var message = "Blubb";
|
2017-07-07 15:46:32 +02:00
|
|
|
|
var messages = new List<ILogContent>();
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
observer.Setup(o => o.Notify(It.IsAny<ILogContent>())).Callback<ILogContent>(m => messages.Add(m));
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
|
|
|
|
sut.Subscribe(observer.Object);
|
|
|
|
|
sut.Info(message);
|
|
|
|
|
sut.Unsubscribe(observer.Object);
|
|
|
|
|
sut.Warn(message);
|
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
observer.Verify(o => o.Notify(It.IsAny<ILogContent>()), Times.Once());
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
|
|
|
|
Assert.IsTrue(messages.Count == 1);
|
|
|
|
|
|
2017-07-07 15:46:32 +02:00
|
|
|
|
Assert.IsTrue((messages[0] as ILogMessage).Severity == LogLevel.Info);
|
|
|
|
|
Assert.IsTrue(message.Equals((messages[0] as ILogMessage).Message));
|
2017-07-06 18:18:39 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|