diff --git a/src/NHibernate.Test/Async/LazyProperty/LazyPropertyFixture.cs b/src/NHibernate.Test/Async/LazyProperty/LazyPropertyFixture.cs index 13d1af2ae3d..cce9ce89adf 100644 --- a/src/NHibernate.Test/Async/LazyProperty/LazyPropertyFixture.cs +++ b/src/NHibernate.Test/Async/LazyProperty/LazyPropertyFixture.cs @@ -9,6 +9,7 @@ using System.Collections; +using NHibernate.Intercept; using NHibernate.Tuple.Entity; using NUnit.Framework; @@ -42,6 +43,11 @@ protected override DebugSessionFactory BuildSessionFactory() protected override void OnSetUp() { + Assert.That( + nameof(Book.FieldInterceptor), + Is.EqualTo(nameof(IFieldInterceptorAccessor.FieldInterceptor)), + $"Test pre-condition not met: entity property {nameof(Book.FieldInterceptor)} should have the same " + + $"name than {nameof(IFieldInterceptorAccessor)}.{nameof(IFieldInterceptorAccessor.FieldInterceptor)}"); using (var s = OpenSession()) using (var tx = s.BeginTransaction()) { @@ -49,7 +55,8 @@ protected override void OnSetUp() { Name = "some name", Id = 1, - ALotOfText = "a lot of text ..." + ALotOfText = "a lot of text ...", + FieldInterceptor = "Why not that name?" }); tx.Commit(); } @@ -76,12 +83,14 @@ public async Task PropertyLoadedNotInitializedAsync() Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.False); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.False); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False); + Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.False); await (NHibernateUtil.InitializeAsync(book)); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False); + Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True); } } @@ -95,6 +104,7 @@ public async Task PropertyLoadedNotInitializedWhenUsingGetAsync() Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False); + Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True); } } @@ -118,6 +128,7 @@ public async Task CanGetValueForNonLazyPropertyAsync() var book = await (s.GetAsync(1)); Assert.That(book.Name, Is.EqualTo("some name")); + Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?")); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False); } } @@ -137,7 +148,6 @@ public async Task CanLoadAndSaveObjectInDifferentSessionsAsync() } } - [Test] public async Task CanUpdateNonLazyWithoutLoadingLazyPropertyAsync() { @@ -147,17 +157,19 @@ public async Task CanUpdateNonLazyWithoutLoadingLazyPropertyAsync() { book = await (s.GetAsync(1)); book.Name += "updated"; + book.FieldInterceptor += "updated"; - Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False); + Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "Before flush and commit"); await (trans.CommitAsync()); + Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "After flush and commit"); } - using (ISession s = OpenSession()) { book = await (s.GetAsync(1)); Assert.That(book.Name, Is.EqualTo("some nameupdated")); + Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?updated")); } } } -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/LazyProperty/Book.cs b/src/NHibernate.Test/LazyProperty/Book.cs index d2dffdb401b..40942a6af47 100644 --- a/src/NHibernate.Test/LazyProperty/Book.cs +++ b/src/NHibernate.Test/LazyProperty/Book.cs @@ -12,5 +12,7 @@ public virtual string ALotOfText get { return _aLotOfText; } set { _aLotOfText = value; } } + + public virtual string FieldInterceptor { get; set; } } -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs b/src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs index df305688a94..4d798386c40 100644 --- a/src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs +++ b/src/NHibernate.Test/LazyProperty/LazyPropertyFixture.cs @@ -1,4 +1,5 @@ using System.Collections; +using NHibernate.Intercept; using NHibernate.Tuple.Entity; using NUnit.Framework; @@ -31,6 +32,11 @@ protected override DebugSessionFactory BuildSessionFactory() protected override void OnSetUp() { + Assert.That( + nameof(Book.FieldInterceptor), + Is.EqualTo(nameof(IFieldInterceptorAccessor.FieldInterceptor)), + $"Test pre-condition not met: entity property {nameof(Book.FieldInterceptor)} should have the same " + + $"name than {nameof(IFieldInterceptorAccessor)}.{nameof(IFieldInterceptorAccessor.FieldInterceptor)}"); using (var s = OpenSession()) using (var tx = s.BeginTransaction()) { @@ -38,7 +44,8 @@ protected override void OnSetUp() { Name = "some name", Id = 1, - ALotOfText = "a lot of text ..." + ALotOfText = "a lot of text ...", + FieldInterceptor = "Why not that name?" }); tx.Commit(); } @@ -65,12 +72,14 @@ public void PropertyLoadedNotInitialized() Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.False); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.False); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False); + Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.False); NHibernateUtil.Initialize(book); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False); + Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True); } } @@ -90,6 +99,7 @@ public void PropertyLoadedNotInitializedWhenUsingGet() Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Id"), Is.True); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "Name"), Is.True); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False); + Assert.That(NHibernateUtil.IsPropertyInitialized(book, nameof(Book.FieldInterceptor)), Is.True); } } @@ -113,6 +123,7 @@ public void CanGetValueForNonLazyProperty() var book = s.Get(1); Assert.That(book.Name, Is.EqualTo("some name")); + Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?")); Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False); } } @@ -132,7 +143,6 @@ public void CanLoadAndSaveObjectInDifferentSessions() } } - [Test] public void CanUpdateNonLazyWithoutLoadingLazyProperty() { @@ -142,17 +152,19 @@ public void CanUpdateNonLazyWithoutLoadingLazyProperty() { book = s.Get(1); book.Name += "updated"; + book.FieldInterceptor += "updated"; - Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False); + Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "Before flush and commit"); trans.Commit(); + Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.False, "After flush and commit"); } - using (ISession s = OpenSession()) { book = s.Get(1); Assert.That(book.Name, Is.EqualTo("some nameupdated")); + Assert.That(book.FieldInterceptor, Is.EqualTo("Why not that name?updated")); } } } -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/LazyProperty/Mappings.hbm.xml b/src/NHibernate.Test/LazyProperty/Mappings.hbm.xml index e4793ff2e61..25eaafa3554 100644 --- a/src/NHibernate.Test/LazyProperty/Mappings.hbm.xml +++ b/src/NHibernate.Test/LazyProperty/Mappings.hbm.xml @@ -9,6 +9,7 @@ + diff --git a/src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs b/src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs index a7b2e78095d..0927a82ce9b 100644 --- a/src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs +++ b/src/NHibernate/Intercept/DefaultDynamicLazyFieldInterceptor.cs @@ -17,7 +17,7 @@ public object Intercept(InvocationInfo info) { if (ReflectHelper.IsPropertyGet(info.TargetMethod)) { - if("get_FieldInterceptor".Equals(methodName)) + if (IsGetFieldInterceptorCall(methodName, info.TargetMethod)) { return FieldInterceptor; } @@ -33,7 +33,7 @@ public object Intercept(InvocationInfo info) } else if (ReflectHelper.IsPropertySet(info.TargetMethod)) { - if ("set_FieldInterceptor".Equals(methodName)) + if (IsSetFieldInterceptorCall(methodName, info.TargetMethod)) { FieldInterceptor = (IFieldInterceptor)info.Arguments[0]; return null; @@ -44,7 +44,7 @@ public object Intercept(InvocationInfo info) } else { - if ("set_FieldInterceptor".Equals(methodName)) + if (IsSetFieldInterceptorCall(methodName, info.TargetMethod)) { FieldInterceptor = (IFieldInterceptor)info.Arguments[0]; return null; @@ -62,5 +62,15 @@ public object Intercept(InvocationInfo info) } return returnValue; } + + private static bool IsGetFieldInterceptorCall(string methodName, MethodInfo targetMethod) + { + return "get_FieldInterceptor".Equals(methodName) && targetMethod.DeclaringType == typeof(IFieldInterceptorAccessor); + } + + private static bool IsSetFieldInterceptorCall(string methodName, MethodInfo targetMethod) + { + return "set_FieldInterceptor".Equals(methodName) && targetMethod.DeclaringType == typeof(IFieldInterceptorAccessor); + } } }