Skip to content

Commit

Permalink
Streamline upgrade experience
Browse files Browse the repository at this point in the history
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
  • Loading branch information
andyward committed Feb 1, 2023
1 parent daf34bb commit 1315fd5
Show file tree
Hide file tree
Showing 10 changed files with 49 additions and 40 deletions.
16 changes: 6 additions & 10 deletions Tests/Ifc4x3ReadingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -13,7 +12,7 @@
namespace Xbim.Essentials.Tests
{
[Collection(nameof(xUnitBootstrap))]
public class Ifc4x3ReadingTests
public class Ifc4x3ReadingTests: TestBase
{

[Theory]
Expand All @@ -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
};
Expand Down Expand Up @@ -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<Ifc4x3.Kernel.IfcProduct>().Count();
Expand Down
4 changes: 2 additions & 2 deletions Tests/ParsingTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
{
Expand Down
4 changes: 2 additions & 2 deletions Tests/ReadingFileWithComments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@
namespace Xbim.Essentials.Tests
{
[TestClass]
public class ReadingFileWithComments
public class ReadingFileWithComments : TestBase
{
[TestMethod]
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
{
Expand Down
6 changes: 2 additions & 4 deletions Tests/UnitNameTest.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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(""))
Expand Down Expand Up @@ -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(""))
Expand Down
18 changes: 18 additions & 0 deletions Tests/Utilities/TestBase.cs
Original file line number Diff line number Diff line change
@@ -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();



}
}
10 changes: 5 additions & 5 deletions Xbim.Common/Model/StepModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,12 @@ private void SetupLogger(ILoggerFactory loggerFactory)
Logger = _loggerFactory.CreateLogger<StepModel>();
}

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<StepModel>();
}
Expand All @@ -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<ILogger<StepModel>>();
}
Expand Down
2 changes: 1 addition & 1 deletion Xbim.Essentials.NetCore.Tests/XmlSerializationTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
{
Expand Down
1 change: 0 additions & 1 deletion Xbim.IO.Esent/Esent/EsentModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ public IModelFactors ModelFactors
/// Constructs a new <see cref="EsentModel"/>
/// </summary>
/// <param name="factory"></param>
[Obsolete]
public EsentModel(IEntityFactory factory) : this(factory, default(ILoggerFactory))
{
}
Expand Down
20 changes: 9 additions & 11 deletions Xbim.IO.MemoryModel/MemoryModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ public static XbimSchemaVersion GetStepFileXbimSchemaVersion(IEnumerable<string>
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
Expand All @@ -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) { }

Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -343,8 +341,8 @@ public static MemoryModel OpenReadStep21(string file, ILogger logger = null, Rep
/// <param name="allowMissingReferences">Allow referenced entities that are not in the model, default false</param>
/// <param name="keepOrder">When true, serialised file will maintain order of entities from the original file (or order of creation)</param>
/// <returns>New memory model</returns>
[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<string> ignoreTypes = null, bool allowMissingReferences = false, bool keepOrder = true)
{
return OpenReadStep21(stream, default(ILoggerFactory), progressDel, ignoreTypes, allowMissingReferences, keepOrder);
Expand All @@ -361,7 +359,7 @@ public static MemoryModel OpenReadStep21(Stream stream, ILogger logger = null, R
/// <param name="allowMissingReferences">Allow referenced entities that are not in the model, default false</param>
/// <param name="keepOrder">When true, serialised file will maintain order of entities from the original file (or order of creation)</param>
/// <returns>New memory model</returns>
public static MemoryModel OpenReadStep21(Stream stream, ILoggerFactory loggerFactory, ReportProgressDelegate progressDel = null,
public static MemoryModel OpenReadStep21(Stream stream, ILoggerFactory loggerFactory = null, ReportProgressDelegate progressDel = null,
IEnumerable<string> ignoreTypes = null, bool allowMissingReferences = false, bool keepOrder = true)
{

Expand Down
8 changes: 4 additions & 4 deletions Xbim.IO.MemoryModel/Xml/XbimXmlReader4.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 1315fd5

Please sign in to comment.