Skip to content

Commit 8a5f50d

Browse files
committed
Merge branch NH-3432, containing backported fixes for dynamic proxy issues NH-3132, NH-2819, NH-2726, NH-3244 and NH-3058.
2 parents 575d3bb + d3f47d6 commit 8a5f50d

19 files changed

+813
-438
lines changed
Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,62 @@
1-
using System.Collections.Generic;
21
using NHibernate.Proxy.DynamicProxy;
32
using NUnit.Framework;
43
using SharpTestsEx;
54

65
namespace NHibernate.Test.DynamicProxyTests.GenericMethodsTests
76
{
7+
[TestFixture]
88
public class GenericMethodShouldBeProxied
99
{
10-
public class MyClass
10+
[Test]
11+
public void CanProxyBasicGenericMethod()
1112
{
12-
public virtual object Method<T>()
13-
{
14-
if(typeof(T) == typeof(int))
15-
{
16-
return 5;
17-
}
18-
if (typeof(T) == typeof(string))
19-
{
20-
return "blha";
21-
}
22-
return default(T);
23-
}
24-
25-
public virtual TRequestedType As<TRequestedType>() where TRequestedType : MyClass
26-
{
27-
return this as TRequestedType;
28-
}
13+
var factory = new ProxyFactory();
14+
var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
15+
c.BasicGenericMethod<int>().Should().Be(5);
16+
c.BasicGenericMethod<string>().Should().Be("blha");
2917
}
3018

3119
[Test]
32-
public void ProxyOfAGenericMethod()
20+
public void CanProxyMethodWithGenericBaseClassConstraint()
3321
{
3422
var factory = new ProxyFactory();
3523
var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
36-
c.Method<int>().Should().Be(5);
37-
c.Method<string>().Should().Be("blha");
24+
c.MethodWithGenericBaseClassConstraint<MyGenericClass<int>, int>().Should().Be(typeof(MyGenericClass<int>));
3825
}
3926

4027
[Test]
41-
public void ProxyOfSelfCastingMethod()
28+
public void CanProxySelfCastingMethod()
4229
{
4330
var factory = new ProxyFactory();
4431
var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
4532
c.As<MyClass>().Should().Not.Be.Null();
4633
}
34+
35+
[Test]
36+
public void CanProxyMethodWithDefaultConstructorConstraint()
37+
{
38+
var factory = new ProxyFactory();
39+
var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
40+
c.MethodWithConstructorConstraint<MyClass>().Should().Not.Be.Null();
41+
}
42+
43+
[Test]
44+
public void CanProxyGenericMethodWithInterfaceConstraint()
45+
{
46+
var factory = new ProxyFactory();
47+
var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyClass()), null);
48+
c.MethodWithInterfaceConstraint<IMyInterface>().Should().Be(typeof(IMyInterface));
49+
}
50+
51+
[Test]
52+
public void CanProxyGenericMethodWithReferenceTypeAndInterfaceConstraint()
53+
{
54+
var factory = new ProxyFactory();
55+
var c = (MyClass)factory.CreateProxy(typeof(MyClass), new PassThroughInterceptor(new MyDerivedClass()), null);
56+
c.MethodWithReferenceTypeAndInterfaceConstraint<MyDerivedClass>().Should().Not.Be.Null();
57+
}
58+
59+
class MyDerivedClass : MyClass, IMyInterface
60+
{ }
4761
}
4862
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace NHibernate.Test.DynamicProxyTests.GenericMethodsTests
2+
{
3+
public interface IMyGenericInterface<TId>
4+
{
5+
}
6+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
namespace NHibernate.Test.DynamicProxyTests.GenericMethodsTests
2+
{
3+
public interface IMyInterface
4+
{
5+
}
6+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
namespace NHibernate.Test.DynamicProxyTests.GenericMethodsTests
2+
{
3+
public class MyClass
4+
{
5+
public virtual object BasicGenericMethod<T>()
6+
{
7+
if (typeof(T) == typeof(int))
8+
return 5;
9+
10+
if (typeof(T) == typeof(string))
11+
return "blha";
12+
13+
return default(T);
14+
}
15+
16+
public virtual object MethodWithGenericBaseClassConstraint<T, TY>() where T : MyGenericClass<TY>
17+
{
18+
return typeof(T);
19+
}
20+
21+
public virtual object MethodWithInterfaceConstraint<T>() where T : IMyInterface
22+
{
23+
return typeof(T);
24+
}
25+
26+
public virtual TRequestedType As<TRequestedType>() where TRequestedType : MyClass
27+
{
28+
return this as TRequestedType;
29+
}
30+
31+
public virtual TRequestedType MethodWithConstructorConstraint<TRequestedType>() where TRequestedType : new()
32+
{
33+
return new TRequestedType();
34+
}
35+
36+
public virtual TRequestedType MethodWithReferenceTypeAndInterfaceConstraint<TRequestedType>() where TRequestedType : class, IMyInterface
37+
{
38+
return this as TRequestedType;
39+
}
40+
}
41+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
namespace NHibernate.Test.DynamicProxyTests.GenericMethodsTests
2+
{
3+
public class MyGenericClass<TId> : IMyGenericInterface<TId>
4+
{
5+
public virtual TRequestedType As<TRequestedType>() where TRequestedType : MyGenericClass<TId>
6+
{
7+
return this as TRequestedType;
8+
}
9+
10+
public virtual TRequestedType AsInterface<TRequestedType>() where TRequestedType : class, IMyGenericInterface<TId>
11+
{
12+
return this as TRequestedType;
13+
}
14+
}
15+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
namespace NHibernate.Test.NHSpecificTest.NH3058
2+
{
3+
public class DomainClass
4+
{
5+
public virtual int Id { get; set; }
6+
public virtual string Name { get; set; }
7+
public virtual string ALotOfText { get; set; }
8+
9+
public virtual string LoadLazyProperty()
10+
{
11+
return ALotOfText;
12+
}
13+
}
14+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.NH3058">
4+
<class name="DomainClass">
5+
<id name="Id">
6+
<generator class="assigned" />
7+
</id>
8+
<property name="Name" />
9+
<property name="ALotOfText" lazy="true" />
10+
</class>
11+
</hibernate-mapping>
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using NHibernate.Context;
2+
using NHibernate.Engine;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3058
6+
{
7+
[TestFixture]
8+
public class SampleTest : BugTestCase
9+
{
10+
public static ISessionFactoryImplementor AmbientSfi { get; private set; }
11+
12+
protected override void BuildSessionFactory()
13+
{
14+
base.BuildSessionFactory();
15+
16+
AmbientSfi = Sfi;
17+
}
18+
19+
protected override void Configure(Cfg.Configuration configuration)
20+
{
21+
base.Configure(configuration);
22+
23+
configuration.Properties.Add("current_session_context_class", "thread_static");
24+
}
25+
26+
protected override ISession OpenSession()
27+
{
28+
var session = base.OpenSession();
29+
30+
CurrentSessionContext.Bind(session);
31+
32+
return session;
33+
}
34+
35+
protected override void OnSetUp()
36+
{
37+
base.OnSetUp();
38+
using (var s = OpenSession())
39+
using (var tx = s.BeginTransaction())
40+
{
41+
var book = new DomainClass
42+
{
43+
Name = "Some name",
44+
ALotOfText = "Some text",
45+
Id = 1
46+
};
47+
48+
s.Persist(book);
49+
tx.Commit();
50+
}
51+
}
52+
53+
protected override void OnTearDown()
54+
{
55+
base.OnTearDown();
56+
using (var s = OpenSession())
57+
using (var tx = s.BeginTransaction())
58+
{
59+
Assert.That(s.CreateSQLQuery("delete from DomainClass").ExecuteUpdate(), Is.EqualTo(1));
60+
tx.Commit();
61+
}
62+
}
63+
64+
[Test]
65+
public void MethodShouldLoadLazyProperty()
66+
{
67+
using (var s = OpenSession())
68+
using (var tx = s.BeginTransaction())
69+
{
70+
var book = s.Load<DomainClass>(1);
71+
72+
Assert.False(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"));
73+
74+
string value = book.LoadLazyProperty();
75+
76+
Assert.That(value, Is.EqualTo("Some text"));
77+
Assert.That(NHibernateUtil.IsPropertyInitialized(book, "ALotOfText"), Is.True);
78+
79+
tx.Commit();
80+
}
81+
}
82+
}
83+
}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System.Collections;
2+
using NHibernate.Criterion;
3+
using NUnit.Framework;
4+
5+
namespace NHibernate.Test.NHSpecificTest.NH3132
6+
{
7+
[TestFixture]
8+
public class Fixture : BugTestCase
9+
{
10+
/// <summary>
11+
/// push some data into the database
12+
/// Really functions as a save test also
13+
/// </summary>
14+
protected override void OnSetUp()
15+
{
16+
base.OnSetUp();
17+
18+
using (var session = OpenSession())
19+
{
20+
using (var tran = session.BeginTransaction())
21+
{
22+
Product product = new Product();
23+
product.Name = "First";
24+
product.Lazy = "Lazy";
25+
26+
session.Save(product);
27+
28+
tran.Commit();
29+
}
30+
}
31+
}
32+
33+
protected override void OnTearDown()
34+
{
35+
base.OnTearDown();
36+
37+
using (var session = OpenSession())
38+
using (var tran = session.BeginTransaction())
39+
{
40+
session.Delete("from Product");
41+
tran.Commit();
42+
}
43+
}
44+
45+
[Test]
46+
public void Query_returns_correct_name()
47+
{
48+
using (var session = OpenSession())
49+
{
50+
Product product = session.CreateCriteria(typeof (Product))
51+
.Add(Restrictions.Eq("Name", "First"))
52+
.UniqueResult<Product>();
53+
54+
Assert.IsNotNull(product);
55+
Assert.AreEqual("First", product.Name);
56+
}
57+
}
58+
59+
[Test]
60+
public void Correct_value_gets_saved()
61+
{
62+
using (var session = OpenSession())
63+
{
64+
var product = session.CreateCriteria(typeof(Product))
65+
.Add(Restrictions.Eq("Name", "First"))
66+
.UniqueResult<Product>();
67+
68+
Assert.That(product, Is.Not.Null);
69+
product.Name = "Changed";
70+
71+
session.Flush();
72+
73+
session.Clear();
74+
75+
var product1 = session.CreateCriteria(typeof(Product))
76+
.Add(Restrictions.Eq("Name", "Changed"))
77+
.UniqueResult<Product>();
78+
79+
Assert.That(product1, Is.Not.Null);
80+
Assert.That(product1.Name, Is.EqualTo("Changed"));
81+
}
82+
}
83+
84+
[Test]
85+
public void Correct_value_gets_saved_with_lazy()
86+
{
87+
using (var session = OpenSession())
88+
{
89+
var product = session.CreateCriteria(typeof(Product))
90+
.Add(Restrictions.Eq("Name", "First"))
91+
.UniqueResult<Product>();
92+
93+
Assert.That(product, Is.Not.Null);
94+
product.Name = "Changed";
95+
product.Lazy = "LazyChanged";
96+
97+
session.Flush();
98+
99+
session.Clear();
100+
101+
var product1 = session.CreateCriteria(typeof(Product))
102+
.Add(Restrictions.Eq("Name", "Changed"))
103+
.UniqueResult<Product>();
104+
105+
Assert.That(product1, Is.Not.Null);
106+
Assert.That(product1.Name, Is.EqualTo("Changed"));
107+
Assert.That(product1.Lazy, Is.EqualTo("LazyChanged"));
108+
}
109+
}
110+
}
111+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2" assembly="NHibernate.Test"
3+
namespace="NHibernate.Test.NHSpecificTest.NH3132">
4+
<class name="Product" table="Products">
5+
<id name="Id" type="Guid">
6+
<generator class="guid" />
7+
</id>
8+
<property name="Name" access="field.camelcase-underscore"/>
9+
<property name="Lazy" lazy="true"/>
10+
</class>
11+
</hibernate-mapping>

0 commit comments

Comments
 (0)