HHH-18612 Avoid writing `import.sql` file to classpath
This commit is contained in:
parent
29278a7677
commit
9f777e8d5b
|
@ -3,8 +3,12 @@ package org.hibernate.orm.test.tool.schema;
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URI;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
|
import java.net.URLClassLoader;
|
||||||
import java.nio.file.Files;
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.EnumSet;
|
import java.util.EnumSet;
|
||||||
import java.util.HashMap;
|
import java.util.HashMap;
|
||||||
|
@ -13,9 +17,10 @@ import java.util.Map;
|
||||||
|
|
||||||
import org.hibernate.boot.Metadata;
|
import org.hibernate.boot.Metadata;
|
||||||
import org.hibernate.boot.MetadataSources;
|
import org.hibernate.boot.MetadataSources;
|
||||||
|
import org.hibernate.boot.registry.BootstrapServiceRegistryBuilder;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistry;
|
import org.hibernate.boot.registry.StandardServiceRegistry;
|
||||||
import org.hibernate.boot.registry.StandardServiceRegistryBuilder;
|
|
||||||
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
import org.hibernate.boot.registry.classloading.spi.ClassLoaderService;
|
||||||
|
import org.hibernate.bytecode.enhance.spi.Enhancer;
|
||||||
import org.hibernate.cfg.AvailableSettings;
|
import org.hibernate.cfg.AvailableSettings;
|
||||||
import org.hibernate.tool.schema.SourceType;
|
import org.hibernate.tool.schema.SourceType;
|
||||||
import org.hibernate.tool.schema.TargetType;
|
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.SourceDescriptor;
|
||||||
import org.hibernate.tool.schema.spi.TargetDescriptor;
|
import org.hibernate.tool.schema.spi.TargetDescriptor;
|
||||||
|
|
||||||
|
import org.hibernate.testing.orm.junit.Jira;
|
||||||
import org.hibernate.testing.util.ServiceRegistryUtil;
|
import org.hibernate.testing.util.ServiceRegistryUtil;
|
||||||
import org.junit.jupiter.api.AfterEach;
|
import org.junit.jupiter.api.AfterEach;
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
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.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||||
import static org.hibernate.tool.schema.internal.SchemaCreatorImpl.DEFAULT_IMPORT_FILE;
|
import static org.hibernate.tool.schema.internal.SchemaCreatorImpl.DEFAULT_IMPORT_FILE;
|
||||||
|
|
||||||
|
@Jira( "https://hibernate.atlassian.net/browse/HHH-15717" )
|
||||||
public class DefaultImportFileExecutionTest {
|
public class DefaultImportFileExecutionTest {
|
||||||
|
|
||||||
private File defaultImportFile;
|
private File defaultImportFile;
|
||||||
private StandardServiceRegistry serviceRegistry;
|
private StandardServiceRegistry serviceRegistry;
|
||||||
private static final String COMMAND = "INSERT INTO TEST_ENTITY (id, name) values (1,'name')";
|
private static final String COMMAND = "INSERT INTO TEST_ENTITY (id, name) values (1,'name')";
|
||||||
|
@ -52,7 +58,9 @@ public class DefaultImportFileExecutionTest {
|
||||||
@BeforeEach
|
@BeforeEach
|
||||||
public void setUp() throws Exception {
|
public void setUp() throws Exception {
|
||||||
defaultImportFile = createDefaultImportFile( "import.sql" );
|
defaultImportFile = createDefaultImportFile( "import.sql" );
|
||||||
serviceRegistry = ServiceRegistryUtil.serviceRegistry();
|
serviceRegistry = ServiceRegistryUtil.serviceRegistryBuilder(
|
||||||
|
new BootstrapServiceRegistryBuilder().applyClassLoader( toClassLoader( defaultImportFile.getParentFile() ) ).build()
|
||||||
|
).build();
|
||||||
}
|
}
|
||||||
|
|
||||||
@AfterEach
|
@AfterEach
|
||||||
|
@ -99,11 +107,9 @@ public class DefaultImportFileExecutionTest {
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
private static File createDefaultImportFile(String fileName) throws Exception {
|
private static File createDefaultImportFile(@SuppressWarnings( "SameParameterValue" ) String fileName) throws Exception {
|
||||||
URL myUrl = Thread.currentThread().getContextClassLoader().getResource( "hibernate.properties" );
|
final Path tmp = Files.createTempDirectory( "default_import" );
|
||||||
String path = myUrl.getPath().replace( "hibernate.properties", fileName );
|
final File file = new File( tmp.toString() + File.separator + fileName );
|
||||||
final File file = new File( path );
|
|
||||||
file.createNewFile();
|
|
||||||
|
|
||||||
try (final FileWriter myWriter = new FileWriter( file )) {
|
try (final FileWriter myWriter = new FileWriter( file )) {
|
||||||
myWriter.write( COMMAND );
|
myWriter.write( COMMAND );
|
||||||
|
@ -112,6 +118,17 @@ public class DefaultImportFileExecutionTest {
|
||||||
return file;
|
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) {
|
private Metadata buildMappings(StandardServiceRegistry registry) {
|
||||||
return new MetadataSources( registry )
|
return new MetadataSources( registry )
|
||||||
.addAnnotatedClass( TestEntity.class )
|
.addAnnotatedClass( TestEntity.class )
|
||||||
|
|
Loading…
Reference in New Issue