|
| 1 | +/* |
| 2 | + * Hibernate, Relational Persistence for Idiomatic Java |
| 3 | + * |
| 4 | + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. |
| 5 | + * See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>. |
| 6 | + */ |
| 7 | +package org.hibernate.test.hbm.uk; |
| 8 | + |
| 9 | +import java.util.Collections; |
| 10 | + |
| 11 | +import org.hibernate.boot.Metadata; |
| 12 | +import org.hibernate.boot.MetadataSources; |
| 13 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 14 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 15 | +import org.hibernate.cfg.AvailableSettings; |
| 16 | +import org.hibernate.dialect.Dialect; |
| 17 | +import org.hibernate.dialect.H2Dialect; |
| 18 | +import org.hibernate.dialect.unique.DefaultUniqueDelegate; |
| 19 | +import org.hibernate.dialect.unique.UniqueDelegate; |
| 20 | +import org.hibernate.mapping.Column; |
| 21 | +import org.hibernate.mapping.Table; |
| 22 | +import org.hibernate.mapping.UniqueKey; |
| 23 | +import org.hibernate.tool.schema.spi.SchemaCreator; |
| 24 | +import org.hibernate.tool.schema.spi.SchemaDropper; |
| 25 | +import org.hibernate.tool.schema.spi.SchemaManagementTool; |
| 26 | + |
| 27 | +import org.hibernate.testing.TestForIssue; |
| 28 | +import org.hibernate.testing.junit4.BaseUnitTestCase; |
| 29 | +import org.hibernate.test.hbm.index.JournalingSchemaToolingTarget; |
| 30 | +import org.junit.After; |
| 31 | +import org.junit.Before; |
| 32 | +import org.junit.Test; |
| 33 | + |
| 34 | +import static org.hamcrest.CoreMatchers.equalTo; |
| 35 | +import static org.junit.Assert.assertThat; |
| 36 | + |
| 37 | +/** |
| 38 | + * @author Steve Ebersole |
| 39 | + */ |
| 40 | +public class UniqueDelegateTest extends BaseUnitTestCase { |
| 41 | + private static int getColumnDefinitionUniquenessFragmentCallCount = 0; |
| 42 | + private static int getTableCreationUniqueConstraintsFragmentCallCount = 0; |
| 43 | + private static int getAlterTableToAddUniqueKeyCommandCallCount = 0; |
| 44 | + private static int getAlterTableToDropUniqueKeyCommandCallCount = 0; |
| 45 | + |
| 46 | + private StandardServiceRegistry ssr; |
| 47 | + |
| 48 | + @Before |
| 49 | + public void before() { |
| 50 | + ssr = new StandardServiceRegistryBuilder() |
| 51 | + .applySetting( AvailableSettings.DIALECT, MyDialect.class ) |
| 52 | + .build(); |
| 53 | + } |
| 54 | + |
| 55 | + @After |
| 56 | + public void after() { |
| 57 | + if ( ssr != null ) { |
| 58 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + @TestForIssue( jiraKey = "HHH-10203" ) |
| 64 | + public void testUniqueDelegateConsulted() { |
| 65 | + final Metadata metadata = new MetadataSources( ssr ) |
| 66 | + .addResource( "org/hibernate/test/hbm/uk/person_unique.hbm.xml" ) |
| 67 | + .buildMetadata(); |
| 68 | + |
| 69 | + final JournalingSchemaToolingTarget target = new JournalingSchemaToolingTarget(); |
| 70 | + final SchemaCreator schemaCreator = ssr.getService( SchemaManagementTool.class ).getSchemaCreator( Collections.emptyMap() ); |
| 71 | + schemaCreator.doCreation( metadata, false, target ); |
| 72 | + |
| 73 | + assertThat( getAlterTableToAddUniqueKeyCommandCallCount, equalTo( 1 ) ); |
| 74 | + assertThat( getColumnDefinitionUniquenessFragmentCallCount, equalTo( 1 ) ); |
| 75 | + assertThat( getTableCreationUniqueConstraintsFragmentCallCount, equalTo( 0 ) ); |
| 76 | + |
| 77 | + final SchemaDropper schemaDropper = ssr.getService( SchemaManagementTool.class ).getSchemaDropper( Collections.emptyMap() ); |
| 78 | + schemaDropper.doDrop( metadata, false, target ); |
| 79 | + |
| 80 | + // unique keys are not dropped explicitly |
| 81 | + assertThat( getAlterTableToAddUniqueKeyCommandCallCount, equalTo( 1 ) ); |
| 82 | + assertThat( getColumnDefinitionUniquenessFragmentCallCount, equalTo( 1 ) ); |
| 83 | + assertThat( getTableCreationUniqueConstraintsFragmentCallCount, equalTo( 0 ) ); |
| 84 | + } |
| 85 | + |
| 86 | + public static class MyDialect extends H2Dialect { |
| 87 | + private MyUniqueDelegate myUniqueDelegate; |
| 88 | + |
| 89 | + public MyDialect() { |
| 90 | + this.myUniqueDelegate = new MyUniqueDelegate( this ); |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public UniqueDelegate getUniqueDelegate() { |
| 95 | + return myUniqueDelegate; |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + public static class MyUniqueDelegate extends DefaultUniqueDelegate { |
| 100 | + |
| 101 | + /** |
| 102 | + * Constructs DefaultUniqueDelegate |
| 103 | + * |
| 104 | + * @param dialect The dialect for which we are handling unique constraints |
| 105 | + */ |
| 106 | + public MyUniqueDelegate(Dialect dialect) { |
| 107 | + super( dialect ); |
| 108 | + } |
| 109 | + |
| 110 | + @Override |
| 111 | + public String getColumnDefinitionUniquenessFragment(Column column) { |
| 112 | + getColumnDefinitionUniquenessFragmentCallCount++; |
| 113 | + return super.getColumnDefinitionUniquenessFragment( column ); |
| 114 | + } |
| 115 | + |
| 116 | + @Override |
| 117 | + public String getTableCreationUniqueConstraintsFragment(Table table) { |
| 118 | + getTableCreationUniqueConstraintsFragmentCallCount++; |
| 119 | + return super.getTableCreationUniqueConstraintsFragment( table ); |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public String getAlterTableToAddUniqueKeyCommand( |
| 124 | + UniqueKey uniqueKey, Metadata metadata) { |
| 125 | + getAlterTableToAddUniqueKeyCommandCallCount++; |
| 126 | + return super.getAlterTableToAddUniqueKeyCommand( uniqueKey, metadata ); |
| 127 | + } |
| 128 | + |
| 129 | + @Override |
| 130 | + public String getAlterTableToDropUniqueKeyCommand( |
| 131 | + UniqueKey uniqueKey, Metadata metadata) { |
| 132 | + getAlterTableToDropUniqueKeyCommandCallCount++; |
| 133 | + return super.getAlterTableToDropUniqueKeyCommand( uniqueKey, metadata ); |
| 134 | + } |
| 135 | + } |
| 136 | +} |
0 commit comments