|
| 1 | +using System.Linq; |
| 2 | +using NHibernate.Cache; |
| 3 | +using NHibernate.Cfg; |
| 4 | +using NHibernate.Cfg.MappingSchema; |
| 5 | +using NHibernate.Linq; |
| 6 | +using NHibernate.Mapping.ByCode; |
| 7 | +using NUnit.Framework; |
| 8 | + |
| 9 | +namespace NHibernate.Test.NHSpecificTest.GH3176 |
| 10 | +{ |
| 11 | + [TestFixture] |
| 12 | + public class ByCodeFixture : TestCaseMappingByCode |
| 13 | + { |
| 14 | + protected override HbmMapping GetMappings() |
| 15 | + { |
| 16 | + var mapper = new ModelMapper(); |
| 17 | + mapper.Class<Entity>( |
| 18 | + rc => |
| 19 | + { |
| 20 | + rc.Id(x => x.Id, m => m.Generator(Generators.Identity)); |
| 21 | + rc.Property(x => x.Name); |
| 22 | + rc.Component( |
| 23 | + x => x.Component, |
| 24 | + m => |
| 25 | + { |
| 26 | + m.Property(x => x.Field); |
| 27 | + m.Lazy(true); |
| 28 | + }); |
| 29 | + rc.Cache(x => x.Usage(CacheUsage.ReadWrite)); |
| 30 | + }); |
| 31 | + |
| 32 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 33 | + } |
| 34 | + |
| 35 | + protected override void Configure(Configuration configuration) |
| 36 | + { |
| 37 | + base.Configure(configuration); |
| 38 | + configuration.Properties[Environment.CacheProvider] = typeof(HashtableCacheProvider).AssemblyQualifiedName; |
| 39 | + configuration.Properties[Environment.UseSecondLevelCache] = "true"; |
| 40 | + configuration.Properties[Environment.GenerateStatistics] = "true"; |
| 41 | + } |
| 42 | + |
| 43 | + protected override void OnSetUp() |
| 44 | + { |
| 45 | + using (var session = OpenSession()) |
| 46 | + using (var transaction = session.BeginTransaction()) |
| 47 | + { |
| 48 | + var e1 = new Entity { Name = "Bob", Component = new Component() { Field = "Jim" } }; |
| 49 | + session.Save(e1); |
| 50 | + |
| 51 | + var e2 = new Entity { Name = "Sally" }; |
| 52 | + session.Save(e2); |
| 53 | + |
| 54 | + transaction.Commit(); |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + protected override void OnTearDown() |
| 59 | + { |
| 60 | + using (var session = OpenSession()) |
| 61 | + using (var transaction = session.BeginTransaction()) |
| 62 | + { |
| 63 | + session.CreateQuery("delete from Entity").ExecuteUpdate(); |
| 64 | + transaction.Commit(); |
| 65 | + } |
| 66 | + } |
| 67 | + |
| 68 | + [Test] |
| 69 | + public void TestPreLoadedData() |
| 70 | + { |
| 71 | + using (var session = OpenSession()) |
| 72 | + using (var transaction = session.BeginTransaction()) |
| 73 | + { |
| 74 | + // Load the entities into the second-level cache. |
| 75 | + session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).ToList(); |
| 76 | + |
| 77 | + var result = session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).First(); |
| 78 | + |
| 79 | + Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True); |
| 80 | + |
| 81 | + var field = result.Component?.Field; |
| 82 | + |
| 83 | + Assert.That(field, Is.EqualTo("Jim")); |
| 84 | + } |
| 85 | + |
| 86 | + using (var session = OpenSession()) |
| 87 | + using (var transaction = session.BeginTransaction()) |
| 88 | + { |
| 89 | + var result = session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).First(); |
| 90 | + |
| 91 | + Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True); |
| 92 | + |
| 93 | + var field = result.Component?.Field; |
| 94 | + |
| 95 | + Assert.That(field, Is.EqualTo("Jim")); |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public void TestNonPreLoadedData() |
| 101 | + { |
| 102 | + using (var session = OpenSession()) |
| 103 | + using (var transaction = session.BeginTransaction()) |
| 104 | + { |
| 105 | + var result = session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).First(); |
| 106 | + |
| 107 | + Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True); |
| 108 | + |
| 109 | + var field = result.Component?.Field; |
| 110 | + |
| 111 | + Assert.That(field, Is.EqualTo("Jim")); |
| 112 | + } |
| 113 | + |
| 114 | + using (var session = OpenSession()) |
| 115 | + using (var transaction = session.BeginTransaction()) |
| 116 | + { |
| 117 | + var result = session.Query<Entity>().WithOptions(o => o.SetCacheable(true)).Fetch(e => e.Component).First(); |
| 118 | + |
| 119 | + Assert.That(NHibernateUtil.IsPropertyInitialized(result, "Component"), Is.True); |
| 120 | + |
| 121 | + var field = result.Component?.Field; |
| 122 | + |
| 123 | + Assert.That(field, Is.EqualTo("Jim")); |
| 124 | + } |
| 125 | + } |
| 126 | + } |
| 127 | +} |
0 commit comments