From 1315fd5f8eb51eac392a7292e6e2f0eef5eca7b7 Mon Sep 17 00:00:00 2001 From: Andy Ward Date: Wed, 1 Feb 2023 19:06:45 +0000 Subject: [PATCH] Streamline upgrade experience Fixed up defaults on obsolete methods so typical upgraders will auto pick up new implementations by default. i.e. will default to ILoggerFactory signature over ILogger when caller didn't provide any logger Addresses all test warnings except Scanner --- Tests/Ifc4x3ReadingTests.cs | 16 ++++++--------- Tests/ParsingTests.cs | 4 ++-- Tests/ReadingFileWithComments.cs | 4 ++-- Tests/UnitNameTest.cs | 6 ++---- Tests/Utilities/TestBase.cs | 18 +++++++++++++++++ Xbim.Common/Model/StepModel.cs | 10 +++++----- .../XmlSerializationTests.cs | 2 +- Xbim.IO.Esent/Esent/EsentModel.cs | 1 - Xbim.IO.MemoryModel/MemoryModel.cs | 20 +++++++++---------- Xbim.IO.MemoryModel/Xml/XbimXmlReader4.cs | 8 ++++---- 10 files changed, 49 insertions(+), 40 deletions(-) create mode 100644 Tests/Utilities/TestBase.cs diff --git a/Tests/Ifc4x3ReadingTests.cs b/Tests/Ifc4x3ReadingTests.cs index d3ad0737c..04b73442e 100644 --- a/Tests/Ifc4x3ReadingTests.cs +++ b/Tests/Ifc4x3ReadingTests.cs @@ -4,7 +4,6 @@ using System.IO; using System.Linq; using Xbim.Common; -using Xbim.Common.Configuration; using Xbim.Ifc; using Xbim.Ifc4x3; using Xbim.IO.Memory; @@ -13,7 +12,7 @@ namespace Xbim.Essentials.Tests { [Collection(nameof(xUnitBootstrap))] - public class Ifc4x3ReadingTests + public class Ifc4x3ReadingTests: TestBase { [Theory] @@ -32,15 +31,17 @@ public class Ifc4x3ReadingTests [InlineData(@"TestFiles\IFC4x3\test2.ifc")] public void CanParseSampleFiles(string file) { + var loggerFactory = new LoggerFactory(); var config = new LoggerConfiguration() .MinimumLevel.Information() .WriteTo.Console() .WriteTo.Debug() .CreateLogger(); - var logger = (new LoggerFactory()).AddSerilog(config).CreateLogger(typeof(IModel)); + + loggerFactory.AddSerilog(config); using var stream = File.OpenRead(file); - var model = new MemoryModel(new EntityFactoryIfc4x3Add1(), logger) + var model = new MemoryModel(new EntityFactoryIfc4x3Add1(), loggerFactory) { AllowMissingReferences = false }; @@ -82,12 +83,7 @@ public void IfcStoreCanOpenSampleFiles(string file) [InlineData(@"TestFiles\IFC4x3\test2.ifc")] public void Ifc4_interfaces_can_be_used_to_read_IFC4x3(string file) { - var config = new LoggerConfiguration() - .MinimumLevel.Information() - .WriteTo.Console() - .WriteTo.Debug() - .CreateLogger(); - var logger = (new LoggerFactory()).AddSerilog(config).CreateLogger(typeof(IModel)); + using var model = IfcStore.Open(file); var productsIfc4x3 = model.Instances.OfType().Count(); diff --git a/Tests/ParsingTests.cs b/Tests/ParsingTests.cs index da02e471a..8e26d98f8 100644 --- a/Tests/ParsingTests.cs +++ b/Tests/ParsingTests.cs @@ -16,7 +16,7 @@ namespace Xbim.Essentials.Tests { [TestClass] - public class ParsingTests + public class ParsingTests : TestBase { private static readonly IEntityFactory ef4 = new Ifc4.EntityFactoryIfc4(); private static readonly IEntityFactory ef2x3 = new Ifc2x3.EntityFactoryIfc2x3(); @@ -298,7 +298,7 @@ public void ScannerTest() { using (var strm = File.OpenRead("TestFiles\\Badly formed Ifc file.ifc")) { - var scanner = new Scanner(strm); + var scanner = new Scanner(strm, LoggerFactory); int tok; do { diff --git a/Tests/ReadingFileWithComments.cs b/Tests/ReadingFileWithComments.cs index ab3677d1d..aeea88b44 100644 --- a/Tests/ReadingFileWithComments.cs +++ b/Tests/ReadingFileWithComments.cs @@ -7,7 +7,7 @@ namespace Xbim.Essentials.Tests { [TestClass] - public class ReadingFileWithComments + public class ReadingFileWithComments : TestBase { [TestMethod] public void ScanningTheFile() @@ -15,7 +15,7 @@ public void ScanningTheFile() const string file = "TestFiles\\mapped-shape-with-transformation.ifc"; using (var stream = File.OpenRead(file) ) { - var s = new Scanner(stream); + var s = new Scanner(stream, LoggerFactory); int t; do { diff --git a/Tests/UnitNameTest.cs b/Tests/UnitNameTest.cs index b525aa1f6..a259cb04a 100644 --- a/Tests/UnitNameTest.cs +++ b/Tests/UnitNameTest.cs @@ -1,7 +1,5 @@ using System; using Microsoft.VisualStudio.TestTools.UnitTesting; -using Xbim.Common.Step21; -using Xbim.Ifc; using Xbim.IO.Memory; namespace Xbim.Essentials.Tests @@ -12,7 +10,7 @@ public class UnitNameTest [TestMethod] public void NamedUnitTest2X3() { - using (var model = new MemoryModel(new Ifc2x3.EntityFactoryIfc2x3(), default)) + using (var model = new MemoryModel(new Ifc2x3.EntityFactoryIfc2x3())) { Ifc2x3.MeasureResource.IfcSIUnit unit; using (var txn = model.BeginTransaction("")) @@ -53,7 +51,7 @@ public void NamedUnitTest2X3() [TestMethod] public void NamedUnitTest4() { - using (var model = new MemoryModel(new Ifc4.EntityFactoryIfc4(), default)) + using (var model = new MemoryModel(new Ifc4.EntityFactoryIfc4())) { Ifc4.MeasureResource.IfcSIUnit unit; using (var txn = model.BeginTransaction("")) diff --git a/Tests/Utilities/TestBase.cs b/Tests/Utilities/TestBase.cs new file mode 100644 index 000000000..f4965c959 --- /dev/null +++ b/Tests/Utilities/TestBase.cs @@ -0,0 +1,18 @@ +using Microsoft.Extensions.Logging; +using Xbim.Common.Configuration; + +namespace Xbim.Essentials.Tests +{ + public abstract class TestBase + { + public TestBase() + { + + } + + protected static ILoggerFactory LoggerFactory { get; } = XbimServices.Current.GetLoggerFactory(); + + + + } +} diff --git a/Xbim.Common/Model/StepModel.cs b/Xbim.Common/Model/StepModel.cs index a50463ffe..ec634a7e1 100644 --- a/Xbim.Common/Model/StepModel.cs +++ b/Xbim.Common/Model/StepModel.cs @@ -101,12 +101,12 @@ private void SetupLogger(ILoggerFactory loggerFactory) Logger = _loggerFactory.CreateLogger(); } - public StepModel(IEntityFactory entityFactory, ILoggerFactory loggerFactory) : this(entityFactory, loggerFactory, 0) + public StepModel(IEntityFactory entityFactory, ILoggerFactory loggerFactory = default) : this(entityFactory, loggerFactory, 0) { } - [Obsolete] - public StepModel(IEntityFactory entityFactory, ILogger logger = null, int labelFrom = 0) : this(entityFactory, default(ILoggerFactory), labelFrom) + [Obsolete("Prefer ILoggerFactory overload instead")] + public StepModel(IEntityFactory entityFactory, ILogger logger, int labelFrom) : this(entityFactory, default(ILoggerFactory), labelFrom) { Logger = logger ?? XbimLogging.CreateLogger(); } @@ -122,8 +122,8 @@ public StepModel(EntityFactoryResolverDelegate factoryResolver, ILoggerFactory l ModelFactors = new XbimModelFactors(Math.PI / 180, 1e-3, 1e-5); } - [Obsolete("Prefer ILoggerFactory overload")] - public StepModel(EntityFactoryResolverDelegate factoryResolver, ILogger logger = null, int labelFrom = 0) : this(factoryResolver, default(ILoggerFactory), labelFrom) + [Obsolete("Prefer ILoggerFactory overload instead")] + public StepModel(EntityFactoryResolverDelegate factoryResolver, ILogger logger, int labelFrom) : this(factoryResolver, default(ILoggerFactory), labelFrom) { Logger = Logger ?? logger ?? XbimServices.Current.ServiceProvider.GetRequiredService>(); } diff --git a/Xbim.Essentials.NetCore.Tests/XmlSerializationTests.cs b/Xbim.Essentials.NetCore.Tests/XmlSerializationTests.cs index da1bec9d6..a8b8c52e8 100644 --- a/Xbim.Essentials.NetCore.Tests/XmlSerializationTests.cs +++ b/Xbim.Essentials.NetCore.Tests/XmlSerializationTests.cs @@ -13,7 +13,7 @@ public class XmlSerializationTests [TestMethod] public void SerializeXML() { - using (var model = new MemoryModel(new EntityFactoryIfc4(), default)) + using (var model = new MemoryModel(new EntityFactoryIfc4())) { using (var txn = model.BeginTransaction("Create")) { diff --git a/Xbim.IO.Esent/Esent/EsentModel.cs b/Xbim.IO.Esent/Esent/EsentModel.cs index 1ba1aefb8..ae3940e57 100644 --- a/Xbim.IO.Esent/Esent/EsentModel.cs +++ b/Xbim.IO.Esent/Esent/EsentModel.cs @@ -73,7 +73,6 @@ public IModelFactors ModelFactors /// Constructs a new /// /// - [Obsolete] public EsentModel(IEntityFactory factory) : this(factory, default(ILoggerFactory)) { } diff --git a/Xbim.IO.MemoryModel/MemoryModel.cs b/Xbim.IO.MemoryModel/MemoryModel.cs index 6a230f75f..0624e3406 100644 --- a/Xbim.IO.MemoryModel/MemoryModel.cs +++ b/Xbim.IO.MemoryModel/MemoryModel.cs @@ -123,7 +123,7 @@ public static XbimSchemaVersion GetStepFileXbimSchemaVersion(IEnumerable schema.StartsWith("Ifc4RC", StringComparison.OrdinalIgnoreCase)) return XbimSchemaVersion.Ifc4; if (string.Equals(schema, "Ifc4x1", StringComparison.OrdinalIgnoreCase)) - return XbimSchemaVersion.Ifc4x1; + return XbimSchemaVersion.Ifc4x1; if (schema.StartsWith("Ifc2x", StringComparison.OrdinalIgnoreCase)) //return this as 2x3 return XbimSchemaVersion.Ifc2X3; if (schema.StartsWith("Ifc4x3", StringComparison.OrdinalIgnoreCase)) //return this as 2x3 @@ -138,19 +138,16 @@ public static XbimSchemaVersion GetStepFileXbimSchemaVersion(Stream stream) { return GetStepFileXbimSchemaVersion(GetStepFileSchemaVersion(stream)); } - public MemoryModel(IEntityFactory entityFactory, IStepFileHeader header, ILoggerFactory loggerFactory = null) : base(entityFactory, loggerFactory, 0) + public MemoryModel(IEntityFactory entityFactory, IStepFileHeader header, ILoggerFactory loggerFactory = default) : base(entityFactory, loggerFactory, 0) { Header = header; } - public MemoryModel(IEntityFactory entityFactory, ILoggerFactory loggerFactory, int labelFrom) : base(entityFactory, loggerFactory, labelFrom) { } + public MemoryModel(IEntityFactory entityFactory, ILoggerFactory loggerFactory = default, int labelFrom = 0) : base(entityFactory, loggerFactory, labelFrom) { } - public MemoryModel(IEntityFactory entityFactory, ILoggerFactory loggerFactory) : this(entityFactory, loggerFactory, 0) { } + // public MemoryModel(IEntityFactory entityFactory, ILoggerFactory loggerFactory) : this(entityFactory, loggerFactory, 0) { } [Obsolete("Prefer ILoggerFactory implementation")] - public MemoryModel(IEntityFactory entityFactory, ILogger logger = null, int labelFrom = 0) : base(entityFactory, logger, labelFrom) { } - - [Obsolete("Prefer ILoggerFactory implementation")] - private MemoryModel(EntityFactoryResolverDelegate resolver, ILogger logger = null, int labelFrom = 0) : base(resolver, logger, labelFrom) { } + public MemoryModel(IEntityFactory entityFactory, ILogger logger, int labelFrom) : base(entityFactory, logger, labelFrom) { } private MemoryModel(EntityFactoryResolverDelegate resolver, ILoggerFactory loggerFactory, int labelFrom = 0) : base(resolver, loggerFactory, labelFrom) { } @@ -262,6 +259,7 @@ public static MemoryModel OpenRead(string fileName, ILoggerFactory loggerFactory return OpenRead(fileName, loggerFactory, null); } + [Obsolete("Prefer method with ILoggerFactory overload")] public static MemoryModel OpenRead(string fileName, ILogger logger, ReportProgressDelegate progressDel = null) { return OpenRead(fileName, default(ILoggerFactory), progressDel); @@ -343,8 +341,8 @@ public static MemoryModel OpenReadStep21(string file, ILogger logger = null, Rep /// Allow referenced entities that are not in the model, default false /// When true, serialised file will maintain order of entities from the original file (or order of creation) /// New memory model - [Obsolete("??Prefer ILoggerFactory implementation")] - public static MemoryModel OpenReadStep21(Stream stream, ILogger logger = null, ReportProgressDelegate progressDel = null, + [Obsolete("Prefer ILoggerFactory implementation")] + public static MemoryModel OpenReadStep21(Stream stream, ILogger logger, ReportProgressDelegate progressDel = null, IEnumerable ignoreTypes = null, bool allowMissingReferences = false, bool keepOrder = true) { return OpenReadStep21(stream, default(ILoggerFactory), progressDel, ignoreTypes, allowMissingReferences, keepOrder); @@ -361,7 +359,7 @@ public static MemoryModel OpenReadStep21(Stream stream, ILogger logger = null, R /// Allow referenced entities that are not in the model, default false /// When true, serialised file will maintain order of entities from the original file (or order of creation) /// New memory model - public static MemoryModel OpenReadStep21(Stream stream, ILoggerFactory loggerFactory, ReportProgressDelegate progressDel = null, + public static MemoryModel OpenReadStep21(Stream stream, ILoggerFactory loggerFactory = null, ReportProgressDelegate progressDel = null, IEnumerable ignoreTypes = null, bool allowMissingReferences = false, bool keepOrder = true) { diff --git a/Xbim.IO.MemoryModel/Xml/XbimXmlReader4.cs b/Xbim.IO.MemoryModel/Xml/XbimXmlReader4.cs index fffe74250..738780be6 100644 --- a/Xbim.IO.MemoryModel/Xml/XbimXmlReader4.cs +++ b/Xbim.IO.MemoryModel/Xml/XbimXmlReader4.cs @@ -857,15 +857,15 @@ private string GetTextFromElement(XmlReader input) return input.Value; } - [Obsolete] - public static IStepFileHeader ReadHeader(Stream input, ILogger logger = null) + [Obsolete("Please use the ILoggerFactory overload")] + public static IStepFileHeader ReadHeader(Stream input, ILogger logger) { var xReader = new XbimXmlReader4(default, logger); - var fakeModel = new Memory.MemoryModel(new Ifc4.EntityFactoryIfc4(), logger); + var fakeModel = new Memory.MemoryModel(new Ifc4.EntityFactoryIfc4(), logger, 0); return xReader.Read(input, fakeModel, true); //using a dummy model to get the assembly correct } - public static IStepFileHeader ReadHeader(Stream input, ILoggerFactory loggerFactory) + public static IStepFileHeader ReadHeader(Stream input, ILoggerFactory loggerFactory = default) { var xReader = new XbimXmlReader4(loggerFactory, default); var fakeModel = new Memory.MemoryModel(new Ifc4.EntityFactoryIfc4(), loggerFactory);