99import jakarta .persistence .Lob ;
1010import jakarta .persistence .Table ;
1111import org .hamcrest .MatcherAssert ;
12- import org .hibernate .boot .registry .StandardServiceRegistry ;
1312import org .hibernate .dialect .HANADialect ;
1413import org .hibernate .engine .config .spi .ConfigurationService ;
1514import org .hibernate .engine .jdbc .env .spi .JdbcEnvironment ;
16- import org .hibernate .testing .orm .junit .DialectContext ;
1715import org .hibernate .testing .orm .junit .DomainModel ;
1816import org .hibernate .testing .orm .junit .DomainModelScope ;
1917import org .hibernate .testing .orm .junit .JiraKey ;
3432import java .io .File ;
3533import java .nio .file .Files ;
3634import java .util .Map ;
37- import java .util .function .Consumer ;
3835import java .util .regex .Matcher ;
3936import java .util .regex .Pattern ;
4037
@@ -50,8 +47,8 @@ public class HANASchemaMigrationTargetScriptCreationTest {
5047private String clobType ;
5148
5249@ BeforeEach
53- void setUp () {
54- final HANADialect dialect = (HANADialect ) DialectContext .getDialect ();
50+ void setUp (DomainModelScope modelScope ) {
51+ final HANADialect dialect = (HANADialect ) modelScope . getDomainModel (). getDatabase () .getDialect ();
5552this .varcharType = dialect .isUseUnicodeStringTypes () ? "nvarchar" : "varchar" ;
5653this .clobType = dialect .isUseUnicodeStringTypes () ? "nclob" : "clob" ;
5754}
@@ -66,7 +63,7 @@ public void testTargetScriptIsCreatedStringTypeDefault(
6663@ TempDir File tmpDir ) throws Exception {
6764var scriptFile = new File ( tmpDir , "script.sql" );
6865
69- exportToScriptFile ( registryScope , null , modelScope , scriptFile );
66+ exportToScriptFile ( registryScope , modelScope , scriptFile );
7067
7168String fileContent = new String ( Files .readAllBytes ( scriptFile .toPath () ) );
7269Pattern fileContentPattern = Pattern .compile ( "create( (column|row))? table test_entity \\ (b boolean[^,]+, c " + this .varcharType + "[^,]+, field " + this .varcharType + "[^,]+, lob " + this .clobType );
@@ -76,47 +73,6 @@ public void testTargetScriptIsCreatedStringTypeDefault(
7673is ( true ) );
7774}
7875
79- private void exportToScriptFile (
80- ServiceRegistryScope registryScope ,
81- Consumer <Map <String , Object >> configurer ,
82- DomainModelScope modelScope ,
83- File scriptFile ) {
84- var schemaCreator = new SchemaCreatorImpl ( registryScope .getRegistry () );
85- schemaCreator .createFromMetadata (
86- modelScope .getDomainModel (),
87- executionOptions ( registryScope .getRegistry (), configurer ),
88- registryScope .getRegistry ().requireService ( JdbcEnvironment .class ).getDialect (),
89- source -> source ,
90- new GenerationTargetToScript ( new ScriptTargetOutputToFile ( scriptFile , "utf8" ), ";" )
91- );
92- }
93-
94- private ExecutionOptions executionOptions (
95- StandardServiceRegistry registry ,
96- Consumer <Map <String , Object >> configurer ) {
97- final Map <String , Object > settings = registry .requireService ( ConfigurationService .class ).getSettings ();
98- if ( configurer != null ) {
99- configurer .accept ( settings );
100- }
101-
102- return new ExecutionOptions () {
103- @ Override
104- public Map <String , Object > getConfigurationValues () {
105- return settings ;
106- }
107-
108- @ Override
109- public boolean shouldManageNamespaces () {
110- return false ;
111- }
112-
113- @ Override
114- public ExceptionHandler getExceptionHandler () {
115- return ExceptionHandlerHaltImpl .INSTANCE ;
116- }
117- };
118- }
119-
12076@ Test
12177@ JiraKey (value = "HHH-12302" )
12278@ ServiceRegistry (settings = {
@@ -129,14 +85,7 @@ public void testTargetScriptIsCreatedStringTypeNVarchar(
12985@ TempDir File tmpDir ) throws Exception {
13086var scriptFile = new File ( tmpDir , "script.sql" );
13187
132- exportToScriptFile (
133- registryScope ,
134- (settingsMap ) -> {
135- settingsMap .put ( "hibernate.dialect.hana.use_unicode_string_types" , true );
136- },
137- modelScope ,
138- scriptFile
139- );
88+ exportToScriptFile ( registryScope , modelScope , scriptFile );
14089
14190String fileContent = new String ( Files .readAllBytes ( scriptFile .toPath () ) );
14291Pattern fileContentPattern = Pattern .compile ( "create( (column|row))? table test_entity \\ (b boolean[^,]+, c nvarchar[^,]+, field nvarchar[^,]+, lob nclob" );
@@ -146,20 +95,15 @@ public void testTargetScriptIsCreatedStringTypeNVarchar(
14695
14796@ Test
14897@ JiraKey (value = "HHH-12302" )
98+ @ ServiceRegistry
99+ @ DomainModel (annotatedClasses = HANASchemaMigrationTargetScriptCreationTest .TestEntity .class )
149100public void testTargetScriptIsCreatedStringTypeVarchar (
150101ServiceRegistryScope registryScope ,
151102DomainModelScope modelScope ,
152103@ TempDir File tmpDir ) throws Exception {
153104var scriptFile = new File ( tmpDir , "script.sql" );
154105
155- exportToScriptFile (
156- registryScope ,
157- (settingsMap ) -> {
158- settingsMap .put ( "hibernate.dialect.hana.use_unicode_string_types" , false );
159- },
160- modelScope ,
161- scriptFile
162- );
106+ exportToScriptFile ( registryScope , modelScope , scriptFile );
163107
164108String fileContent = new String ( Files .readAllBytes ( scriptFile .toPath () ) );
165109Pattern fileContentPattern = Pattern .compile ( "create( (column|row))? table test_entity \\ (b boolean[^,]+, c " + this .varcharType + "[^,]+, field " + this .varcharType + "[^,]+, lob " + this .clobType );
@@ -169,13 +113,15 @@ public void testTargetScriptIsCreatedStringTypeVarchar(
169113
170114@ Test
171115@ JiraKey (value = "HHH-12132" )
116+ @ ServiceRegistry
117+ @ DomainModel (annotatedClasses = HANASchemaMigrationTargetScriptCreationTest .TestEntity .class )
172118public void testTargetScriptIsCreatedBooleanTypeDefault (
173119ServiceRegistryScope registryScope ,
174120DomainModelScope modelScope ,
175121@ TempDir File tmpDir ) throws Exception {
176122var scriptFile = new File ( tmpDir , "script.sql" );
177123
178- exportToScriptFile ( registryScope , null , modelScope , scriptFile );
124+ exportToScriptFile ( registryScope , modelScope , scriptFile );
179125
180126String fileContent = new String ( Files .readAllBytes ( scriptFile .toPath () ) );
181127Pattern fileContentPattern = Pattern .compile ( "create( (column|row))? table test_entity \\ (b boolean[^,]+, c " + this .varcharType + "[^,]+, field " + this .varcharType + "[^,]+, lob " + this .clobType );
@@ -185,20 +131,17 @@ public void testTargetScriptIsCreatedBooleanTypeDefault(
185131
186132@ Test
187133@ JiraKey (value = "HHH-12132" )
134+ @ ServiceRegistry (settings = {
135+ @ Setting (name = "hibernate.dialect.hana.use_legacy_boolean_type" , value = "true" )
136+ })
137+ @ DomainModel (annotatedClasses = HANASchemaMigrationTargetScriptCreationTest .TestEntity .class )
188138public void testTargetScriptIsCreatedBooleanTypeLegacy (
189139ServiceRegistryScope registryScope ,
190140DomainModelScope modelScope ,
191141@ TempDir File tmpDir ) throws Exception {
192142var scriptFile = new File ( tmpDir , "script.sql" );
193143
194- exportToScriptFile (
195- registryScope ,
196- (settingsMap ) -> {
197- settingsMap .put ( "hibernate.dialect.hana.use_legacy_boolean_type" , true );
198- },
199- modelScope ,
200- scriptFile
201- );
144+ exportToScriptFile ( registryScope , modelScope , scriptFile );
202145
203146String fileContent = new String ( Files .readAllBytes ( scriptFile .toPath () ) );
204147Pattern fileContentPattern = Pattern .compile ( "create( (column|row))? table test_entity \\ (b tinyint[^,]+, c " + this .varcharType + "[^,]+, field " + this .varcharType + "[^,]+, lob " + this .clobType );
@@ -208,27 +151,61 @@ public void testTargetScriptIsCreatedBooleanTypeLegacy(
208151
209152@ Test
210153@ JiraKey (value = "HHH-12132" )
154+ @ ServiceRegistry
155+ @ DomainModel (annotatedClasses = HANASchemaMigrationTargetScriptCreationTest .TestEntity .class )
211156public void testTargetScriptIsCreatedBooleanType (
212157ServiceRegistryScope registryScope ,
213158DomainModelScope modelScope ,
214159@ TempDir File tmpDir ) throws Exception {
215160var scriptFile = new File ( tmpDir , "script.sql" );
216161
217- exportToScriptFile (
218- registryScope ,
219- (settingsMap ) -> {
220- settingsMap .put ( "hibernate.dialect.hana.use_legacy_boolean_type" , false );
221- },
222- modelScope ,
223- scriptFile
224- );
162+ exportToScriptFile ( registryScope , modelScope , scriptFile );
225163
226164String fileContent = new String ( Files .readAllBytes ( scriptFile .toPath () ) );
227165Pattern fileContentPattern = Pattern .compile ( "create( (column|row))? table test_entity \\ (b boolean[^,]+, c " + this .varcharType + "[^,]+, field " + this .varcharType + "[^,]+, lob " + this .clobType );
228166Matcher fileContentMatcher = fileContentPattern .matcher ( fileContent .toLowerCase () );
229167MatcherAssert .assertThat ( "Script file : " + fileContent .toLowerCase (), fileContentMatcher .find (), is ( true ) );
230168}
231169
170+ private void exportToScriptFile (
171+ ServiceRegistryScope registryScope ,
172+ DomainModelScope modelScope ,
173+ File scriptFile ) {
174+ var schemaCreator = new SchemaCreatorImpl ( registryScope .getRegistry () );
175+ final GenerationTargetToScript target =
176+ new GenerationTargetToScript ( new ScriptTargetOutputToFile ( scriptFile , "utf8" ), ";" );
177+ modelScope .getDomainModel ().orderColumns ( true );
178+ try {
179+ target .prepare ();
180+ final Map <String , Object > settings = registryScope .getRegistry ().requireService ( ConfigurationService .class ).getSettings ();
181+ schemaCreator .createFromMetadata (
182+ modelScope .getDomainModel (),
183+ new ExecutionOptions () {
184+ @ Override
185+ public Map <String , Object > getConfigurationValues () {
186+ return settings ;
187+ }
188+
189+ @ Override
190+ public boolean shouldManageNamespaces () {
191+ return false ;
192+ }
193+
194+ @ Override
195+ public ExceptionHandler getExceptionHandler () {
196+ return ExceptionHandlerHaltImpl .INSTANCE ;
197+ }
198+ },
199+ registryScope .getRegistry ().requireService ( JdbcEnvironment .class ).getDialect (),
200+ source -> source ,
201+ target
202+ );
203+ }
204+ finally {
205+ target .release ();
206+ }
207+ }
208+
232209@ Entity
233210@ Table (name = "test_entity" )
234211public static class TestEntity {
0 commit comments