Skip to content

CharacterTypeHandler#getNullableResult() throws StringIndexOutOfBoundsException #2368

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 10, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public void setNonNullParameter(PreparedStatement ps, int i, Character parameter
@Override
public Character getNullableResult(ResultSet rs, String columnName) throws SQLException {
String columnValue = rs.getString(columnName);
if (columnValue != null) {
if (columnValue != null && !columnValue.isEmpty()) {
return columnValue.charAt(0);
} else {
return null;
Expand All @@ -43,7 +43,7 @@ public Character getNullableResult(ResultSet rs, String columnName) throws SQLEx
@Override
public Character getNullableResult(ResultSet rs, int columnIndex) throws SQLException {
String columnValue = rs.getString(columnIndex);
if (columnValue != null) {
if (columnValue != null && !columnValue.isEmpty()) {
return columnValue.charAt(0);
} else {
return null;
Expand All @@ -53,7 +53,7 @@ public Character getNullableResult(ResultSet rs, int columnIndex) throws SQLExce
@Override
public Character getNullableResult(CallableStatement cs, int columnIndex) throws SQLException {
String columnValue = cs.getString(columnIndex);
if (columnValue != null) {
if (columnValue != null && !columnValue.isEmpty()) {
return columnValue.charAt(0);
} else {
return null;
Expand Down
20 changes: 20 additions & 0 deletions src/test/java/org/apache/ibatis/type/CharacterTypeHandlerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,24 @@ public void shouldGetResultNullFromCallableStatement() throws Exception {
verify(cs, never()).wasNull();
}

@Test
void testEmptyStringGetStringByName() throws Exception {
when(rs.getString("column")).thenReturn("");
assertNull(TYPE_HANDLER.getResult(rs, "column"));
verify(rs, never()).wasNull();
}

@Test
void testEmptyStringGetStringByIndex() throws Exception {
when(rs.getString(1)).thenReturn("");
assertNull(TYPE_HANDLER.getResult(rs, 1));
verify(rs, never()).wasNull();
}

@Test
void testEmptyStringCallableStatementGetStringByIndex() throws Exception {
when(cs.getString(1)).thenReturn("");
assertNull(TYPE_HANDLER.getResult(cs, 1));
verify(cs, never()).wasNull();
}
}