Skip to content

Commit

Permalink
chore(engine): avoid more potential NPE in ExceptionUtil (#3826)
Browse files Browse the repository at this point in the history
* add ExceptionUtilTest

closes to #3887
  • Loading branch information
doublep authored Oct 20, 2023
1 parent a72b378 commit bd8dabf
Show file tree
Hide file tree
Showing 2 changed files with 117 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -144,13 +144,17 @@ public static SQLException unwrapException(ProcessEngineException genericPersist

public static boolean checkValueTooLongException(SQLException sqlException) {
String message = sqlException.getMessage();
if (message != null) {
message = message.toLowerCase();
} else {
return false;
}
return message.contains("too long") ||
message.contains("too large") ||
message.contains("TOO LARGE") ||
message.contains("ORA-01461") ||
message.contains("ORA-01401") ||
message.contains("ora-01461") ||
message.contains("ora-01401") ||
message.contains("data would be truncated") ||
message.contains("SQLCODE=-302, SQLSTATE=22001");
message.contains("sqlcode=-302, sqlstate=22001");
}

public static boolean checkValueTooLongException(ProcessEngineException genericPersistenceException) {
Expand All @@ -171,11 +175,17 @@ public static boolean checkConstraintViolationException(ProcessEngineException g
}

String message = sqlException.getMessage();
if (message != null) {
message = message.toLowerCase();
} else {
return false;
}

return message.contains("constraint") ||
message.contains("violat") ||
message.toLowerCase().contains("duplicate") ||
message.contains("ORA-00001") ||
message.contains("SQLCODE=-803, SQLSTATE=23505");
message.contains("duplicate") ||
message.contains("ora-00001") ||
message.contains("sqlcode=-803, sqlstate=23505");
}

public static boolean checkForeignKeyConstraintViolation(PersistenceException persistenceException) {
Expand All @@ -189,7 +199,13 @@ public static boolean checkForeignKeyConstraintViolation(PersistenceException pe
}

public static boolean checkForeignKeyConstraintViolation(SQLException sqlException) {
String message = sqlException.getMessage().toLowerCase();
String message = sqlException.getMessage();
if (message != null) {
message = message.toLowerCase();
} else {
return false;
}

String sqlState = sqlException.getSQLState();
int errorCode = sqlException.getErrorCode();

Expand Down Expand Up @@ -218,7 +234,13 @@ public static boolean checkVariableIntegrityViolation(PersistenceException persi
return false;
}

String message = sqlException.getMessage().toLowerCase();
String message = sqlException.getMessage();
if (message != null) {
message = message.toLowerCase();
} else {
return false;
}

String sqlState = sqlException.getSQLState();
int errorCode = sqlException.getErrorCode();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* Copyright Camunda Services GmbH and/or licensed to Camunda Services GmbH
* under one or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information regarding copyright
* ownership. Camunda licenses this file to you under the Apache License,
* Version 2.0; you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.camunda.bpm.engine.impl.util;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;

import java.sql.SQLException;
import org.apache.ibatis.exceptions.PersistenceException;
import org.camunda.bpm.engine.ProcessEngineException;
import org.junit.Test;

public class ExceptionUtilTest {

@Test
public void checkValueTooLongException() {
assertThat(ExceptionUtil.checkValueTooLongException(mock(SQLException.class))).isFalse();

SQLException tooLong = mock(SQLException.class);
doReturn("too long").when(tooLong).getMessage();
assertThat(ExceptionUtil.checkValueTooLongException(tooLong)).isTrue();
}

@Test
public void checkConstraintViolationException() {
assertThat(ExceptionUtil.checkConstraintViolationException(new ProcessEngineException(new PersistenceException(mock(SQLException.class))))).isFalse();

SQLException constraintViolation = mock(SQLException.class);
doReturn("ora-00001").when(constraintViolation).getMessage();
assertThat(ExceptionUtil.checkConstraintViolationException(new ProcessEngineException(new PersistenceException(constraintViolation)))).isTrue();
}

@Test
public void checkForeignKeyConstraintViolation() {
assertThat(ExceptionUtil.checkForeignKeyConstraintViolation(mock(SQLException.class))).isFalse();

SQLException constraintViolation = mock(SQLException.class);
doReturn("integrity constraint").when(constraintViolation).getMessage();
assertThat(ExceptionUtil.checkForeignKeyConstraintViolation(constraintViolation)).isTrue();
}

@Test
public void checkVariableIntegrityViolation() {
assertThat(ExceptionUtil.checkVariableIntegrityViolation(new PersistenceException(mock(SQLException.class)))).isFalse();

SQLException integrityViolation = mock(SQLException.class);
doReturn("act_uniq_variable").when(integrityViolation).getMessage();
doReturn("23505").when(integrityViolation).getSQLState();
assertThat(ExceptionUtil.checkVariableIntegrityViolation(new PersistenceException(integrityViolation))).isTrue();
}

@Test
public void checkCrdbTransactionRetryException() {
assertThat(ExceptionUtil.checkCrdbTransactionRetryException(mock(SQLException.class))).isFalse();

SQLException retry = mock(SQLException.class);
doReturn("restart transaction").when(retry).getMessage();
doReturn(40001).when(retry).getErrorCode();
assertThat(ExceptionUtil.checkCrdbTransactionRetryException(retry)).isTrue();
}

@Test
public void checkDeadlockException() {
assertThat(ExceptionUtil.checkDeadlockException(mock(SQLException.class))).isFalse();

SQLException deadlock = mock(SQLException.class);
doReturn("40P01").when(deadlock).getSQLState(); // PostgreSQL
assertThat(ExceptionUtil.checkDeadlockException(deadlock)).isTrue();
}

}

0 comments on commit bd8dabf

Please sign in to comment.