|
| 1 | +//------------------------------------------------------------------------------ |
| 2 | +// <auto-generated> |
| 3 | +// This code was generated by AsyncGenerator. |
| 4 | +// |
| 5 | +// Changes to this file may cause incorrect behavior and will be lost if |
| 6 | +// the code is regenerated. |
| 7 | +// </auto-generated> |
| 8 | +//------------------------------------------------------------------------------ |
| 9 | + |
| 10 | + |
| 11 | +using System; |
| 12 | +using NHibernate.Cfg.MappingSchema; |
| 13 | +using NHibernate.Mapping.ByCode; |
| 14 | +using NUnit.Framework; |
| 15 | + |
| 16 | +namespace NHibernate.Test.StaticProxyTest.InterfaceHandling |
| 17 | +{ |
| 18 | + using System.Threading.Tasks; |
| 19 | + [TestFixture] |
| 20 | + public class FixtureAsync : TestCaseMappingByCode |
| 21 | + { |
| 22 | + private readonly Guid _id = Guid.NewGuid(); |
| 23 | + |
| 24 | + protected override HbmMapping GetMappings() |
| 25 | + { |
| 26 | + var mapper = new ModelMapper(); |
| 27 | + |
| 28 | + #region Subclass hierarchy |
| 29 | + |
| 30 | + mapper.Class<EntityClassProxy>( |
| 31 | + rc => |
| 32 | + { |
| 33 | + rc.Id(x => x.Id); |
| 34 | + rc.Property(x => x.Name); |
| 35 | + }); |
| 36 | + |
| 37 | + mapper.UnionSubclass<SubEntityInterfaceProxy>( |
| 38 | + rc => |
| 39 | + { |
| 40 | + rc.Proxy(typeof(ISubEntityProxy)); |
| 41 | + |
| 42 | + rc.Property(x => x.AnotherName); |
| 43 | + }); |
| 44 | + |
| 45 | + mapper.UnionSubclass<AnotherSubEntityInterfaceProxy>( |
| 46 | + rc => |
| 47 | + { |
| 48 | + rc.Proxy(typeof(IAnotherEntityProxy)); |
| 49 | + |
| 50 | + rc.Property(x => x.AnotherName); |
| 51 | + }); |
| 52 | + |
| 53 | + mapper.Class<EntityWithSuperClassInterfaceLookup>( |
| 54 | + rc => |
| 55 | + { |
| 56 | + rc.Table("ClassWithInterfaceLookup"); |
| 57 | + rc.Id(x => x.Id); |
| 58 | + rc.Property(x => x.Name); |
| 59 | + rc.ManyToOne(x => x.EntityLookup, x => x.Class(typeof(EntityClassProxy))); |
| 60 | + }); |
| 61 | + |
| 62 | + #endregion Subclass hierarchy |
| 63 | + |
| 64 | + mapper.Class<EntitySimple>( |
| 65 | + rc => |
| 66 | + { |
| 67 | + rc.Id(x => x.Id); |
| 68 | + rc.Property(x => x.Name); |
| 69 | + }); |
| 70 | + |
| 71 | + mapper.Class<EntityExplicitInterface>( |
| 72 | + rc => |
| 73 | + { |
| 74 | + rc.Id(x => x.Id); |
| 75 | + rc.Property(x => x.Name); |
| 76 | + }); |
| 77 | + |
| 78 | + mapper.Class<EntityMultiInterfaces>( |
| 79 | + rc => |
| 80 | + { |
| 81 | + rc.Id(x => x.Id); |
| 82 | + rc.Property(x => x.Name); |
| 83 | + }); |
| 84 | + |
| 85 | + mapper.Class<EntityMixExplicitImplicitInterface>( |
| 86 | + rc => |
| 87 | + { |
| 88 | + rc.Table("multiInterface"); |
| 89 | + rc.Id(x => x.Id); |
| 90 | + rc.Property(x => x.Name); |
| 91 | + }); |
| 92 | + |
| 93 | + mapper.Class<EntityMultiIdProxy>( |
| 94 | + rc => |
| 95 | + { |
| 96 | + rc.Proxy(typeof(IMultiIdProxy)); |
| 97 | + rc.Id(x => x.Id); |
| 98 | + rc.Property(x => x.Name); |
| 99 | + }); |
| 100 | + |
| 101 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 102 | + } |
| 103 | + |
| 104 | + [Test] |
| 105 | + public async Task ProxyForBaseSubclassCanBeCreatedAsync() |
| 106 | + { |
| 107 | + using (var session = OpenSession()) |
| 108 | + { |
| 109 | + var entity = await (session.LoadAsync<EntityClassProxy>(_id)); |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + //Id access via implicit interface should not lead to proxy initialization |
| 114 | + [Test] |
| 115 | + public async Task ProxyClassIdAccessByImplicitInterfaceAsync() |
| 116 | + { |
| 117 | + using (var session = OpenSession()) |
| 118 | + { |
| 119 | + var entity = (IEntity) await (session.LoadAsync<EntitySimple>(_id)); |
| 120 | + CanAccessIEntityId(entity); |
| 121 | + ThrowOnIEntityNameAccess(entity); |
| 122 | + Assert.That(entity.Id, Is.EqualTo(_id)); |
| 123 | + |
| 124 | + var multiInterface = await (session.LoadAsync<EntityMultiInterfaces>(_id)); |
| 125 | + CanAccessIEntityId(multiInterface); |
| 126 | + CanAccessIEntity2Id(multiInterface); |
| 127 | + Assert.That(multiInterface.Id, Is.EqualTo(_id)); |
| 128 | + } |
| 129 | + } |
| 130 | + |
| 131 | + [Test] |
| 132 | + public async Task ProxyClassIdAccessExplicitInterfaceAsync() |
| 133 | + { |
| 134 | + using (var session = OpenSession()) |
| 135 | + { |
| 136 | + var entity = await (session.LoadAsync<EntityExplicitInterface>(_id)); |
| 137 | + |
| 138 | + ThrowOnIEntityIdAccess(entity); |
| 139 | + Assert.That(entity.Id, Is.EqualTo(_id)); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + [Test] |
| 144 | + public async Task ProxyClassIdAccessBothImplicitExplicitInterfacesAsync() |
| 145 | + { |
| 146 | + using (var session = OpenSession()) |
| 147 | + { |
| 148 | + var entity = await (session.LoadAsync<EntityMixExplicitImplicitInterface>(_id)); |
| 149 | + |
| 150 | + //IEntity2 is implicit and should be accessible without proxy initialization |
| 151 | + CanAccessIEntity2Id(entity); |
| 152 | + ThrowOnIEntityIdAccess(entity); |
| 153 | + } |
| 154 | + } |
| 155 | + |
| 156 | + [Test] |
| 157 | + public async Task ProxyInterfaceIdAccessAsync() |
| 158 | + { |
| 159 | + using (var session = OpenSession()) |
| 160 | + { |
| 161 | + var entity = await (session.LoadAsync<ISubEntityProxy>(_id)); |
| 162 | + |
| 163 | + CanAccessIEntityId(entity); |
| 164 | + } |
| 165 | + } |
| 166 | + |
| 167 | + [KnownBug("GH-2271")] |
| 168 | + [Test] |
| 169 | + public async Task ProxyInterfaceIdAccessFromDifferentInterfacesAsync() |
| 170 | + { |
| 171 | + using (var session = OpenSession()) |
| 172 | + { |
| 173 | + var entity = await (session.LoadAsync<IMultiIdProxy>(_id)); |
| 174 | + |
| 175 | + CanAccessIEntityId(entity); |
| 176 | + CanAccessIEntity2Id(entity); |
| 177 | + } |
| 178 | + } |
| 179 | + |
| 180 | + private void ThrowOnIEntityNameAccess(IEntity entity) |
| 181 | + { |
| 182 | + Assert.That(() => entity.Name, Throws.TypeOf<ObjectNotFoundException>(), "IEntity.Name access should lead to proxy initialization"); |
| 183 | + } |
| 184 | + |
| 185 | + private void ThrowOnIEntityIdAccess(IEntity entity) |
| 186 | + { |
| 187 | + Assert.That(() => entity.Id, Throws.TypeOf<ObjectNotFoundException>(), "IEntity.Id access should lead to proxy initialization"); |
| 188 | + } |
| 189 | + |
| 190 | + private void CanAccessIEntityId(IEntity entity) |
| 191 | + { |
| 192 | + Assert.That(() => entity.Id, Throws.Nothing, "Failed to access proxy IEntity.Id interface"); |
| 193 | + Assert.That(entity.Id, Is.EqualTo(_id)); |
| 194 | + } |
| 195 | + |
| 196 | + private void CanAccessIEntity2Id(IEntity2 entity) |
| 197 | + { |
| 198 | + Assert.That(() => entity.Id, Throws.Nothing, "Failed to access proxy IEntity2.Id interface"); |
| 199 | + Assert.That(entity.Id, Is.EqualTo(_id)); |
| 200 | + } |
| 201 | + } |
| 202 | +} |
0 commit comments