Skip to content

Commit b10bab1

Browse files
committed
Tests for proxy interface handling
1 parent ad676cc commit b10bab1

File tree

5 files changed

+458
-0
lines changed

5 files changed

+458
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
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+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System;
2+
3+
namespace NHibernate.Test.StaticProxyTest.InterfaceHandling
4+
{
5+
public interface IEntity
6+
{
7+
Guid Id { get; set; }
8+
9+
string Name { get; set; }
10+
}
11+
12+
public interface IEntityId
13+
{
14+
Guid Id { get; set; }
15+
}
16+
}
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
using System;
2+
3+
namespace NHibernate.Test.StaticProxyTest.InterfaceHandling
4+
{
5+
public class EntitySimple : IEntity
6+
{
7+
public virtual Guid Id { get; set; }
8+
9+
public virtual string Name { get; set; }
10+
}
11+
12+
public class EntityMultiInterfaces : IEntity, IEntityId
13+
{
14+
public virtual Guid Id { get; set; }
15+
16+
public virtual string Name { get; set; }
17+
}
18+
19+
public class EntityExplicitInterface : IEntity
20+
{
21+
private Guid id;
22+
private string name;
23+
24+
public virtual Guid Id { get; set; }
25+
26+
public virtual string Name { get; set; }
27+
28+
Guid IEntity.Id
29+
{
30+
get => id;
31+
set => id = value;
32+
}
33+
34+
string IEntity.Name
35+
{
36+
get => name;
37+
set => name = value;
38+
}
39+
}
40+
41+
public class EntityMixExplicitImplicitInterface : IEntity, IEntityId
42+
{
43+
private Guid id;
44+
private string name;
45+
46+
public virtual Guid Id { get; set; }
47+
48+
public virtual string Name { get; set; }
49+
50+
Guid IEntity.Id
51+
{
52+
get => id;
53+
set => id = value;
54+
}
55+
56+
string IEntity.Name
57+
{
58+
get => name;
59+
set => name = value;
60+
}
61+
}
62+
63+
public class EntityWithSuperClassInterfaceLookup
64+
{
65+
public virtual Guid Id { get; set; }
66+
67+
public virtual string Name { get; set; }
68+
69+
public virtual IEntity EntityLookup { get; set; }
70+
}
71+
}

0 commit comments

Comments
 (0)