4
4
*/
5
5
package org .hibernate .orm .test .lob ;
6
6
7
- import org .hibernate .bugs .TestEntity ;
7
+ import jakarta .persistence .Entity ;
8
+ import jakarta .persistence .Id ;
9
+ import jakarta .persistence .Lob ;
8
10
import org .hibernate .engine .jdbc .env .internal .NonContextualLobCreator ;
9
11
import org .hibernate .testing .orm .junit .DomainModel ;
10
12
import org .hibernate .testing .orm .junit .JiraKey ;
11
13
import org .hibernate .testing .orm .junit .SessionFactory ;
12
14
import org .hibernate .testing .orm .junit .SessionFactoryScope ;
15
+ import org .junit .jupiter .api .Assertions ;
13
16
import org .junit .jupiter .api .Test ;
17
+ import org .junit .jupiter .api .io .TempDir ;
14
18
15
19
import java .io .File ;
20
+ import java .io .FileOutputStream ;
16
21
import java .io .IOException ;
17
22
import java .io .InputStream ;
18
23
import java .sql .Blob ;
19
24
import java .sql .SQLException ;
25
+ import java .util .Random ;
20
26
import java .util .jar .JarEntry ;
21
27
import java .util .jar .JarFile ;
28
+ import java .util .zip .ZipEntry ;
29
+ import java .util .zip .ZipOutputStream ;
22
30
23
- import static org .junit .jupiter .api .Assertions .assertEquals ;
24
-
25
- @ DomainModel (annotatedClasses = TestEntity .class )
31
+ @ DomainModel (annotatedClasses = JarFileEntryBlobTest .TestEntity .class )
26
32
@ SessionFactory
27
- @ JiraKey ( "HHH-19464" )
33
+ @ JiraKey ("HHH-19464" )
28
34
class JarFileEntryBlobTest {
29
35
36
+ public static final String ZIP_ENTRY_NAME = "test.bin" ;
37
+
30
38
@ Test
31
- void hibernate_blob_streaming (SessionFactoryScope scope ) throws Exception {
32
- final var zipFilePath = getClass ().getClassLoader ().getResource ( "org/hibernate/orm/test/lob/JarFileEntryBlobTest.zip" );
33
- File file = new File ( zipFilePath .toURI () );
39
+ void hibernate_blob_streaming (SessionFactoryScope scope , @ TempDir File tempDir ) throws Exception {
40
+ final var file = createZipFile ( tempDir );
34
41
35
42
try (JarFile jarFile = new JarFile ( file )) {
36
- JarEntry entry = jarFile .getJarEntry ( "pizza.png" );
43
+ JarEntry entry = jarFile .getJarEntry ( ZIP_ENTRY_NAME );
37
44
long size = entry .getSize ();
38
45
scope .inTransaction ( entityManager -> {
39
46
try {
@@ -56,12 +63,61 @@ void hibernate_blob_streaming(SessionFactoryScope scope) throws Exception {
56
63
scope .inStatelessSession ( session -> {
57
64
final var entity = session .get ( TestEntity .class , 1L );
58
65
try {
59
- assertEquals ( size , entity .getData ().length () );
66
+ Assertions . assertEquals ( size , entity .getData ().length () );
60
67
}
61
68
catch (SQLException e ) {
62
69
throw new RuntimeException ( e );
63
70
}
64
71
} );
65
72
}
66
73
}
74
+
75
+ private static File createZipFile (File tempDir ) throws IOException {
76
+ final var testFile = new File ( tempDir , ZIP_ENTRY_NAME );
77
+ final var bytes = new byte [320000 ];
78
+ new Random ().nextBytes ( bytes );
79
+ try (var output = new FileOutputStream ( testFile )) {
80
+ try (var zipStream = new ZipOutputStream ( output )) {
81
+ final var zipEntry = new ZipEntry ( ZIP_ENTRY_NAME );
82
+ zipStream .putNextEntry ( zipEntry );
83
+ zipStream .write ( bytes );
84
+ }
85
+ }
86
+ return testFile ;
87
+ }
88
+
89
+ @ Entity
90
+ public static class TestEntity {
91
+
92
+ @ Id
93
+ Long id ;
94
+
95
+ @ Lob
96
+ Blob data ;
97
+
98
+ public Long getId () {
99
+ return id ;
100
+ }
101
+
102
+ public void setId (Long id ) {
103
+ this .id = id ;
104
+ }
105
+
106
+ public Blob getData () {
107
+ return data ;
108
+ }
109
+
110
+ public InputStream getInputStream () {
111
+ try {
112
+ return data .getBinaryStream ();
113
+ }
114
+ catch (SQLException e ) {
115
+ throw new IllegalArgumentException ( "Could not obtain requested input stream" , e );
116
+ }
117
+ }
118
+
119
+ public void setData (Blob data ) {
120
+ this .data = data ;
121
+ }
122
+ }
67
123
}
0 commit comments