-
Notifications
You must be signed in to change notification settings - Fork 934
/
Copy pathFixture.cs
224 lines (201 loc) · 7.8 KB
/
Fixture.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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
using System;
using System.Collections;
using System.Linq;
using NHibernate.Cfg;
using NHibernate.Impl;
using NUnit.Framework;
namespace NHibernate.Test.SessionBuilder
{
[TestFixture]
public class Fixture : TestCase
{
protected override string MappingsAssembly => "NHibernate.Test";
protected override IList Mappings => new [] { "SessionBuilder.Mappings.hbm.xml" };
protected override void Configure(Configuration configuration)
{
// Do not use .Instance here, we want another instance.
configuration.Interceptor = new EmptyInterceptor();
}
[Test]
public void CanSetAutoClose()
{
var sb = Sfi.WithOptions();
CanSetAutoClose(sb);
using (var s = sb.OpenSession())
{
CanSetAutoClose(s.SessionWithOptions());
}
}
private void CanSetAutoClose<T>(T sb) where T : ISessionBuilder<T>
{
var options = DebugSessionFactory.GetCreationOptions(sb);
CanSet(sb, sb.AutoClose, () => options.ShouldAutoClose,
sb is ISharedSessionBuilder ssb ? ssb.AutoClose : default(Func<ISharedSessionBuilder>),
// initial values
false,
// values
true, false);
}
[Test]
public void CanSetConnection()
{
var sb = Sfi.WithOptions();
CanSetConnection(sb);
using (var s = sb.OpenSession())
{
CanSetConnection(s.SessionWithOptions());
}
}
private void CanSetConnection<T>(T sb) where T : ISessionBuilder<T>
{
var sbType = sb.GetType().Name;
var conn = Sfi.ConnectionProvider.GetConnection();
try
{
var options = DebugSessionFactory.GetCreationOptions(sb);
Assert.IsNull(options.UserSuppliedConnection, $"{sbType}: Initial value");
var fsb = sb.Connection(conn);
Assert.AreEqual(conn, options.UserSuppliedConnection, $"{sbType}: After call with a connection");
Assert.AreEqual(sb, fsb, $"{sbType}: Unexpected fluent return after call with a connection");
if (sb is ISharedSessionBuilder ssb)
{
var sharedOptions = (ISharedSessionCreationOptions)options;
Assert.IsFalse(sharedOptions.IsTransactionCoordinatorShared, $"{sbType}: Transaction coordinator shared before sharing");
Assert.IsNull(sharedOptions.ConnectionManager, $"{sbType}: Connection manager shared before sharing");
var fssb = ssb.Connection();
// Sharing connection shares the connection manager, not the connection.
Assert.IsNull(options.UserSuppliedConnection, $"{sbType}: After call with previous session connection");
Assert.IsTrue(sharedOptions.IsTransactionCoordinatorShared, $"{sbType}: Transaction coordinator not shared after sharing");
Assert.IsNotNull(sharedOptions.ConnectionManager, $"{sbType}: Connection manager not shared after sharing");
Assert.AreEqual(sb, fssb, $"{sbType}: Unexpected fluent return on shared");
fsb = sb.Connection(null);
Assert.IsNull(options.UserSuppliedConnection, $"{sbType}: After call with null");
Assert.IsFalse(sharedOptions.IsTransactionCoordinatorShared, $"{sbType}: Transaction coordinator shared after un-sharing");
Assert.IsNull(sharedOptions.ConnectionManager, $"{sbType}: Connection manager shared after un-sharing");
Assert.AreEqual(sb, fsb, $"{sbType}: Unexpected fluent return after un-sharing");
}
else
{
fsb = sb.Connection(null);
Assert.IsNull(options.UserSuppliedConnection, $"{sbType}: After call with null");
Assert.AreEqual(sb, fsb, $"{sbType}: Unexpected fluent return after call with null");
}
}
finally
{
Sfi.ConnectionProvider.CloseConnection(conn);
}
}
[Test]
public void CanSetConnectionOnStateless()
{
var sb = Sfi.WithStatelessOptions();
var sbType = sb.GetType().Name;
var conn = Sfi.ConnectionProvider.GetConnection();
try
{
var options = DebugSessionFactory.GetCreationOptions(sb);
Assert.IsNull(options.UserSuppliedConnection, $"{sbType}: Initial value");
var fsb = sb.Connection(conn);
Assert.AreEqual(conn, options.UserSuppliedConnection, $"{sbType}: After call with a connection");
Assert.AreEqual(sb, fsb, $"{sbType}: Unexpected fluent return after call with a connection");
fsb = sb.Connection(null);
Assert.IsNull(options.UserSuppliedConnection, $"{sbType}: After call with null");
Assert.AreEqual(sb, fsb, $"{sbType}: Unexpected fluent return after call with null");
}
finally
{
Sfi.ConnectionProvider.CloseConnection(conn);
}
}
[Test]
public void CanSetConnectionReleaseMode()
{
var sb = Sfi.WithOptions();
CanSetConnectionReleaseMode(sb);
using (var s = sb.OpenSession())
{
CanSetConnectionReleaseMode(s.SessionWithOptions());
}
}
private void CanSetConnectionReleaseMode<T>(T sb) where T : ISessionBuilder<T>
{
var options = DebugSessionFactory.GetCreationOptions(sb);
CanSet(sb, sb.ConnectionReleaseMode, () => options.SessionConnectionReleaseMode,
sb is ISharedSessionBuilder ssb ? ssb.ConnectionReleaseMode : default(Func<ISharedSessionBuilder>),
// initial values
Sfi.Settings.ConnectionReleaseMode,
// values
ConnectionReleaseMode.OnClose, ConnectionReleaseMode.AfterStatement, ConnectionReleaseMode.AfterTransaction);
}
[Test]
public void CanSetFlushMode()
{
var sb = Sfi.WithOptions();
CanSetFlushMode(sb);
using (var s = sb.OpenSession())
{
CanSetFlushMode(s.SessionWithOptions());
}
}
private void CanSetFlushMode<T>(T sb) where T : ISessionBuilder<T>
{
var options = DebugSessionFactory.GetCreationOptions(sb);
CanSet(sb, sb.FlushMode, () => options.InitialSessionFlushMode,
sb is ISharedSessionBuilder ssb ? ssb.FlushMode : default(Func<ISharedSessionBuilder>),
// initial values
Sfi.Settings.DefaultFlushMode,
// values
FlushMode.Always, FlushMode.Auto, FlushMode.Commit, FlushMode.Manual);
}
[Test]
public void CanSetInterceptor()
{
var sb = Sfi.WithOptions();
CanSetInterceptor(sb);
using (var s = sb.OpenSession())
{
CanSetInterceptor(s.SessionWithOptions());
}
}
private void CanSetInterceptor<T>(T sb) where T : ISessionBuilder<T>
{
var sbType = sb.GetType().Name;
// Do not use .Instance here, we want another instance.
var interceptor = new EmptyInterceptor();
var options = DebugSessionFactory.GetCreationOptions(sb);
Assert.AreEqual(Sfi.Interceptor, options.SessionInterceptor, $"{sbType}: Initial value");
var fsb = sb.Interceptor(interceptor);
Assert.AreEqual(interceptor, options.SessionInterceptor, $"{sbType}: After call with an interceptor");
Assert.AreEqual(sb, fsb, $"{sbType}: Unexpected fluent return after call with an interceptor");
if (sb is ISharedSessionBuilder ssb)
{
var fssb = ssb.Interceptor();
Assert.AreEqual(EmptyInterceptor.Instance, options.SessionInterceptor, $"{sbType}: After call with shared interceptor");
Assert.AreEqual(sb, fssb, $"{sbType}: Unexpected fluent return on shared");
}
Assert.Throws<ArgumentNullException>(() => sb.Interceptor(null), $"{sbType}: After call with null");
fsb = sb.NoInterceptor();
Assert.AreEqual(EmptyInterceptor.Instance, options.SessionInterceptor, $"{sbType}: After no call");
Assert.AreEqual(sb, fsb, $"{sbType}: Unexpected fluent return after no call");
}
private void CanSet<T, V>(T sb, Func<V, T> setter, Func<V> getter, Func<ISharedSessionBuilder> shared, V initialValue,
params V[] values) where T : ISessionBuilder<T>
{
var sbType = sb.GetType().Name;
Assert.AreEqual(initialValue, getter(), $"{sbType}: Initial value");
if (shared != null)
{
var fssb = shared();
Assert.AreEqual(values.Last(), getter(), $"{sbType}: After call with shared setting");
Assert.AreEqual(sb, fssb, $"{sbType}: Unexpected fluent return on shared");
}
foreach (var value in values)
{
var fsb = setter(value);
Assert.AreEqual(value, getter(), $"{sbType}: After call with {value}");
Assert.AreEqual(sb, fsb, $"{sbType}: Unexpected fluent return after call with {value}");
}
}
}
}