From af36a0fcc05e7c0fa3437dd3109fa30afca0e4f6 Mon Sep 17 00:00:00 2001 From: Matthias Gernand Date: Thu, 4 Nov 2021 21:05:37 +0100 Subject: [PATCH] Test re-factoring. --- .../Program.cs | 31 +++++++ .../NHibernate.Logging.CommonLogging.csproj | 14 ++- .../CommonLogging/CommonLogMock.cs | 47 +++------- .../CommonLogging/CommonLoggingLoggerTest.cs | 91 ++++++++++--------- 4 files changed, 104 insertions(+), 79 deletions(-) diff --git a/Source/NHibernate.Logging.CommonLogging.Demo/Program.cs b/Source/NHibernate.Logging.CommonLogging.Demo/Program.cs index 071c551..378a6ca 100644 --- a/Source/NHibernate.Logging.CommonLogging.Demo/Program.cs +++ b/Source/NHibernate.Logging.CommonLogging.Demo/Program.cs @@ -38,6 +38,37 @@ public static void Main() Console.WriteLine(customer); } + Console.WriteLine(); + Console.WriteLine("-------------------------------------------------"); + INHibernateLogger logger = NHibernateLogger.For("Demo"); + + logger.Debug("Single Message"); + logger.Debug("Formatted Message {0}", 1); + logger.Debug(new Exception("Exception"), "Exception Message"); + logger.Debug(new Exception("Exception"), "Formatted Exception Message {0}", 1); + + logger.Info("Single Message"); + logger.Info("Formatted Message {0}", 1); + logger.Info(new Exception("Exception"), "Exception Message"); + logger.Info(new Exception("Exception"), "Formatted Exception Message {0}", 1); + + logger.Warn("Single Message"); + logger.Warn("Formatted Message {0}", 1); + logger.Warn(new Exception("Exception"), "Exception Message"); + logger.Warn(new Exception("Exception"), "Formatted Exception Message {0}", 1); + + logger.Error("Single Message"); + logger.Error("Formatted Message {0}", 1); + logger.Error(new Exception("Exception"), "Exception Message"); + logger.Error(new Exception("Exception"), "Formatted Exception Message {0}", 1); + + logger.Fatal("Single Message"); + logger.Fatal("Formatted Message {0}", 1); + logger.Fatal(new Exception("Exception"), "Exception Message"); + logger.Fatal(new Exception("Exception"), "Formatted Exception Message {0}", 1); + + Console.WriteLine("-------------------------------------------------"); + Console.WriteLine(); Console.WriteLine("Press any key to quit..."); Console.ReadKey(); diff --git a/Source/NHibernate.Logging.CommonLogging/NHibernate.Logging.CommonLogging.csproj b/Source/NHibernate.Logging.CommonLogging/NHibernate.Logging.CommonLogging.csproj index f602154..5c3b15e 100644 --- a/Source/NHibernate.Logging.CommonLogging/NHibernate.Logging.CommonLogging.csproj +++ b/Source/NHibernate.Logging.CommonLogging/NHibernate.Logging.CommonLogging.csproj @@ -22,14 +22,18 @@ - - + - - Always - + + Always + + + + + + diff --git a/Source/NHibernate.Logging.UnitTests/CommonLogging/CommonLogMock.cs b/Source/NHibernate.Logging.UnitTests/CommonLogging/CommonLogMock.cs index 85c8364..8340768 100644 --- a/Source/NHibernate.Logging.UnitTests/CommonLogging/CommonLogMock.cs +++ b/Source/NHibernate.Logging.UnitTests/CommonLogging/CommonLogMock.cs @@ -1,27 +1,10 @@ -namespace NHibernate.Logging.UnitTests +namespace NHibernate.Logging.UnitTests.CommonLogging { using System; using Common.Logging; internal class CommonLogMock : ILog { - public int trace; - public int traceException; - public int traceFormat; - public int debug; - public int debugException; - public int debugFormat; - public int info; - public int infoException; - public int infoFormat; - public int warn; - public int warnException; - public int warnFormat; - public int error; - public int errorException; - public int errorFormat; - public int fatal; - public int fatalException; public int isTraceEnabled; public int isDebugEnabled; public int isInfoEnabled; @@ -84,17 +67,17 @@ public void Trace(IFormatProvider formatProvider, Action f #region Debug public void Debug(object message) { - this.debug++; + throw new NotImplementedException(); } public void Debug(object message, Exception exception) { - this.debugException++; + throw new NotImplementedException(); } public void DebugFormat(string format, params object[] args) { - this.debugFormat++; + throw new NotImplementedException(); } public void DebugFormat(string format, Exception exception, params object[] args) @@ -136,17 +119,17 @@ public void Debug(IFormatProvider formatProvider, Action f #region Info public void Info(object message) { - this.info++; + throw new NotImplementedException(); } public void Info(object message, Exception exception) { - this.infoException++; + throw new NotImplementedException(); } public void InfoFormat(string format, params object[] args) { - this.infoFormat++; + throw new NotImplementedException(); } public void InfoFormat(string format, Exception exception, params object[] args) @@ -188,17 +171,17 @@ public void Info(IFormatProvider formatProvider, Action fo #region Warn public void Warn(object message) { - this.warn++; + throw new NotImplementedException(); } public void Warn(object message, Exception exception) { - this.warnException++; + throw new NotImplementedException(); } public void WarnFormat(string format, params object[] args) { - this.warnFormat++; + throw new NotImplementedException(); } public void WarnFormat(string format, Exception exception, params object[] args) @@ -240,17 +223,17 @@ public void Warn(IFormatProvider formatProvider, Action fo #region Error public void Error(object message) { - this.error++; + throw new NotImplementedException(); } public void Error(object message, Exception exception) { - this.errorException++; + throw new NotImplementedException(); } public void ErrorFormat(string format, params object[] args) { - this.errorFormat++; + throw new NotImplementedException(); } public void ErrorFormat(string format, Exception exception, params object[] args) @@ -292,12 +275,12 @@ public void Error(IFormatProvider formatProvider, Action f #region Fatal public void Fatal(object message) { - this.fatal++; + throw new NotImplementedException(); } public void Fatal(object message, Exception exception) { - this.fatalException++; + throw new NotImplementedException(); } public void FatalFormat(string format, params object[] args) diff --git a/Source/NHibernate.Logging.UnitTests/CommonLogging/CommonLoggingLoggerTest.cs b/Source/NHibernate.Logging.UnitTests/CommonLogging/CommonLoggingLoggerTest.cs index 398ee15..bcefe18 100644 --- a/Source/NHibernate.Logging.UnitTests/CommonLogging/CommonLoggingLoggerTest.cs +++ b/Source/NHibernate.Logging.UnitTests/CommonLogging/CommonLoggingLoggerTest.cs @@ -1,4 +1,4 @@ -namespace NHibernate.Logging.UnitTests +namespace NHibernate.Logging.UnitTests.CommonLogging { using FluentAssertions; using NHibernate; @@ -8,49 +8,56 @@ [TestFixture] public class CommonLoggingLoggerTest { + private CommonLogMock logMock; + private CommonLoggingLogger logger; + + [SetUp] + public void SetUp() + { + this.logMock = new CommonLogMock(); + this.logger = new CommonLoggingLogger(this.logMock); + } + + [Test] + public void ShouldCall_IsTranceEnabled() + { + this.logger.IsEnabled(NHibernateLogLevel.Trace); + this.logMock.isTraceEnabled.Should().Be(1); + } + + [Test] + public void ShouldCall_IsDebugEnabled() + { + this.logger.IsEnabled(NHibernateLogLevel.Debug); + this.logMock.isDebugEnabled.Should().Be(1); + } + + [Test] + public void ShouldCall_IsInfoEnabled() + { + this.logger.IsEnabled(NHibernateLogLevel.Info); + this.logMock.isInfoEnabled.Should().Be(1); + } + + [Test] + public void ShouldCall_IsWarnEnabled() + { + this.logger.IsEnabled(NHibernateLogLevel.Warn); + this.logMock.isWarnEnabled.Should().Be(1); + } + + [Test] + public void ShouldCall_IsErrorEnabled() + { + this.logger.IsEnabled(NHibernateLogLevel.Error); + this.logMock.isErrorEnabled.Should().Be(1); + } + [Test] - public void CallingMethods() + public void ShouldCall_IsFatalEnabled() { - CommonLogMock logMock = new CommonLogMock(); - CommonLoggingLogger logger = new CommonLoggingLogger(logMock); - - logger.IsEnabled(NHibernateLogLevel.Trace); - logger.IsEnabled(NHibernateLogLevel.Debug); - logger.IsEnabled(NHibernateLogLevel.Info); - logger.IsEnabled(NHibernateLogLevel.Warn); - logger.IsEnabled(NHibernateLogLevel.Error); - logger.IsEnabled(NHibernateLogLevel.Fatal); - - logger.Log(NHibernateLogLevel.Trace, default, null); - logger.Log(NHibernateLogLevel.Debug, default, null); - logger.Log(NHibernateLogLevel.Info, default, null); - logger.Log(NHibernateLogLevel.Warn, default, null); - logger.Log(NHibernateLogLevel.Error, default, null); - logger.Log(NHibernateLogLevel.Fatal, default, null); - - logMock.trace.Should().Be(1); - logMock.traceException.Should().Be(1); - logMock.traceFormat.Should().Be(1); - logMock.debug.Should().Be(1); - logMock.debugException.Should().Be(1); - logMock.debugFormat.Should().Be(1); - logMock.info.Should().Be(1); - logMock.infoException.Should().Be(1); - logMock.infoFormat.Should().Be(1); - logMock.warn.Should().Be(1); - logMock.warnException.Should().Be(1); - logMock.warnFormat.Should().Be(1); - logMock.error.Should().Be(1); - logMock.errorException.Should().Be(1); - logMock.errorFormat.Should().Be(1); - logMock.fatal.Should().Be(1); - logMock.fatalException.Should().Be(1); - logMock.isTraceEnabled.Should().Be(1); - logMock.isDebugEnabled.Should().Be(1); - logMock.isInfoEnabled.Should().Be(1); - logMock.isWarnEnabled.Should().Be(1); - logMock.isErrorEnabled.Should().Be(1); - logMock.isFatalEnabled.Should().Be(1); + this.logger.IsEnabled(NHibernateLogLevel.Fatal); + this.logMock.isFatalEnabled.Should().Be(1); } } }