diff --git a/src/NHibernate.Test/Async/NHSpecificTest/GH1439/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/GH1439/Fixture.cs new file mode 100644 index 00000000000..0962d06c353 --- /dev/null +++ b/src/NHibernate.Test/Async/NHSpecificTest/GH1439/Fixture.cs @@ -0,0 +1,131 @@ +//------------------------------------------------------------------------------ +// +// This code was generated by AsyncGenerator. +// +// Changes to this file may cause incorrect behavior and will be lost if +// the code is regenerated. +// +//------------------------------------------------------------------------------ + + +using System.Linq; +using NHibernate.Cfg.MappingSchema; +using NHibernate.Intercept; +using NHibernate.Mapping.ByCode; +using NUnit.Framework; +using NHibernate.Linq; + +namespace NHibernate.Test.NHSpecificTest.GH1439 +{ + using System.Threading.Tasks; + [TestFixture] + public class FixtureAsync : TestCaseMappingByCode + { + protected override HbmMapping GetMappings() + { + var mapper = new ModelMapper(); + + mapper.Class(rc => + { + rc.ComposedId( + c => + { + c.Property(t => t.Id); + c.Property(t => t.Name); + }); + + rc.Property(x => x.LazyProperty, x => x.Lazy(true)); + }); + + mapper.Class(rc => + { + rc.ComponentAsId(e => e.Id, map => + { + map.Property(c => c.Id); + map.Property(c => c.Name); + }); + + rc.Property(e => e.LazyProperty, map => map.Lazy(true)); + }); + + return mapper.CompileMappingForAllExplicitlyAddedEntities(); + } + + protected override void OnSetUp() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + var e1 = new CompositeEntity { Id = 1, Name = "Bob", LazyProperty = "LazyProperty"}; + session.Save(e1); + var e2 = new EntityWithComponentId + { Id = new ComponentId { Id = 1, Name = "Bob" }, LazyProperty = "LazyProperty" }; + session.Save(e2); + transaction.Commit(); + } + } + + protected override void OnTearDown() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + session.CreateQuery("delete from System.Object").ExecuteUpdate(); + transaction.Commit(); + } + } + + [Test] + [KnownBug("#1439")] + public async Task LazyPropertyShouldBeUninitializedAndLoadableAsync() + { + using (var session = OpenSession()) + using (var tran = session.BeginTransaction()) + { + var e1 = await (session.Query().SingleAsync()); + Assert.Multiple( + () => + { + Assert.That( + NHibernateUtil.IsPropertyInitialized(e1, nameof(CompositeEntity.LazyProperty)), + Is.False, + "Lazy property initialization status"); + Assert.That( + FieldInterceptionHelper.IsInstrumented(e1), + Is.True, + "Entity IsInstrumented"); + Assert.That( + e1, + Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty")); + }); + await (tran.CommitAsync()); + } + } + + [Test] + public async Task LazyPropertyShouldBeUninitializedAndLoadableWithComponentIdAsync() + { + using (var session = OpenSession()) + using (var tran = session.BeginTransaction()) + { + var e2 = await (session.Query().SingleAsync()); + Assert.Multiple( + () => + { + Assert.That( + NHibernateUtil.IsPropertyInitialized(e2, nameof(CompositeEntity.LazyProperty)), + Is.False, + "Lazy property initialization status"); + Assert.That( + FieldInterceptionHelper.IsInstrumented(e2), + Is.True, + "Entity IsInstrumented"); + Assert.That( + e2, + Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty")); + }); + await (tran.CommitAsync()); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1439/CompositeEntity.cs b/src/NHibernate.Test/NHSpecificTest/GH1439/CompositeEntity.cs new file mode 100644 index 00000000000..0b0e4477a21 --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1439/CompositeEntity.cs @@ -0,0 +1,31 @@ +using System; + +namespace NHibernate.Test.NHSpecificTest.GH1439 +{ + public class CompositeEntity : IEquatable + { + public virtual int Id { get; set; } + public virtual string Name { get; set; } + public virtual string LazyProperty { get; set; } + + public virtual bool Equals(CompositeEntity other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Id == other.Id && string.Equals(Name, other.Name); + } + + public override bool Equals(object obj) + { + return Equals(obj as CompositeEntity); + } + + public override int GetHashCode() + { + unchecked + { + return (Id * 397) ^ (Name != null ? Name.GetHashCode() : 0); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1439/EntityWithComponentId.cs b/src/NHibernate.Test/NHSpecificTest/GH1439/EntityWithComponentId.cs new file mode 100644 index 00000000000..2b8263161ed --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1439/EntityWithComponentId.cs @@ -0,0 +1,36 @@ +using System; + +namespace NHibernate.Test.NHSpecificTest.GH1439 +{ + public class EntityWithComponentId + { + public virtual ComponentId Id { get; set; } + public virtual string LazyProperty { get; set; } + } + + public class ComponentId : IEquatable + { + public int Id { get; set; } + public string Name { get; set; } + + public bool Equals(ComponentId other) + { + if (ReferenceEquals(null, other)) return false; + if (ReferenceEquals(this, other)) return true; + return Id == other.Id && string.Equals(Name, other.Name); + } + + public override bool Equals(object obj) + { + return Equals(obj as ComponentId); + } + + public override int GetHashCode() + { + unchecked + { + return (Id * 397) ^ (Name != null ? Name.GetHashCode() : 0); + } + } + } +} diff --git a/src/NHibernate.Test/NHSpecificTest/GH1439/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/GH1439/Fixture.cs new file mode 100644 index 00000000000..5a12bfe77fe --- /dev/null +++ b/src/NHibernate.Test/NHSpecificTest/GH1439/Fixture.cs @@ -0,0 +1,119 @@ +using System.Linq; +using NHibernate.Cfg.MappingSchema; +using NHibernate.Intercept; +using NHibernate.Mapping.ByCode; +using NUnit.Framework; + +namespace NHibernate.Test.NHSpecificTest.GH1439 +{ + [TestFixture] + public class Fixture : TestCaseMappingByCode + { + protected override HbmMapping GetMappings() + { + var mapper = new ModelMapper(); + + mapper.Class(rc => + { + rc.ComposedId( + c => + { + c.Property(t => t.Id); + c.Property(t => t.Name); + }); + + rc.Property(x => x.LazyProperty, x => x.Lazy(true)); + }); + + mapper.Class(rc => + { + rc.ComponentAsId(e => e.Id, map => + { + map.Property(c => c.Id); + map.Property(c => c.Name); + }); + + rc.Property(e => e.LazyProperty, map => map.Lazy(true)); + }); + + return mapper.CompileMappingForAllExplicitlyAddedEntities(); + } + + protected override void OnSetUp() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + var e1 = new CompositeEntity { Id = 1, Name = "Bob", LazyProperty = "LazyProperty"}; + session.Save(e1); + var e2 = new EntityWithComponentId + { Id = new ComponentId { Id = 1, Name = "Bob" }, LazyProperty = "LazyProperty" }; + session.Save(e2); + transaction.Commit(); + } + } + + protected override void OnTearDown() + { + using (var session = OpenSession()) + using (var transaction = session.BeginTransaction()) + { + session.CreateQuery("delete from System.Object").ExecuteUpdate(); + transaction.Commit(); + } + } + + [Test] + [KnownBug("#1439")] + public void LazyPropertyShouldBeUninitializedAndLoadable() + { + using (var session = OpenSession()) + using (var tran = session.BeginTransaction()) + { + var e1 = session.Query().Single(); + Assert.Multiple( + () => + { + Assert.That( + NHibernateUtil.IsPropertyInitialized(e1, nameof(CompositeEntity.LazyProperty)), + Is.False, + "Lazy property initialization status"); + Assert.That( + FieldInterceptionHelper.IsInstrumented(e1), + Is.True, + "Entity IsInstrumented"); + Assert.That( + e1, + Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty")); + }); + tran.Commit(); + } + } + + [Test] + public void LazyPropertyShouldBeUninitializedAndLoadableWithComponentId() + { + using (var session = OpenSession()) + using (var tran = session.BeginTransaction()) + { + var e2 = session.Query().Single(); + Assert.Multiple( + () => + { + Assert.That( + NHibernateUtil.IsPropertyInitialized(e2, nameof(CompositeEntity.LazyProperty)), + Is.False, + "Lazy property initialization status"); + Assert.That( + FieldInterceptionHelper.IsInstrumented(e2), + Is.True, + "Entity IsInstrumented"); + Assert.That( + e2, + Has.Property(nameof(CompositeEntity.LazyProperty)).EqualTo("LazyProperty")); + }); + tran.Commit(); + } + } + } +}