HHH-11134 StringIndexOutOfBoundsException in BooleanTypeDescriptor fix

This commit is contained in:
Arnold Galovics 2016-12-22 12:56:54 +01:00 committed by Chris Cranford
parent a21706bf02
commit dd9c68a862
2 changed files with 62 additions and 1 deletions

View File

@ -95,11 +95,18 @@ public class BooleanTypeDescriptor extends AbstractTypeDescriptor<Boolean> {
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;
}

View File

@ -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);
}
}