Skip to content

Commit

Permalink
fix: SqlMethodAnalyzer to handle empty SQL query and not throw IndexO…
Browse files Browse the repository at this point in the history
…utOfBoundsException (#798)
  • Loading branch information
aaronchung-bitquill authored Dec 20, 2023
1 parent f29004c commit dd2c4bc
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,11 @@ public boolean doesOpenTransaction(final Connection conn, final String methodNam
}

private String getFirstSqlStatement(final String sql) {
String statement = parseMultiStatementQueries(sql).get(0);
List<String> statementList = parseMultiStatementQueries(sql);
if (statementList.isEmpty()) {
return sql;
}
String statement = statementList.get(0);
statement = statement.toUpperCase();
statement = statement.replaceAll("\\s*/\\*(.*?)\\*/\\s*", " ").trim();
return statement;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

package software.amazon.jdbc.util;

import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
Expand All @@ -34,6 +35,8 @@
import org.mockito.MockitoAnnotations;

class SqlMethodAnalyzerTest {
private static final String EXECUTE_METHOD = "execute";
private static final String EMPTY_SQL = "";

@Mock Connection conn;

Expand Down Expand Up @@ -68,6 +71,11 @@ void testOpenTransaction(final String methodName, final String sql, final boolea
assertEquals(expected, actual);
}

@Test
void testOpenTransactionWithEmptySqlDoesNotThrow() {
assertDoesNotThrow(() -> sqlMethodAnalyzer.doesOpenTransaction(conn, EXECUTE_METHOD, new String[]{EMPTY_SQL}));
}

@ParameterizedTest
@MethodSource("closeTransactionQueries")
void testCloseTransaction(final String methodName, final String sql, final boolean expected) {
Expand All @@ -82,6 +90,11 @@ void testCloseTransaction(final String methodName, final String sql, final boole
assertEquals(expected, actual);
}

@Test
void testCloseTransactionWithEmptySqlDoesNotThrow() {
assertDoesNotThrow(() -> sqlMethodAnalyzer.doesCloseTransaction(conn, EXECUTE_METHOD, new String[]{EMPTY_SQL}));
}

@Test
void testDoesSwitchAutoCommitFalseTrue() throws SQLException {
assertFalse(sqlMethodAnalyzer.doesSwitchAutoCommitFalseTrue(conn, "Connection.setAutoCommit",
Expand Down Expand Up @@ -123,6 +136,11 @@ void testIsStatementSettingAutoCommit(final String methodName, final String sql,
assertEquals(expected, actual);
}

@Test
void testIsStatementSettingAutoCommitWithEmptySqlDoesNotThrow() {
assertDoesNotThrow(() -> sqlMethodAnalyzer.isStatementSettingAutoCommit(EXECUTE_METHOD, new String[]{EMPTY_SQL}));
}

@ParameterizedTest
@MethodSource("getAutoCommitQueries")
void testGetAutoCommit(final String sql, final Boolean expected) {
Expand All @@ -137,6 +155,11 @@ void testGetAutoCommit(final String sql, final Boolean expected) {
assertEquals(expected, actual);
}

@Test
void testGetAutoCommitWithEmptySqlDoesNotThrow() {
assertDoesNotThrow(() -> sqlMethodAnalyzer.getAutoCommitValueFromSqlStatement(new String[]{EMPTY_SQL}));
}

@ParameterizedTest
@MethodSource("getIsMethodClosingSqlObjectMethods")
void testIsMethodClosingSqlObject(final String methodName, final boolean expected) {
Expand Down

0 comments on commit dd2c4bc

Please sign in to comment.