diff --git a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanTypeDescriptor.java b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanTypeDescriptor.java index 4c4cccbef6..991d4efe4c 100644 --- a/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanTypeDescriptor.java +++ b/hibernate-core/src/main/java/org/hibernate/type/descriptor/java/BooleanTypeDescriptor.java @@ -95,11 +95,18 @@ public class BooleanTypeDescriptor extends AbstractTypeDescriptor { return isTrue( (Character) value ) ? TRUE : FALSE; } if ( String.class.isInstance( value ) ) { - return isTrue( ( (String) value ).charAt( 0 ) ) ? TRUE : FALSE; + return isTrue((String) value) ? TRUE : FALSE; } throw unknownWrap( value.getClass() ); } + private boolean isTrue(String strValue) { + if (strValue != null && !strValue.isEmpty()) { + return isTrue(strValue.charAt(0)); + } + return false; + } + private boolean isTrue(char charValue) { return charValue == characterValueTrue || charValue == characterValueTrueLC; } diff --git a/hibernate-core/src/test/java/org/hibernate/type/descriptor/java/BooleanTypeDescriptorTest.java b/hibernate-core/src/test/java/org/hibernate/type/descriptor/java/BooleanTypeDescriptorTest.java new file mode 100644 index 0000000000..f752d935cf --- /dev/null +++ b/hibernate-core/src/test/java/org/hibernate/type/descriptor/java/BooleanTypeDescriptorTest.java @@ -0,0 +1,54 @@ +package org.hibernate.type.descriptor.java; + +import org.junit.Test; + +import static org.junit.Assert.*; + +public class BooleanTypeDescriptorTest { + private BooleanTypeDescriptor underTest = new BooleanTypeDescriptor(); + + @Test + public void testWrapShouldReturnTrueWhenYStringGiven() { + // given + // when + Boolean result = underTest.wrap("Y", null); + // then + assertTrue(result); + } + + @Test + public void testWrapShouldReturnFalseWhenFStringGiven() { + // given + // when + Boolean result = underTest.wrap("N", null); + // then + assertFalse(result); + } + + @Test + public void testWrapShouldReturnFalseWhenRandomStringGiven() { + // given + // when + Boolean result = underTest.wrap("k", null); + // then + assertFalse(result); + } + + @Test + public void testWrapShouldReturnNullWhenNullStringGiven() { + // given + // when + Boolean result = underTest.wrap(null, null); + // then + assertNull(result); + } + + @Test + public void testWrapShouldReturnFalseWhenEmptyStringGiven() { + // given + // when + Boolean result = underTest.wrap("", null); + // then + assertFalse(result); + } +} \ No newline at end of file