Skip to content

Commit c5f48c0

Browse files
committed
[#912] Test using the default catalog name for MSSQL
1 parent 24fc353 commit c5f48c0

File tree

1 file changed

+75
-8
lines changed

1 file changed

+75
-8
lines changed

hibernate-reactive-core/src/test/java/org/hibernate/reactive/SchemaUpdateSqlServerTestBase.java

Lines changed: 75 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
import java.io.Serializable;
99
import java.util.Objects;
10+
import java.util.concurrent.CompletionStage;
1011
import javax.persistence.CascadeType;
1112
import javax.persistence.Entity;
1213
import javax.persistence.ForeignKey;
@@ -38,7 +39,12 @@
3839

3940
public abstract class SchemaUpdateSqlServerTestBase extends BaseReactiveTest {
4041

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
4248
extends SchemaUpdateSqlServerTestBase {
4349

4450
@Override
@@ -49,7 +55,29 @@ protected Configuration constructConfiguration(String hbm2DdlOption) {
4955
}
5056
}
5157

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 {
5381

5482
@Override
5583
protected Configuration constructConfiguration(String hbm2DdlOption) {
@@ -59,6 +87,24 @@ protected Configuration constructConfiguration(String hbm2DdlOption) {
5987
}
6088
}
6189

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+
62108
protected Configuration constructConfiguration(String hbm2DdlOption) {
63109
Configuration configuration = constructConfiguration();
64110
configuration.setProperty( Settings.HBM2DDL_AUTO, hbm2DdlOption );
@@ -69,15 +115,36 @@ protected Configuration constructConfiguration(String hbm2DdlOption) {
69115
@Rule
70116
public DatabaseSelectionRule dbRule = DatabaseSelectionRule.runOnlyFor( SQLSERVER );
71117

118+
public String addCatalog(String name) {
119+
return name;
120+
}
121+
72122
@Before
73123
@Override
74124
public void before(TestContext context) {
75125
Configuration createHbm2ddlConf = constructConfiguration( "create" );
76126
createHbm2ddlConf.addAnnotatedClass( ASimpleFirst.class );
77127
createHbm2ddlConf.addAnnotatedClass( AOther.class );
78128

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() );
81148
}
82149

83150
@After
@@ -102,19 +169,19 @@ public void testValidationSucceed(TestContext context) {
102169
test( context, setupSessionFactory( configuration ) );
103170
}
104171

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).
108174
@Test
109175
public void testValidationFails(TestContext context) {
110176
Configuration configuration = constructConfiguration( "validate" );
111177
configuration.addAnnotatedClass( AAnother.class );
112178

179+
final String errorMessage = "Schema-validation: missing table [" + addCatalog( "dbo.AAnother" ) + "]";
113180
test( context, setupSessionFactory( configuration )
114181
.handle( (unused, throwable) -> {
115182
context.assertNotNull( throwable );
116183
context.assertEquals( throwable.getClass(), SchemaManagementException.class );
117-
context.assertEquals( throwable.getMessage(), "Schema-validation: missing table [dbo.AAnother]" );
184+
context.assertEquals( throwable.getMessage(), errorMessage );
118185
return null;
119186
} ) );
120187
}

0 commit comments

Comments
 (0)