Skip to content

Commit 569b49c

Browse files
committed
Merge branches 'whut-NH-3573', 'cremor-NH-3731', 'hazzik-NH-3468', 'lesnyh-NH-3739' and 'gliljas-NH-2504' into 4.0.x
6 parents 5e3f957 + e21cf5a + 71937fd + e8d101d + cafeaba + 3df5ff8 commit 569b49c

File tree

17 files changed

+543
-96
lines changed

17 files changed

+543
-96
lines changed
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NHibernate.Test.LazyComponentTest
7+
{
8+
public class Address
9+
{
10+
public virtual string Country { get; set; }
11+
public virtual string City { get; set; }
12+
}
13+
}
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
using System.Collections;
2+
using NUnit.Framework;
3+
4+
namespace NHibernate.Test.LazyComponentTest
5+
{
6+
[TestFixture]
7+
public class LazyComponentTestFixture : TestCase
8+
{
9+
protected override IList Mappings
10+
{
11+
get { return new[] {"LazyComponentTest.Person.hbm.xml"}; }
12+
}
13+
14+
protected override string MappingsAssembly
15+
{
16+
get { return "NHibernate.Test"; }
17+
}
18+
19+
protected override void OnSetUp()
20+
{
21+
using (var s = OpenSession())
22+
using (var t = s.BeginTransaction())
23+
{
24+
var person = new Person
25+
{
26+
Name = "Gabor",
27+
Address = new Address
28+
{
29+
Country = "HUN",
30+
City = "Budapest"
31+
}
32+
};
33+
s.Persist(person);
34+
t.Commit();
35+
}
36+
}
37+
38+
protected override void OnTearDown()
39+
{
40+
using (var s = OpenSession())
41+
using (var t = s.BeginTransaction())
42+
{
43+
s.CreateQuery("delete from Person").ExecuteUpdate();
44+
t.Commit();
45+
}
46+
}
47+
48+
[Test]
49+
public void LazyLoadTest()
50+
{
51+
using (var s = OpenSession())
52+
using (var t = s.BeginTransaction())
53+
{
54+
var p = s.CreateQuery("from Person p where name='Gabor'").UniqueResult<Person>();
55+
// make sure component has not been initialized yet
56+
Assert.That(NHibernateUtil.IsPropertyInitialized(p, "Address"), Is.False);
57+
58+
t.Commit();
59+
}
60+
}
61+
62+
[Test]
63+
public void LazyDeleteTest()
64+
{
65+
using (var s = OpenSession())
66+
using (var t = s.BeginTransaction())
67+
{
68+
var p = s.CreateQuery("from Person p where name='Gabor'").UniqueResult<Person>();
69+
// make sure component has not been initialized yet
70+
Assert.That(NHibernateUtil.IsPropertyInitialized(p, "Address"), Is.False);
71+
s.Delete(p);
72+
t.Commit();
73+
}
74+
}
75+
76+
[Test]
77+
public void LazyUpdateTest()
78+
{
79+
using (var s = OpenSession())
80+
using (var t = s.BeginTransaction())
81+
{
82+
var p = s.CreateQuery("from Person p where name='Gabor'").UniqueResult<Person>();
83+
// make sure component has not been initialized yet
84+
Assert.That(!NHibernateUtil.IsPropertyInitialized(p, "Address"));
85+
86+
p.Address.City = "Baja";
87+
s.Update(p);
88+
89+
t.Commit();
90+
}
91+
using (var s = OpenSession())
92+
using (var t = s.BeginTransaction())
93+
{
94+
var p = s.CreateQuery("from Person p where name='Gabor'").UniqueResult<Person>();
95+
Assert.That(p.Address.City, Is.EqualTo("Baja"));
96+
97+
t.Commit();
98+
}
99+
}
100+
}
101+
}
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
6+
namespace NHibernate.Test.LazyComponentTest
7+
{
8+
public class Person
9+
{
10+
public virtual string Name { get; set; }
11+
public virtual Address Address { get; set; }
12+
}
13+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?xml version="1.0" encoding="utf-8" ?>
2+
<hibernate-mapping xmlns="urn:nhibernate-mapping-2.2"
3+
assembly="NHibernate.Test"
4+
namespace="NHibernate.Test.LazyComponentTest">
5+
6+
<class name="Person">
7+
<id name="Name"/>
8+
<component name="Address" lazy="true">
9+
<property name="Country" />
10+
<property name="City" />
11+
</component>
12+
</class>
13+
14+
</hibernate-mapping>

src/NHibernate.Test/Linq/QueryCacheableTests.cs

Lines changed: 61 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,5 +114,65 @@ public void CacheableRegionBeforeOtherClauses()
114114
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(2));
115115
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1));
116116
}
117-
}
117+
118+
[Test]
119+
public void GroupByQueryIsCacheable()
120+
{
121+
Sfi.Statistics.Clear();
122+
Sfi.QueryCache.Clear();
123+
124+
var c = db
125+
.Customers
126+
.GroupBy(x => x.Address.Country)
127+
.Select(x=>x.Key)
128+
.Cacheable()
129+
.ToList();
130+
131+
c = db
132+
.Customers
133+
.GroupBy(x => x.Address.Country)
134+
.Select(x => x.Key)
135+
.ToList();
136+
137+
c = db
138+
.Customers
139+
.GroupBy(x => x.Address.Country)
140+
.Select(x => x.Key)
141+
.Cacheable()
142+
.ToList();
143+
144+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2));
145+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1));
146+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1));
147+
}
148+
149+
[Test]
150+
public void GroupByQueryIsCacheable2()
151+
{
152+
Sfi.Statistics.Clear();
153+
Sfi.QueryCache.Clear();
154+
155+
var c = db
156+
.Customers.Cacheable()
157+
.GroupBy(x => x.Address.Country)
158+
.Select(x => x.Key)
159+
.ToList();
160+
161+
c = db
162+
.Customers
163+
.GroupBy(x => x.Address.Country)
164+
.Select(x => x.Key)
165+
.ToList();
166+
167+
c = db
168+
.Customers.Cacheable()
169+
.GroupBy(x => x.Address.Country)
170+
.Select(x => x.Key)
171+
.ToList();
172+
173+
Assert.That(Sfi.Statistics.QueryExecutionCount, Is.EqualTo(2));
174+
Assert.That(Sfi.Statistics.QueryCachePutCount, Is.EqualTo(1));
175+
Assert.That(Sfi.Statistics.QueryCacheHitCount, Is.EqualTo(1));
176+
}
177+
}
118178
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
using System;
2+
using System.Collections.Generic;
3+
4+
namespace NHibernate.Test.NHSpecificTest.NH3731
5+
{
6+
[Serializable]
7+
class Parent
8+
{
9+
public Parent()
10+
{
11+
ChildrenList = new List<ListChild>();
12+
ChildrenMap = new Dictionary<string, MapChild>();
13+
}
14+
15+
public virtual Guid Id { get; set; }
16+
public virtual string Name { get; set; }
17+
public virtual IList<ListChild> ChildrenList { get; set; }
18+
public virtual IDictionary<string, MapChild> ChildrenMap { get; set; }
19+
}
20+
21+
[Serializable]
22+
class ListChild
23+
{
24+
public virtual Guid Id { get; set; }
25+
public virtual string Name { get; set; }
26+
}
27+
28+
[Serializable]
29+
class MapChild
30+
{
31+
public virtual Guid Id { get; set; }
32+
public virtual string Name { get; set; }
33+
}
34+
}
Lines changed: 145 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,145 @@
1+
using System.IO;
2+
using System.Linq;
3+
using System.Runtime.Serialization.Formatters.Binary;
4+
using NHibernate.Cfg.MappingSchema;
5+
using NHibernate.Linq;
6+
using NHibernate.Mapping.ByCode;
7+
using NUnit.Framework;
8+
9+
namespace NHibernate.Test.NHSpecificTest.NH3731
10+
{
11+
public class ByCodeFixture : TestCaseMappingByCode
12+
{
13+
protected override HbmMapping GetMappings()
14+
{
15+
var mapper = new ModelMapper();
16+
mapper.Class<Parent>(rc =>
17+
{
18+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
19+
rc.Property(x => x.Name);
20+
rc.List(x => x.ChildrenList, c => c.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.DeleteOrphans), x => x.OneToMany());
21+
rc.Map(x => x.ChildrenMap, c => c.Cascade(Mapping.ByCode.Cascade.All | Mapping.ByCode.Cascade.DeleteOrphans), x => x.OneToMany());
22+
});
23+
mapper.Class<ListChild>(rc =>
24+
{
25+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
26+
rc.Property(x => x.Name);
27+
});
28+
mapper.Class<MapChild>(rc =>
29+
{
30+
rc.Id(x => x.Id, m => m.Generator(Generators.GuidComb));
31+
rc.Property(x => x.Name);
32+
});
33+
34+
return mapper.CompileMappingForAllExplicitlyAddedEntities();
35+
}
36+
37+
protected override void OnSetUp()
38+
{
39+
using (ISession session = OpenSession())
40+
using (ITransaction transaction = session.BeginTransaction())
41+
{
42+
var p = new Parent { Name = "Parent" };
43+
p.ChildrenList.Add(new ListChild { Name = "ListChild 1" });
44+
p.ChildrenList.Add(new ListChild { Name = "ListChild 2" });
45+
p.ChildrenList.Add(new ListChild { Name = "ListChild 3" });
46+
p.ChildrenMap.Add("first", new MapChild { Name = "MapChild 1" });
47+
p.ChildrenMap.Add("second", new MapChild { Name = "MapChild 2" });
48+
p.ChildrenMap.Add("third", new MapChild { Name = "MapChild 3" });
49+
50+
session.Save(p);
51+
52+
session.Flush();
53+
transaction.Commit();
54+
}
55+
}
56+
57+
protected override void OnTearDown()
58+
{
59+
using (ISession session = OpenSession())
60+
using (ITransaction transaction = session.BeginTransaction())
61+
{
62+
session.Delete("from System.Object");
63+
64+
session.Flush();
65+
transaction.Commit();
66+
}
67+
}
68+
69+
[Test]
70+
public void Serializing_Session_After_Reordering_ChildrenList_Should_Work()
71+
{
72+
using (ISession session = OpenSession())
73+
{
74+
using (ITransaction transaction = session.BeginTransaction())
75+
{
76+
var p = session.Query<Parent>().Single();
77+
var c = p.ChildrenList.Last();
78+
p.ChildrenList.Remove(c);
79+
p.ChildrenList.Insert(p.ChildrenList.Count - 1, c);
80+
session.Flush();
81+
transaction.Commit();
82+
}
83+
84+
using (MemoryStream stream = new MemoryStream())
85+
{
86+
BinaryFormatter formatter = new BinaryFormatter();
87+
formatter.Serialize(stream, session);
88+
89+
Assert.AreNotEqual(0, stream.Length);
90+
}
91+
}
92+
}
93+
94+
[Test]
95+
public void Serializing_Session_After_Deleting_First_Child_In_List_Should_Work()
96+
{
97+
using (ISession session = OpenSession())
98+
{
99+
using (ITransaction transaction = session.BeginTransaction())
100+
{
101+
var p = session.Query<Parent>().Single();
102+
p.ChildrenList.RemoveAt(0);
103+
session.Flush();
104+
transaction.Commit();
105+
}
106+
107+
using (MemoryStream stream = new MemoryStream())
108+
{
109+
BinaryFormatter formatter = new BinaryFormatter();
110+
formatter.Serialize(stream, session);
111+
112+
Assert.AreNotEqual(0, stream.Length);
113+
}
114+
}
115+
}
116+
117+
[Test]
118+
public void Serializing_Session_After_Changing_Key_ChildrenMap_Should_Work()
119+
{
120+
using (ISession session = OpenSession())
121+
{
122+
using (ITransaction transaction = session.BeginTransaction())
123+
{
124+
var p = session.Query<Parent>().Single();
125+
var firstChild = p.ChildrenMap["first"];
126+
var secondChild = p.ChildrenMap["second"];
127+
p.ChildrenMap.Remove("first");
128+
p.ChildrenMap.Remove("second");
129+
p.ChildrenMap.Add("first", secondChild);
130+
p.ChildrenMap.Add("second", firstChild);
131+
session.Flush();
132+
transaction.Commit();
133+
}
134+
135+
using (MemoryStream stream = new MemoryStream())
136+
{
137+
BinaryFormatter formatter = new BinaryFormatter();
138+
formatter.Serialize(stream, session);
139+
140+
Assert.AreNotEqual(0, stream.Length);
141+
}
142+
}
143+
}
144+
}
145+
}

0 commit comments

Comments
 (0)