7
7
8
8
import java .io .Serializable ;
9
9
import java .util .Objects ;
10
+ import java .util .concurrent .CompletionStage ;
10
11
import javax .persistence .CascadeType ;
11
12
import javax .persistence .Entity ;
12
13
import javax .persistence .ForeignKey ;
38
39
39
40
public abstract class SchemaUpdateSqlServerTestBase extends BaseReactiveTest {
40
41
41
- public static class IndividuallySchemaUpdateSqlServerTestBase
42
+ private static final String DEFAULT_CATALOG_NAME = "master" ;
43
+
44
+ /**
45
+ * Test INDIVIDUALLY option without setting the default catalog name
46
+ */
47
+ public static class IndividuallySchemaUpdateSqlServerTest
42
48
extends SchemaUpdateSqlServerTestBase {
43
49
44
50
@ Override
@@ -49,7 +55,29 @@ protected Configuration constructConfiguration(String hbm2DdlOption) {
49
55
}
50
56
}
51
57
52
- public static class GroupedSchemaUpdateSqlServerTestBase extends SchemaUpdateSqlServerTestBase {
58
+ /**
59
+ * Test INDIVIDUALLY option when we set the catalog name to the default name
60
+ */
61
+ public static class IndividuallySchemaUpdateWithCatalogTest
62
+ extends SchemaUpdateSqlServerTestBase {
63
+
64
+ @ Override
65
+ protected Configuration constructConfiguration (String hbm2DdlOption ) {
66
+ final Configuration configuration = super .constructConfiguration ( hbm2DdlOption );
67
+ configuration .setProperty ( Settings .DEFAULT_CATALOG , DEFAULT_CATALOG_NAME );
68
+ return configuration ;
69
+ }
70
+
71
+ @ Override
72
+ public String addCatalog (String name ) {
73
+ return DEFAULT_CATALOG_NAME + "." + name ;
74
+ }
75
+ }
76
+
77
+ /**
78
+ * Test GROUPED option without setting the default catalog name
79
+ */
80
+ public static class GroupedSchemaUpdateSqlServerTest extends SchemaUpdateSqlServerTestBase {
53
81
54
82
@ Override
55
83
protected Configuration constructConfiguration (String hbm2DdlOption ) {
@@ -59,6 +87,24 @@ protected Configuration constructConfiguration(String hbm2DdlOption) {
59
87
}
60
88
}
61
89
90
+ /**
91
+ * Test GROUPED option when we set the catalog name to default name
92
+ */
93
+ public static class GroupedSchemaUpdateWithCatalogNameTest extends SchemaUpdateSqlServerTestBase {
94
+
95
+ @ Override
96
+ protected Configuration constructConfiguration (String hbm2DdlOption ) {
97
+ final Configuration configuration = super .constructConfiguration ( hbm2DdlOption );
98
+ configuration .setProperty ( Settings .DEFAULT_CATALOG , DEFAULT_CATALOG_NAME );
99
+ return configuration ;
100
+ }
101
+
102
+ @ Override
103
+ public String addCatalog (String name ) {
104
+ return DEFAULT_CATALOG_NAME + "." + name ;
105
+ }
106
+ }
107
+
62
108
protected Configuration constructConfiguration (String hbm2DdlOption ) {
63
109
Configuration configuration = constructConfiguration ();
64
110
configuration .setProperty ( Settings .HBM2DDL_AUTO , hbm2DdlOption );
@@ -69,15 +115,36 @@ protected Configuration constructConfiguration(String hbm2DdlOption) {
69
115
@ Rule
70
116
public DatabaseSelectionRule dbRule = DatabaseSelectionRule .runOnlyFor ( SQLSERVER );
71
117
118
+ public String addCatalog (String name ) {
119
+ return name ;
120
+ }
121
+
72
122
@ Before
73
123
@ Override
74
124
public void before (TestContext context ) {
75
125
Configuration createHbm2ddlConf = constructConfiguration ( "create" );
76
126
createHbm2ddlConf .addAnnotatedClass ( ASimpleFirst .class );
77
127
createHbm2ddlConf .addAnnotatedClass ( AOther .class );
78
128
79
- test ( context , setupSessionFactory ( createHbm2ddlConf )
80
- .thenCompose ( v -> factoryManager .stop () ) );
129
+ test ( context , dropSequenceIfExists ( createHbm2ddlConf )
130
+ .thenCompose ( ignore -> setupSessionFactory ( createHbm2ddlConf )
131
+ .thenCompose ( v -> factoryManager .stop () ) ) );
132
+ }
133
+
134
+ // See HHH-14835: Vert.x throws an exception when the catalog is specified.
135
+ // Because it happens during schema creation, the error is ignored and the build won't fail
136
+ // if one of the previous tests has already created the sequence.
137
+ // This method makes sure that the sequence is deleted if it exists, so that these tests
138
+ // fail consistently when the wrong ORM version is used.
139
+ private CompletionStage <Void > dropSequenceIfExists (Configuration createHbm2ddlConf ) {
140
+ return setupSessionFactory ( createHbm2ddlConf )
141
+ .thenCompose ( v -> getSessionFactory ()
142
+ .withTransaction ( (session , transaction ) -> session
143
+ // No need to add the catalog name because MSSQL doesn't support it
144
+ .createNativeQuery ( "drop sequence if exists dbo.hibernate_sequence" )
145
+ .executeUpdate () ) )
146
+ .handle ( (res , err ) -> null )
147
+ .thenCompose ( v -> factoryManager .stop () );
81
148
}
82
149
83
150
@ After
@@ -102,19 +169,19 @@ public void testValidationSucceed(TestContext context) {
102
169
test ( context , setupSessionFactory ( configuration ) );
103
170
}
104
171
105
- //TODO: I'm just checking that the validation fails because the table is missing, but we need more tests to check that
106
- // it fails for other scenarios: missing column, wrong type (?) and so on. (I don't know exactly what cases `validate`
107
- // actually checks).
172
+ //TODO: We need more tests to check that it fails for other scenarios:
173
+ // missing column, wrong type (?) and so on. (I don't know exactly what cases `validate` actually checks).
108
174
@ Test
109
175
public void testValidationFails (TestContext context ) {
110
176
Configuration configuration = constructConfiguration ( "validate" );
111
177
configuration .addAnnotatedClass ( AAnother .class );
112
178
179
+ final String errorMessage = "Schema-validation: missing table [" + addCatalog ( "dbo.AAnother" ) + "]" ;
113
180
test ( context , setupSessionFactory ( configuration )
114
181
.handle ( (unused , throwable ) -> {
115
182
context .assertNotNull ( throwable );
116
183
context .assertEquals ( throwable .getClass (), SchemaManagementException .class );
117
- context .assertEquals ( throwable .getMessage (), "Schema-validation: missing table [dbo.AAnother]" );
184
+ context .assertEquals ( throwable .getMessage (), errorMessage );
118
185
return null ;
119
186
} ) );
120
187
}
0 commit comments