|
4 | 4 | */ |
5 | 5 | package org.hibernate.orm.test.schematools; |
6 | 6 |
|
7 | | -import java.lang.annotation.RetentionPolicy; |
8 | | -import java.util.ArrayList; |
9 | | -import java.util.EnumSet; |
10 | | -import java.util.List; |
11 | | -import java.util.Map; |
12 | | - |
13 | | -import org.hibernate.boot.Metadata; |
14 | | -import org.hibernate.boot.MetadataSources; |
| 7 | +import jakarta.persistence.Basic; |
| 8 | +import jakarta.persistence.Column; |
| 9 | +import jakarta.persistence.Entity; |
| 10 | +import jakarta.persistence.EnumType; |
| 11 | +import jakarta.persistence.Enumerated; |
| 12 | +import jakarta.persistence.Id; |
| 13 | +import jakarta.persistence.Table; |
15 | 14 | import org.hibernate.boot.registry.StandardServiceRegistry; |
16 | | -import org.hibernate.cfg.AvailableSettings; |
| 15 | +import org.hibernate.boot.spi.MetadataImplementor; |
| 16 | +import org.hibernate.cfg.JdbcSettings; |
| 17 | +import org.hibernate.dialect.H2Dialect; |
17 | 18 | import org.hibernate.dialect.MySQLDialect; |
18 | 19 | import org.hibernate.engine.config.spi.ConfigurationService; |
19 | 20 | import org.hibernate.orm.test.tool.schema.ExecutionOptionsTestImpl; |
20 | 21 | import org.hibernate.service.spi.ServiceRegistryImplementor; |
| 22 | +import org.hibernate.testing.orm.junit.BaseUnitTest; |
| 23 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 24 | +import org.hibernate.testing.orm.junit.DomainModelScope; |
| 25 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 26 | +import org.hibernate.testing.orm.junit.RequiresDialect; |
| 27 | +import org.hibernate.testing.orm.junit.ServiceRegistry; |
| 28 | +import org.hibernate.testing.orm.junit.ServiceRegistryScope; |
| 29 | +import org.hibernate.testing.orm.junit.SettingProvider; |
21 | 30 | import org.hibernate.tool.schema.SourceType; |
22 | 31 | import org.hibernate.tool.schema.TargetType; |
23 | 32 | import org.hibernate.tool.schema.internal.HibernateSchemaManagementTool; |
24 | | -import org.hibernate.tool.schema.spi.GenerationTarget; |
25 | 33 | import org.hibernate.tool.schema.internal.exec.JdbcContext; |
| 34 | +import org.hibernate.tool.schema.spi.GenerationTarget; |
26 | 35 | import org.hibernate.tool.schema.spi.SchemaCreator; |
27 | 36 | import org.hibernate.tool.schema.spi.SchemaManagementTool; |
28 | 37 | import org.hibernate.tool.schema.spi.ScriptSourceInput; |
29 | 38 | import org.hibernate.tool.schema.spi.ScriptTargetOutput; |
30 | 39 | import org.hibernate.tool.schema.spi.SourceDescriptor; |
31 | 40 | import org.hibernate.tool.schema.spi.TargetDescriptor; |
32 | | - |
33 | | -import org.hibernate.testing.orm.junit.JiraKey; |
34 | | -import org.hibernate.testing.orm.junit.ServiceRegistryScope; |
35 | | -import org.hibernate.testing.util.ServiceRegistryUtil; |
36 | 41 | import org.junit.jupiter.api.Test; |
37 | 42 |
|
38 | | -import jakarta.persistence.Basic; |
39 | | -import jakarta.persistence.Column; |
40 | | -import jakarta.persistence.Entity; |
41 | | -import jakarta.persistence.EnumType; |
42 | | -import jakarta.persistence.Enumerated; |
43 | | -import jakarta.persistence.Id; |
44 | | -import jakarta.persistence.Table; |
| 43 | +import java.lang.annotation.RetentionPolicy; |
| 44 | +import java.util.ArrayList; |
| 45 | +import java.util.EnumSet; |
| 46 | +import java.util.List; |
| 47 | +import java.util.Map; |
45 | 48 |
|
46 | 49 | import static org.assertj.core.api.Assertions.assertThat; |
47 | 50 |
|
| 51 | +@SuppressWarnings("JUnitMalformedDeclaration") |
48 | 52 | @JiraKey("HHH-16670") |
| 53 | +@BaseUnitTest |
| 54 | +@ServiceRegistry( |
| 55 | + settingProviders = @SettingProvider( |
| 56 | + settingName = JdbcSettings.DIALECT, |
| 57 | + provider = EnumCheckColumnDefinitionTests.CustomDialectConfigProvider.class |
| 58 | + ) |
| 59 | +) |
| 60 | +@DomainModel( annotatedClasses = EnumCheckColumnDefinitionTests.SimpleEntity.class ) |
| 61 | +@RequiresDialect( |
| 62 | + value = H2Dialect.class, |
| 63 | + comment = "Even though we use specialized Dialect, we still have calls happening to the " |
| 64 | + + "underlying driver which will blow up on various underlying drivers. Nothing here is " |
| 65 | + + "Dialect-specific anyway, besides what the specialized Dialect exposes." |
| 66 | +) |
49 | 67 | public class EnumCheckColumnDefinitionTests { |
50 | 68 | @Test |
51 | | - public void testFallbackToolIsPickedUp() { |
52 | | - ServiceRegistryScope.using( |
53 | | - () -> { |
54 | | - return ServiceRegistryUtil.serviceRegistryBuilder() |
55 | | - .applySetting( AvailableSettings.DIALECT, CustomDialect.class.getName() ) |
56 | | - .build(); |
| 69 | + public void testFallbackToolIsPickedUp(ServiceRegistryScope registryScope, DomainModelScope modelScope) { |
| 70 | + final StandardServiceRegistry registry = registryScope.getRegistry(); |
| 71 | + final MetadataImplementor domainModel = modelScope.getDomainModel(); |
| 72 | + |
| 73 | + final HibernateSchemaManagementTool tool = (HibernateSchemaManagementTool) registry.getService( SchemaManagementTool.class ); |
| 74 | + final Map<String, Object> settings = registry.getService( ConfigurationService.class ).getSettings(); |
| 75 | + final SchemaCreator schemaCreator = tool.getSchemaCreator( settings ); |
| 76 | + schemaCreator.doCreation( |
| 77 | + domainModel, |
| 78 | + new ExecutionOptionsTestImpl(), |
| 79 | + contributed -> true, |
| 80 | + new SourceDescriptor() { |
| 81 | + @Override |
| 82 | + public SourceType getSourceType() { |
| 83 | + return SourceType.METADATA; |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public ScriptSourceInput getScriptSourceInput() { |
| 88 | + return null; |
| 89 | + } |
57 | 90 | }, |
58 | | - (registryScope) -> { |
59 | | - final StandardServiceRegistry registry = registryScope.getRegistry(); |
60 | | - final Metadata metadata = new MetadataSources( registry ) |
61 | | - .addAnnotatedClass( SimpleEntity.class ) |
62 | | - .buildMetadata(); |
63 | | - |
64 | | - final HibernateSchemaManagementTool tool = (HibernateSchemaManagementTool) registry.getService( SchemaManagementTool.class ); |
65 | | - final Map<String, Object> settings = registry.getService( ConfigurationService.class ).getSettings(); |
66 | | - final SchemaCreator schemaCreator = tool.getSchemaCreator( settings ); |
67 | | - schemaCreator.doCreation( |
68 | | - metadata, |
69 | | - new ExecutionOptionsTestImpl(), |
70 | | - contributed -> true, |
71 | | - new SourceDescriptor() { |
72 | | - @Override |
73 | | - public SourceType getSourceType() { |
74 | | - return SourceType.METADATA; |
75 | | - } |
76 | | - |
77 | | - @Override |
78 | | - public ScriptSourceInput getScriptSourceInput() { |
79 | | - return null; |
80 | | - } |
81 | | - }, |
82 | | - new TargetDescriptor() { |
83 | | - @Override |
84 | | - public EnumSet<TargetType> getTargetTypes() { |
85 | | - return EnumSet.of( TargetType.DATABASE ); |
86 | | - } |
87 | | - |
88 | | - @Override |
89 | | - public ScriptTargetOutput getScriptTargetOutput() { |
90 | | - return null; |
91 | | - } |
92 | | - } |
93 | | - ); |
94 | | - |
95 | | - assertThat( CollectingGenerationTarget.commands.get( 0) ).contains( "enum ('SOURCE','CLASS','RUNTIME')"); |
| 91 | + new TargetDescriptor() { |
| 92 | + @Override |
| 93 | + public EnumSet<TargetType> getTargetTypes() { |
| 94 | + return EnumSet.of( TargetType.DATABASE ); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public ScriptTargetOutput getScriptTargetOutput() { |
| 99 | + return null; |
| 100 | + } |
96 | 101 | } |
97 | 102 | ); |
| 103 | + |
| 104 | + assertThat( CollectingGenerationTarget.commands.get( 0) ).contains( "enum ('SOURCE','CLASS','RUNTIME')"); |
98 | 105 | } |
99 | 106 |
|
100 | 107 | private static class CollectingGenerationTarget implements GenerationTarget { |
@@ -125,6 +132,13 @@ protected GenerationTarget buildDatabaseTarget(JdbcContext jdbcContext, boolean |
125 | 132 | } |
126 | 133 | } |
127 | 134 |
|
| 135 | + public static class CustomDialectConfigProvider implements SettingProvider.Provider<Class<CustomDialect>> { |
| 136 | + @Override |
| 137 | + public Class<CustomDialect> getSetting() { |
| 138 | + return CustomDialect.class; |
| 139 | + } |
| 140 | + } |
| 141 | + |
128 | 142 | public static class CustomDialect extends MySQLDialect { |
129 | 143 | @Override |
130 | 144 | public SchemaManagementTool getFallbackSchemaManagementTool(Map<String, Object> configurationValues, ServiceRegistryImplementor registry) { |
|
0 commit comments