Skip to content

Commit 881a932

Browse files
bahusoidfredericDelaporte
authored andcommitted
Reuse SchemaExport in CreateSchema/DropSchema in tests (#1519)
* Additionally, no need to call Initialize from private SchemaExport.Execute method. Renamed it to ExecuteInitialized
1 parent 825c795 commit 881a932

File tree

9 files changed

+18
-26
lines changed

9 files changed

+18
-26
lines changed

src/NHibernate.Test/Async/NHSpecificTest/NH1635/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ protected override bool AppliesTo(Dialect.Dialect dialect)
6262
protected override void CreateSchema()
6363
{
6464
var script = new StringBuilder();
65-
new SchemaExport(cfg).Create(sl=> script.Append(sl) , true);
65+
SchemaExport.Create(sl => script.Append(sl), true);
6666
Assert.That(script.ToString(), Does.Not.Contain("LatestMessage"));
6767
}
6868

src/NHibernate.Test/Async/NHSpecificTest/NH1939/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ public async Task Can_Parameterise_Auxiliary_Database_ObjectsAsync()
4040
{
4141
schemaBuilder = new StringBuilder();
4242

43-
SchemaExport schemaExport = new SchemaExport(cfg);
43+
SchemaExport schemaExport = SchemaExport;
4444
await (schemaExport.ExecuteAsync(AddString, false, false));
4545

4646
string schema = schemaBuilder.ToString();

src/NHibernate.Test/Async/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,6 @@ protected override void OnSetUp()
4343
}
4444
}
4545

46-
private object SchemaExport(NHibernate.Cfg.Configuration cfg)
47-
{
48-
throw new NotImplementedException();
49-
}
50-
5146
protected override void OnTearDown()
5247
{
5348
base.OnTearDown();
@@ -161,4 +156,4 @@ public async Task MultiCriteriaQueriesWithStringsShouldExecuteCorrectlyAsync()
161156
}
162157
}
163158
}
164-
}
159+
}

src/NHibernate.Test/NHSpecificTest/NH1635/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ private void CleanUp()
5050
protected override void CreateSchema()
5151
{
5252
var script = new StringBuilder();
53-
new SchemaExport(cfg).Create(sl=> script.Append(sl) , true);
53+
SchemaExport.Create(sl => script.Append(sl), true);
5454
Assert.That(script.ToString(), Does.Not.Contain("LatestMessage"));
5555
}
5656

src/NHibernate.Test/NHSpecificTest/NH1939/Fixture.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void Can_Parameterise_Auxiliary_Database_Objects()
2929
{
3030
schemaBuilder = new StringBuilder();
3131

32-
SchemaExport schemaExport = new SchemaExport(cfg);
32+
SchemaExport schemaExport = SchemaExport;
3333
schemaExport.Execute(AddString, false, false);
3434

3535
string schema = schemaBuilder.ToString();

src/NHibernate.Test/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,6 @@ protected override void OnSetUp()
3232
}
3333
}
3434

35-
private object SchemaExport(NHibernate.Cfg.Configuration cfg)
36-
{
37-
throw new NotImplementedException();
38-
}
39-
4035
protected override void OnTearDown()
4136
{
4237
base.OnTearDown();
@@ -150,4 +145,4 @@ public void MultiCriteriaQueriesWithStringsShouldExecuteCorrectly()
150145
}
151146
}
152147
}
153-
}
148+
}

src/NHibernate.Test/TestCase.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public abstract class TestCase
2222
private const bool OutputDdl = false;
2323
protected Configuration cfg;
2424
private DebugSessionFactory _sessionFactory;
25+
private SchemaExport _schemaExport;
2526

2627
private static readonly ILog log = LogManager.GetLogger(typeof(TestCase));
2728

@@ -48,6 +49,8 @@ protected virtual string MappingsAssembly
4849
get { return "NHibernate.DomainModel"; }
4950
}
5051

52+
protected SchemaExport SchemaExport => _schemaExport ?? (_schemaExport = new SchemaExport(cfg));
53+
5154
static TestCase()
5255
{
5356
// Configure log4net here since configuration through an attribute doesn't always work.
@@ -281,12 +284,12 @@ protected virtual void AddMappings(Configuration configuration)
281284

282285
protected virtual void CreateSchema()
283286
{
284-
new SchemaExport(cfg).Create(OutputDdl, true);
287+
SchemaExport.Create(OutputDdl, true);
285288
}
286289

287290
protected virtual void DropSchema()
288291
{
289-
DropSchema(OutputDdl, new SchemaExport(cfg), Sfi);
292+
DropSchema(OutputDdl, SchemaExport, Sfi);
290293
}
291294

292295
public static void DropSchema(bool useStdOut, SchemaExport export, ISessionFactoryImplementor sfi)
@@ -315,6 +318,7 @@ private void Cleanup()
315318
Sfi?.Close();
316319
_sessionFactory = null;
317320
cfg = null;
321+
_schemaExport = null;
318322
}
319323

320324
public int ExecuteStatement(string sql)

src/NHibernate/Async/Tool/hbm2ddl/SchemaExport.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,11 +145,10 @@ public partial class SchemaExport
145145
return ExecuteAsync(null, execute, true, exportOutput, cancellationToken);
146146
}
147147

148-
private async Task ExecuteAsync(Action<string> scriptAction, bool execute, bool throwOnError, TextWriter exportOutput,
148+
private async Task ExecuteInitializedAsync(Action<string> scriptAction, bool execute, bool throwOnError, TextWriter exportOutput,
149149
DbCommand statement, string sql, CancellationToken cancellationToken = default(CancellationToken))
150150
{
151151
cancellationToken.ThrowIfCancellationRequested();
152-
await (InitializeAsync(cancellationToken)).ConfigureAwait(false);
153152
try
154153
{
155154
string formatted = formatter.Format(sql);
@@ -268,14 +267,14 @@ public async Task ExecuteAsync(Action<string> scriptAction, bool execute, bool j
268267
{
269268
for (int i = 0; i < dropSQL.Length; i++)
270269
{
271-
await (ExecuteAsync(scriptAction, execute, false, exportOutput, statement, dropSQL[i], cancellationToken)).ConfigureAwait(false);
270+
await (ExecuteInitializedAsync(scriptAction, execute, false, exportOutput, statement, dropSQL[i], cancellationToken)).ConfigureAwait(false);
272271
}
273272

274273
if (!justDrop)
275274
{
276275
for (int j = 0; j < createSQL.Length; j++)
277276
{
278-
await (ExecuteAsync(scriptAction, execute, true, exportOutput, statement, createSQL[j], cancellationToken)).ConfigureAwait(false);
277+
await (ExecuteInitializedAsync(scriptAction, execute, true, exportOutput, statement, createSQL[j], cancellationToken)).ConfigureAwait(false);
279278
}
280279
}
281280
}

src/NHibernate/Tool/hbm2ddl/SchemaExport.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -164,10 +164,9 @@ public void Drop(TextWriter exportOutput, bool execute)
164164
Execute(null, execute, true, exportOutput);
165165
}
166166

167-
private void Execute(Action<string> scriptAction, bool execute, bool throwOnError, TextWriter exportOutput,
167+
private void ExecuteInitialized(Action<string> scriptAction, bool execute, bool throwOnError, TextWriter exportOutput,
168168
DbCommand statement, string sql)
169169
{
170-
Initialize();
171170
try
172171
{
173172
string formatted = formatter.Format(sql);
@@ -272,14 +271,14 @@ public void Execute(Action<string> scriptAction, bool execute, bool justDrop, Db
272271
{
273272
for (int i = 0; i < dropSQL.Length; i++)
274273
{
275-
Execute(scriptAction, execute, false, exportOutput, statement, dropSQL[i]);
274+
ExecuteInitialized(scriptAction, execute, false, exportOutput, statement, dropSQL[i]);
276275
}
277276

278277
if (!justDrop)
279278
{
280279
for (int j = 0; j < createSQL.Length; j++)
281280
{
282-
Execute(scriptAction, execute, true, exportOutput, statement, createSQL[j]);
281+
ExecuteInitialized(scriptAction, execute, true, exportOutput, statement, createSQL[j]);
283282
}
284283
}
285284
}

0 commit comments

Comments
 (0)