forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSessionCacheTest.cs
135 lines (115 loc) · 2.66 KB
/
SessionCacheTest.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
using System;
using System.Collections;
using NHibernate.DomainModel;
using NUnit.Framework;
namespace NHibernate.Test
{
/// <summary>
/// Summary description for SessionCacheTest.
/// </summary>
[TestFixture]
public class SessionCacheTest : TestCase
{
protected override string[] Mappings
{
get { return new string[] {"Simple.hbm.xml"}; }
}
[Test]
public void MakeCollectionTransient()
{
ISession fixture = OpenSession();
for (long i = 1L; i < 6L; i++)
{
Simple s = new Simple((int) i);
s.Address = "dummy collection address " + i;
s.Date = DateTime.Now;
s.Name = "dummy collection name " + i;
s.Pay = i * 1279L;
fixture.Save(s, i);
}
fixture.Flush();
IList list = fixture.CreateCriteria(typeof(Simple)).List();
Assert.IsNotNull(list);
Assert.IsTrue(list.Count == 5);
Assert.IsTrue(fixture.Contains(list[2]));
fixture.Clear();
Assert.IsTrue(list.Count == 5);
Assert.IsFalse(fixture.Contains(list[2]));
fixture.Flush();
Assert.IsTrue(list.Count == 5);
fixture.Delete("from System.Object o");
fixture.Flush();
fixture.Close();
}
[Test]
public void LoadAfterNotExists()
{
ISession fixture = OpenSession();
// First, prime the fixture session to think the entity does not exist
try
{
fixture.Load(typeof(Simple), -1L);
}
catch (ObjectNotFoundException)
{
// this is expected
}
// Next, lets create that entity under the covers
ISession anotherSession = null;
try
{
anotherSession = OpenSession();
Simple oneSimple = new Simple(1);
oneSimple.Name = "hidden entity";
oneSimple.Address = "SessionCacheTest.LoadAfterNotExists";
oneSimple.Date = DateTime.Now;
oneSimple.Pay = 1000000f;
anotherSession.Save(oneSimple, -1L);
anotherSession.Flush();
}
finally
{
QuietlyClose(anotherSession);
}
// Verify that the original session is still unable to see the new entry...
try
{
fixture.Load(typeof(Simple), -1L);
}
catch (ObjectNotFoundException)
{
}
// Now, lets clear the original session at which point it should be able to see the new entity
fixture.Clear();
string failedMessage = "Unable to load entity with id = -1.";
try
{
Simple dummy = fixture.Load(typeof(Simple), -1L) as Simple;
Assert.IsNotNull(dummy, failedMessage);
fixture.Delete(dummy);
fixture.Flush();
}
catch (ObjectNotFoundException)
{
Assert.Fail(failedMessage);
}
finally
{
QuietlyClose(fixture);
}
}
private void QuietlyClose(ISession session)
{
if (session != null)
{
try
{
session.Close();
}
catch
{
}
}
}
}
}