From 9071c16e7d429067dcce83a56fafeecf7d70c3e9 Mon Sep 17 00:00:00 2001 From: Marco Belladelli Date: Fri, 13 Sep 2024 12:05:19 +0200 Subject: [PATCH] HHH-18612 Avoid writing `import.sql` file to classpath --- .../DefaultImportFileExecutionTest.java | 33 ++++++++++++++----- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/hibernate-core/src/test/java/org/hibernate/orm/test/tool/schema/DefaultImportFileExecutionTest.java b/hibernate-core/src/test/java/org/hibernate/orm/test/tool/schema/DefaultImportFileExecutionTest.java index ed486db443..dcf17761f8 100644 --- a/hibernate-core/src/test/java/org/hibernate/orm/test/tool/schema/DefaultImportFileExecutionTest.java +++ b/hibernate-core/src/test/java/org/hibernate/orm/test/tool/schema/DefaultImportFileExecutionTest.java @@ -3,8 +3,12 @@ package org.hibernate.orm.test.tool.schema; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.net.MalformedURLException; +import java.net.URI; import java.net.URL; +import java.net.URLClassLoader; import java.nio.file.Files; +import java.nio.file.Path; import java.util.ArrayList; import java.util.EnumSet; import java.util.HashMap; @@ -13,9 +17,10 @@ import java.util.Map; import org.hibernate.boot.Metadata; import org.hibernate.boot.MetadataSources; +import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder; import org.hibernate.boot.registry.StandardServiceRegistry; -import org.hibernate.boot.registry.StandardServiceRegistryBuilder; import org.hibernate.boot.registry.classloading.spi.ClassLoaderService; +import org.hibernate.bytecode.enhance.spi.Enhancer; import org.hibernate.cfg.AvailableSettings; import org.hibernate.tool.schema.SourceType; import org.hibernate.tool.schema.TargetType; @@ -30,6 +35,7 @@ import org.hibernate.tool.schema.spi.ScriptTargetOutput; import org.hibernate.tool.schema.spi.SourceDescriptor; import org.hibernate.tool.schema.spi.TargetDescriptor; +import org.hibernate.testing.orm.junit.Jira; import org.hibernate.testing.util.ServiceRegistryUtil; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -42,8 +48,8 @@ import jakarta.persistence.Table; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.hibernate.tool.schema.internal.SchemaCreatorImpl.DEFAULT_IMPORT_FILE; +@Jira( "https://hibernate.atlassian.net/browse/HHH-15717" ) public class DefaultImportFileExecutionTest { - private File defaultImportFile; private StandardServiceRegistry serviceRegistry; private static final String COMMAND = "INSERT INTO TEST_ENTITY (id, name) values (1,'name')"; @@ -52,7 +58,9 @@ public class DefaultImportFileExecutionTest { @BeforeEach public void setUp() throws Exception { defaultImportFile = createDefaultImportFile( "import.sql" ); - serviceRegistry = ServiceRegistryUtil.serviceRegistry(); + serviceRegistry = ServiceRegistryUtil.serviceRegistryBuilder( + new BootstrapServiceRegistryBuilder().applyClassLoader( toClassLoader( defaultImportFile.getParentFile() ) ).build() + ).build(); } @AfterEach @@ -99,11 +107,9 @@ public class DefaultImportFileExecutionTest { ); } - private static File createDefaultImportFile(String fileName) throws Exception { - URL myUrl = Thread.currentThread().getContextClassLoader().getResource( "hibernate.properties" ); - String path = myUrl.getPath().replace( "hibernate.properties", fileName ); - final File file = new File( path ); - file.createNewFile(); + private static File createDefaultImportFile(@SuppressWarnings( "SameParameterValue" ) String fileName) throws Exception { + final Path tmp = Files.createTempDirectory( "default_import" ); + final File file = new File( tmp.toString() + File.separator + fileName ); try (final FileWriter myWriter = new FileWriter( file )) { myWriter.write( COMMAND ); @@ -112,6 +118,17 @@ public class DefaultImportFileExecutionTest { return file; } + private static ClassLoader toClassLoader(File classesDir) { + final URI classesDirUri = classesDir.toURI(); + try { + final URL url = classesDirUri.toURL(); + return new URLClassLoader( new URL[] { url }, Enhancer.class.getClassLoader() ); + } + catch (MalformedURLException e) { + throw new RuntimeException( "Unable to resolve classpath entry to URL : " + classesDir.getAbsolutePath(), e ); + } + } + private Metadata buildMappings(StandardServiceRegistry registry) { return new MetadataSources( registry ) .addAnnotatedClass( TestEntity.class )