|
| 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; |
| 13 | +using NHibernate.Cfg.MappingSchema; |
| 14 | +using NHibernate.Mapping.ByCode; |
| 15 | +using NUnit.Framework; |
| 16 | + |
| 17 | +namespace NHibernate.Test.StaticProxyTest.InterfaceHandling |
| 18 | +{ |
| 19 | + using System.Threading.Tasks; |
| 20 | + [TestFixture] |
| 21 | + public class FixtureAsync : TestCaseMappingByCode |
| 22 | + { |
| 23 | + private Guid _id = Guid.NewGuid(); |
| 24 | + |
| 25 | + protected override HbmMapping GetMappings() |
| 26 | + { |
| 27 | + var mapper = new ModelMapper(); |
| 28 | + |
| 29 | + #region Subclass hierarchy |
| 30 | + |
| 31 | + mapper.Class<EntityClassProxy>( |
| 32 | + rc => |
| 33 | + { |
| 34 | + rc.Id(x => x.Id); |
| 35 | + rc.Property(x => x.Name); |
| 36 | + }); |
| 37 | + |
| 38 | + mapper.UnionSubclass<SubEntityInterfaceProxy>( |
| 39 | + rc => |
| 40 | + { |
| 41 | + rc.Proxy(typeof(ISubEntityProxy)); |
| 42 | + |
| 43 | + rc.Property(x => x.AnotherName); |
| 44 | + }); |
| 45 | + |
| 46 | + mapper.UnionSubclass<AnotherSubEntityInterfaceProxy>( |
| 47 | + rc => |
| 48 | + { |
| 49 | + rc.Proxy(typeof(IAnotherSubEntityProxy)); |
| 50 | + |
| 51 | + rc.Property(x => x.AnotherName); |
| 52 | + }); |
| 53 | + |
| 54 | + mapper.Class<EntityWithSuperClassInterfaceLookup>( |
| 55 | + rc => |
| 56 | + { |
| 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 | + |
| 68 | + rc.Id(x => x.Id); |
| 69 | + rc.Property(x => x.Name); |
| 70 | + }); |
| 71 | + |
| 72 | + mapper.Class<EntityExplicitInterface>( |
| 73 | + rc => |
| 74 | + { |
| 75 | + |
| 76 | + rc.Id(x => x.Id); |
| 77 | + rc.Property(x => x.Name); |
| 78 | + }); |
| 79 | + |
| 80 | + mapper.Class<EntityMultiInterfaces>( |
| 81 | + rc => |
| 82 | + { |
| 83 | + |
| 84 | + rc.Id(x => x.Id); |
| 85 | + rc.Property(x => x.Name); |
| 86 | + }); |
| 87 | + |
| 88 | + mapper.Class<EntityMixExplicitImplicitInterface>( |
| 89 | + rc => |
| 90 | + { |
| 91 | + |
| 92 | + rc.Id(x => x.Id); |
| 93 | + rc.Property(x => x.Name); |
| 94 | + }); |
| 95 | + |
| 96 | + return mapper.CompileMappingForAllExplicitlyAddedEntities(); |
| 97 | + } |
| 98 | + |
| 99 | + [Test] |
| 100 | + public async Task ProxyForBaseSubclassCanBeCreatedAsync() |
| 101 | + { |
| 102 | + using (var session = OpenSession()) |
| 103 | + { |
| 104 | + var entity = await (session.LoadAsync<EntityClassProxy>(_id)); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + [Test] |
| 109 | + public async Task ProxyForBaseSubclassByInterfaceAsync() |
| 110 | + { |
| 111 | + using (var session = OpenSession()) |
| 112 | + { |
| 113 | + var entity = (IEntity) await (session.LoadAsync(typeof(EntityClassProxy), _id)); |
| 114 | + ThrowOnIdAccess(entity); |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + [Test] |
| 119 | + public async Task ProxyForEntityImplicitInterfaceAsync() |
| 120 | + { |
| 121 | + using (var session = OpenSession()) |
| 122 | + { |
| 123 | + var entity = (IEntity) await (session.LoadAsync<EntitySimple>(_id)); |
| 124 | + ThrowOnIdAccess(entity); |
| 125 | + ThrowOnNameAccess(entity); |
| 126 | + |
| 127 | + var multiInterface = await (session.LoadAsync<EntityMultiInterfaces>(_id)); |
| 128 | + ThrowOnIdAccess(multiInterface); |
| 129 | + ThrowOnIdAccessSecond(multiInterface); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + [Test] |
| 134 | + public async Task ProxyForEntityInitializeOnExplicitInterfaceAsync() |
| 135 | + { |
| 136 | + using (var session = OpenSession()) |
| 137 | + { |
| 138 | + var entity = await (session.LoadAsync<EntityExplicitInterface>(_id)); |
| 139 | + ThrowOnIdAccess(entity); |
| 140 | + } |
| 141 | + } |
| 142 | + |
| 143 | + [Test] |
| 144 | + public async Task ProxyForEntityWithMixedImplicitExplicitInterfacesAreHandledCorrectlyAsync() |
| 145 | + { |
| 146 | + using (var session = OpenSession()) |
| 147 | + { |
| 148 | + var entity = await (session.LoadAsync<EntityMixExplicitImplicitInterface>(_id)); |
| 149 | + ThrowOnIdAccessSecond(entity); |
| 150 | + ThrowOnIdAccess(entity); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + private void ThrowOnNameAccess(IEntity entity) |
| 155 | + { |
| 156 | + Assert.That(() => entity.Name, Throws.TypeOf<ObjectNotFoundException>(), "Non Id interface access should lead to proxy initialization"); |
| 157 | + } |
| 158 | + |
| 159 | + private void ThrowOnIdAccess(IEntity entity) |
| 160 | + { |
| 161 | + Assert.That(() => entity.Id, Throws.TypeOf<ObjectNotFoundException>(), "IEntity.Id access should lead to proxy initialization"); |
| 162 | + } |
| 163 | + |
| 164 | + private void ThrowOnIdAccessSecond(IEntityId entity) |
| 165 | + { |
| 166 | + Assert.That(() => entity.Id, Throws.TypeOf<ObjectNotFoundException>(), "IEntityId.Id access should lead to proxy initialization"); |
| 167 | + } |
| 168 | + } |
| 169 | +} |
0 commit comments