|
| 1 | +/* |
| 2 | + * SPDX-License-Identifier: Apache-2.0 |
| 3 | + * Copyright Red Hat Inc. and Hibernate Authors |
| 4 | + */ |
| 5 | +package org.hibernate.orm.test.lob; |
| 6 | + |
| 7 | +import org.hibernate.bugs.TestEntity; |
| 8 | +import org.hibernate.engine.jdbc.env.internal.NonContextualLobCreator; |
| 9 | +import org.hibernate.testing.orm.junit.DomainModel; |
| 10 | +import org.hibernate.testing.orm.junit.JiraKey; |
| 11 | +import org.hibernate.testing.orm.junit.SessionFactory; |
| 12 | +import org.hibernate.testing.orm.junit.SessionFactoryScope; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import java.io.File; |
| 16 | +import java.io.IOException; |
| 17 | +import java.io.InputStream; |
| 18 | +import java.sql.Blob; |
| 19 | +import java.sql.SQLException; |
| 20 | +import java.util.jar.JarEntry; |
| 21 | +import java.util.jar.JarFile; |
| 22 | + |
| 23 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 24 | + |
| 25 | +@DomainModel(annotatedClasses = TestEntity.class) |
| 26 | +@SessionFactory |
| 27 | +@JiraKey( "HHH-19464" ) |
| 28 | +class JarFileEntryBlobTest { |
| 29 | + |
| 30 | + @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() ); |
| 34 | + |
| 35 | + try (JarFile jarFile = new JarFile( file )) { |
| 36 | + JarEntry entry = jarFile.getJarEntry( "pizza.png" ); |
| 37 | + long size = entry.getSize(); |
| 38 | + scope.inTransaction( entityManager -> { |
| 39 | + try { |
| 40 | + InputStream is = jarFile.getInputStream( entry ); |
| 41 | + Blob blob = NonContextualLobCreator.INSTANCE.wrap( |
| 42 | + NonContextualLobCreator.INSTANCE.createBlob( is, size ) |
| 43 | + ); |
| 44 | + TestEntity e = new TestEntity(); |
| 45 | + e.setId( 1L ); |
| 46 | + e.setData( blob ); |
| 47 | + |
| 48 | + entityManager.persist( e ); |
| 49 | + } |
| 50 | + catch (IOException e) { |
| 51 | + throw new RuntimeException( e ); |
| 52 | + } |
| 53 | + } |
| 54 | + ); |
| 55 | + |
| 56 | + scope.inStatelessSession( session -> { |
| 57 | + final var entity = session.get( TestEntity.class, 1L ); |
| 58 | + try { |
| 59 | + assertEquals( size, entity.getData().length() ); |
| 60 | + } |
| 61 | + catch (SQLException e) { |
| 62 | + throw new RuntimeException( e ); |
| 63 | + } |
| 64 | + } ); |
| 65 | + } |
| 66 | + } |
| 67 | +} |
0 commit comments