44 */
55package org .hibernate .orm .test .annotations .onetoone ;
66
7- import java .io .File ;
8- import java .io .IOException ;
9- import java .nio .file .Files ;
10- import java .util .List ;
117import jakarta .persistence .Column ;
128import jakarta .persistence .Entity ;
139import jakarta .persistence .FetchType ;
1713import jakarta .persistence .JoinTable ;
1814import jakarta .persistence .OneToOne ;
1915import jakarta .persistence .Table ;
20-
2116import org .hibernate .cfg .AvailableSettings ;
22- import org .hibernate .cfg .Configuration ;
2317import org .hibernate .dialect .H2Dialect ;
24-
25- import org .hibernate .testing .RequiresDialect ;
18+ import org .hibernate .testing .orm .junit .DomainModel ;
2619import org .hibernate .testing .orm .junit .JiraKey ;
27- import org .hibernate .testing .junit4 .BaseCoreFunctionalTestCase ;
28- import org .junit .After ;
29- import org .junit .Test ;
20+ import org .hibernate .testing .orm .junit .RequiresDialect ;
21+ import org .hibernate .testing .orm .junit .ServiceRegistry ;
22+ import org .hibernate .testing .orm .junit .SessionFactory ;
23+ import org .hibernate .testing .orm .junit .SessionFactoryScope ;
24+ import org .hibernate .testing .orm .junit .Setting ;
25+ import org .hibernate .testing .orm .junit .SettingProvider ;
26+ import org .junit .jupiter .api .AfterEach ;
27+ import org .junit .jupiter .api .Test ;
3028
31- import static org .junit .Assert .assertTrue ;
32- import static org .junit .Assert .fail ;
33-
34- @ JiraKey (value = "HHH-13959" )
35- public class OneToOneJoinTableUniquenessTest extends BaseCoreFunctionalTestCase {
36- File output ;
37-
38- @ Override
39- protected Class <?>[] getAnnotatedClasses () {
40- return new Class [] { Profile .class , PersonRole .class };
41- }
29+ import java .io .File ;
30+ import java .io .IOException ;
31+ import java .nio .file .Files ;
32+ import java .nio .file .Path ;
33+ import java .util .List ;
4234
43- @ Override
44- protected void configure (Configuration configuration ) {
35+ import static org .assertj .core .api .Assertions .assertThat ;
4536
46- try {
47- output = File .createTempFile ( "update_script" , ".sql" );
48- }
49- catch (IOException e ) {
50- e .printStackTrace ();
51- fail ( e .getMessage () );
37+ @ JiraKey (value = "HHH-13959" )
38+ @ DomainModel (
39+ annotatedClasses = {
40+ OneToOneJoinTableUniquenessTest .Profile .class ,
41+ OneToOneJoinTableUniquenessTest .PersonRole .class
42+ }
43+ )
44+ @ SessionFactory
45+ @ ServiceRegistry (
46+ settings = {
47+ @ Setting (name = AvailableSettings .JAKARTA_HBM2DDL_SCRIPTS_ACTION , value = "create" ),
48+ @ Setting (name = AvailableSettings .JAKARTA_HBM2DDL_DATABASE_ACTION , value = "create-drop" ),
49+ @ Setting (name = AvailableSettings .FORMAT_SQL , value = "false" )
50+ },
51+ settingProviders = @ SettingProvider (
52+ settingName = AvailableSettings .JAKARTA_HBM2DDL_SCRIPTS_CREATE_TARGET ,
53+ provider = OneToOneJoinTableUniquenessTest .ScriptCreateTargetProvider .class
54+ )
55+ )
56+ public class OneToOneJoinTableUniquenessTest {
57+
58+ public static class ScriptCreateTargetProvider implements SettingProvider .Provider <String > {
59+ static Path path ;
60+
61+ @ Override
62+ public String getSetting () {
63+ try {
64+ File output = File .createTempFile ( "update_script" , ".sql" );
65+ path = output .toPath ();
66+ return path .toString ();
67+ }
68+ catch (IOException e ) {
69+ throw new RuntimeException ( e );
70+ }
5271 }
53- String value = output .toPath ().toString ();
54- configuration .setProperty ( AvailableSettings .JAKARTA_HBM2DDL_SCRIPTS_CREATE_TARGET , value );
55- configuration .setProperty ( AvailableSettings .JAKARTA_HBM2DDL_SCRIPTS_ACTION , "create" );
56- configuration .setProperty ( AvailableSettings .JAKARTA_HBM2DDL_DATABASE_ACTION , "create-drop" );
57- configuration .setProperty ( AvailableSettings .FORMAT_SQL , "false" );
5872 }
5973
60- @ After
61- public void tearDown () {
62- inTransaction (
63- session -> {
64- session .createQuery ( "delete from PersonRole" ).executeUpdate ();
65- session .createQuery ( "delete from Profile" ).executeUpdate ();
66- }
67- );
74+ @ AfterEach
75+ public void tearDown (SessionFactoryScope scope ) {
76+ scope .getSessionFactory ().getSchemaManager ().truncateMappedObjects ();
6877 }
6978
7079 @ Test
7180 @ RequiresDialect (H2Dialect .class )
72- public void testJoinTableColumnAreBothNotNull () throws Exception {
73- List <String > commands = Files .readAllLines ( output .toPath () );
81+ public void testJoinTableColumnAreBothNotNull (SessionFactoryScope scope ) throws Exception {
82+ scope .getSessionFactory ();
83+ List <String > commands = Files .readAllLines ( ScriptCreateTargetProvider .path );
7484 boolean isJoinTableCreated = false ;
7585 for ( String command : commands ) {
7686 String lowerCaseCommand = command .toLowerCase ();
7787 if ( lowerCaseCommand .contains ( "create table profile_person_role" ) ) {
7888 isJoinTableCreated = true ;
79- assertTrue ( lowerCaseCommand .contains ( "personrole_roleid bigint not null" ) );
80- assertTrue ( lowerCaseCommand .contains ( "id bigint not null" ) );
89+ assertThat ( lowerCaseCommand ) .contains ( "personrole_roleid bigint not null" );
90+ assertThat ( lowerCaseCommand ) .contains ( "id bigint not null" );
8191 }
8292 }
83- assertTrue ( "The Join table was not created" , isJoinTableCreated );
93+ assertThat ( isJoinTableCreated )
94+ .describedAs ( "The Join table was not created" )
95+ .isTrue ();
8496 }
8597
8698 @ Test
87- public void testPersistProfile () {
88- inTransaction (
99+ public void testPersistProfile (SessionFactoryScope scope ) {
100+ scope . inTransaction (
89101 session -> {
90102 Profile p = new Profile ();
91103 session .persist ( p );
@@ -94,8 +106,8 @@ public void testPersistProfile() {
94106 }
95107
96108 @ Test
97- public void testPersistPersonRole () {
98- inTransaction (
109+ public void testPersistPersonRole (SessionFactoryScope scope ) {
110+ scope . inTransaction (
99111 session -> {
100112 PersonRole personRole = new PersonRole ();
101113 session .persist ( personRole );
@@ -104,8 +116,8 @@ public void testPersistPersonRole() {
104116 }
105117
106118 @ Test
107- public void testPersistBothSameTime () {
108- inTransaction (
119+ public void testPersistBothSameTime (SessionFactoryScope scope ) {
120+ scope . inTransaction (
109121 session -> {
110122 PersonRole personRole = new PersonRole ();
111123 Profile profile = new Profile ();
@@ -118,8 +130,8 @@ public void testPersistBothSameTime() {
118130 }
119131
120132 @ Test
121- public void testPersistBothAndAssociateLater () {
122- inTransaction (
133+ public void testPersistBothAndAssociateLater (SessionFactoryScope scope ) {
134+ scope . inTransaction (
123135 session -> {
124136 PersonRole personRole = new PersonRole ();
125137 Profile profile = new Profile ();
@@ -159,7 +171,7 @@ public void setId(Long id) {
159171 @ OneToOne (fetch = FetchType .LAZY )
160172 @ JoinTable (
161173 name = "profile_person_role" ,
162- inverseJoinColumns = { @ JoinColumn (unique = true , nullable = false ) }
174+ inverseJoinColumns = {@ JoinColumn (unique = true , nullable = false )}
163175 )
164176 public PersonRole getPersonRole () {
165177 return this .personRole ;
0 commit comments