diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/PrimitiveByteArrayTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/PrimitiveByteArrayTypeDescriptor.java index f8515a3606..af79652f7d 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/PrimitiveByteArrayTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/PrimitiveByteArrayTypeDescriptor.java @@ -57,6 +57,11 @@ public class PrimitiveByteArrayTypeDescriptor extends AbstractTypeDescriptor. + */ +package org.hibernate.test.type; + +import javax.persistence.Column; +import javax.persistence.Entity; +import javax.persistence.Id; + +import org.hibernate.Session; +import org.hibernate.resource.transaction.spi.TransactionStatus; + +import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase; +import org.junit.Test; + +import static org.junit.Assert.assertArrayEquals; +import static org.junit.Assert.fail; + +/** + * @author Vlad MIhalcea + */ +public class BinaryTypeTest extends BaseNonConfigCoreFunctionalTestCase { + @Override + protected Class[] getAnnotatedClasses() { + return new Class[] {Image.class}; + } + + @Test + public void testByteArrayStringRepresentation() { + Session s = openSession(); + s.getTransaction().begin(); + try { + Image image = new Image(); + image.id = 1L; + image.content = new byte[] {1, 2, 3}; + + s.save( image ); + s.getTransaction().commit(); + } + catch (Exception e) { + if ( s.getTransaction() != null && s.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { + s.getTransaction().rollback(); + } + fail( e.getMessage() ); + } + finally { + s.close(); + } + + s = openSession(); + s.getTransaction().begin(); + try { + assertArrayEquals( new byte[] {1, 2, 3}, s.get( Image.class, 1L ).content ); + s.getTransaction().commit(); + } + catch (Exception e) { + if ( s.getTransaction() != null && s.getTransaction().getStatus() == TransactionStatus.ACTIVE ) { + s.getTransaction().rollback(); + } + fail( e.getMessage() ); + } + finally { + s.close(); + } + } + + @Entity(name = "Image") + public static class Image { + + @Id + private Long id; + + @Column(name = "content") + private byte[] content; + } + + @Override + protected boolean isCleanupTestDataRequired() { + return true; + } +} diff --git a/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/PrimitiveByteArrayDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/PrimitiveByteArrayDescriptorTest.java new file mode 100644 index 0000000000..fd884a21e2 --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/test/type/descriptor/java/PrimitiveByteArrayDescriptorTest.java @@ -0,0 +1,46 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * License: GNU Lesser General Public License (LGPL), version 2.1 or later. + * See the lgpl.txt file in the root directory or . + */ +package org.hibernate.test.type.descriptor.java; + +import org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +/** + * @author Vlad Mihalcea + */ +public class PrimitiveByteArrayDescriptorTest extends AbstractDescriptorTest { + + private final byte[] original = new byte[] {1, 2, 3}; + + private final byte[] copy = new byte[] {1, 2, 3}; + + private final byte[] different = new byte[] {3, 2, 1}; + + public PrimitiveByteArrayDescriptorTest() { + super( PrimitiveByteArrayTypeDescriptor.INSTANCE ); + } + + @Override + protected Data getTestData() { + return new Data<>( original, copy, different ); + } + + @Override + protected boolean shouldBeMutable() { + return true; + } + + @Test + public void testExtractLoggableRepresentation() { + assertEquals("null", PrimitiveByteArrayTypeDescriptor.INSTANCE.extractLoggableRepresentation(null)); + assertEquals("[]", PrimitiveByteArrayTypeDescriptor.INSTANCE.extractLoggableRepresentation(new byte[] {} )); + assertEquals("[1, 2, 3]", PrimitiveByteArrayTypeDescriptor.INSTANCE.extractLoggableRepresentation(original)); + } +} \ No newline at end of file