forked from nhibernate/nhibernate-core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFixture.cs
333 lines (302 loc) · 11.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
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 string[] 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 value
false,
// values
true, false);
}
[Test]
public void CanSetAutoJoinTransaction()
{
var sb = Sfi.WithOptions();
CanSetAutoJoinTransaction(sb);
CanSetAutoJoinTransactionOnStateless(Sfi.WithStatelessOptions());
using (var s = sb.OpenSession())
{
CanSetAutoJoinTransaction(s.SessionWithOptions());
CanSetAutoJoinTransactionOnStateless(s.StatelessSessionWithOptions());
}
}
private void CanSetAutoJoinTransaction<T>(T sb) where T : ISessionBuilder<T>
{
var options = DebugSessionFactory.GetCreationOptions(sb);
CanSet(sb, sb.AutoJoinTransaction, () => options.ShouldAutoJoinTransaction,
sb is ISharedSessionBuilder ssb ? ssb.AutoJoinTransaction : default(Func<ISharedSessionBuilder>),
// initial value
true,
// values
false, true);
}
private void CanSetAutoJoinTransactionOnStateless<T>(T sb) where T : IStatelessSessionBuilder
{
var options = DebugSessionFactory.GetCreationOptions(sb);
CanSetOnStateless(
sb, sb.AutoJoinTransaction, () => options.ShouldAutoJoinTransaction,
sb is ISharedStatelessSessionBuilder ssb ? ssb.AutoJoinTransaction : default(Func<ISharedStatelessSessionBuilder>),
// initial value
true,
// values
false, true);
}
[Test]
public void CanSetConnection()
{
var sb = Sfi.WithOptions();
CanSetConnection(sb);
CanSetConnectionOnStateless(Sfi.WithStatelessOptions());
using (var s = sb.OpenSession())
{
CanSetConnection(s.SessionWithOptions());
CanSetConnectionOnStateless(s.StatelessSessionWithOptions());
}
}
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);
}
}
private void CanSetConnectionOnStateless<T>(T sb) where T : IStatelessSessionBuilder
{
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 ISharedStatelessSessionBuilder 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 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 value
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 value
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}");
}
}
private void CanSetOnStateless<T, V>(
T sb, Func<V, T> setter, Func<V> getter, Func<ISharedStatelessSessionBuilder> shared, V initialValue,
params V[] values) where T : IStatelessSessionBuilder
{
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}");
}
}
[Test]
public void ThrowWhenOpeningFromDisposedFactory()
{
try
{
Sfi.Dispose();
Assert.That(Sfi.OpenSession, Throws.InstanceOf(typeof(ObjectDisposedException)));
}
finally
{
RebuildSessionFactory();
}
}
[Test]
public void ThrowWhenUsingSessionFromDisposedFactory()
{
using (var session = Sfi.OpenSession())
{
try
{
Sfi.Dispose();
Assert.That(() => session.Get<Entity>(Guid.Empty), Throws.InstanceOf(typeof(ObjectDisposedException)));
}
finally
{
RebuildSessionFactory();
}
}
}
}
}