|
| 1 | +/* Hibernate, Relational Persistence for Idiomatic Java |
| 2 | + * |
| 3 | + * SPDX-License-Identifier: Apache-2.0 |
| 4 | + * Copyright: Red Hat Inc. and Hibernate Authors |
| 5 | + */ |
| 6 | +package org.hibernate.reactive.schema; |
| 7 | + |
| 8 | + |
| 9 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 10 | +import org.hibernate.cfg.Configuration; |
| 11 | +import org.hibernate.reactive.BaseReactiveTest; |
| 12 | +import org.hibernate.reactive.annotations.DisabledFor; |
| 13 | +import org.hibernate.reactive.provider.Settings; |
| 14 | +import org.hibernate.tool.schema.spi.SchemaManagementException; |
| 15 | + |
| 16 | +import org.junit.jupiter.api.AfterEach; |
| 17 | + |
| 18 | +import org.junit.jupiter.api.BeforeEach; |
| 19 | +import org.junit.jupiter.api.Test; |
| 20 | + |
| 21 | +import io.vertx.junit5.Timeout; |
| 22 | +import io.vertx.junit5.VertxTestContext; |
| 23 | +import jakarta.persistence.Entity; |
| 24 | +import jakarta.persistence.GeneratedValue; |
| 25 | +import jakarta.persistence.Id; |
| 26 | +import jakarta.persistence.Table; |
| 27 | + |
| 28 | +import static java.util.concurrent.TimeUnit.MINUTES; |
| 29 | +import static org.hibernate.reactive.containers.DatabaseConfiguration.DBType.DB2; |
| 30 | +import static org.hibernate.tool.schema.JdbcMetadaAccessStrategy.GROUPED; |
| 31 | +import static org.hibernate.tool.schema.JdbcMetadaAccessStrategy.INDIVIDUALLY; |
| 32 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 33 | +import static org.junit.jupiter.api.Assertions.assertNotNull; |
| 34 | + |
| 35 | +/** |
| 36 | + * Test schema validation at startup for all the supported types: |
| 37 | + * - Missing table validation error |
| 38 | + * - No validation error when everything is fine |
| 39 | + * - TODO: Missing column |
| 40 | + * - TODO: Wrong column type |
| 41 | + */ |
| 42 | +@DisabledFor(value = DB2, reason = "No InformationExtractor for Dialect [org.hibernate.dialect.DB2Dialect..]") |
| 43 | +public abstract class SchemaArrayTypesValidationTestBase extends BaseReactiveTest { |
| 44 | + |
| 45 | + public static class IndividuallyStrategyTest extends SchemaArrayTypesValidationTestBase { |
| 46 | + |
| 47 | + @Override |
| 48 | + protected Configuration constructConfiguration(String hbm2DdlOption) { |
| 49 | + final Configuration configuration = super.constructConfiguration( hbm2DdlOption ); |
| 50 | + configuration.setProperty( Settings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, INDIVIDUALLY.toString() ); |
| 51 | + configuration.setProperty( Settings.SHOW_SQL, System.getProperty( Settings.SHOW_SQL, "true" ) ); |
| 52 | + return configuration; |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + public static class GroupedStrategyTest extends SchemaArrayTypesValidationTestBase { |
| 57 | + |
| 58 | + @Override |
| 59 | + protected Configuration constructConfiguration(String hbm2DdlOption) { |
| 60 | + final Configuration configuration = super.constructConfiguration( hbm2DdlOption ); |
| 61 | + configuration.setProperty( Settings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, GROUPED.toString() ); |
| 62 | + configuration.setProperty( Settings.SHOW_SQL, System.getProperty( Settings.SHOW_SQL, "true" ) ); |
| 63 | + return configuration; |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + protected Configuration constructConfiguration(String action) { |
| 68 | + Configuration configuration = super.constructConfiguration(); |
| 69 | + configuration.setProperty( Settings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, INDIVIDUALLY.toString() ); |
| 70 | + configuration.setProperty( Settings.HBM2DDL_AUTO, action ); |
| 71 | + return configuration; |
| 72 | + } |
| 73 | + |
| 74 | + @BeforeEach |
| 75 | + @Override |
| 76 | + public void before(VertxTestContext context) { |
| 77 | + Configuration createConf = constructConfiguration( "create" ); |
| 78 | + createConf.addAnnotatedClass( ArrayTypesTestEntity.class ); |
| 79 | + |
| 80 | + // Make sure that the extra table is not in the db |
| 81 | + Configuration dropConf = constructConfiguration( "drop" ); |
| 82 | + dropConf.addAnnotatedClass( Extra.class ); |
| 83 | + |
| 84 | + test( context, setupSessionFactory( dropConf ) |
| 85 | + .thenCompose( v -> factoryManager.stop() ) |
| 86 | + .thenCompose( v -> setupSessionFactory( createConf ) ) |
| 87 | + .thenCompose( v -> factoryManager.stop() ) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + @AfterEach |
| 92 | + @Override |
| 93 | + public void after(VertxTestContext context) { |
| 94 | + super.after( context ); |
| 95 | + closeFactory( context ); |
| 96 | + } |
| 97 | + |
| 98 | + // When we have created the table, the validation should pass |
| 99 | + @Test |
| 100 | + @Timeout(value = 10, timeUnit = MINUTES) |
| 101 | + public void testValidationSucceeds(VertxTestContext context) { |
| 102 | + Configuration validateConf = constructConfiguration( "validate" ); |
| 103 | + validateConf.addAnnotatedClass( ArrayTypesTestEntity.class ); |
| 104 | + |
| 105 | + StandardServiceRegistryBuilder builder = new StandardServiceRegistryBuilder() |
| 106 | + .applySettings( validateConf.getProperties() ); |
| 107 | + test( context, setupSessionFactory( validateConf ) ); |
| 108 | + } |
| 109 | + |
| 110 | + // Validation should fail if a table is missing |
| 111 | + @Test |
| 112 | + @Timeout(value = 10, timeUnit = MINUTES) |
| 113 | + public void testValidationFails(VertxTestContext context) { |
| 114 | + Configuration validateConf = constructConfiguration( "validate" ); |
| 115 | + validateConf.addAnnotatedClass( ArrayTypesTestEntity.class ); |
| 116 | + // The table mapping this entity shouldn't be in the db |
| 117 | + validateConf.addAnnotatedClass( Extra.class ); |
| 118 | + |
| 119 | + final String errorMessage = "Schema-validation: missing table [" + Extra.TABLE_NAME + "]"; |
| 120 | + test( context, setupSessionFactory( validateConf ) |
| 121 | + .handle( (unused, throwable) -> { |
| 122 | + assertNotNull( throwable ); |
| 123 | + assertEquals( throwable.getClass(), SchemaManagementException.class ); |
| 124 | + assertEquals( throwable.getMessage(), errorMessage ); |
| 125 | + return null; |
| 126 | + } ) |
| 127 | + ); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * An extra entity used for validation, |
| 132 | + * it should not be created at start up |
| 133 | + */ |
| 134 | + @Entity(name = "Extra") |
| 135 | + @Table(name = Extra.TABLE_NAME) |
| 136 | + public static class Extra { |
| 137 | + public static final String TABLE_NAME = "EXTRA_TABLE"; |
| 138 | + @Id |
| 139 | + @GeneratedValue |
| 140 | + private Integer id; |
| 141 | + |
| 142 | + private String description; |
| 143 | + } |
| 144 | +} |
0 commit comments