33import java .io .File ;
44import java .io .FileWriter ;
55import java .io .IOException ;
6+ import java .net .MalformedURLException ;
7+ import java .net .URI ;
68import java .net .URL ;
9+ import java .net .URLClassLoader ;
710import java .nio .file .Files ;
11+ import java .nio .file .Path ;
812import java .util .ArrayList ;
913import java .util .EnumSet ;
1014import java .util .HashMap ;
1317
1418import org .hibernate .boot .Metadata ;
1519import org .hibernate .boot .MetadataSources ;
20+ import org .hibernate .boot .registry .BootstrapServiceRegistryBuilder ;
1621import org .hibernate .boot .registry .StandardServiceRegistry ;
17- import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
1822import org .hibernate .boot .registry .classloading .spi .ClassLoaderService ;
23+ import org .hibernate .bytecode .enhance .spi .Enhancer ;
1924import org .hibernate .cfg .AvailableSettings ;
2025import org .hibernate .tool .schema .SourceType ;
2126import org .hibernate .tool .schema .TargetType ;
3035import org .hibernate .tool .schema .spi .SourceDescriptor ;
3136import org .hibernate .tool .schema .spi .TargetDescriptor ;
3237
38+ import org .hibernate .testing .orm .junit .Jira ;
3339import org .hibernate .testing .util .ServiceRegistryUtil ;
3440import org .junit .jupiter .api .AfterEach ;
3541import org .junit .jupiter .api .BeforeEach ;
4248import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
4349import static org .hibernate .tool .schema .internal .SchemaCreatorImpl .DEFAULT_IMPORT_FILE ;
4450
51+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-15717" )
4552public class DefaultImportFileExecutionTest {
46-
4753 private File defaultImportFile ;
4854 private StandardServiceRegistry serviceRegistry ;
4955 private static final String COMMAND = "INSERT INTO TEST_ENTITY (id, name) values (1,'name')" ;
@@ -52,7 +58,9 @@ public class DefaultImportFileExecutionTest {
5258 @ BeforeEach
5359 public void setUp () throws Exception {
5460 defaultImportFile = createDefaultImportFile ( "import.sql" );
55- serviceRegistry = ServiceRegistryUtil .serviceRegistry ();
61+ serviceRegistry = ServiceRegistryUtil .serviceRegistryBuilder (
62+ new BootstrapServiceRegistryBuilder ().applyClassLoader ( toClassLoader ( defaultImportFile .getParentFile () ) ).build ()
63+ ).build ();
5664 }
5765
5866 @ AfterEach
@@ -99,11 +107,9 @@ private void createSchema(TargetDescriptorImpl targetDescriptor) {
99107 );
100108 }
101109
102- private static File createDefaultImportFile (String fileName ) throws Exception {
103- URL myUrl = Thread .currentThread ().getContextClassLoader ().getResource ( "hibernate.properties" );
104- String path = myUrl .getPath ().replace ( "hibernate.properties" , fileName );
105- final File file = new File ( path );
106- file .createNewFile ();
110+ private static File createDefaultImportFile (@ SuppressWarnings ( "SameParameterValue" ) String fileName ) throws Exception {
111+ final Path tmp = Files .createTempDirectory ( "default_import" );
112+ final File file = new File ( tmp .toString () + File .separator + fileName );
107113
108114 try (final FileWriter myWriter = new FileWriter ( file )) {
109115 myWriter .write ( COMMAND );
@@ -112,6 +118,17 @@ private static File createDefaultImportFile(String fileName) throws Exception {
112118 return file ;
113119 }
114120
121+ private static ClassLoader toClassLoader (File classesDir ) {
122+ final URI classesDirUri = classesDir .toURI ();
123+ try {
124+ final URL url = classesDirUri .toURL ();
125+ return new URLClassLoader ( new URL [] { url }, Enhancer .class .getClassLoader () );
126+ }
127+ catch (MalformedURLException e ) {
128+ throw new RuntimeException ( "Unable to resolve classpath entry to URL : " + classesDir .getAbsolutePath (), e );
129+ }
130+ }
131+
115132 private Metadata buildMappings (StandardServiceRegistry registry ) {
116133 return new MetadataSources ( registry )
117134 .addAnnotatedClass ( TestEntity .class )
0 commit comments