|
4 | 4 | */
|
5 | 5 | package org.hibernate.tool.schema.extract.spi;
|
6 | 6 |
|
7 |
| -import org.hibernate.boot.model.relational.QualifiedTableName; |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import org.hibernate.boot.Metadata; |
| 10 | +import org.hibernate.boot.MetadataSources; |
| 11 | +import org.hibernate.boot.model.naming.Identifier; |
| 12 | +import org.hibernate.boot.model.relational.Database; |
| 13 | +import org.hibernate.boot.model.relational.Namespace; |
| 14 | +import org.hibernate.boot.model.relational.SqlStringGenerationContext; |
| 15 | +import org.hibernate.boot.model.relational.internal.SqlStringGenerationContextImpl; |
8 | 16 | import org.hibernate.boot.registry.StandardServiceRegistry;
|
| 17 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 18 | +import org.hibernate.cfg.AvailableSettings; |
| 19 | +import org.hibernate.dialect.H2Dialect; |
| 20 | +import org.hibernate.dialect.OracleDialect; |
| 21 | +import org.hibernate.engine.jdbc.connections.spi.ConnectionProvider; |
| 22 | +import org.hibernate.engine.jdbc.env.internal.JdbcEnvironmentInitiator; |
| 23 | +import org.hibernate.engine.jdbc.spi.JdbcServices; |
9 | 24 | import org.hibernate.mapping.Table;
|
| 25 | +import org.hibernate.orm.test.util.DdlTransactionIsolatorTestingImpl; |
| 26 | +import org.hibernate.resource.transaction.spi.DdlTransactionIsolator; |
| 27 | +import org.hibernate.testing.orm.junit.BaseSessionFactoryFunctionalTest; |
| 28 | +import org.hibernate.testing.orm.junit.DomainModel; |
10 | 29 | import org.hibernate.testing.orm.junit.JiraKey;
|
| 30 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 31 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 32 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
11 | 33 | import org.hibernate.testing.util.ServiceRegistryUtil;
|
12 |
| -import org.junit.Assert; |
| 34 | +import org.hibernate.tool.schema.JdbcMetadaAccessStrategy; |
| 35 | +import org.hibernate.tool.schema.extract.internal.DatabaseInformationImpl; |
| 36 | +import org.hibernate.tool.schema.extract.internal.ExtractionContextImpl; |
| 37 | +import org.hibernate.tool.schema.internal.HibernateSchemaManagementTool; |
| 38 | +import org.hibernate.tool.schema.spi.ExtractionTool; |
| 39 | +import org.hibernate.tool.schema.spi.SchemaManagementTool; |
| 40 | +import org.junit.jupiter.api.AfterEach; |
| 41 | +import org.junit.jupiter.api.BeforeEach; |
13 | 42 | import org.junit.jupiter.api.Test;
|
14 |
| -import org.hibernate.engine.jdbc.env.spi.JdbcEnvironment; |
15 |
| -import org.hibernate.engine.jdbc.env.spi.IdentifierHelper; |
16 |
| -import org.hibernate.tool.schema.extract.internal.TableInformationImpl; |
17 |
| -import org.hibernate.boot.model.relational.Namespace.Name; |
18 |
| -import org.hibernate.boot.model.naming.Identifier; |
19 |
| -import org.mockito.Mockito; |
20 | 43 |
|
| 44 | +import java.util.Objects; |
| 45 | + |
| 46 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 47 | +import static org.hamcrest.core.Is.is; |
| 48 | + |
| 49 | +@RequiresDialect(H2Dialect.class) |
| 50 | +@RequiresDialect(OracleDialect.class) |
| 51 | +@DomainModel( |
| 52 | + annotatedClasses = { |
| 53 | + NameSpaceTablesInformationTest.TestEntity.class |
| 54 | + } |
| 55 | +) |
| 56 | +@SessionFactory( |
| 57 | + exportSchema = false |
| 58 | +) |
21 | 59 |
|
22 |
| -public class NameSpaceTablesInformationTest { |
| 60 | +public class NameSpaceTablesInformationTest extends BaseSessionFactoryFunctionalTest { |
| 61 | + |
| 62 | + @BeforeEach |
| 63 | + public void setUp(SessionFactoryScope scope) { |
| 64 | + scope.inTransaction( |
| 65 | + session -> |
| 66 | + session.createNativeQuery( |
| 67 | + "create table TEST_TABLE ( Field1 int, Field2 int NOT NULL, Field3 int NOT NULL)" ) |
| 68 | + .executeUpdate() |
| 69 | + ); |
| 70 | + } |
| 71 | + |
| 72 | + @AfterEach |
| 73 | + public void tearDown(SessionFactoryScope scope) { |
| 74 | + scope.inTransaction( |
| 75 | + session -> |
| 76 | + session.createNativeQuery( "drop table TEST_TABLE" ).executeUpdate() |
| 77 | + ); |
| 78 | + } |
23 | 79 |
|
24 | 80 | @Test
|
25 | 81 | @JiraKey(value = "HHH-14270")
|
26 |
| - public void testNameSpaceTablesInformation() { |
27 |
| - StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistry(); |
28 |
| - JdbcEnvironment jdbcEnvironment = ssr.getService( JdbcEnvironment.class ); |
29 |
| - IdentifierHelper identifierHelper = jdbcEnvironment.getIdentifierHelper(); |
30 |
| - |
31 |
| - NameSpaceTablesInformation nameSpaceTablesInformation = new NameSpaceTablesInformation(identifierHelper); |
32 |
| - Name schemaName = new Name( new Identifier( "-", false ), new Identifier( "-", false ) ); |
33 |
| - InformationExtractor informationExtractor = Mockito.mock( InformationExtractor.class ); |
34 |
| - QualifiedTableName tableName = new QualifiedTableName( schemaName, new Identifier( "-", false ) ); |
35 |
| - |
36 |
| - TableInformation tableInformation = new TableInformationImpl( informationExtractor, identifierHelper, tableName, false, null ); |
37 |
| - nameSpaceTablesInformation.addTableInformation( tableInformation ); |
38 |
| - final Table table = new Table( "orm", tableName.getTableName().getText() ); |
39 |
| - nameSpaceTablesInformation.getTableInformation( table ); |
40 |
| - boolean tableMatched = tableInformation.getName().getTableName().getText().equals( tableName.getTableName().getText() ); |
41 |
| - Assert.assertTrue("Table matched: ", tableMatched); |
| 82 | + public void testNameSpaceTablesInformation() throws Exception { |
| 83 | + StandardServiceRegistry ssr = ServiceRegistryUtil.serviceRegistryBuilder() |
| 84 | + .applySetting( |
| 85 | + AvailableSettings.HBM2DDL_JDBC_METADATA_EXTRACTOR_STRATEGY, |
| 86 | + JdbcMetadaAccessStrategy.GROUPED |
| 87 | + ) |
| 88 | + .build(); |
| 89 | + DdlTransactionIsolator ddlTransactionIsolator = null; |
| 90 | + ExtractionContextImpl extractionContext = null; |
| 91 | + |
| 92 | + final MetadataSources metadataSources = new MetadataSources( ssr ); |
| 93 | + metadataSources.addAnnotatedClass( TestEntity.class ); |
| 94 | + final Metadata metadata = metadataSources.buildMetadata(); |
| 95 | + final NameSpaceTablesInformation tablesInformation = new NameSpaceTablesInformation( metadata.getDatabase().getJdbcEnvironment().getIdentifierHelper() ); |
| 96 | + |
| 97 | + try { |
| 98 | + ddlTransactionIsolator = buildDdlTransactionIsolator( ssr ); |
| 99 | + extractionContext = buildContext( ssr, ddlTransactionIsolator ); |
| 100 | + TableInformation tableInformation = buildInformationExtractor( extractionContext ).getTable( |
| 101 | + null, |
| 102 | + null, |
| 103 | + new Identifier( "TEST_TABLE", false ) |
| 104 | + ); |
| 105 | + tablesInformation.addTableInformation( tableInformation ); |
| 106 | + Iterable<Namespace> namespaces = metadata.getDatabase().getNamespaces(); |
| 107 | + |
| 108 | + boolean testTableFound = false; |
| 109 | + for ( Namespace namespace : namespaces ) { |
| 110 | + for ( Table table : namespace.getTables() ) { |
| 111 | + final TableInformation tableInfo = tablesInformation.getTableInformation( table ); |
| 112 | + if ( tableInfo != null ) { |
| 113 | + testTableFound = Objects.equals( tableInfo.getName().getTableName().getText(), "TEST_TABLE" ); |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + assertThat("TEST_TABLE is existing in DB.", |
| 119 | + testTableFound, |
| 120 | + is(true)); |
| 121 | + } |
| 122 | + finally { |
| 123 | + if ( extractionContext != null ) { |
| 124 | + extractionContext.cleanup(); |
| 125 | + } |
| 126 | + if ( ddlTransactionIsolator != null ) { |
| 127 | + ddlTransactionIsolator.release(); |
| 128 | + } |
| 129 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 130 | + } |
| 131 | + } |
| 132 | + |
| 133 | + private InformationExtractor buildInformationExtractor(ExtractionContextImpl extractionContext) throws Exception { |
| 134 | + ExtractionTool extractionTool = new HibernateSchemaManagementTool().getExtractionTool(); |
| 135 | + |
| 136 | + return extractionTool.createInformationExtractor( extractionContext ); |
| 137 | + } |
| 138 | + |
| 139 | + private static ExtractionContextImpl buildContext( |
| 140 | + StandardServiceRegistry ssr, |
| 141 | + DdlTransactionIsolator ddlTransactionIsolator) throws Exception { |
| 142 | + Database database = new MetadataSources( ssr ).buildMetadata().getDatabase(); |
| 143 | + |
| 144 | + SqlStringGenerationContext sqlStringGenerationContext = SqlStringGenerationContextImpl.forTests( database.getJdbcEnvironment() ); |
| 145 | + |
| 146 | + DatabaseInformation dbInfo = buildDatabaseInformation( |
| 147 | + ssr, |
| 148 | + database, |
| 149 | + sqlStringGenerationContext, |
| 150 | + ddlTransactionIsolator |
| 151 | + ); |
| 152 | + |
| 153 | + return new ExtractionContextImpl( |
| 154 | + ssr, |
| 155 | + database.getJdbcEnvironment(), |
| 156 | + sqlStringGenerationContext, |
| 157 | + ssr.getService( JdbcServices.class ).getBootstrapJdbcConnectionAccess(), |
| 158 | + (ExtractionContext.DatabaseObjectAccess) dbInfo |
| 159 | + ); |
| 160 | + } |
| 161 | + |
| 162 | + private static DatabaseInformationImpl buildDatabaseInformation( |
| 163 | + StandardServiceRegistry ssr, |
| 164 | + Database database, |
| 165 | + SqlStringGenerationContext sqlStringGenerationContext, |
| 166 | + DdlTransactionIsolator ddlTransactionIsolator) throws Exception { |
| 167 | + return new DatabaseInformationImpl( |
| 168 | + ssr, |
| 169 | + database.getJdbcEnvironment(), |
| 170 | + sqlStringGenerationContext, |
| 171 | + ddlTransactionIsolator, |
| 172 | + database.getServiceRegistry().getService( SchemaManagementTool.class ) |
| 173 | + ); |
| 174 | + } |
| 175 | + |
| 176 | + private static DdlTransactionIsolator buildDdlTransactionIsolator(StandardServiceRegistry ssr) { |
| 177 | + final ConnectionProvider connectionProvider = ssr.getService( ConnectionProvider.class ); |
| 178 | + return new DdlTransactionIsolatorTestingImpl( |
| 179 | + ssr, |
| 180 | + new JdbcEnvironmentInitiator.ConnectionProviderJdbcConnectionAccess( connectionProvider ) |
| 181 | + ); |
| 182 | + } |
| 183 | + |
| 184 | + @Entity |
| 185 | + @jakarta.persistence.Table(name = "TEST_TABLE") |
| 186 | + public static class TestEntity { |
| 187 | + |
| 188 | + @Id |
| 189 | + private Long id; |
| 190 | + |
| 191 | + public void setId(Long id) { |
| 192 | + this.id = id; |
| 193 | + } |
| 194 | + |
| 195 | + public Long getId() { |
| 196 | + return id; |
| 197 | + } |
42 | 198 | }
|
43 | 199 | }
|
0 commit comments