Skip to content

Commit de89c9a

Browse files
committed
HHH-19464 Create random temporary ZIP file instead of using one from ressources
1 parent 04396ad commit de89c9a

File tree

2 files changed

+66
-10
lines changed

2 files changed

+66
-10
lines changed

hibernate-core/src/test/java/org/hibernate/orm/test/lob/JarFileEntryBlobTest.java

Lines changed: 66 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,43 @@
44
*/
55
package 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;
810
import org.hibernate.engine.jdbc.env.internal.NonContextualLobCreator;
911
import org.hibernate.testing.orm.junit.DomainModel;
1012
import org.hibernate.testing.orm.junit.JiraKey;
1113
import org.hibernate.testing.orm.junit.SessionFactory;
1214
import org.hibernate.testing.orm.junit.SessionFactoryScope;
15+
import org.junit.jupiter.api.Assertions;
1316
import org.junit.jupiter.api.Test;
17+
import org.junit.jupiter.api.io.TempDir;
1418

1519
import java.io.File;
20+
import java.io.FileOutputStream;
1621
import java.io.IOException;
1722
import java.io.InputStream;
1823
import java.sql.Blob;
1924
import java.sql.SQLException;
25+
import java.util.Random;
2026
import java.util.jar.JarEntry;
2127
import 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")
2834
class 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

Comments
 (0)