Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Part 4 of nullable and modernization #129

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 3 additions & 11 deletions src/log4net.Tests/Appender/DebugAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,17 +139,11 @@ public void MethodNameCategoryTest()

private class TestErrorHandler : IErrorHandler
{
private bool m_errorOccured = false;
public bool ErrorOccured { get; private set; }

public bool ErrorOccured
public void Error(string message, Exception? e, ErrorCode errorCode)
{
get { return m_errorOccured; }
}
#region IErrorHandler Members

public void Error(string message, Exception e, ErrorCode errorCode)
{
m_errorOccured = true;
ErrorOccured = true;
}

public void Error(string message, Exception e)
Expand All @@ -161,8 +155,6 @@ public void Error(string message)
{
Error(message, null, ErrorCode.GenericFailure);
}

#endregion
}
}
}
Expand Down
14 changes: 10 additions & 4 deletions src/log4net.Tests/Appender/RollingFileAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -69,17 +69,17 @@ private sealed class SilentErrorHandler : IErrorHandler

public void Error(string message)
{
m_buffer.Append(message + "\n");
m_buffer.Append(message + '\n');
}

public void Error(string message, Exception e)
{
m_buffer.Append(message + "\n" + e.Message + "\n");
m_buffer.Append(message + '\n' + e.Message + '\n');
}

public void Error(string message, Exception e, ErrorCode errorCode)
public void Error(string message, Exception? e, ErrorCode errorCode)
{
m_buffer.Append(message + "\n" + e.Message + "\n");
m_buffer.Append(message + '\n' + e?.Message + '\n');
}
}

Expand Down Expand Up @@ -1862,7 +1862,9 @@ private void VerifyInitializeRollBackups(int iBackups, int iMaxSizeRollBackups)
string sBaseFile = "LogFile.log";
var arrFiles = new List<string> { "junk1" };
for (int i = 0; i < iBackups; i++)
{
arrFiles.Add(MakeFileName(sBaseFile, i));
}

RollingFileAppender rfa = new();
rfa.RollingStyle = RollingFileAppender.RollingMode.Size;
Expand All @@ -1876,9 +1878,13 @@ private void VerifyInitializeRollBackups(int iBackups, int iMaxSizeRollBackups)
// 2 = file.log.1
// 3 = file.log.2
if (iBackups is 0 or 1)
{
Assert.AreEqual(0, rfa.CurrentSizeRollBackups);
}
else
{
Assert.AreEqual(Math.Min(iBackups - 1, iMaxSizeRollBackups), rfa.CurrentSizeRollBackups);
}
}

/// <summary>
Expand Down
27 changes: 11 additions & 16 deletions src/log4net.Tests/Appender/SmtpPickupDirAppenderTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@
#endregion

using System;
using System.Collections;
using System.Diagnostics;
using System.IO;
using System.Text;

Expand All @@ -41,33 +39,31 @@ public class SmtpPickupDirAppenderTest

private class SilentErrorHandler : IErrorHandler
{
private StringBuilder m_buffer = new StringBuilder();
private readonly StringBuilder m_buffer = new();

public string Message
{
get { return m_buffer.ToString(); }
}
public string Message => m_buffer.ToString();

public void Error(string message)
{
m_buffer.Append(message + "\n");
m_buffer.Append(message + '\n');
}

public void Error(string message, Exception e)
{
m_buffer.Append(message + "\n" + e.Message + "\n");
m_buffer.Append(message + '\n' + e.Message + '\n');
}

public void Error(string message, Exception e, ErrorCode errorCode)
{
m_buffer.Append(message + "\n" + e.Message + "\n");
m_buffer.Append(message + '\n' + e.Message + '\n');
}
}

public SmtpPickupDirAppenderTest()
{
_testPickupDir = Path.Combine(Directory.GetCurrentDirectory(), "SmtpPickupDirAppenderTest_PickupDir");
}

/// <summary>
/// Sets up variables used for the tests
/// </summary>
Expand Down Expand Up @@ -131,8 +127,7 @@ private ILogger CreateLogger(SmtpPickupDirAppender appender)
{
Repository.Hierarchy.Hierarchy h = (Repository.Hierarchy.Hierarchy)LogManager.CreateRepository("TestRepository");

PatternLayout layout = new PatternLayout();
layout.ConversionPattern = "%m%n";
var layout = new PatternLayout { ConversionPattern = "%m%n" };
layout.ActivateOptions();

appender.Layout = layout;
Expand Down Expand Up @@ -199,7 +194,7 @@ public void TestOutputContainsSentDate()
}
Assert.IsTrue(hasDateHeader, "Output must contains a date header");

Assert.AreEqual("", sh.Message, "Unexpected error message");
Assert.AreEqual(string.Empty, sh.Message, "Unexpected error message");
}

/// <summary>
Expand All @@ -220,7 +215,7 @@ public void TestConfigurableFileExtension()
Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length);
FileInfo fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]);
Assert.AreEqual("." + fileExtension, fileInfo.Extension);
Assert.DoesNotThrow(delegate { new Guid(fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length)); }); // Assert that filename before extension is a guid
Assert.IsTrue(Guid.TryParse(fileInfo.Name.Substring(0, fileInfo.Name.Length - fileInfo.Extension.Length), out _));

Assert.AreEqual("", sh.Message, "Unexpected error message");
}
Expand All @@ -241,9 +236,9 @@ public void TestDefaultFileNameIsAGuid()
Assert.AreEqual(1, Directory.GetFiles(_testPickupDir).Length);
FileInfo fileInfo = new FileInfo(Directory.GetFiles(_testPickupDir)[0]);
Assert.IsEmpty(fileInfo.Extension);
Assert.DoesNotThrow(delegate { new Guid(fileInfo.Name); }); // Assert that filename is a guid
Assert.IsTrue(Guid.TryParse(fileInfo.Name, out _));

Assert.AreEqual("", sh.Message, "Unexpected error message");
Assert.AreEqual(string.Empty, sh.Message, "Unexpected error message");
}
}
}
143 changes: 79 additions & 64 deletions src/log4net.Tests/DateFormatter/AbsoluteTimeDateFormatterTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,88 +18,103 @@
#endregion

using System;
using System.Globalization;
using System.IO;
using System.Text;
using log4net.DateFormatter;
using NUnit.Framework;

namespace log4net.Tests.DateFormatter
{

[TestFixture]
public class AbsoluteTimeDateFormatterTest
[TestFixture]
public class AbsoluteTimeDateFormatterTest
{
[TearDown]
public void ResetCounts()
{
FormatterOne.Invocations = 0;
}

[TearDown]
public void resetCounts()
{
FormatterOne.invocations = 0;
}

[Test]
public void CacheWorksForSameTicks()
{
StringWriter sw = new StringWriter();
FormatterOne f1 = new FormatterOne();
FormatterOne f2 = new FormatterOne();
DateTime dt = DateTime.Now;
f1.FormatDate(dt, sw);
f2.FormatDate(dt, sw);
Assert.AreEqual(1, FormatterOne.invocations);
}
[Test]
public void CacheWorksForSameTicks()
{
StringWriter sw = new StringWriter();
FormatterOne f1 = new FormatterOne();
FormatterOne f2 = new FormatterOne();
DateTime dt = DateTime.Now;
f1.FormatDate(dt, sw);
f2.FormatDate(dt, sw);
Assert.AreEqual(1, FormatterOne.Invocations);
}

[Test]
public void CacheWorksForSameSecond()
{
StringWriter sw = new StringWriter();
FormatterOne f1 = new FormatterOne();
FormatterOne f2 = new FormatterOne();
DateTime dt1 = DateTime.Today;
DateTime dt2 = dt1.AddMilliseconds(600);
f1.FormatDate(dt1, sw);
f2.FormatDate(dt2, sw);
Assert.AreEqual(1, FormatterOne.invocations);
}
[Test]
public void CacheWorksForSameSecond()
{
StringWriter sw = new StringWriter();
FormatterOne f1 = new FormatterOne();
FormatterOne f2 = new FormatterOne();
DateTime dt1 = DateTime.Today;
DateTime dt2 = dt1.AddMilliseconds(600);
f1.FormatDate(dt1, sw);
f2.FormatDate(dt2, sw);
Assert.AreEqual(1, FormatterOne.Invocations);
}

[Test]
public void CacheExpiresWhenCrossingSecond()
{
StringWriter sw = new StringWriter();
FormatterOne f1 = new FormatterOne();
FormatterOne f2 = new FormatterOne();
DateTime dt1 = DateTime.Today.AddMinutes(1);
DateTime dt2 = dt1.AddMilliseconds(1100);
f1.FormatDate(dt1, sw);
f2.FormatDate(dt2, sw);
Assert.AreEqual(2, FormatterOne.invocations);
}
[Test]
public void CacheExpiresWhenCrossingSecond()
{
StringWriter sw = new StringWriter();
FormatterOne f1 = new FormatterOne();
FormatterOne f2 = new FormatterOne();
DateTime dt1 = DateTime.Today.AddMinutes(1);
DateTime dt2 = dt1.AddMilliseconds(1100);
f1.FormatDate(dt1, sw);
f2.FormatDate(dt2, sw);
Assert.AreEqual(2, FormatterOne.Invocations);
}

[Test]
public void CacheIsLocalToSubclass()
{
StringWriter sw = new StringWriter();
FormatterOne f1 = new FormatterOne();
FormatterTwo f2 = new FormatterTwo();
DateTime dt1 = DateTime.Today.AddMinutes(10);
f1.FormatDate(dt1, sw);
f2.FormatDate(dt1, sw);
Assert.AreEqual(2, FormatterOne.invocations);
}
[Test]
public void CacheIsLocalToSubclass()
{
StringWriter sw = new StringWriter();
FormatterOne f1 = new FormatterOne();
FormatterTwo f2 = new FormatterTwo();
DateTime dt1 = DateTime.Today.AddMinutes(10);
f1.FormatDate(dt1, sw);
f2.FormatDate(dt1, sw);
Assert.AreEqual(2, FormatterOne.Invocations);
}

internal class FormatterOne : AbsoluteTimeDateFormatter
[Test]
public void TestFormattingResults()
{
internal static int invocations = 0;
var formatter = new AbsoluteTimeDateFormatter();
var sb = new StringBuilder();
using var writer = new StringWriter(sb, CultureInfo.InvariantCulture);

override protected void FormatDateWithoutMillis(DateTime dateToFormat,
StringBuilder buffer)
{
invocations++;
}

// Tests for prepended 0 characters for 2-digit and 3-digit portions.
formatter.FormatDate(new DateTime(1970, 1, 1, 1, 1, 1).AddMilliseconds(1), writer);
Assert.AreEqual("01:01:01,001", sb.ToString());
sb.Clear();

// Non-zero-prepend case.
formatter.FormatDate(new DateTime(2100, 12, 30, 11, 59, 59).AddMilliseconds(100), writer);
Assert.AreEqual("11:59:59,100", sb.ToString());
sb.Clear();
}
}

internal class FormatterOne : AbsoluteTimeDateFormatter
{
internal static int Invocations;

internal class FormatterTwo : FormatterOne
override protected void FormatDateWithoutMillis(DateTime dateToFormat, StringBuilder buffer)
{
Invocations++;
}
}

internal class FormatterTwo : FormatterOne
{
}
}
59 changes: 59 additions & 0 deletions src/log4net.Tests/DateFormatter/DateTimeDateFormatterTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#region Apache License
//
// Licensed to the Apache Software Foundation (ASF) under one or more
// contributor license agreements. See the NOTICE file distributed with
// this work for additional information regarding copyright ownership.
// The ASF licenses this file to you under the Apache License, Version 2.0
// (the "License"); you may not use this file except in compliance with
// the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
#endregion

using System;
using System.Globalization;
using System.IO;
using System.Text;
using log4net.DateFormatter;
using NUnit.Framework;

namespace log4net.Tests.DateFormatter
{
[TestFixture]
public class DateTimeDateFormatterTest
{
[Test]
public void TestFormattingResults()
{
var sb = new StringBuilder();
using var writer = new StringWriter(sb, CultureInfo.InvariantCulture);

// Tests for prepended 0 characters for 2-digit and 3-digit portions.
var formatter = new DateTimeDateFormatter();
formatter.FormatDate(new DateTime(1970, 1, 1, 1, 1, 1).AddMilliseconds(1), writer);
Assert.AreEqual("01 Jan 1970 01:01:01,001", sb.ToString());
sb.Clear();

// Non-zero-prepend case.
var formatter2 = new DifferentTypeNameDateTimeDateFormatter();
formatter2.FormatDate(new DateTime(2100, 12, 30, 11, 59, 59).AddMilliseconds(100), writer);
Assert.AreEqual("30 Dec 2100 11:59:59,100", sb.ToString());
sb.Clear();
}

/// <summary>
/// Internal caching of datetime strings is by datetime class name. Avoid waiting
/// by adding another type name.
/// </summary>
private sealed class DifferentTypeNameDateTimeDateFormatter : DateTimeDateFormatter
{
}
}
}