Skip to content

Commit 270833e

Browse files
Test the explicit interface case
And check access into constructor, and check proxy on type with interfaces. To be squashed
1 parent 08797aa commit 270833e

File tree

1 file changed

+96
-5
lines changed

1 file changed

+96
-5
lines changed

src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs

Lines changed: 96 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,41 @@ public class InternalInterfaceTestClass : IInternal
3333

3434
public interface IPublic
3535
{
36-
int Id { get; }
36+
int Id { get; set; }
37+
string Name { get; set; }
3738
}
3839

3940
[Serializable]
4041
public class PublicInterfaceTestClass : IPublic
4142
{
4243
public virtual int Id { get; set; }
44+
public virtual string Name { get; set; }
45+
46+
public PublicInterfaceTestClass()
47+
{
48+
// Check access to properties from the default constructor do not fail once proxified
49+
Assert.That(Id, Is.Zero);
50+
Assert.That(Name, Is.Null);
51+
Id = -1;
52+
Name = string.Empty;
53+
}
54+
}
55+
56+
[Serializable]
57+
public class PublicExplicitInterfaceTestClass : IPublic
58+
{
59+
int IPublic.Id { get; set; }
60+
string IPublic.Name { get; set; }
61+
62+
public PublicExplicitInterfaceTestClass()
63+
{
64+
// Check access to properties from the default constructor do not fail once proxified
65+
IPublic pub = this;
66+
Assert.That(pub.Id, Is.Zero);
67+
Assert.That(pub.Name, Is.Null);
68+
pub.Id = -1;
69+
pub.Name = string.Empty;
70+
}
4371
}
4472

4573
[Serializable]
@@ -130,7 +158,7 @@ public void VerifyProxyForClassWithInternalInterface()
130158
}
131159

132160
[Test]
133-
public void VerifyProxyForClassWithAdditionalInterface()
161+
public void VerifyProxyOnInterface()
134162
{
135163
var factory = new StaticProxyFactory();
136164
factory.PostInstantiate(
@@ -139,7 +167,7 @@ public void VerifyProxyForClassWithAdditionalInterface()
139167
// By way of the "proxy" attribute on the "class" mapping, an interface to use for the
140168
// lazy entity load proxy instead of the persistentClass can be specified. This is "translated" into
141169
// having an additional interface in the interface list, instead of just having INHibernateProxy.
142-
// (Quite a loosy semantic...)
170+
// (Quite a loose semantic...)
143171
new HashSet<System.Type> { typeof(INHibernateProxy), typeof(IPublic) },
144172
null, null, null);
145173

@@ -157,6 +185,69 @@ public void VerifyProxyForClassWithAdditionalInterface()
157185
#endif
158186
}
159187

188+
[Test]
189+
public void VerifyProxyForClassWithAdditionalInterface()
190+
{
191+
var factory = new StaticProxyFactory();
192+
factory.PostInstantiate(
193+
typeof(PublicInterfaceTestClass).FullName,
194+
typeof(PublicInterfaceTestClass),
195+
new HashSet<System.Type> {typeof(INHibernateProxy)},
196+
null, null, null);
197+
198+
#if NETFX
199+
VerifyGeneratedAssembly(
200+
() =>
201+
{
202+
#endif
203+
var proxy = factory.GetProxy(1, null);
204+
Assert.That(proxy, Is.Not.Null);
205+
Assert.That(proxy, Is.InstanceOf<IPublic>());
206+
Assert.That(proxy, Is.InstanceOf<PublicInterfaceTestClass>());
207+
208+
// Check interface and implicit implementations do both call the delegated state
209+
var state = new PublicInterfaceTestClass { Id = 5, Name = "State" };
210+
proxy.HibernateLazyInitializer.SetImplementation(state);
211+
var pub = (IPublic) proxy;
212+
var ent = (PublicInterfaceTestClass) proxy;
213+
Assert.That(pub.Id, Is.EqualTo(5), "IPublic.Id");
214+
Assert.That(ent.Id, Is.EqualTo(5), "entity.Id");
215+
Assert.That(pub.Name, Is.EqualTo("State"), "IPublic.State");
216+
Assert.That(ent.Name, Is.EqualTo("State"), "entity.State");
217+
ent.Id = 10;
218+
pub.Name = "Test";
219+
Assert.That(pub.Id, Is.EqualTo(10), "IPublic.Id");
220+
Assert.That(state.Id, Is.EqualTo(10), "state.Id");
221+
Assert.That(ent.Name, Is.EqualTo("Test"), "entity.Name");
222+
Assert.That(state.Name, Is.EqualTo("Test"), "state.Name");
223+
#if NETFX
224+
});
225+
#endif
226+
}
227+
228+
[Test]
229+
public void VerifyProxyForClassWithAdditionalExplicitInterface()
230+
{
231+
var factory = new StaticProxyFactory();
232+
factory.PostInstantiate(
233+
typeof(PublicExplicitInterfaceTestClass).FullName,
234+
typeof(PublicExplicitInterfaceTestClass),
235+
new HashSet<System.Type> {typeof(INHibernateProxy)},
236+
null, null, null);
237+
#if NETFX
238+
VerifyGeneratedAssembly(
239+
() =>
240+
{
241+
#endif
242+
var proxy = factory.GetProxy(1, null);
243+
Assert.That(proxy, Is.Not.Null);
244+
Assert.That(proxy, Is.InstanceOf<IPublic>());
245+
Assert.That(proxy, Is.InstanceOf<PublicExplicitInterfaceTestClass>());
246+
#if NETFX
247+
});
248+
#endif
249+
}
250+
160251
[Test]
161252
public void VerifyProxyForAbstractClass()
162253
{
@@ -331,10 +422,10 @@ public void VerifyFieldInterceptorProxyWithAdditionalInterface()
331422
// By way of the "proxy" attribute on the "class" mapping, an interface to use for the
332423
// lazy entity load proxy instead of the persistentClass can be specified. This is "translated" into
333424
// having an additional interface in the interface list, instead of just having INHibernateProxy.
334-
// (Quite a loosy semantic...)
425+
// (Quite a loose semantic...)
335426
// The field interceptor proxy ignores this setting, as it does not delegate its implementation
336427
// to an instance of the persistentClass, and so cannot implement interface methods if it does not
337-
// inherit the persitentClass.
428+
// inherit the persistentClass.
338429
new HashSet<System.Type> {typeof(INHibernateProxy), typeof(IPublic)},
339430
null, null, null);
340431
#if NETFX

0 commit comments

Comments
 (0)