44 */
55package org .hibernate .orm .test .lob ;
66
7- import org .hibernate .bugs .TestEntity ;
7+ import jakarta .persistence .Entity ;
8+ import jakarta .persistence .Id ;
9+ import jakarta .persistence .Lob ;
810import org .hibernate .engine .jdbc .env .internal .NonContextualLobCreator ;
911import org .hibernate .testing .orm .junit .DomainModel ;
1012import org .hibernate .testing .orm .junit .JiraKey ;
1113import org .hibernate .testing .orm .junit .SessionFactory ;
1214import org .hibernate .testing .orm .junit .SessionFactoryScope ;
15+ import org .junit .jupiter .api .Assertions ;
1316import org .junit .jupiter .api .Test ;
17+ import org .junit .jupiter .api .io .TempDir ;
1418
1519import java .io .File ;
20+ import java .io .FileOutputStream ;
1621import java .io .IOException ;
1722import java .io .InputStream ;
1823import java .sql .Blob ;
1924import java .sql .SQLException ;
25+ import java .util .Random ;
2026import java .util .jar .JarEntry ;
2127import java .util .jar .JarFile ;
28+ import java .util .zip .ZipEntry ;
29+ import java .util .zip .ZipOutputStream ;
2230
23- import static org .junit .jupiter .api .Assertions .assertEquals ;
24-
25- @ DomainModel (annotatedClasses = TestEntity .class )
31+ @ DomainModel (annotatedClasses = JarFileEntryBlobTest .TestEntity .class )
2632@ SessionFactory
27- @ JiraKey ( "HHH-19464" )
33+ @ JiraKey ("HHH-19464" )
2834class JarFileEntryBlobTest {
2935
36+ public static final String ZIP_ENTRY_NAME = "test.bin" ;
37+
3038 @ 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 );
3441
3542 try (JarFile jarFile = new JarFile ( file )) {
36- JarEntry entry = jarFile .getJarEntry ( "pizza.png" );
43+ JarEntry entry = jarFile .getJarEntry ( ZIP_ENTRY_NAME );
3744 long size = entry .getSize ();
3845 scope .inTransaction ( entityManager -> {
3946 try {
@@ -56,12 +63,61 @@ void hibernate_blob_streaming(SessionFactoryScope scope) throws Exception {
5663 scope .inStatelessSession ( session -> {
5764 final var entity = session .get ( TestEntity .class , 1L );
5865 try {
59- assertEquals ( size , entity .getData ().length () );
66+ Assertions . assertEquals ( size , entity .getData ().length () );
6067 }
6168 catch (SQLException e ) {
6269 throw new RuntimeException ( e );
6370 }
6471 } );
6572 }
6673 }
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+ }
67123}
0 commit comments