From 6032c4dbfb649242746e7add613c163c222c0fd5 Mon Sep 17 00:00:00 2001 From: Strong Liu Date: Wed, 7 Sep 2011 16:59:48 +0800 Subject: [PATCH] HHH-6533 ByteTypeDescriptor is not working properly --- .../descriptor/java/ByteTypeDescriptor.java | 5 +- .../test/typedescriptor/ByteTest.java | 104 ++++++++++++++++++ .../typedescriptor/VariousTypesEntity.java | 55 +++++++++ 3 files changed, 162 insertions(+), 2 deletions(-) create mode 100644 hibernate-testsuite/src/test/java/org/hibernate/test/typedescriptor/ByteTest.java create mode 100644 hibernate-testsuite/src/test/java/org/hibernate/test/typedescriptor/VariousTypesEntity.java diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteTypeDescriptor.java index 86fb6e54cc..2050ed40bf 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/ByteTypeDescriptor.java @@ -29,6 +29,7 @@ import org.hibernate.type.descriptor.WrapperOptions; * Descriptor for {@link Byte} handling. * * @author Steve Ebersole + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) */ public class ByteTypeDescriptor extends AbstractTypeDescriptor { public static final ByteTypeDescriptor INSTANCE = new ByteTypeDescriptor(); @@ -39,12 +40,12 @@ public class ByteTypeDescriptor extends AbstractTypeDescriptor { @SuppressWarnings({ "UnnecessaryUnboxing" }) public String toString(Byte value) { - return Integer.toHexString( value.byteValue() - Byte.MIN_VALUE ); + return value == null ? null : value.toString(); } @SuppressWarnings({ "UnnecessaryBoxing" }) public Byte fromString(String string) { - return Byte.valueOf( (byte) (Integer.parseInt( string, 16) + Byte.MIN_VALUE) ); + return Byte.valueOf( string ); } @SuppressWarnings({ "unchecked" }) diff --git a/hibernate-testsuite/src/test/java/org/hibernate/test/typedescriptor/ByteTest.java b/hibernate-testsuite/src/test/java/org/hibernate/test/typedescriptor/ByteTest.java new file mode 100644 index 0000000000..37293f5d06 --- /dev/null +++ b/hibernate-testsuite/src/test/java/org/hibernate/test/typedescriptor/ByteTest.java @@ -0,0 +1,104 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.typedescriptor; + +import junit.framework.Test; + +import org.hibernate.Session; +import org.hibernate.Transaction; +import org.hibernate.testing.junit.functional.FunctionalTestCase; +import org.hibernate.testing.junit.functional.FunctionalTestClassTestSuite; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +public class ByteTest extends FunctionalTestCase { + public static final byte TEST_VALUE = 65; + + public ByteTest(String str) { + super( str ); + } + + public static Test suite() { + return new FunctionalTestClassTestSuite( ByteTest.class ); + } + + @Override + public Class[] getAnnotatedClasses() { + return new Class[] { + VariousTypesEntity.class + }; + } + + public void testByteDataPersistenceAndRetrieval() { + Session session = openSession(); + Transaction transaction = session.beginTransaction(); + VariousTypesEntity entity = new VariousTypesEntity(); + entity.setId( 1 ); + entity.setByteData( TEST_VALUE ); + session.persist( entity ); + transaction.commit(); + session.close(); + + // Testing sample value. + session = openSession(); + transaction = session.beginTransaction(); + entity = (VariousTypesEntity) session.createQuery( + " from VariousTypesEntity " + + " where byteData = org.hibernate.test.typedescriptor.ByteTest.TEST_VALUE " + ).uniqueResult(); + assertNotNull( entity ); + assertEquals( TEST_VALUE, entity.getByteData() ); + entity.setByteData( Byte.MIN_VALUE ); + session.update( entity ); + transaction.commit(); + session.close(); + + // Testing minimal value. + session = openSession(); + transaction = session.beginTransaction(); + entity = (VariousTypesEntity) session.createQuery( + " from VariousTypesEntity " + + " where byteData = java.lang.Byte.MIN_VALUE " + ).uniqueResult(); + assertNotNull( entity ); + assertEquals( Byte.MIN_VALUE, entity.getByteData() ); + entity.setByteData( Byte.MAX_VALUE ); + session.update( entity ); + transaction.commit(); + session.close(); + + // Testing maximal value. + session = openSession(); + transaction = session.beginTransaction(); + entity = (VariousTypesEntity) session.createQuery( + " from VariousTypesEntity " + + " where byteData = java.lang.Byte.MAX_VALUE " + ).uniqueResult(); + assertNotNull( entity ); + assertEquals( Byte.MAX_VALUE, entity.getByteData() ); + transaction.commit(); + session.close(); + } +} \ No newline at end of file diff --git a/hibernate-testsuite/src/test/java/org/hibernate/test/typedescriptor/VariousTypesEntity.java b/hibernate-testsuite/src/test/java/org/hibernate/test/typedescriptor/VariousTypesEntity.java new file mode 100644 index 0000000000..c6589fca1b --- /dev/null +++ b/hibernate-testsuite/src/test/java/org/hibernate/test/typedescriptor/VariousTypesEntity.java @@ -0,0 +1,55 @@ +/* + * Hibernate, Relational Persistence for Idiomatic Java + * + * Copyright (c) 2011, Red Hat Inc. or third-party contributors as + * indicated by the @author tags or express copyright attribution + * statements applied by the authors. All third-party contributions are + * distributed under license by Red Hat Inc. + * + * This copyrighted material is made available to anyone wishing to use, modify, + * copy, or redistribute it subject to the terms and conditions of the GNU + * Lesser General Public License, as published by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY + * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License + * for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this distribution; if not, write to: + * Free Software Foundation, Inc. + * 51 Franklin Street, Fifth Floor + * Boston, MA 02110-1301 USA + */ +package org.hibernate.test.typedescriptor; + +import java.io.Serializable; +import javax.persistence.Entity; +import javax.persistence.Id; + +/** + * @author Lukasz Antoniak (lukasz dot antoniak at gmail dot com) + */ +@Entity +public class VariousTypesEntity implements Serializable { + @Id + private Integer id; + + private byte byteData; + + public Integer getId() { + return id; + } + + public void setId(Integer id) { + this.id = id; + } + + public byte getByteData() { + return byteData; + } + + public void setByteData(byte byteData) { + this.byteData = byteData; + } +} \ No newline at end of file