3
3
import java .io .File ;
4
4
import java .io .FileWriter ;
5
5
import java .io .IOException ;
6
+ import java .net .MalformedURLException ;
7
+ import java .net .URI ;
6
8
import java .net .URL ;
9
+ import java .net .URLClassLoader ;
7
10
import java .nio .file .Files ;
11
+ import java .nio .file .Path ;
8
12
import java .util .ArrayList ;
9
13
import java .util .EnumSet ;
10
14
import java .util .HashMap ;
13
17
14
18
import org .hibernate .boot .Metadata ;
15
19
import org .hibernate .boot .MetadataSources ;
20
+ import org .hibernate .boot .registry .BootstrapServiceRegistryBuilder ;
16
21
import org .hibernate .boot .registry .StandardServiceRegistry ;
17
- import org .hibernate .boot .registry .StandardServiceRegistryBuilder ;
18
22
import org .hibernate .boot .registry .classloading .spi .ClassLoaderService ;
23
+ import org .hibernate .bytecode .enhance .spi .Enhancer ;
19
24
import org .hibernate .cfg .AvailableSettings ;
20
25
import org .hibernate .tool .schema .SourceType ;
21
26
import org .hibernate .tool .schema .TargetType ;
30
35
import org .hibernate .tool .schema .spi .SourceDescriptor ;
31
36
import org .hibernate .tool .schema .spi .TargetDescriptor ;
32
37
38
+ import org .hibernate .testing .orm .junit .Jira ;
33
39
import org .hibernate .testing .util .ServiceRegistryUtil ;
34
40
import org .junit .jupiter .api .AfterEach ;
35
41
import org .junit .jupiter .api .BeforeEach ;
42
48
import static org .assertj .core .api .AssertionsForClassTypes .assertThat ;
43
49
import static org .hibernate .tool .schema .internal .SchemaCreatorImpl .DEFAULT_IMPORT_FILE ;
44
50
51
+ @ Jira ( "https://hibernate.atlassian.net/browse/HHH-15717" )
45
52
public class DefaultImportFileExecutionTest {
46
-
47
53
private File defaultImportFile ;
48
54
private StandardServiceRegistry serviceRegistry ;
49
55
private static final String COMMAND = "INSERT INTO TEST_ENTITY (id, name) values (1,'name')" ;
@@ -52,7 +58,9 @@ public class DefaultImportFileExecutionTest {
52
58
@ BeforeEach
53
59
public void setUp () throws Exception {
54
60
defaultImportFile = createDefaultImportFile ( "import.sql" );
55
- serviceRegistry = ServiceRegistryUtil .serviceRegistry ();
61
+ serviceRegistry = ServiceRegistryUtil .serviceRegistryBuilder (
62
+ new BootstrapServiceRegistryBuilder ().applyClassLoader ( toClassLoader ( defaultImportFile .getParentFile () ) ).build ()
63
+ ).build ();
56
64
}
57
65
58
66
@ AfterEach
@@ -99,11 +107,9 @@ private void createSchema(TargetDescriptorImpl targetDescriptor) {
99
107
);
100
108
}
101
109
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 );
107
113
108
114
try (final FileWriter myWriter = new FileWriter ( file )) {
109
115
myWriter .write ( COMMAND );
@@ -112,6 +118,17 @@ private static File createDefaultImportFile(String fileName) throws Exception {
112
118
return file ;
113
119
}
114
120
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
+
115
132
private Metadata buildMappings (StandardServiceRegistry registry ) {
116
133
return new MetadataSources ( registry )
117
134
.addAnnotatedClass ( TestEntity .class )
0 commit comments