Skip to content

Commit f5f2f3c

Browse files
committed
Tests: Removed a bunch of duplicate code for Linq tests.
1 parent 8a8a653 commit f5f2f3c

File tree

2 files changed

+10
-242
lines changed

2 files changed

+10
-242
lines changed

src/NHibernate.Test/Linq/ReadonlyTestCase.cs

Lines changed: 7 additions & 239 deletions
Original file line numberDiff line numberDiff line change
@@ -11,257 +11,25 @@
1111

1212
namespace NHibernate.Test.Linq
1313
{
14-
public abstract class ReadonlyTestCase
14+
public abstract class ReadonlyTestCase : TestCase
1515
{
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()
11717
{
118-
OnFixtureTeardown();
119-
120-
Cleanup();
12118
}
12219

123-
protected virtual void OnFixtureSetup()
20+
protected override void DropSchema()
12421
{
12522
}
12623

127-
protected virtual void OnFixtureTeardown()
24+
protected override bool CheckDatabaseWasCleaned()
12825
{
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.
17727
return true;
17828
}
17929

180-
private bool CheckConnectionsWereClosed()
30+
protected override void ApplyCacheSettings(Configuration configuration)
18131
{
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.
19033
}
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
26634
}
26735
}

src/NHibernate.Test/TestCase.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ private bool CheckSessionWasClosed()
191191
return true;
192192
}
193193

194-
private bool CheckDatabaseWasCleaned()
194+
protected virtual bool CheckDatabaseWasCleaned()
195195
{
196196
if (sessions.GetAllClassMetadata().Count == 0)
197197
{
@@ -255,7 +255,7 @@ protected virtual void CreateSchema()
255255
new SchemaExport(cfg).Create(OutputDdl, true);
256256
}
257257

258-
private void DropSchema()
258+
protected virtual void DropSchema()
259259
{
260260
new SchemaExport(cfg).Drop(OutputDdl, true);
261261
}
@@ -337,7 +337,7 @@ protected virtual ISession OpenSession(IInterceptor sessionLocalInterceptor)
337337
return lastOpenedSession;
338338
}
339339

340-
protected void ApplyCacheSettings(Configuration configuration)
340+
protected virtual void ApplyCacheSettings(Configuration configuration)
341341
{
342342
if (CacheConcurrencyStrategy == null)
343343
{

0 commit comments

Comments
 (0)