2017-07-06 18:18:39 +02:00
|
|
|
|
/*
|
2018-01-16 08:24:00 +01:00
|
|
|
|
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
|
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;
|
|
|
|
|
using SafeExamBrowser.Contracts.Logging;
|
|
|
|
|
using SafeExamBrowser.Core.Logging;
|
|
|
|
|
|
|
|
|
|
namespace SafeExamBrowser.Core.UnitTests.Logging
|
|
|
|
|
{
|
|
|
|
|
[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";
|
|
|
|
|
var content = new LogText("I'm some raw log text...");
|
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);
|
|
|
|
|
sut.Log(content);
|
2017-07-06 18:18:39 +02:00
|
|
|
|
|
|
|
|
|
var log = sut.GetLog();
|
|
|
|
|
|
2018-01-23 15:33:54 +01:00
|
|
|
|
Assert.IsTrue(log.Count == 8);
|
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));
|
|
|
|
|
|
|
|
|
|
Assert.IsTrue(content.Text.Equals((log[7] as ILogText).Text));
|
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));
|
|
|
|
|
Assert.ThrowsException<ArgumentNullException>(() => sut.Log((ILogContent) 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
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|