11
11
12
12
namespace NHibernate . Test . Linq
13
13
{
14
- public abstract class ReadonlyTestCase
14
+ public abstract class ReadonlyTestCase : TestCase
15
15
{
16
- protected Configuration _cfg ;
17
- protected ISessionFactoryImplementor _sessions ;
18
-
19
- private static readonly ILog log = LogManager . GetLogger ( typeof ( TestCase ) ) ;
20
-
21
- protected Dialect . Dialect Dialect
22
- {
23
- get { return NHibernate . Dialect . Dialect . GetDialect ( _cfg . Properties ) ; }
24
- }
25
-
26
- protected TestDialect TestDialect
27
- {
28
- get { return TestDialect . GetTestDialect ( Dialect ) ; }
29
- }
30
-
31
- /// <summary>
32
- /// To use in in-line test
33
- /// </summary>
34
- protected bool IsClassicParser
35
- {
36
- get
37
- {
38
- return _sessions . Settings . QueryTranslatorFactory is ClassicQueryTranslatorFactory ;
39
- }
40
- }
41
-
42
- /// <summary>
43
- /// To use in in-line test
44
- /// </summary>
45
- protected bool IsAntlrParser
46
- {
47
- get
48
- {
49
- return _sessions . Settings . QueryTranslatorFactory is ASTQueryTranslatorFactory ;
50
- }
51
- }
52
-
53
- protected ISession lastOpenedSession ;
54
- private DebugConnectionProvider connectionProvider ;
55
-
56
- /// <summary>
57
- /// Mapping files used in the TestCase
58
- /// </summary>
59
- protected abstract IList Mappings { get ; }
60
-
61
- /// <summary>
62
- /// Assembly to load mapping files from (default is NHibernate.DomainModel).
63
- /// </summary>
64
- protected virtual string MappingsAssembly
65
- {
66
- get { return "NHibernate.DomainModel" ; }
67
- }
68
-
69
- static ReadonlyTestCase ( )
70
- {
71
- // Configure log4net here since configuration through an attribute doesn't always work.
72
- XmlConfigurator . Configure ( ) ;
73
- }
74
-
75
- /// <summary>
76
- /// Creates the tables used in this TestCase
77
- /// </summary>
78
- [ TestFixtureSetUp ]
79
- public void TestFixtureSetUp ( )
80
- {
81
- try
82
- {
83
- Configure ( ) ;
84
- if ( ! AppliesTo ( Dialect ) )
85
- {
86
- Assert . Ignore ( GetType ( ) + " does not apply to " + Dialect ) ;
87
- }
88
-
89
- BuildSessionFactory ( ) ;
90
-
91
- if ( ! AppliesTo ( _sessions ) )
92
- {
93
- Cleanup ( ) ;
94
- Assert . Ignore ( GetType ( ) + " does not apply with the current session-factory configuration" ) ;
95
- }
96
-
97
- OnFixtureSetup ( ) ;
98
- }
99
- catch ( Exception e )
100
- {
101
- log . Error ( "Error while setting up the test fixture" , e ) ;
102
- throw ;
103
- }
104
- }
105
-
106
- /// <summary>
107
- /// Removes the tables used in this TestCase.
108
- /// </summary>
109
- /// <remarks>
110
- /// If the tables are not cleaned up sometimes SchemaExport runs into
111
- /// Sql errors because it can't drop tables because of the FKs. This
112
- /// will occur if the TestCase does not have the same hbm.xml files
113
- /// included as a previous one.
114
- /// </remarks>
115
- [ TestFixtureTearDown ]
116
- public void TestFixtureTearDown ( )
16
+ protected override void CreateSchema ( )
117
17
{
118
- OnFixtureTeardown ( ) ;
119
-
120
- Cleanup ( ) ;
121
18
}
122
19
123
- protected virtual void OnFixtureSetup ( )
20
+ protected override void DropSchema ( )
124
21
{
125
22
}
126
23
127
- protected virtual void OnFixtureTeardown ( )
24
+ protected override bool CheckDatabaseWasCleaned ( )
128
25
{
129
- }
130
-
131
- protected virtual void OnSetUp ( )
132
- {
133
- }
134
-
135
- /// <summary>
136
- /// Set up the test. This method is not overridable, but it calls
137
- /// <see cref="OnSetUp" /> which is.
138
- /// </summary>
139
- [ SetUp ]
140
- public void SetUp ( )
141
- {
142
- OnSetUp ( ) ;
143
- }
144
-
145
- protected virtual void OnTearDown ( )
146
- {
147
- }
148
-
149
- /// <summary>
150
- /// Checks that the test case cleans up after itself. This method
151
- /// is not overridable, but it calls <see cref="OnTearDown" /> which is.
152
- /// </summary>
153
- [ TearDown ]
154
- public void TearDown ( )
155
- {
156
- OnTearDown ( ) ;
157
-
158
- bool wasClosed = CheckSessionWasClosed ( ) ;
159
- bool wereConnectionsClosed = CheckConnectionsWereClosed ( ) ;
160
- bool fail = ! wasClosed || ! wereConnectionsClosed ;
161
-
162
- if ( fail )
163
- {
164
- Assert . Fail ( "Test didn't clean up after itself. session closed: " + wasClosed + " connection closed: " + wereConnectionsClosed ) ;
165
- }
166
- }
167
-
168
- private bool CheckSessionWasClosed ( )
169
- {
170
- if ( lastOpenedSession != null && lastOpenedSession . IsOpen )
171
- {
172
- log . Error ( "Test case didn't close a session, closing" ) ;
173
- lastOpenedSession . Close ( ) ;
174
- return false ;
175
- }
176
-
26
+ // We are read-only, so we're theoretically always clean.
177
27
return true ;
178
28
}
179
29
180
- private bool CheckConnectionsWereClosed ( )
30
+ protected override void ApplyCacheSettings ( Configuration configuration )
181
31
{
182
- if ( connectionProvider == null || ! connectionProvider . HasOpenConnections )
183
- {
184
- return true ;
185
- }
186
-
187
- log . Error ( "Test case didn't close all open connections, closing" ) ;
188
- connectionProvider . CloseAllConnections ( ) ;
189
- return false ;
32
+ // Patrick Earl: I wasn't sure if making this do nothing was important, but I left it here since it wasn't running in the code when I changed it.
190
33
}
191
-
192
- private void Configure ( )
193
- {
194
- _cfg = new Configuration ( ) ;
195
- if ( TestConfigurationHelper . hibernateConfigFile != null )
196
- _cfg . Configure ( TestConfigurationHelper . hibernateConfigFile ) ;
197
-
198
- Assembly assembly = Assembly . Load ( MappingsAssembly ) ;
199
-
200
- foreach ( string file in Mappings )
201
- {
202
- _cfg . AddResource ( MappingsAssembly + "." + file , assembly ) ;
203
- }
204
-
205
- Configure ( _cfg ) ;
206
- }
207
-
208
- protected virtual void BuildSessionFactory ( )
209
- {
210
- _sessions = ( ISessionFactoryImplementor ) _cfg . BuildSessionFactory ( ) ;
211
- connectionProvider = _sessions . ConnectionProvider as DebugConnectionProvider ;
212
- }
213
-
214
- private void Cleanup ( )
215
- {
216
- if ( _sessions != null )
217
- {
218
- _sessions . Close ( ) ;
219
- }
220
- _sessions = null ;
221
- connectionProvider = null ;
222
- lastOpenedSession = null ;
223
- _cfg = null ;
224
- }
225
-
226
- protected ISessionFactoryImplementor Sfi
227
- {
228
- get { return _sessions ; }
229
- }
230
-
231
- protected virtual ISession OpenSession ( )
232
- {
233
- lastOpenedSession = _sessions . OpenSession ( ) ;
234
- return lastOpenedSession ;
235
- }
236
-
237
- protected virtual ISession OpenSession ( IInterceptor sessionLocalInterceptor )
238
- {
239
- lastOpenedSession = _sessions . OpenSession ( sessionLocalInterceptor ) ;
240
- return lastOpenedSession ;
241
- }
242
-
243
- #region Properties overridable by subclasses
244
-
245
- protected virtual bool AppliesTo ( Dialect . Dialect dialect )
246
- {
247
- return true ;
248
- }
249
-
250
- protected virtual bool AppliesTo ( ISessionFactoryImplementor factory )
251
- {
252
- return true ;
253
- }
254
-
255
- protected virtual void Configure ( Configuration configuration )
256
- {
257
- }
258
-
259
- protected virtual string CacheConcurrencyStrategy
260
- {
261
- get { return "nonstrict-read-write" ; }
262
- //get { return null; }
263
- }
264
-
265
- #endregion
266
34
}
267
35
}
0 commit comments