Skip to content

Commit 20fbd03

Browse files
fredericDelaportehazzik
authored andcommitted
Test the explicit interface case
And check access into constructor, and check proxy on type with interfaces. To be squashed
1 parent 73cc71c commit 20fbd03

File tree

1 file changed

+111
-4
lines changed

1 file changed

+111
-4
lines changed

src/NHibernate.Test/StaticProxyTest/StaticProxyFactoryFixture.cs

Lines changed: 111 additions & 4 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]
@@ -172,7 +200,7 @@ public void VerifyProxyForClassWithAdditionalInterface()
172200
// By way of the "proxy" attribute on the "class" mapping, an interface to use for the
173201
// lazy entity load proxy instead of the persistentClass can be specified. This is "translated" into
174202
// having an additional interface in the interface list, instead of just having INHibernateProxy.
175-
// (Quite a loosy semantic...)
203+
// (Quite a loose semantic...)
176204
new HashSet<System.Type> { typeof(INHibernateProxy), typeof(IPublic) },
177205
null, null, null);
178206

@@ -190,6 +218,85 @@ public void VerifyProxyForClassWithAdditionalInterface()
190218
#endif
191219
}
192220

221+
[Test]
222+
public void VerifyProxyForClassWithInterface()
223+
{
224+
var factory = new StaticProxyFactory();
225+
factory.PostInstantiate(
226+
typeof(PublicInterfaceTestClass).FullName,
227+
typeof(PublicInterfaceTestClass),
228+
new HashSet<System.Type> {typeof(INHibernateProxy)},
229+
null, null, null);
230+
231+
#if NETFX
232+
VerifyGeneratedAssembly(
233+
() =>
234+
{
235+
#endif
236+
var proxy = factory.GetProxy(1, null);
237+
Assert.That(proxy, Is.Not.Null);
238+
Assert.That(proxy, Is.InstanceOf<IPublic>());
239+
Assert.That(proxy, Is.InstanceOf<PublicInterfaceTestClass>());
240+
241+
// Check interface and implicit implementations do both call the delegated state
242+
var state = new PublicInterfaceTestClass { Id = 5, Name = "State" };
243+
proxy.HibernateLazyInitializer.SetImplementation(state);
244+
var pub = (IPublic) proxy;
245+
var ent = (PublicInterfaceTestClass) proxy;
246+
Assert.That(pub.Id, Is.EqualTo(5), "IPublic.Id");
247+
Assert.That(ent.Id, Is.EqualTo(5), "entity.Id");
248+
Assert.That(pub.Name, Is.EqualTo("State"), "IPublic.Name");
249+
Assert.That(ent.Name, Is.EqualTo("State"), "entity.Name");
250+
ent.Id = 10;
251+
pub.Name = "Test";
252+
Assert.That(pub.Id, Is.EqualTo(10), "IPublic.Id");
253+
Assert.That(state.Id, Is.EqualTo(10), "state.Id");
254+
Assert.That(ent.Name, Is.EqualTo("Test"), "entity.Name");
255+
Assert.That(state.Name, Is.EqualTo("Test"), "state.Name");
256+
#if NETFX
257+
});
258+
#endif
259+
}
260+
261+
[Test]
262+
public void VerifyProxyForClassWithExplicitInterface()
263+
{
264+
var factory = new StaticProxyFactory();
265+
factory.PostInstantiate(
266+
typeof(PublicExplicitInterfaceTestClass).FullName,
267+
typeof(PublicExplicitInterfaceTestClass),
268+
new HashSet<System.Type> {typeof(INHibernateProxy)},
269+
null, null, null);
270+
#if NETFX
271+
VerifyGeneratedAssembly(
272+
() =>
273+
{
274+
#endif
275+
var proxy = factory.GetProxy(1, null);
276+
Assert.That(proxy, Is.Not.Null);
277+
Assert.That(proxy, Is.InstanceOf<IPublic>());
278+
Assert.That(proxy, Is.InstanceOf<PublicExplicitInterfaceTestClass>());
279+
280+
// Check interface and implicit implementations do both call the delegated state
281+
IPublic state = new PublicExplicitInterfaceTestClass();
282+
state.Id = 5;
283+
state.Name = "State";
284+
proxy.HibernateLazyInitializer.SetImplementation(state);
285+
var entity = (IPublic) proxy;
286+
Assert.That(entity.Id, Is.EqualTo(5), "Id");
287+
Assert.That(entity.Name, Is.EqualTo("State"), "Name");
288+
289+
entity.Id = 10;
290+
entity.Name = "Test";
291+
Assert.That(entity.Id, Is.EqualTo(10), "entity.Id");
292+
Assert.That(state.Id, Is.EqualTo(10), "state.Id");
293+
Assert.That(entity.Name, Is.EqualTo("Test"), "entity.Name");
294+
Assert.That(state.Name, Is.EqualTo("Test"), "state.Name");
295+
#if NETFX
296+
});
297+
#endif
298+
}
299+
193300
[Test]
194301
public void VerifyProxyForRefOutClass()
195302
{
@@ -409,10 +516,10 @@ public void VerifyFieldInterceptorProxyWithAdditionalInterface()
409516
// By way of the "proxy" attribute on the "class" mapping, an interface to use for the
410517
// lazy entity load proxy instead of the persistentClass can be specified. This is "translated" into
411518
// having an additional interface in the interface list, instead of just having INHibernateProxy.
412-
// (Quite a loosy semantic...)
519+
// (Quite a loose semantic...)
413520
// The field interceptor proxy ignores this setting, as it does not delegate its implementation
414521
// to an instance of the persistentClass, and so cannot implement interface methods if it does not
415-
// inherit the persitentClass.
522+
// inherit the persistentClass.
416523
new HashSet<System.Type> {typeof(INHibernateProxy), typeof(IPublic)},
417524
null, null, null);
418525
#if NETFX

0 commit comments

Comments
 (0)