forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAbstractMultipleCollectionFetchFixture.cs
157 lines (137 loc) · 3.65 KB
/
AbstractMultipleCollectionFetchFixture.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
using System;
using System.Collections;
using System.Collections.Generic;
using NUnit.Framework;
namespace NHibernate.Test.MultipleCollectionFetchTest
{
[TestFixture]
public abstract class AbstractMultipleCollectionFetchFixture : TestCase
{
protected override string MappingsAssembly
{
get { return "NHibernate.Test"; }
}
protected abstract void AddToCollection(ICollection<Person> collection, Person person);
protected abstract ICollection<Person> CreateCollection();
protected override void OnTearDown()
{
base.OnTearDown();
using (ISession s = OpenSession())
{
s.Delete("from Person p where p.Parent is null");
s.Flush();
}
}
private Person CreateGrandparent()
{
Person parent = new Person();
parent.Children = CreateCollection();
for (int i = 0; i < 2; i++)
{
Person child = new Person();
child.Parent = parent;
AddToCollection(parent.Children, child);
child.Children = CreateCollection();
for (int j = 0; j < 3; j++)
{
Person grandChild = new Person();
grandChild.Parent = child;
AddToCollection(child.Children, grandChild);
}
}
return parent;
}
protected virtual void RunLinearJoinFetchTest(Person parent)
{
using (ISession s = OpenSession())
using(ITransaction tx = s.BeginTransaction())
{
s.Save(parent);
tx.Commit();
}
using (ISession s = OpenSession())
{
Person p = (Person) s.CreateQuery(
"select p from Person p join fetch p.Children c join fetch c.Children gc")
.UniqueResult();
Assert.IsTrue(NHibernateUtil.IsInitialized(p.Children));
Assert.AreEqual(2, p.Children.Count);
foreach (Person child in p.Children)
{
Assert.IsTrue(NHibernateUtil.IsInitialized(child));
Assert.IsTrue(NHibernateUtil.IsInitialized(child.Children));
Assert.AreEqual(3, child.Children.Count);
}
}
}
// Tests "linear" join fetch, i.e. A join A.B join A.B.C
[Test]
public void MultipleCollectionsLinearJoinFetch()
{
Person parent = CreateGrandparent();
RunLinearJoinFetchTest(parent);
}
private Person CreateParentAndFriend()
{
Person parent = new Person();
parent.Children = CreateCollection();
for (int i = 0; i < 2; i++)
{
Person child = new Person();
child.Parent = parent;
AddToCollection(parent.Children, child);
}
parent.Friends = CreateCollection();
for (int i = 0; i < 3; i++)
{
Person friend = new Person();
AddToCollection(parent.Friends, friend);
}
return parent;
}
protected virtual void RunNonLinearJoinFetchTest(Person person)
{
using (ISession s = OpenSession())
{
s.Save(person);
s.Flush();
}
try
{
using (ISession s = OpenSession())
{
Person p = (Person) s.CreateQuery(
"select p from Person p join fetch p.Children join fetch p.Friends f")
.UniqueResult();
Assert.IsTrue(NHibernateUtil.IsInitialized(p.Children));
Assert.AreEqual(2, p.Children.Count);
foreach (Person child in p.Children)
{
Assert.IsTrue(NHibernateUtil.IsInitialized(child));
}
Assert.IsTrue(NHibernateUtil.IsInitialized(p.Friends));
Assert.AreEqual(3, p.Friends.Count);
foreach (Person friend in p.Friends)
{
Assert.IsTrue(NHibernateUtil.IsInitialized(friend));
}
}
}
finally
{
using (ISession s = OpenSession())
{
s.Delete("from Person p where p.Parent is null");
s.Flush();
}
}
}
// Tests "non-linear" join fetch, i.e. A join A.B join A.C
[Test]
public void MultipleCollectionsNonLinearJoinFetch()
{
Person person = CreateParentAndFriend();
RunNonLinearJoinFetchTest(person);
}
}
}