Skip to content

Commit 5e8bb78

Browse files
timovevladmihalcea
authored andcommitted
HHH-10780 - Provide a PrimitiveByteArrayTypeDescriptor toString implementation
1 parent 455368d commit 5e8bb78

File tree

3 files changed

+135
-0
lines changed

3 files changed

+135
-0
lines changed

hibernate-core/src/main/java/org/hibernate/type/descriptor/java/PrimitiveByteArrayTypeDescriptor.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,11 @@ public String toString(byte[] bytes) {
5757
return buf.toString();
5858
}
5959

60+
@Override
61+
public String extractLoggableRepresentation(byte[] value) {
62+
return (value == null) ? super.extractLoggableRepresentation( null ) : Arrays.toString( value );
63+
}
64+
6065
public byte[] fromString(String string) {
6166
if ( string == null ) {
6267
return null;
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.type;
8+
9+
import javax.persistence.Column;
10+
import javax.persistence.Entity;
11+
import javax.persistence.Id;
12+
13+
import org.hibernate.Session;
14+
import org.hibernate.resource.transaction.spi.TransactionStatus;
15+
16+
import org.hibernate.testing.junit4.BaseNonConfigCoreFunctionalTestCase;
17+
import org.junit.Test;
18+
19+
import static org.junit.Assert.assertArrayEquals;
20+
import static org.junit.Assert.fail;
21+
22+
/**
23+
* @author Vlad MIhalcea
24+
*/
25+
public class BinaryTypeTest extends BaseNonConfigCoreFunctionalTestCase {
26+
@Override
27+
protected Class[] getAnnotatedClasses() {
28+
return new Class[] {Image.class};
29+
}
30+
31+
@Test
32+
public void testByteArrayStringRepresentation() {
33+
Session s = openSession();
34+
s.getTransaction().begin();
35+
try {
36+
Image image = new Image();
37+
image.id = 1L;
38+
image.content = new byte[] {1, 2, 3};
39+
40+
s.save( image );
41+
s.getTransaction().commit();
42+
}
43+
catch (Exception e) {
44+
if ( s.getTransaction() != null && s.getTransaction().getStatus() == TransactionStatus.ACTIVE ) {
45+
s.getTransaction().rollback();
46+
}
47+
fail( e.getMessage() );
48+
}
49+
finally {
50+
s.close();
51+
}
52+
53+
s = openSession();
54+
s.getTransaction().begin();
55+
try {
56+
assertArrayEquals( new byte[] {1, 2, 3}, s.find( Image.class, 1L ).content );
57+
s.getTransaction().commit();
58+
}
59+
catch (Exception e) {
60+
if ( s.getTransaction() != null && s.getTransaction().getStatus() == TransactionStatus.ACTIVE ) {
61+
s.getTransaction().rollback();
62+
}
63+
fail( e.getMessage() );
64+
}
65+
finally {
66+
s.close();
67+
}
68+
}
69+
70+
@Entity(name = "Image")
71+
public static class Image {
72+
73+
@Id
74+
private Long id;
75+
76+
@Column(name = "content")
77+
private byte[] content;
78+
}
79+
80+
@Override
81+
protected boolean isCleanupTestDataRequired() {
82+
return true;
83+
}
84+
}
Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
* Hibernate, Relational Persistence for Idiomatic Java
3+
*
4+
* License: GNU Lesser General Public License (LGPL), version 2.1 or later.
5+
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
6+
*/
7+
package org.hibernate.test.type.descriptor.java;
8+
9+
import org.hibernate.type.descriptor.java.PrimitiveByteArrayTypeDescriptor;
10+
11+
import org.junit.Test;
12+
13+
import static org.junit.Assert.assertEquals;
14+
15+
/**
16+
* @author Vlad Mihalcea
17+
*/
18+
public class PrimitiveByteArrayDescriptorTest extends AbstractDescriptorTest<byte[]> {
19+
20+
private final byte[] original = new byte[] {1, 2, 3};
21+
22+
private final byte[] copy = new byte[] {1, 2, 3};
23+
24+
private final byte[] different = new byte[] {3, 2, 1};
25+
26+
public PrimitiveByteArrayDescriptorTest() {
27+
super( PrimitiveByteArrayTypeDescriptor.INSTANCE );
28+
}
29+
30+
@Override
31+
protected Data<byte[]> getTestData() {
32+
return new Data<>( original, copy, different );
33+
}
34+
35+
@Override
36+
protected boolean shouldBeMutable() {
37+
return true;
38+
}
39+
40+
@Test
41+
public void testExtractLoggableRepresentation() {
42+
assertEquals("null", PrimitiveByteArrayTypeDescriptor.INSTANCE.extractLoggableRepresentation(null));
43+
assertEquals("[]", PrimitiveByteArrayTypeDescriptor.INSTANCE.extractLoggableRepresentation(new byte[] {} ));
44+
assertEquals("[1, 2, 3]", PrimitiveByteArrayTypeDescriptor.INSTANCE.extractLoggableRepresentation(original));
45+
}
46+
}

0 commit comments

Comments
 (0)