44 */
55package 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 ;
157import jakarta .persistence .Entity ;
168import jakarta .persistence .Id ;
179import jakarta .persistence .JoinColumn ;
1810import jakarta .persistence .OneToMany ;
1911import 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 ;
2613import org .hibernate .dialect .Dialect ;
2714import 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 ;
2824import org .hibernate .tool .hbm2ddl .SchemaExport ;
2925import 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
4038import 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