HHH-10780 - Provide a PrimitiveByteArrayTypeDescriptor toString implementation
This commit is contained in:
parent
86e9efe788
commit
adba228941
|
@ -57,6 +57,11 @@ public class PrimitiveByteArrayTypeDescriptor extends AbstractTypeDescriptor<byt
|
|||
return buf.toString();
|
||||
}
|
||||
|
||||
@Override
|
||||
public String extractLoggableRepresentation(byte[] value) {
|
||||
return (value == null) ? super.extractLoggableRepresentation( null ) : Arrays.toString( value );
|
||||
}
|
||||
|
||||
public byte[] fromString(String string) {
|
||||
if ( string == null ) {
|
||||
return null;
|
||||
|
|
|
@ -0,0 +1,84 @@
|
|||
/*
|
||||
* 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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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;
|
||||
}
|
||||
}
|
|
@ -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 <http://www.gnu.org/licenses/lgpl-2.1.html>.
|
||||
*/
|
||||
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<byte[]> {
|
||||
|
||||
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<byte[]> 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));
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue