Skip to content

Commit dc9a404

Browse files
committed
HHH-19916 - Drop JUnit 4 usage
Not converted... * org.hibernate.orm.test.type.AbstractJavaTimeTypeTest subtypes - crazy parameterization (see org.hibernate.orm.test.tm.InterceptorTransactionTest)
1 parent 2195e96 commit dc9a404

12 files changed

+457
-585
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/derivedid/ColumnLengthTest.java

Lines changed: 37 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,6 @@
44
*/
55
package org.hibernate.orm.test.schemaupdate.derivedid;
66

7-
import java.io.File;
8-
import java.io.Serializable;
9-
import java.nio.file.Files;
10-
import java.util.EnumSet;
11-
import java.util.List;
127
import jakarta.persistence.Column;
138
import jakarta.persistence.Embeddable;
149
import jakarta.persistence.EmbeddedId;
@@ -17,68 +12,57 @@
1712
import jakarta.persistence.ManyToOne;
1813
import jakarta.persistence.MapsId;
1914
import jakarta.persistence.Table;
20-
21-
import org.hibernate.boot.MetadataSources;
22-
import org.hibernate.boot.registry.StandardServiceRegistry;
23-
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
24-
import org.hibernate.boot.spi.MetadataImplementor;
25-
import org.hibernate.cfg.Environment;
2615
import org.hibernate.dialect.H2Dialect;
16+
import org.hibernate.testing.orm.junit.BaseUnitTest;
17+
import org.hibernate.testing.orm.junit.DomainModel;
18+
import org.hibernate.testing.orm.junit.DomainModelScope;
19+
import org.hibernate.testing.orm.junit.RequiresDialect;
20+
import org.hibernate.testing.orm.junit.ServiceRegistry;
21+
import org.hibernate.testing.orm.junit.Setting;
2722
import org.hibernate.tool.hbm2ddl.SchemaExport;
2823
import org.hibernate.tool.schema.TargetType;
24+
import org.junit.jupiter.api.Assertions;
25+
import org.junit.jupiter.api.Test;
26+
import org.junit.jupiter.api.io.TempDir;
2927

30-
import org.hibernate.testing.RequiresDialect;
31-
import org.hibernate.testing.junit4.BaseUnitTestCase;
32-
import org.hibernate.testing.util.ServiceRegistryUtil;
33-
import org.junit.After;
34-
import org.junit.Before;
35-
import org.junit.Test;
28+
import java.io.File;
29+
import java.io.Serializable;
30+
import java.nio.file.Files;
31+
import java.util.EnumSet;
32+
import java.util.List;
3633

37-
import static org.junit.Assert.assertTrue;
34+
import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_AUTO;
3835

3936
/**
4037
* @author Andrea Boriero
4138
*/
39+
@SuppressWarnings("JUnitMalformedDeclaration")
4240
@RequiresDialect(H2Dialect.class)
43-
public class ColumnLengthTest extends BaseUnitTestCase {
44-
45-
private StandardServiceRegistry ssr;
46-
private File outputFile;
47-
private MetadataImplementor metadata;
41+
@BaseUnitTest
42+
@ServiceRegistry(settings = @Setting(name = HBM2DDL_AUTO, value = "none"))
43+
@DomainModel(annotatedClasses = {
44+
ColumnLengthTest.Employee.class,
45+
ColumnLengthTest.Dependent.class
46+
})
47+
public class ColumnLengthTest {
4848

49-
@Before
50-
public void setUp() throws Exception {
51-
outputFile = File.createTempFile( "update_script", ".sql" );
52-
outputFile.deleteOnExit();
53-
54-
ssr = ServiceRegistryUtil.serviceRegistryBuilder()
55-
.applySetting( Environment.HBM2DDL_AUTO, "none" )
56-
.build();
49+
@Test
50+
public void testColumnLengthsAreApplied(DomainModelScope modelScope, @TempDir File tempDir) throws Exception {
51+
final var scriptFile = new File( tempDir, "update_script.sql" );
5752

58-
metadata = (MetadataImplementor) new MetadataSources( ssr )
59-
.addAnnotatedClass( Employee.class )
60-
.addAnnotatedClass( Dependent.class )
61-
.buildMetadata();
53+
final var metadata = modelScope.getDomainModel();
6254
metadata.orderColumns( true );
6355
metadata.validate();
64-
}
65-
66-
@After
67-
public void tearDown() {
68-
StandardServiceRegistryBuilder.destroy( ssr );
69-
}
7056

71-
@Test
72-
public void testTheColumnsLenghtAreApplied() throws Exception {
7357
new SchemaExport()
74-
.setOutputFile( outputFile.getAbsolutePath() )
58+
.setOutputFile( scriptFile.getAbsolutePath() )
7559
.setDelimiter( ";" )
7660
.setFormat( false )
7761
.createOnly( EnumSet.of( TargetType.SCRIPT ), metadata );
7862

79-
List<String> commands = Files.readAllLines( outputFile.toPath() );
63+
final var commands = Files.readAllLines( scriptFile.toPath() );
8064

81-
assertTrue( checkCommandIsGenerated(
65+
Assertions.assertTrue( checkCommandIsGenerated(
8266
commands,
8367
"create table DEPENDENT (FK2 varchar(10) not null, FK1 varchar(32) not null, name varchar(255) not null, primary key (FK1, FK2, name));"
8468
) );
@@ -94,27 +78,31 @@ boolean checkCommandIsGenerated(List<String> generatedCommands, String toCheck)
9478
return false;
9579
}
9680

81+
@SuppressWarnings("unused")
9782
@Embeddable
98-
public class EmployeeId implements Serializable {
83+
public static class EmployeeId implements Serializable {
9984
@Column(name = "first_name", length = 32)
10085
String firstName;
10186
@Column(name = "last_name", length = 10)
10287
String lastName;
10388
}
10489

90+
@SuppressWarnings("unused")
10591
@Entity
106-
@Table(name = "EMLOYEE")
92+
@Table(name = "EMPLOYEE")
10793
public static class Employee {
10894
@EmbeddedId
10995
EmployeeId id;
11096
}
11197

98+
@SuppressWarnings("unused")
11299
@Embeddable
113-
public class DependentId implements Serializable {
100+
public static class DependentId implements Serializable {
114101
String name;
115102
EmployeeId empPK;
116103
}
117104

105+
@SuppressWarnings("unused")
118106
@Entity
119107
@Table(name = "DEPENDENT")
120108
public static class Dependent {

hibernate-core/src/test/java/org/hibernate/orm/test/schemaupdate/foreignkeys/ForeignKeyDropTest.java

Lines changed: 62 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -4,112 +4,100 @@
44
*/
55
package org.hibernate.orm.test.schemaupdate.foreignkeys;
66

7-
import java.io.File;
8-
import java.io.IOException;
9-
import java.nio.file.Files;
10-
import java.util.EnumSet;
11-
import java.util.List;
12-
import java.util.Set;
13-
import java.util.regex.Matcher;
14-
import java.util.regex.Pattern;
157
import jakarta.persistence.Entity;
168
import jakarta.persistence.Id;
179
import jakarta.persistence.JoinColumn;
1810
import jakarta.persistence.OneToMany;
1911
import jakarta.persistence.Table;
20-
21-
import org.hibernate.boot.MetadataSources;
22-
import org.hibernate.boot.registry.StandardServiceRegistry;
23-
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
24-
import org.hibernate.boot.spi.MetadataImplementor;
25-
import org.hibernate.cfg.Environment;
12+
import org.hamcrest.MatcherAssert;
2613
import org.hibernate.dialect.Dialect;
2714
import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment;
15+
import org.hibernate.testing.orm.junit.BaseUnitTest;
16+
import org.hibernate.testing.orm.junit.DialectFeatureChecks;
17+
import org.hibernate.testing.orm.junit.DomainModel;
18+
import org.hibernate.testing.orm.junit.DomainModelScope;
19+
import org.hibernate.testing.orm.junit.JiraKey;
20+
import org.hibernate.testing.orm.junit.RequiresDialectFeature;
21+
import org.hibernate.testing.orm.junit.ServiceRegistry;
22+
import org.hibernate.testing.orm.junit.ServiceRegistryScope;
23+
import org.hibernate.testing.orm.junit.Setting;
2824
import org.hibernate.tool.hbm2ddl.SchemaExport;
2925
import org.hibernate.tool.schema.TargetType;
26+
import org.junit.jupiter.api.Test;
27+
import org.junit.jupiter.api.io.TempDir;
3028

31-
import org.hibernate.testing.DialectChecks;
32-
import org.hibernate.testing.RequiresDialectFeature;
33-
import org.hibernate.testing.orm.junit.JiraKey;
34-
import org.hibernate.testing.junit4.BaseUnitTestCase;
35-
import org.hibernate.testing.util.ServiceRegistryUtil;
36-
import org.junit.After;
37-
import org.junit.Before;
38-
import org.junit.Test;
29+
import java.io.File;
30+
import java.io.IOException;
31+
import java.nio.file.Files;
32+
import java.util.EnumSet;
33+
import java.util.List;
34+
import java.util.Set;
35+
import java.util.regex.Matcher;
36+
import java.util.regex.Pattern;
3937

4038
import static org.hamcrest.core.Is.is;
41-
import static org.junit.Assert.assertThat;
39+
import static org.hibernate.cfg.JdbcSettings.FORMAT_SQL;
40+
import static org.hibernate.cfg.JdbcSettings.SHOW_SQL;
41+
import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_AUTO;
4242

4343
/**
4444
* @author Andrea Boriero
4545
*/
46+
@SuppressWarnings("JUnitMalformedDeclaration")
4647
@JiraKey(value = "HHH-12271")
47-
@RequiresDialectFeature(DialectChecks.SupportDropConstraints.class)
48-
public class ForeignKeyDropTest extends BaseUnitTestCase {
49-
private File output;
50-
private MetadataImplementor metadata;
51-
private StandardServiceRegistry ssr;
52-
private SchemaExport schemaExport;
53-
54-
@Before
55-
public void setUp() throws Exception {
56-
output = File.createTempFile( "update_script", ".sql" );
57-
output.deleteOnExit();
58-
ssr = ServiceRegistryUtil.serviceRegistryBuilder()
59-
.applySetting( Environment.HBM2DDL_AUTO, "none" )
60-
.applySetting( Environment.FORMAT_SQL, "false" )
61-
.applySetting( Environment.SHOW_SQL, "true" )
62-
.build();
63-
metadata = (MetadataImplementor) new MetadataSources( ssr )
64-
.addAnnotatedClass( ParentEntity.class )
65-
.addAnnotatedClass( ChildEntity.class )
66-
.buildMetadata();
67-
metadata.orderColumns( false );
68-
metadata.validate();
69-
schemaExport = new SchemaExport().setHaltOnError( false ).setOutputFile( output.getAbsolutePath() );
70-
}
71-
48+
@BaseUnitTest
49+
@RequiresDialectFeature(feature = DialectFeatureChecks.SupportDropConstraints.class)
50+
@ServiceRegistry(settings = {
51+
@Setting(name = HBM2DDL_AUTO, value = "none"),
52+
@Setting(name = FORMAT_SQL, value = "false"),
53+
@Setting(name = SHOW_SQL, value = "true")
54+
})
55+
@DomainModel(annotatedClasses = {
56+
ForeignKeyDropTest.ParentEntity.class,
57+
ForeignKeyDropTest.ChildEntity.class
58+
})
59+
public class ForeignKeyDropTest {
7260
@Test
7361
@JiraKey(value = "HHH-11236")
74-
public void testForeignKeyDropIsCorrectlyGenerated() throws Exception {
75-
76-
schemaExport
77-
.drop( EnumSet.of( TargetType.SCRIPT, TargetType.DATABASE ), metadata );
62+
public void testForeignKeyDropIsCorrectlyGenerated(
63+
ServiceRegistryScope registryScope,
64+
DomainModelScope modelScope,
65+
@TempDir File tmpDir) throws Exception {
66+
final var metadata = modelScope.getDomainModel();
67+
metadata.orderColumns( false );
68+
metadata.validate();
7869

79-
assertThat(
80-
"The ddl foreign key drop command has not been properly generated",
81-
checkDropForeignKeyConstraint( "CHILD_ENTITY" ),
82-
is( true )
83-
);
84-
}
70+
final var scriptFile = new File( tmpDir, "script.sql" );
8571

86-
@After
87-
public void tearDown() {
88-
StandardServiceRegistryBuilder.destroy( ssr );
89-
}
72+
final var schemaExport = new SchemaExport().setHaltOnError( false ).setOutputFile( scriptFile.getAbsolutePath() );
73+
schemaExport.drop( EnumSet.of( TargetType.SCRIPT, TargetType.DATABASE ), metadata );
9074

91-
protected Dialect getDialect() {
92-
return ssr.getService( JdbcEnvironment.class ).getDialect();
75+
final Dialect dialect = registryScope.getRegistry().requireService( JdbcEnvironment.class ).getDialect();
76+
MatcherAssert.assertThat( "The ddl foreign key drop command has not been properly generated",
77+
checkDropForeignKeyConstraint( "CHILD_ENTITY", scriptFile, dialect ), is( true ) );
9378
}
9479

95-
private boolean checkDropForeignKeyConstraint(String tableName) throws IOException {
80+
private boolean checkDropForeignKeyConstraint(
81+
String tableName,
82+
File scriptFile,
83+
Dialect dialect) throws IOException {
9684
boolean matches = false;
97-
String regex = getDialect().getAlterTableString( tableName );
98-
regex += " " + getDialect().getDropForeignKeyString() + " ";
85+
String regex = dialect.getAlterTableString( tableName );
86+
regex += " " + dialect.getDropForeignKeyString() + " ";
9987

100-
if ( getDialect().supportsIfExistsBeforeConstraintName() ) {
88+
if ( dialect.supportsIfExistsBeforeConstraintName() ) {
10189
regex += "if exists ";
10290
}
10391
regex += "fk(.)*";
104-
if ( getDialect().supportsIfExistsAfterConstraintName() ) {
92+
if ( dialect.supportsIfExistsAfterConstraintName() ) {
10593
regex += " if exists";
10694
}
10795

108-
return isMatching( matches, regex.toLowerCase() );
96+
return isMatching( matches, regex.toLowerCase(), scriptFile );
10997
}
11098

111-
private boolean isMatching(boolean matches, String regex) throws IOException {
112-
List<String> commands = Files.readAllLines( output.toPath() );
99+
private boolean isMatching(boolean matches, String regex, File scriptFile) throws IOException {
100+
List<String> commands = Files.readAllLines( scriptFile.toPath() );
113101

114102
Pattern p = Pattern.compile( regex );
115103
for ( String line : commands ) {
@@ -121,6 +109,7 @@ private boolean isMatching(boolean matches, String regex) throws IOException {
121109
return matches;
122110
}
123111

112+
@SuppressWarnings("unused")
124113
@Entity(name = "ParentEntity")
125114
@Table(name = "PARENT_ENTITY")
126115
public static class ParentEntity {
@@ -132,6 +121,7 @@ public static class ParentEntity {
132121
Set<ChildEntity> children;
133122
}
134123

124+
@SuppressWarnings("unused")
135125
@Entity(name = "ChildEntity")
136126
@Table(name = "CHILD_ENTITY")
137127
public static class ChildEntity {

0 commit comments

Comments
 (0)