|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: LGPL-2.1-or-later |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.tool.schema; |
| 6 | + |
| 7 | +import jakarta.persistence.Entity; |
| 8 | +import jakarta.persistence.Id; |
| 9 | +import jakarta.persistence.Table; |
| 10 | +import org.hibernate.boot.Metadata; |
| 11 | +import org.hibernate.boot.MetadataSources; |
| 12 | +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; |
| 13 | +import org.hibernate.boot.registry.StandardServiceRegistry; |
| 14 | +import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; |
| 15 | +import org.hibernate.bytecode.enhance.spi.Enhancer; |
| 16 | +import org.hibernate.testing.util.ServiceRegistryUtil; |
| 17 | +import org.hibernate.tool.schema.SourceType; |
| 18 | +import org.hibernate.tool.schema.TargetType; |
| 19 | +import org.hibernate.tool.schema.internal.SchemaCreatorImpl; |
| 20 | +import org.hibernate.tool.schema.spi.CommandAcceptanceException; |
| 21 | +import org.hibernate.tool.schema.spi.ContributableMatcher; |
| 22 | +import org.hibernate.tool.schema.spi.ExceptionHandler; |
| 23 | +import org.hibernate.tool.schema.spi.ExecutionOptions; |
| 24 | +import org.hibernate.tool.schema.spi.ScriptSourceInput; |
| 25 | +import org.hibernate.tool.schema.spi.ScriptTargetOutput; |
| 26 | +import org.hibernate.tool.schema.spi.SourceDescriptor; |
| 27 | +import org.hibernate.tool.schema.spi.TargetDescriptor; |
| 28 | +import org.junit.jupiter.api.AfterEach; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | + |
| 32 | +import java.io.File; |
| 33 | +import java.io.FileWriter; |
| 34 | +import java.io.IOException; |
| 35 | +import java.net.MalformedURLException; |
| 36 | +import java.net.URI; |
| 37 | +import java.net.URL; |
| 38 | +import java.net.URLClassLoader; |
| 39 | +import java.nio.file.Files; |
| 40 | +import java.nio.file.Path; |
| 41 | +import java.util.ArrayList; |
| 42 | +import java.util.EnumSet; |
| 43 | +import java.util.HashMap; |
| 44 | +import java.util.List; |
| 45 | +import java.util.Map; |
| 46 | + |
| 47 | +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; |
| 48 | +import static org.hibernate.cfg.SchemaToolingSettings.HBM2DDL_SKIP_DEFAULT_IMPORT_FILE; |
| 49 | +import static org.hibernate.tool.schema.internal.SchemaCreatorImpl.DEFAULT_IMPORT_FILE; |
| 50 | + |
| 51 | +public class SkipDefaultImportFileExecutionTest { |
| 52 | + private File defaultImportFile; |
| 53 | + private StandardServiceRegistry serviceRegistry; |
| 54 | + private static final String COMMAND = "INSERT INTO TEST_ENTITY (id, name) values (1,'name')"; |
| 55 | + |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + public void setUp() throws Exception { |
| 59 | + defaultImportFile = createDefaultImportFile( "import.sql" ); |
| 60 | + serviceRegistry = ServiceRegistryUtil.serviceRegistryBuilder( |
| 61 | + new BootstrapServiceRegistryBuilder().applyClassLoader( |
| 62 | + toClassLoader( defaultImportFile.getParentFile() ) ).build() |
| 63 | + ).build(); |
| 64 | + } |
| 65 | + |
| 66 | + @AfterEach |
| 67 | + public void tearDown() { |
| 68 | + serviceRegistry.close(); |
| 69 | + if ( defaultImportFile.exists() ) { |
| 70 | + try { |
| 71 | + Files.delete( defaultImportFile.toPath() ); |
| 72 | + } |
| 73 | + catch (IOException e) { |
| 74 | + throw new RuntimeException( e ); |
| 75 | + } |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testImportScriptIsNotExecutedWhitSkipDefaultImportFileSetting() { |
| 81 | + assertThat( serviceRegistry.getService( ClassLoaderService.class ) |
| 82 | + .locateResource( DEFAULT_IMPORT_FILE ) ).isNotNull(); |
| 83 | + |
| 84 | + TargetDescriptorImpl targetDescriptor = TargetDescriptorImpl.INSTANCE; |
| 85 | + |
| 86 | + createSchema( targetDescriptor ); |
| 87 | + |
| 88 | + TestScriptTargetOutput targetOutput = (TestScriptTargetOutput) targetDescriptor.getScriptTargetOutput(); |
| 89 | + |
| 90 | + assertNoInsertCommandsFromDeaultImportFileHasBeenExecuted( targetOutput ); |
| 91 | + } |
| 92 | + |
| 93 | + private static void assertNoInsertCommandsFromDeaultImportFileHasBeenExecuted(TestScriptTargetOutput targetOutput) { |
| 94 | + assertThat( targetOutput.getInsertCommands().size() ).isEqualTo( 0 ) |
| 95 | + .as( "Despite setting hibernate.hbm2ddl.skip_default_import_file to true the default import.sql file insert command hs been executed" ); |
| 96 | + } |
| 97 | + |
| 98 | + private void createSchema(TargetDescriptorImpl targetDescriptor) { |
| 99 | + |
| 100 | + final Metadata mappings = buildMappings( serviceRegistry ); |
| 101 | + |
| 102 | + final SchemaCreatorImpl schemaCreator = new SchemaCreatorImpl( serviceRegistry ); |
| 103 | + |
| 104 | + final Map<String, Object> options = new HashMap<>(); |
| 105 | + options.put( HBM2DDL_SKIP_DEFAULT_IMPORT_FILE, "true" ); |
| 106 | + schemaCreator.doCreation( |
| 107 | + mappings, |
| 108 | + new ExecutionOptionsTestImpl( options ), |
| 109 | + ContributableMatcher.ALL, |
| 110 | + SourceDescriptorImpl.INSTANCE, |
| 111 | + targetDescriptor |
| 112 | + ); |
| 113 | + } |
| 114 | + |
| 115 | + private static File createDefaultImportFile(@SuppressWarnings("SameParameterValue") String fileName) |
| 116 | + throws Exception { |
| 117 | + final Path tmp = Files.createTempDirectory( "default_import" ); |
| 118 | + final File file = new File( tmp.toString() + File.separator + fileName ); |
| 119 | + |
| 120 | + try (final FileWriter myWriter = new FileWriter( file )) { |
| 121 | + myWriter.write( COMMAND ); |
| 122 | + } |
| 123 | + |
| 124 | + return file; |
| 125 | + } |
| 126 | + |
| 127 | + private static ClassLoader toClassLoader(File classesDir) { |
| 128 | + final URI classesDirUri = classesDir.toURI(); |
| 129 | + try { |
| 130 | + final URL url = classesDirUri.toURL(); |
| 131 | + return new URLClassLoader( new URL[] {url}, Enhancer.class.getClassLoader() ); |
| 132 | + } |
| 133 | + catch (MalformedURLException e) { |
| 134 | + throw new RuntimeException( "Unable to resolve classpath entry to URL : " + classesDir.getAbsolutePath(), |
| 135 | + e ); |
| 136 | + } |
| 137 | + } |
| 138 | + |
| 139 | + private Metadata buildMappings(StandardServiceRegistry registry) { |
| 140 | + return new MetadataSources( registry ) |
| 141 | + .addAnnotatedClass( TestEntity.class ) |
| 142 | + .buildMetadata(); |
| 143 | + } |
| 144 | + |
| 145 | + @Entity(name = "TestEntity") |
| 146 | + @Table(name = "TEST_ENTITY") |
| 147 | + public static class TestEntity { |
| 148 | + @Id |
| 149 | + private long id; |
| 150 | + |
| 151 | + private String name; |
| 152 | + } |
| 153 | + |
| 154 | + public class ExecutionOptionsTestImpl implements ExecutionOptions, ExceptionHandler { |
| 155 | + Map<String, Object> configValues; |
| 156 | + |
| 157 | + public ExecutionOptionsTestImpl(Map<String, Object> configValues) { |
| 158 | + this.configValues = configValues; |
| 159 | + } |
| 160 | + |
| 161 | + @Override |
| 162 | + public Map<String, Object> getConfigurationValues() { |
| 163 | + return configValues; |
| 164 | + } |
| 165 | + |
| 166 | + @Override |
| 167 | + public boolean shouldManageNamespaces() { |
| 168 | + return true; |
| 169 | + } |
| 170 | + |
| 171 | + @Override |
| 172 | + public ExceptionHandler getExceptionHandler() { |
| 173 | + return this; |
| 174 | + } |
| 175 | + |
| 176 | + @Override |
| 177 | + public void handleException(CommandAcceptanceException exception) { |
| 178 | + throw exception; |
| 179 | + } |
| 180 | + } |
| 181 | + |
| 182 | + private static class SourceDescriptorImpl implements SourceDescriptor { |
| 183 | + /** |
| 184 | + * Singleton access |
| 185 | + */ |
| 186 | + public static final SourceDescriptorImpl INSTANCE = new SourceDescriptorImpl(); |
| 187 | + |
| 188 | + @Override |
| 189 | + public SourceType getSourceType() { |
| 190 | + return SourceType.METADATA; |
| 191 | + } |
| 192 | + |
| 193 | + @Override |
| 194 | + public ScriptSourceInput getScriptSourceInput() { |
| 195 | + return null; |
| 196 | + } |
| 197 | + } |
| 198 | + |
| 199 | + private static class TargetDescriptorImpl implements TargetDescriptor { |
| 200 | + /** |
| 201 | + * Singleton access |
| 202 | + */ |
| 203 | + public static final TargetDescriptorImpl INSTANCE = new TargetDescriptorImpl(); |
| 204 | + |
| 205 | + @Override |
| 206 | + public EnumSet<TargetType> getTargetTypes() { |
| 207 | + return EnumSet.of( TargetType.SCRIPT ); |
| 208 | + } |
| 209 | + |
| 210 | + @Override |
| 211 | + public ScriptTargetOutput getScriptTargetOutput() { |
| 212 | + return TestScriptTargetOutput.INSTANCE; |
| 213 | + } |
| 214 | + } |
| 215 | + |
| 216 | + private static class TestScriptTargetOutput implements ScriptTargetOutput { |
| 217 | + |
| 218 | + public static final TestScriptTargetOutput INSTANCE = new TestScriptTargetOutput(); |
| 219 | + |
| 220 | + List<String> insertCommands = new ArrayList<>(); |
| 221 | + |
| 222 | + @Override |
| 223 | + public void prepare() { |
| 224 | + |
| 225 | + } |
| 226 | + |
| 227 | + @Override |
| 228 | + public void accept(String command) { |
| 229 | + if ( command.toLowerCase().startsWith( "insert" ) ) { |
| 230 | + insertCommands.add( command ); |
| 231 | + } |
| 232 | + } |
| 233 | + |
| 234 | + @Override |
| 235 | + public void release() { |
| 236 | + |
| 237 | + } |
| 238 | + |
| 239 | + public List<String> getInsertCommands() { |
| 240 | + return insertCommands; |
| 241 | + } |
| 242 | + } |
| 243 | +} |
0 commit comments