|
| 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.schemavalidation; |
| 8 | + |
| 9 | +import java.util.EnumSet; |
| 10 | +import java.util.Map; |
| 11 | +import javax.persistence.Entity; |
| 12 | +import javax.persistence.Id; |
| 13 | + |
| 14 | +import org.hibernate.annotations.Type; |
| 15 | +import org.hibernate.boot.MetadataSources; |
| 16 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 17 | +import org.hibernate.boot.registry.StandardServiceRegistryBuilder; |
| 18 | +import org.hibernate.boot.spi.MetadataImplementor; |
| 19 | +import org.hibernate.engine.config.spi.ConfigurationService; |
| 20 | +import org.hibernate.tool.schema.SourceType; |
| 21 | +import org.hibernate.tool.schema.TargetType; |
| 22 | +import org.hibernate.tool.schema.internal.ExceptionHandlerLoggedImpl; |
| 23 | +import org.hibernate.tool.schema.spi.ExceptionHandler; |
| 24 | +import org.hibernate.tool.schema.spi.ExecutionOptions; |
| 25 | +import org.hibernate.tool.schema.spi.SchemaManagementTool; |
| 26 | +import org.hibernate.tool.schema.spi.ScriptSourceInput; |
| 27 | +import org.hibernate.tool.schema.spi.ScriptTargetOutput; |
| 28 | +import org.hibernate.tool.schema.spi.SourceDescriptor; |
| 29 | +import org.hibernate.tool.schema.spi.TargetDescriptor; |
| 30 | + |
| 31 | +import org.hibernate.testing.FailureExpected; |
| 32 | +import org.hibernate.testing.TestForIssue; |
| 33 | +import org.hibernate.testing.junit4.BaseUnitTestCase; |
| 34 | +import org.junit.After; |
| 35 | +import org.junit.Before; |
| 36 | +import org.junit.Test; |
| 37 | + |
| 38 | +/** |
| 39 | + * @author Steve Ebersole |
| 40 | + */ |
| 41 | +@TestForIssue( jiraKey = "HHH-9693" ) |
| 42 | +public class LongVarcharValidationTest extends BaseUnitTestCase implements ExecutionOptions { |
| 43 | + private StandardServiceRegistry ssr; |
| 44 | + |
| 45 | + @Before |
| 46 | + public void beforeTest() { |
| 47 | + ssr = new StandardServiceRegistryBuilder().build(); |
| 48 | + } |
| 49 | + |
| 50 | + @After |
| 51 | + public void afterTest() { |
| 52 | + if ( ssr != null ) { |
| 53 | + StandardServiceRegistryBuilder.destroy( ssr ); |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void testValidation() { |
| 59 | + MetadataImplementor metadata = (MetadataImplementor) new MetadataSources( ssr ) |
| 60 | + .addAnnotatedClass( Translation.class ) |
| 61 | + .buildMetadata(); |
| 62 | + metadata.validate(); |
| 63 | + |
| 64 | + |
| 65 | + // create the schema |
| 66 | + createSchema( metadata ); |
| 67 | + |
| 68 | + try { |
| 69 | + doValidation( metadata ); |
| 70 | + } |
| 71 | + finally { |
| 72 | + dropSchema( metadata ); |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + private void doValidation(MetadataImplementor metadata) { |
| 77 | + ssr.getService( SchemaManagementTool.class ).getSchemaValidator( null ).doValidation( |
| 78 | + metadata, |
| 79 | + this |
| 80 | + ); |
| 81 | + } |
| 82 | + |
| 83 | + private void createSchema(MetadataImplementor metadata) { |
| 84 | + ssr.getService( SchemaManagementTool.class ).getSchemaCreator( null ).doCreation( |
| 85 | + metadata, |
| 86 | + this, |
| 87 | + new SourceDescriptor() { |
| 88 | + @Override |
| 89 | + public SourceType getSourceType() { |
| 90 | + return SourceType.METADATA; |
| 91 | + } |
| 92 | + |
| 93 | + @Override |
| 94 | + public ScriptSourceInput getScriptSourceInput() { |
| 95 | + return null; |
| 96 | + } |
| 97 | + }, |
| 98 | + new TargetDescriptor() { |
| 99 | + @Override |
| 100 | + public EnumSet<TargetType> getTargetTypes() { |
| 101 | + return EnumSet.of( TargetType.DATABASE ); |
| 102 | + } |
| 103 | + |
| 104 | + @Override |
| 105 | + public ScriptTargetOutput getScriptTargetOutput() { |
| 106 | + return null; |
| 107 | + } |
| 108 | + } |
| 109 | + ); |
| 110 | + } |
| 111 | + |
| 112 | + private void dropSchema(MetadataImplementor metadata) { |
| 113 | + ssr.getService( SchemaManagementTool.class ).getSchemaDropper( null ).doDrop( |
| 114 | + metadata, |
| 115 | + this, |
| 116 | + new SourceDescriptor() { |
| 117 | + @Override |
| 118 | + public SourceType getSourceType() { |
| 119 | + return SourceType.METADATA; |
| 120 | + } |
| 121 | + |
| 122 | + @Override |
| 123 | + public ScriptSourceInput getScriptSourceInput() { |
| 124 | + return null; |
| 125 | + } |
| 126 | + }, |
| 127 | + new TargetDescriptor() { |
| 128 | + @Override |
| 129 | + public EnumSet<TargetType> getTargetTypes() { |
| 130 | + return EnumSet.of( TargetType.DATABASE ); |
| 131 | + } |
| 132 | + |
| 133 | + @Override |
| 134 | + public ScriptTargetOutput getScriptTargetOutput() { |
| 135 | + return null; |
| 136 | + } |
| 137 | + } |
| 138 | + ); |
| 139 | + } |
| 140 | + |
| 141 | + @Entity |
| 142 | + public static class Translation { |
| 143 | + @Id |
| 144 | + public Integer id; |
| 145 | + @Type( type = "text" ) |
| 146 | + String text; |
| 147 | + } |
| 148 | + |
| 149 | + @Override |
| 150 | + public Map getConfigurationValues() { |
| 151 | + return ssr.getService( ConfigurationService.class ).getSettings(); |
| 152 | + } |
| 153 | + |
| 154 | + @Override |
| 155 | + public boolean shouldManageNamespaces() { |
| 156 | + return false; |
| 157 | + } |
| 158 | + |
| 159 | + @Override |
| 160 | + public ExceptionHandler getExceptionHandler() { |
| 161 | + return ExceptionHandlerLoggedImpl.INSTANCE; |
| 162 | + } |
| 163 | +} |
0 commit comments