Skip to content

Commit afe25b1

Browse files
committed
Fix HANA test after dropping JUnit 4 API
1 parent 83fe9de commit afe25b1

File tree

1 file changed

+57
-80
lines changed

1 file changed

+57
-80
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/HANASchemaMigrationTargetScriptCreationTest.java

Lines changed: 57 additions & 80 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,9 @@
99
import jakarta.persistence.Lob;
1010
import jakarta.persistence.Table;
1111
import org.hamcrest.MatcherAssert;
12-
import org.hibernate.boot.registry.StandardServiceRegistry;
1312
import org.hibernate.dialect.HANADialect;
1413
import org.hibernate.engine.config.spi.ConfigurationService;
1514
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
16-
import org.hibernate.testing.orm.junit.DialectContext;
1715
import org.hibernate.testing.orm.junit.DomainModel;
1816
import org.hibernate.testing.orm.junit.DomainModelScope;
1917
import org.hibernate.testing.orm.junit.JiraKey;
@@ -34,7 +32,6 @@
3432
import java.io.File;
3533
import java.nio.file.Files;
3634
import java.util.Map;
37-
import java.util.function.Consumer;
3835
import java.util.regex.Matcher;
3936
import java.util.regex.Pattern;
4037

@@ -50,8 +47,8 @@ public class HANASchemaMigrationTargetScriptCreationTest {
5047
private 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();
5552
this.varcharType = dialect.isUseUnicodeStringTypes() ? "nvarchar" : "varchar";
5653
this.clobType = dialect.isUseUnicodeStringTypes() ? "nclob" : "clob";
5754
}
@@ -66,7 +63,7 @@ public void testTargetScriptIsCreatedStringTypeDefault(
6663
@TempDir File tmpDir) throws Exception {
6764
var scriptFile = new File( tmpDir, "script.sql" );
6865

69-
exportToScriptFile( registryScope, null, modelScope, scriptFile );
66+
exportToScriptFile( registryScope, modelScope, scriptFile );
7067

7168
String fileContent = new String( Files.readAllBytes( scriptFile.toPath() ) );
7269
Pattern 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(
7673
is( 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 {
13086
var 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

14190
String fileContent = new String( Files.readAllBytes( scriptFile.toPath() ) );
14291
Pattern 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)
149100
public void testTargetScriptIsCreatedStringTypeVarchar(
150101
ServiceRegistryScope registryScope,
151102
DomainModelScope modelScope,
152103
@TempDir File tmpDir) throws Exception {
153104
var 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

164108
String fileContent = new String( Files.readAllBytes( scriptFile.toPath() ) );
165109
Pattern 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)
172118
public void testTargetScriptIsCreatedBooleanTypeDefault(
173119
ServiceRegistryScope registryScope,
174120
DomainModelScope modelScope,
175121
@TempDir File tmpDir) throws Exception {
176122
var scriptFile = new File( tmpDir, "script.sql" );
177123

178-
exportToScriptFile( registryScope, null, modelScope, scriptFile );
124+
exportToScriptFile( registryScope, modelScope, scriptFile );
179125

180126
String fileContent = new String( Files.readAllBytes( scriptFile.toPath() ) );
181127
Pattern 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)
188138
public void testTargetScriptIsCreatedBooleanTypeLegacy(
189139
ServiceRegistryScope registryScope,
190140
DomainModelScope modelScope,
191141
@TempDir File tmpDir) throws Exception {
192142
var 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

203146
String fileContent = new String( Files.readAllBytes( scriptFile.toPath() ) );
204147
Pattern 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)
211156
public void testTargetScriptIsCreatedBooleanType(
212157
ServiceRegistryScope registryScope,
213158
DomainModelScope modelScope,
214159
@TempDir File tmpDir) throws Exception {
215160
var 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

226164
String fileContent = new String( Files.readAllBytes( scriptFile.toPath() ) );
227165
Pattern fileContentPattern = Pattern.compile( "create( (column|row))? table test_entity \\(b boolean[^,]+, c " + this.varcharType + "[^,]+, field " + this.varcharType + "[^,]+, lob " + this.clobType );
228166
Matcher fileContentMatcher = fileContentPattern.matcher( fileContent.toLowerCase() );
229167
MatcherAssert.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")
234211
public static class TestEntity {

0 commit comments

Comments
 (0)