Skip to content

Commit

Permalink
Fix String index out of range when wrapping an char
Browse files Browse the repository at this point in the history
  • Loading branch information
dreab8 authored and beikov committed Feb 17, 2022
1 parent cc750a9 commit 40bcb97
Show file tree
Hide file tree
Showing 2 changed files with 91 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ public <X> Character wrap(X value, WrapperOptions options) {
if (value instanceof Character) {
return (Character) value;
}
if (value instanceof String) {
if ( value instanceof String ) {
if ( value.equals( "" ) ) {
return ' ';
}
final String str = (String) value;
return str.charAt( 0 );
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*
* 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.orm.test.type;

import org.hibernate.testing.orm.junit.DomainModel;
import org.hibernate.testing.orm.junit.SessionFactory;
import org.hibernate.testing.orm.junit.SessionFactoryScope;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import jakarta.persistence.Entity;
import jakarta.persistence.Id;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertNull;

@DomainModel(
annotatedClasses = CharacterTypeTest.TestEntity.class
)
@SessionFactory
public class CharacterTypeTest {

@BeforeEach
public void setUp(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
TestEntity dataTypes = new TestEntity( 1, ' ' );
session.persist( dataTypes );
}
);
}

@Test
public void transientTest(SessionFactoryScope scope) {
scope.inTransaction(
session -> {
TestEntity d1 = session.find( TestEntity.class, 1 );
assertNotNull( d1 );
assertEquals( ' ', d1.getCharacterData() );
d1.setCharacterData( null );
}
);

scope.inTransaction(
session -> {
TestEntity d1 = session.find( TestEntity.class, 1 );
assertNotNull( d1 );
assertNull( d1.getCharacterData() );
}
);
}

@Entity(name = "TestEntity")
public static class TestEntity {

@Id
private Integer id;

private Character characterData;

public TestEntity() {
}

public TestEntity(Integer id, Character characterData) {
this.id = id;
this.characterData = characterData;
}

public Integer getId() {
return id;
}

public void setCharacterData(Character characterData) {
this.characterData = characterData;
}

public Character getCharacterData() {
return characterData;
}
}

}

0 comments on commit 40bcb97

Please sign in to comment.