Skip to content

Commit 43bfbf1

Browse files
committed
Adding tests to 2nd level cache.
SVN: trunk@3259
1 parent be8ceb5 commit 43bfbf1

File tree

4 files changed

+177
-10
lines changed

4 files changed

+177
-10
lines changed

src/NHibernate.Test/NHibernate.Test-2.0.csproj

+4
Original file line numberDiff line numberDiff line change
@@ -553,6 +553,7 @@
553553
<Compile Include="ReflectionOptimizerTest\LcgFixture.cs" />
554554
<Compile Include="SchemaUpdate\MigrationFixture.cs" />
555555
<Compile Include="SchemaUpdate\Version.cs" />
556+
<Compile Include="SecondLevelCacheTest\AnotherItem.cs" />
556557
<Compile Include="SecondLevelCacheTest\Item.cs" />
557558
<Compile Include="SecondLevelCacheTest\SecondLevelCacheTest.cs" />
558559
<Compile Include="Assertions\HaveSerializableAttributeAsserter.cs" />
@@ -1113,6 +1114,9 @@
11131114
<ItemGroup>
11141115
<EmbeddedResource Include="NHSpecificTest\LoadingNullEntityInSet\Mappings.hbm.xml" />
11151116
</ItemGroup>
1117+
<ItemGroup>
1118+
<Service Include="{B4F97281-0DBD-4835-9ED8-7DFB966E87FF}" />
1119+
</ItemGroup>
11161120
<ItemGroup>
11171121
<Folder Include="Properties\" />
11181122
<Folder Include="Unionsubclass2\" />
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
namespace NHibernate.Test.SecondLevelCacheTests
2+
{
3+
public class AnotherItem
4+
{
5+
private int id;
6+
private string name;
7+
8+
public AnotherItem()
9+
{
10+
11+
}
12+
13+
public AnotherItem(string name)
14+
{
15+
this.name = name;
16+
}
17+
18+
public virtual int Id
19+
{
20+
get { return id; }
21+
set { id = value; }
22+
}
23+
24+
public virtual string Name
25+
{
26+
get { return name; }
27+
set { name = value; }
28+
}
29+
}
30+
}

src/NHibernate.Test/SecondLevelCacheTest/Item.hbm.xml

+8
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,12 @@
1717
<one-to-many class="NHibernate.Test.SecondLevelCacheTests.Item, NHibernate.Test"/>
1818
</bag>
1919
</class>
20+
21+
<class name="NHibernate.Test.SecondLevelCacheTests.AnotherItem, NHibernate.Test">
22+
<cache usage="read-write"/>
23+
<id name="Id">
24+
<generator class="assigned"/>
25+
</id>
26+
<property name="Name"/>
27+
</class>
2028
</hibernate-mapping>

src/NHibernate.Test/SecondLevelCacheTest/SecondLevelCacheTest.cs

+135-10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55

66
namespace NHibernate.Test.SecondLevelCacheTests
77
{
8+
using System.Data;
9+
using Expressions;
10+
811
[TestFixture]
912
public class SecondLevelCacheTest : TestCase
1013
{
@@ -15,7 +18,7 @@ protected override string MappingsAssembly
1518

1619
protected override IList Mappings
1720
{
18-
get { return new string[] {"SecondLevelCacheTest.Item.hbm.xml"}; }
21+
get { return new string[] { "SecondLevelCacheTest.Item.hbm.xml" }; }
1922
}
2023

2124
protected override void OnSetUp()
@@ -25,6 +28,7 @@ protected override void OnSetUp()
2528
sessions = cfg.BuildSessionFactory();
2629

2730
using (ISession session = OpenSession())
31+
using(ITransaction tx = session.BeginTransaction())
2832
{
2933
Item item = new Item();
3034
item.Id = 1;
@@ -37,7 +41,15 @@ protected override void OnSetUp()
3741
session.Save(child);
3842
item.Children.Add(child);
3943
}
40-
session.Flush();
44+
45+
for (int i = 0; i < 5; i++)
46+
{
47+
AnotherItem obj = new AnotherItem("Item #" + i);
48+
obj.Id = i+1;
49+
session.Save(obj);
50+
}
51+
52+
tx.Commit();
4153
}
4254

4355
sessions.Evict(typeof(Item));
@@ -49,6 +61,7 @@ protected override void OnTearDown()
4961
using (ISession session = OpenSession())
5062
{
5163
session.Delete("from Item"); //cleaning up
64+
session.Delete("from AnotherItem"); //cleaning up
5265
session.Flush();
5366
}
5467
}
@@ -58,7 +71,7 @@ public void CachedQueriesHandlesEntitiesParametersCorrectly()
5871
{
5972
using (ISession session = OpenSession())
6073
{
61-
Item one = (Item) session.Load(typeof(Item), 1);
74+
Item one = (Item)session.Load(typeof(Item), 1);
6275
IList results = session.CreateQuery("from Item item where item.Parent = :parent")
6376
.SetEntity("parent", one)
6477
.SetCacheable(true).List();
@@ -71,7 +84,7 @@ public void CachedQueriesHandlesEntitiesParametersCorrectly()
7184

7285
using (ISession session = OpenSession())
7386
{
74-
Item two = (Item) session.Load(typeof(Item), 2);
87+
Item two = (Item)session.Load(typeof(Item), 2);
7588
IList results = session.CreateQuery("from Item item where item.Parent = :parent")
7689
.SetEntity("parent", two)
7790
.SetCacheable(true).List();
@@ -84,14 +97,14 @@ public void DeleteItemFromCollectionThatIsInTheSecondLevelCache()
8497
{
8598
using (ISession session = OpenSession())
8699
{
87-
Item item = (Item) session.Load(typeof(Item), 1);
100+
Item item = (Item)session.Load(typeof(Item), 1);
88101
Assert.IsTrue(item.Children.Count == 4); // just force it into the second level cache here
89102
}
90103
int childId = -1;
91104
using (ISession session = OpenSession())
92105
{
93-
Item item = (Item) session.Load(typeof(Item), 1);
94-
Item child = (Item) item.Children[0];
106+
Item item = (Item)session.Load(typeof(Item), 1);
107+
Item child = (Item)item.Children[0];
95108
childId = child.Id;
96109
session.Delete(child);
97110
item.Children.Remove(child);
@@ -100,7 +113,7 @@ public void DeleteItemFromCollectionThatIsInTheSecondLevelCache()
100113

101114
using (ISession session = OpenSession())
102115
{
103-
Item item = (Item) session.Load(typeof(Item), 1);
116+
Item item = (Item)session.Load(typeof(Item), 1);
104117
Assert.AreEqual(3, item.Children.Count);
105118
foreach (Item child in item.Children)
106119
{
@@ -115,7 +128,7 @@ public void InsertItemToCollectionOnTheSecondLevelCache()
115128
{
116129
using (ISession session = OpenSession())
117130
{
118-
Item item = (Item) session.Load(typeof(Item), 1);
131+
Item item = (Item)session.Load(typeof(Item), 1);
119132
Item child = new Item();
120133
child.Id = 6;
121134
item.Children.Add(child);
@@ -125,10 +138,122 @@ public void InsertItemToCollectionOnTheSecondLevelCache()
125138

126139
using (ISession session = OpenSession())
127140
{
128-
Item item = (Item) session.Load(typeof(Item), 1);
141+
Item item = (Item)session.Load(typeof(Item), 1);
129142
int count = item.Children.Count;
130143
Assert.AreEqual(5, count);
131144
}
132145
}
146+
147+
[Test]
148+
public void SecondLevelCacheWithCriteriaQueries()
149+
{
150+
using (ISession session = OpenSession())
151+
{
152+
IList list = session.CreateCriteria(typeof(AnotherItem))
153+
.Add(Expression.Gt("Id", 2))
154+
.SetCacheable(true)
155+
.List();
156+
Assert.AreEqual(3, list.Count);
157+
158+
using (IDbCommand cmd = session.Connection.CreateCommand())
159+
{
160+
cmd.CommandText = "DELETE FROM AnotherItem";
161+
cmd.ExecuteNonQuery();
162+
}
163+
}
164+
165+
using (ISession session = OpenSession())
166+
{
167+
//should bring from cache
168+
IList list = session.CreateCriteria(typeof(AnotherItem))
169+
.Add(Expression.Gt("Id", 2))
170+
.SetCacheable(true)
171+
.List();
172+
Assert.AreEqual(3, list.Count);
173+
}
174+
}
175+
176+
[Test]
177+
public void SecondLevelCacheWithCriteriaQueriesForItemWithCollections()
178+
{
179+
using (ISession session = OpenSession())
180+
{
181+
IList list = session.CreateCriteria(typeof(Item))
182+
.Add(Expression.Gt("Id", 2))
183+
.SetCacheable(true)
184+
.List();
185+
Assert.AreEqual(3, list.Count);
186+
187+
using (IDbCommand cmd = session.Connection.CreateCommand())
188+
{
189+
cmd.CommandText = "DELETE FROM Item";
190+
cmd.ExecuteNonQuery();
191+
}
192+
}
193+
194+
using (ISession session = OpenSession())
195+
{
196+
//should bring from cache
197+
IList list = session.CreateCriteria(typeof(Item))
198+
.Add(Expression.Gt("Id", 2))
199+
.SetCacheable(true)
200+
.List();
201+
Assert.AreEqual(3, list.Count);
202+
}
203+
}
204+
205+
[Test]
206+
public void SecondLevelCacheWithHqlQueriesForItemWithCollections()
207+
{
208+
using (ISession session = OpenSession())
209+
{
210+
IList list = session.CreateQuery("from Item i where i.Id > 2")
211+
.SetCacheable(true)
212+
.List();
213+
Assert.AreEqual(3, list.Count);
214+
215+
using (IDbCommand cmd = session.Connection.CreateCommand())
216+
{
217+
cmd.CommandText = "DELETE FROM Item";
218+
cmd.ExecuteNonQuery();
219+
}
220+
}
221+
222+
using (ISession session = OpenSession())
223+
{
224+
//should bring from cache
225+
IList list = session.CreateQuery("from Item i where i.Id > 2")
226+
.SetCacheable(true)
227+
.List();
228+
Assert.AreEqual(3, list.Count);
229+
}
230+
}
231+
232+
[Test]
233+
public void SecondLevelCacheWithHqlQueries()
234+
{
235+
using (ISession session = OpenSession())
236+
{
237+
IList list = session.CreateQuery("from AnotherItem i where i.Id > 2")
238+
.SetCacheable(true)
239+
.List();
240+
Assert.AreEqual(3, list.Count);
241+
242+
using (IDbCommand cmd = session.Connection.CreateCommand())
243+
{
244+
cmd.CommandText = "DELETE FROM AnotherItem";
245+
cmd.ExecuteNonQuery();
246+
}
247+
}
248+
249+
using (ISession session = OpenSession())
250+
{
251+
//should bring from cache
252+
IList list = session.CreateQuery("from AnotherItem i where i.Id > 2")
253+
.SetCacheable(true)
254+
.List();
255+
Assert.AreEqual(3, list.Count);
256+
}
257+
}
133258
}
134259
}

0 commit comments

Comments
 (0)