From dd1a179e4267edbf1554dc6fc6f99228f94e5594 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Wed, 10 Jan 2018 12:46:23 +0530 Subject: [PATCH 1/3] Reuse SchemaExport in tests --- .../Async/NHSpecificTest/NH1635/Fixture.cs | 2 +- .../Async/NHSpecificTest/NH1939/Fixture.cs | 2 +- src/NHibernate.Test/NHSpecificTest/NH1635/Fixture.cs | 2 +- src/NHibernate.Test/NHSpecificTest/NH1939/Fixture.cs | 2 +- src/NHibernate.Test/TestCase.cs | 8 ++++++-- 5 files changed, 10 insertions(+), 6 deletions(-) diff --git a/src/NHibernate.Test/Async/NHSpecificTest/NH1635/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/NH1635/Fixture.cs index 4db1c58cbc0..547febf49df 100644 --- a/src/NHibernate.Test/Async/NHSpecificTest/NH1635/Fixture.cs +++ b/src/NHibernate.Test/Async/NHSpecificTest/NH1635/Fixture.cs @@ -62,7 +62,7 @@ protected override bool AppliesTo(Dialect.Dialect dialect) protected override void CreateSchema() { var script = new StringBuilder(); - new SchemaExport(cfg).Create(sl=> script.Append(sl) , true); + SchemaExport.Create(sl => script.Append(sl), true); Assert.That(script.ToString(), Does.Not.Contain("LatestMessage")); } diff --git a/src/NHibernate.Test/Async/NHSpecificTest/NH1939/Fixture.cs b/src/NHibernate.Test/Async/NHSpecificTest/NH1939/Fixture.cs index 2dc71394bd3..fc01172cb66 100644 --- a/src/NHibernate.Test/Async/NHSpecificTest/NH1939/Fixture.cs +++ b/src/NHibernate.Test/Async/NHSpecificTest/NH1939/Fixture.cs @@ -40,7 +40,7 @@ public async Task Can_Parameterise_Auxiliary_Database_ObjectsAsync() { schemaBuilder = new StringBuilder(); - SchemaExport schemaExport = new SchemaExport(cfg); + SchemaExport schemaExport = SchemaExport; await (schemaExport.ExecuteAsync(AddString, false, false)); string schema = schemaBuilder.ToString(); diff --git a/src/NHibernate.Test/NHSpecificTest/NH1635/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/NH1635/Fixture.cs index e8c9087c338..b1277c1a92b 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH1635/Fixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH1635/Fixture.cs @@ -50,7 +50,7 @@ private void CleanUp() protected override void CreateSchema() { var script = new StringBuilder(); - new SchemaExport(cfg).Create(sl=> script.Append(sl) , true); + SchemaExport.Create(sl => script.Append(sl), true); Assert.That(script.ToString(), Does.Not.Contain("LatestMessage")); } diff --git a/src/NHibernate.Test/NHSpecificTest/NH1939/Fixture.cs b/src/NHibernate.Test/NHSpecificTest/NH1939/Fixture.cs index 1af2cc04be4..ab34367fc3a 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH1939/Fixture.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH1939/Fixture.cs @@ -29,7 +29,7 @@ public void Can_Parameterise_Auxiliary_Database_Objects() { schemaBuilder = new StringBuilder(); - SchemaExport schemaExport = new SchemaExport(cfg); + SchemaExport schemaExport = SchemaExport; schemaExport.Execute(AddString, false, false); string schema = schemaBuilder.ToString(); diff --git a/src/NHibernate.Test/TestCase.cs b/src/NHibernate.Test/TestCase.cs index b09d221d225..24c685ad7e4 100644 --- a/src/NHibernate.Test/TestCase.cs +++ b/src/NHibernate.Test/TestCase.cs @@ -22,6 +22,7 @@ public abstract class TestCase private const bool OutputDdl = false; protected Configuration cfg; private DebugSessionFactory _sessionFactory; + private SchemaExport _schemaExport; private static readonly ILog log = LogManager.GetLogger(typeof(TestCase)); @@ -48,6 +49,8 @@ protected virtual string MappingsAssembly get { return "NHibernate.DomainModel"; } } + protected SchemaExport SchemaExport => _schemaExport ?? (_schemaExport = new SchemaExport(cfg)); + static TestCase() { // Configure log4net here since configuration through an attribute doesn't always work. @@ -281,12 +284,12 @@ protected virtual void AddMappings(Configuration configuration) protected virtual void CreateSchema() { - new SchemaExport(cfg).Create(OutputDdl, true); + SchemaExport.Create(OutputDdl, true); } protected virtual void DropSchema() { - DropSchema(OutputDdl, new SchemaExport(cfg), Sfi); + DropSchema(OutputDdl, SchemaExport, Sfi); } public static void DropSchema(bool useStdOut, SchemaExport export, ISessionFactoryImplementor sfi) @@ -315,6 +318,7 @@ private void Cleanup() Sfi?.Close(); _sessionFactory = null; cfg = null; + _schemaExport = null; } public int ExecuteStatement(string sql) From b3a72d4b08dc7a93c2de8d7e8d5560a480695f76 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Wed, 10 Jan 2018 20:58:53 +0530 Subject: [PATCH 2/3] removed private unused method that caused warning --- .../Async/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs | 7 +------ .../NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs | 7 +------ 2 files changed, 2 insertions(+), 12 deletions(-) diff --git a/src/NHibernate.Test/Async/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs b/src/NHibernate.Test/Async/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs index dc150949c5e..332bf71a088 100644 --- a/src/NHibernate.Test/Async/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs +++ b/src/NHibernate.Test/Async/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs @@ -43,11 +43,6 @@ protected override void OnSetUp() } } - private object SchemaExport(NHibernate.Cfg.Configuration cfg) - { - throw new NotImplementedException(); - } - protected override void OnTearDown() { base.OnTearDown(); @@ -161,4 +156,4 @@ public async Task MultiCriteriaQueriesWithStringsShouldExecuteCorrectlyAsync() } } } -} \ No newline at end of file +} diff --git a/src/NHibernate.Test/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs b/src/NHibernate.Test/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs index fbb9fa8cd75..8e953579395 100644 --- a/src/NHibernate.Test/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs +++ b/src/NHibernate.Test/NHSpecificTest/NH2195/SQLiteMultiCriteriaTest.cs @@ -32,11 +32,6 @@ protected override void OnSetUp() } } - private object SchemaExport(NHibernate.Cfg.Configuration cfg) - { - throw new NotImplementedException(); - } - protected override void OnTearDown() { base.OnTearDown(); @@ -150,4 +145,4 @@ public void MultiCriteriaQueriesWithStringsShouldExecuteCorrectly() } } } -} \ No newline at end of file +} From f46986eb295c0cbca7424d7cc0350c1f9f2fba56 Mon Sep 17 00:00:00 2001 From: Roman Artiukhin Date: Wed, 10 Jan 2018 22:05:17 +0530 Subject: [PATCH 3/3] No need to call Initilize from private SchemaExport.Execute method. Renamed it to ExecuteInitialized --- src/NHibernate/Async/Tool/hbm2ddl/SchemaExport.cs | 7 +++---- src/NHibernate/Tool/hbm2ddl/SchemaExport.cs | 7 +++---- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/src/NHibernate/Async/Tool/hbm2ddl/SchemaExport.cs b/src/NHibernate/Async/Tool/hbm2ddl/SchemaExport.cs index 807e50786d8..c9a73c99807 100644 --- a/src/NHibernate/Async/Tool/hbm2ddl/SchemaExport.cs +++ b/src/NHibernate/Async/Tool/hbm2ddl/SchemaExport.cs @@ -145,11 +145,10 @@ public partial class SchemaExport return ExecuteAsync(null, execute, true, exportOutput, cancellationToken); } - private async Task ExecuteAsync(Action scriptAction, bool execute, bool throwOnError, TextWriter exportOutput, + private async Task ExecuteInitializedAsync(Action scriptAction, bool execute, bool throwOnError, TextWriter exportOutput, DbCommand statement, string sql, CancellationToken cancellationToken = default(CancellationToken)) { cancellationToken.ThrowIfCancellationRequested(); - await (InitializeAsync(cancellationToken)).ConfigureAwait(false); try { string formatted = formatter.Format(sql); @@ -268,14 +267,14 @@ public async Task ExecuteAsync(Action scriptAction, bool execute, bool j { for (int i = 0; i < dropSQL.Length; i++) { - await (ExecuteAsync(scriptAction, execute, false, exportOutput, statement, dropSQL[i], cancellationToken)).ConfigureAwait(false); + await (ExecuteInitializedAsync(scriptAction, execute, false, exportOutput, statement, dropSQL[i], cancellationToken)).ConfigureAwait(false); } if (!justDrop) { for (int j = 0; j < createSQL.Length; j++) { - await (ExecuteAsync(scriptAction, execute, true, exportOutput, statement, createSQL[j], cancellationToken)).ConfigureAwait(false); + await (ExecuteInitializedAsync(scriptAction, execute, true, exportOutput, statement, createSQL[j], cancellationToken)).ConfigureAwait(false); } } } diff --git a/src/NHibernate/Tool/hbm2ddl/SchemaExport.cs b/src/NHibernate/Tool/hbm2ddl/SchemaExport.cs index 6e42d9ddbbb..1f4168c95f3 100644 --- a/src/NHibernate/Tool/hbm2ddl/SchemaExport.cs +++ b/src/NHibernate/Tool/hbm2ddl/SchemaExport.cs @@ -164,10 +164,9 @@ public void Drop(TextWriter exportOutput, bool execute) Execute(null, execute, true, exportOutput); } - private void Execute(Action scriptAction, bool execute, bool throwOnError, TextWriter exportOutput, + private void ExecuteInitialized(Action scriptAction, bool execute, bool throwOnError, TextWriter exportOutput, DbCommand statement, string sql) { - Initialize(); try { string formatted = formatter.Format(sql); @@ -272,14 +271,14 @@ public void Execute(Action scriptAction, bool execute, bool justDrop, Db { for (int i = 0; i < dropSQL.Length; i++) { - Execute(scriptAction, execute, false, exportOutput, statement, dropSQL[i]); + ExecuteInitialized(scriptAction, execute, false, exportOutput, statement, dropSQL[i]); } if (!justDrop) { for (int j = 0; j < createSQL.Length; j++) { - Execute(scriptAction, execute, true, exportOutput, statement, createSQL[j]); + ExecuteInitialized(scriptAction, execute, true, exportOutput, statement, createSQL[j]); } } }