forked from plusone/plusone-commons
refactor!: 使用 JdbcUpdateAffectedIncorrectNumberOfRowsException 取代 DataOperationResultException
- `DataOperationResultException` 重命名为 `JdbcUpdateAffectedIncorrectNumberOfRowsException` - `AssertTools` 中的 `checkAffectedRows` 相关方法移到 `JdbcUpdateAffectedIncorrectNumberOfRowsException` 中
This commit is contained in:
@@ -1,81 +0,0 @@
|
||||
/*
|
||||
* Copyright 2024-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://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 xyz.zhouxy.plusone.commons.exception.system;
|
||||
|
||||
/**
|
||||
* DataOperationResultException
|
||||
*
|
||||
* <p>
|
||||
* 当数据操作的结果不符合预期时抛出。
|
||||
*
|
||||
* <p>
|
||||
* 比如当一个 insert 或 update 操作时,预计影响数据库中的一行数据,但结果却影响了零条数据或多条数据,
|
||||
* 当出现这种始料未及的诡异情况时,抛出 {@link DataOperationResultException} 并回滚事务。
|
||||
* 后续需要排查原因。
|
||||
*
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class DataOperationResultException extends SysException {
|
||||
private static final long serialVersionUID = 992754090625352516L;
|
||||
|
||||
private final long expected;
|
||||
private final long actual;
|
||||
|
||||
/**
|
||||
* 创建一个 {@code DataOperationResultException} 对象
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actual 实际影响的行数
|
||||
*/
|
||||
public DataOperationResultException(long expected, long actual) {
|
||||
super(String.format("The number of rows affected is expected to be %d, but is: %d", expected, actual));
|
||||
this.expected = expected;
|
||||
this.actual = actual;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个 {@code DataOperationResultException} 对象
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actual 实际影响的行数
|
||||
* @param message 错误信息
|
||||
*/
|
||||
public DataOperationResultException(long expected, long actual, String message) {
|
||||
super(message);
|
||||
this.expected = expected;
|
||||
this.actual = actual;
|
||||
}
|
||||
|
||||
/**
|
||||
* 预期影响的行数
|
||||
*
|
||||
* @return the expected
|
||||
*/
|
||||
public long getExpected() {
|
||||
return expected;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实际影响的行数
|
||||
*
|
||||
* @return the actual
|
||||
*/
|
||||
public long getActual() {
|
||||
return actual;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,288 @@
|
||||
/*
|
||||
* Copyright 2024-2025 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* https://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 xyz.zhouxy.plusone.commons.exception.system;
|
||||
|
||||
import java.util.function.Supplier;
|
||||
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
/**
|
||||
* JdbcUpdateAffectedIncorrectNumberOfRowsException
|
||||
*
|
||||
* <p>
|
||||
* 当数据操作的结果不符合预期时抛出。
|
||||
*
|
||||
* <p>
|
||||
* 比如当一个 insert 或 update 操作时,预计影响数据库中的一行数据,但结果却影响了零条数据或多条数据,
|
||||
* 当出现这种始料未及的诡异情况时,抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException} 并回滚事务。
|
||||
* 后续需要排查原因。
|
||||
*
|
||||
* @author ZhouXY108 <luquanlion@outlook.com>
|
||||
* @since 1.0.0
|
||||
*/
|
||||
public final class JdbcUpdateAffectedIncorrectNumberOfRowsException extends SysException {
|
||||
private static final long serialVersionUID = 992754090625352516L;
|
||||
|
||||
private final long expected;
|
||||
private final long actual;
|
||||
|
||||
/**
|
||||
* 创建一个 {@code JdbcUpdateAffectedIncorrectNumberOfRowsException} 对象
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actual 实际影响的行数
|
||||
*/
|
||||
public JdbcUpdateAffectedIncorrectNumberOfRowsException(long expected, long actual) {
|
||||
super(String.format("The number of rows affected is expected to be %d, but is: %d", expected, actual));
|
||||
this.expected = expected;
|
||||
this.actual = actual;
|
||||
}
|
||||
|
||||
/**
|
||||
* 创建一个 {@code JdbcUpdateAffectedIncorrectNumberOfRowsException} 对象
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actual 实际影响的行数
|
||||
* @param message 错误信息
|
||||
*/
|
||||
public JdbcUpdateAffectedIncorrectNumberOfRowsException(long expected, long actual, String message) {
|
||||
super(message);
|
||||
this.expected = expected;
|
||||
this.actual = actual;
|
||||
}
|
||||
|
||||
/**
|
||||
* 预期影响的行数
|
||||
*
|
||||
* @return the expected
|
||||
*/
|
||||
public long getExpected() {
|
||||
return expected;
|
||||
}
|
||||
|
||||
/**
|
||||
* 实际影响的行数
|
||||
*
|
||||
* @return the actual
|
||||
*/
|
||||
public long getActual() {
|
||||
return actual;
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #region - AffectedRows
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
*/
|
||||
public static void checkAffectedRows(int expected, int actualRowCount) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(expected, actualRowCount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessage 异常信息
|
||||
*/
|
||||
public static void checkAffectedRows(int expected, int actualRowCount,
|
||||
@Nullable String errorMessage) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(expected, actualRowCount, errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageSupplier 异常信息
|
||||
*/
|
||||
public static void checkAffectedRows(int expected, int actualRowCount,
|
||||
Supplier<String> errorMessageSupplier) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(expected, actualRowCount, errorMessageSupplier.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageTemplate 异常信息模板
|
||||
* @param errorMessageArgs 异常信息参数
|
||||
*/
|
||||
public static void checkAffectedRows(int expected, int actualRowCount,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(expected, actualRowCount,
|
||||
String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
*/
|
||||
public static void checkAffectedRows(long expected, long actualRowCount) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(expected, actualRowCount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessage 异常信息
|
||||
*/
|
||||
public static void checkAffectedRows(long expected, long actualRowCount,
|
||||
@Nullable String errorMessage) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(expected, actualRowCount, errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageSupplier 异常信息
|
||||
*/
|
||||
public static void checkAffectedRows(long expected, long actualRowCount,
|
||||
Supplier<String> errorMessageSupplier) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(expected, actualRowCount, errorMessageSupplier.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageTemplate 异常信息模板
|
||||
* @param errorMessageArgs 异常信息参数
|
||||
*/
|
||||
public static void checkAffectedRows(long expected, long actualRowCount,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new JdbcUpdateAffectedIncorrectNumberOfRowsException(expected, actualRowCount,
|
||||
String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
*/
|
||||
public static void checkAffectedOneRow(int actualRowCount) {
|
||||
checkAffectedRows(1, actualRowCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessage 异常信息
|
||||
*/
|
||||
public static void checkAffectedOneRow(int actualRowCount, String errorMessage) {
|
||||
checkAffectedRows(1, actualRowCount, errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageSupplier 异常信息
|
||||
*/
|
||||
public static void checkAffectedOneRow(int actualRowCount, Supplier<String> errorMessageSupplier) {
|
||||
checkAffectedRows(1, actualRowCount, errorMessageSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageTemplate 异常信息模板
|
||||
* @param errorMessageArgs 异常信息参数
|
||||
*/
|
||||
public static void checkAffectedOneRow(int actualRowCount,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
checkAffectedRows(1, actualRowCount, errorMessageTemplate, errorMessageArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param result 实际影响的数据量
|
||||
*/
|
||||
public static void checkAffectedOneRow(long result) {
|
||||
checkAffectedRows(1L, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessage 异常信息
|
||||
*/
|
||||
public static void checkAffectedOneRow(long actualRowCount, String errorMessage) {
|
||||
checkAffectedRows(1L, actualRowCount, errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageSupplier 异常信息
|
||||
*/
|
||||
public static void checkAffectedOneRow(long actualRowCount, Supplier<String> errorMessageSupplier) {
|
||||
checkAffectedRows(1L, actualRowCount, errorMessageSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link JdbcUpdateAffectedIncorrectNumberOfRowsException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageTemplate 异常信息模板
|
||||
* @param errorMessageArgs 异常信息参数
|
||||
*/
|
||||
public static void checkAffectedOneRow(long actualRowCount,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
checkAffectedRows(1L, actualRowCount, errorMessageTemplate, errorMessageArgs);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - AffectedRows
|
||||
// ================================
|
||||
|
||||
}
|
||||
@@ -22,7 +22,6 @@ import java.util.function.Supplier;
|
||||
import javax.annotation.Nullable;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.exception.DataNotExistsException;
|
||||
import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
|
||||
|
||||
/**
|
||||
* 断言工具
|
||||
@@ -445,208 +444,6 @@ public class AssertTools {
|
||||
// #endregion - Exists
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - AffectedRows
|
||||
// ================================
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
*/
|
||||
public static void checkAffectedRows(int expected, int actualRowCount) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new DataOperationResultException(expected, actualRowCount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessage 异常信息
|
||||
*/
|
||||
public static void checkAffectedRows(int expected, int actualRowCount,
|
||||
@Nullable String errorMessage) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new DataOperationResultException(expected, actualRowCount, errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageSupplier 异常信息
|
||||
*/
|
||||
public static void checkAffectedRows(int expected, int actualRowCount,
|
||||
Supplier<String> errorMessageSupplier) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new DataOperationResultException(expected, actualRowCount, errorMessageSupplier.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageTemplate 异常信息模板
|
||||
* @param errorMessageArgs 异常信息参数
|
||||
*/
|
||||
public static void checkAffectedRows(int expected, int actualRowCount,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new DataOperationResultException(expected, actualRowCount,
|
||||
String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
*/
|
||||
public static void checkAffectedRows(long expected, long actualRowCount) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new DataOperationResultException(expected, actualRowCount);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessage 异常信息
|
||||
*/
|
||||
public static void checkAffectedRows(long expected, long actualRowCount,
|
||||
@Nullable String errorMessage) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new DataOperationResultException(expected, actualRowCount, errorMessage);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageSupplier 异常信息
|
||||
*/
|
||||
public static void checkAffectedRows(long expected, long actualRowCount,
|
||||
Supplier<String> errorMessageSupplier) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new DataOperationResultException(expected, actualRowCount, errorMessageSupplier.get());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量与预计不同时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param expected 预期影响的行数
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageTemplate 异常信息模板
|
||||
* @param errorMessageArgs 异常信息参数
|
||||
*/
|
||||
public static void checkAffectedRows(long expected, long actualRowCount,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
if (expected != actualRowCount) {
|
||||
throw new DataOperationResultException(expected, actualRowCount,
|
||||
String.format(errorMessageTemplate, errorMessageArgs));
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
*/
|
||||
public static void checkAffectedOneRow(int actualRowCount) {
|
||||
checkAffectedRows(1, actualRowCount);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessage 异常信息
|
||||
*/
|
||||
public static void checkAffectedOneRow(int actualRowCount, String errorMessage) {
|
||||
checkAffectedRows(1, actualRowCount, errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageSupplier 异常信息
|
||||
*/
|
||||
public static void checkAffectedOneRow(int actualRowCount, Supplier<String> errorMessageSupplier) {
|
||||
checkAffectedRows(1, actualRowCount, errorMessageSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageTemplate 异常信息模板
|
||||
* @param errorMessageArgs 异常信息参数
|
||||
*/
|
||||
public static void checkAffectedOneRow(int actualRowCount,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
checkAffectedRows(1, actualRowCount, errorMessageTemplate, errorMessageArgs);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param result 实际影响的数据量
|
||||
*/
|
||||
public static void checkAffectedOneRow(long result) {
|
||||
checkAffectedRows(1L, result);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessage 异常信息
|
||||
*/
|
||||
public static void checkAffectedOneRow(long actualRowCount, String errorMessage) {
|
||||
checkAffectedRows(1L, actualRowCount, errorMessage);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageSupplier 异常信息
|
||||
*/
|
||||
public static void checkAffectedOneRow(long actualRowCount, Supplier<String> errorMessageSupplier) {
|
||||
checkAffectedRows(1L, actualRowCount, errorMessageSupplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 当影响的数据量不为 1 时抛出 {@link DataOperationResultException}。
|
||||
*
|
||||
* @param actualRowCount 实际影响的行数
|
||||
* @param errorMessageTemplate 异常信息模板
|
||||
* @param errorMessageArgs 异常信息参数
|
||||
*/
|
||||
public static void checkAffectedOneRow(long actualRowCount,
|
||||
String errorMessageTemplate, Object... errorMessageArgs) {
|
||||
checkAffectedRows(1L, actualRowCount, errorMessageTemplate, errorMessageArgs);
|
||||
}
|
||||
|
||||
// ================================
|
||||
// #endregion - AffectedRows
|
||||
// ================================
|
||||
|
||||
// ================================
|
||||
// #region - Condition
|
||||
// ================================
|
||||
|
||||
@@ -18,6 +18,7 @@ package xyz.zhouxy.plusone.commons.util;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.*;
|
||||
import static xyz.zhouxy.plusone.commons.util.AssertTools.*;
|
||||
import static xyz.zhouxy.plusone.commons.exception.system.JdbcUpdateAffectedIncorrectNumberOfRowsException.*;
|
||||
|
||||
import java.lang.reflect.Constructor;
|
||||
import java.time.LocalDate;
|
||||
@@ -28,7 +29,7 @@ import java.util.function.Supplier;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
import xyz.zhouxy.plusone.commons.exception.DataNotExistsException;
|
||||
import xyz.zhouxy.plusone.commons.exception.system.DataOperationResultException;
|
||||
import xyz.zhouxy.plusone.commons.exception.system.JdbcUpdateAffectedIncorrectNumberOfRowsException;
|
||||
|
||||
@SuppressWarnings("null")
|
||||
public class AssertToolsTests {
|
||||
@@ -786,7 +787,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedRows(expectedValue, 25);
|
||||
|
||||
DataOperationResultException e0 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e0 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedRows(expectedValue, 108));
|
||||
assertEquals(String.format("The number of rows affected is expected to be %d, but is: %d", expectedValue, 108),
|
||||
e0.getMessage());
|
||||
@@ -801,7 +802,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedRows(expectedValue, 25, message);
|
||||
|
||||
DataOperationResultException e1 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e1 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedRows(expectedValue, 108, message));
|
||||
assertEquals(message, e1.getMessage());
|
||||
|
||||
@@ -809,7 +810,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedRows(expectedValue, 25, nullMessage);
|
||||
|
||||
e1 = assertThrows(DataOperationResultException.class,
|
||||
e1 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedRows(expectedValue, 108, nullMessage));
|
||||
assertNull(e1.getMessage());
|
||||
}
|
||||
@@ -822,7 +823,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedRows(expectedValue, 25, messageSupplier);
|
||||
|
||||
DataOperationResultException e2 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e2 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedRows(expectedValue, 108, messageSupplier));
|
||||
assertEquals(messageSupplier.get(), e2.getMessage());
|
||||
|
||||
@@ -840,7 +841,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedRows(expectedValue, 25, "预计是 %d,结果是 %d。", expectedValue, 25);
|
||||
|
||||
DataOperationResultException e3 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e3 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedRows(expectedValue, 108, "预计是 %d,结果是 %d。", expectedValue, 108));
|
||||
assertEquals("预计是 25,结果是 108。", e3.getMessage());
|
||||
|
||||
@@ -856,7 +857,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedRows(expectedValue, 25L);
|
||||
|
||||
DataOperationResultException e0 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e0 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedRows(expectedValue, 108L));
|
||||
assertEquals(String.format("The number of rows affected is expected to be %d, but is: %d", expectedValue, 108L),
|
||||
e0.getMessage());
|
||||
@@ -871,7 +872,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedRows(expectedValue, 25L, message);
|
||||
|
||||
DataOperationResultException e1 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e1 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedRows(expectedValue, 108L, message));
|
||||
assertEquals(message, e1.getMessage());
|
||||
|
||||
@@ -879,7 +880,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedRows(expectedValue, 25L, nullMessage);
|
||||
|
||||
e1 = assertThrows(DataOperationResultException.class,
|
||||
e1 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedRows(expectedValue, 108L, nullMessage));
|
||||
assertNull(e1.getMessage());
|
||||
}
|
||||
@@ -892,7 +893,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedRows(expectedValue, 25L, messageSupplier);
|
||||
|
||||
DataOperationResultException e2 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e2 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedRows(expectedValue, 108L, messageSupplier));
|
||||
assertEquals(messageSupplier.get(), e2.getMessage());
|
||||
|
||||
@@ -910,7 +911,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedRows(expectedValue, 25L, "预计是 %d,结果是 %d。", expectedValue, 25L);
|
||||
|
||||
DataOperationResultException e3 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e3 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedRows(expectedValue, 108L, "预计是 %d,结果是 %d。", expectedValue, 108L));
|
||||
assertEquals("预计是 25,结果是 108。", e3.getMessage());
|
||||
|
||||
@@ -924,7 +925,7 @@ public class AssertToolsTests {
|
||||
void testCheckAffectedOneRow_int() {
|
||||
checkAffectedOneRow(1);
|
||||
|
||||
DataOperationResultException e0 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e0 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedOneRow(108));
|
||||
assertEquals(String.format("The number of rows affected is expected to be 1, but is: %d", 108),
|
||||
e0.getMessage());
|
||||
@@ -937,7 +938,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedOneRow(1, message);
|
||||
|
||||
DataOperationResultException e1 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e1 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedOneRow(108, message));
|
||||
assertEquals(message, e1.getMessage());
|
||||
|
||||
@@ -945,7 +946,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedOneRow(1, nullMessage);
|
||||
|
||||
e1 = assertThrows(DataOperationResultException.class,
|
||||
e1 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedOneRow(108, nullMessage));
|
||||
assertNull(e1.getMessage());
|
||||
}
|
||||
@@ -956,7 +957,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedOneRow(1, messageSupplier);
|
||||
|
||||
DataOperationResultException e2 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e2 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedOneRow(108, messageSupplier));
|
||||
assertEquals(messageSupplier.get(), e2.getMessage());
|
||||
|
||||
@@ -972,7 +973,7 @@ public class AssertToolsTests {
|
||||
void testCheckAffectedOneRow_int_messageFormat() {
|
||||
checkAffectedOneRow(1, "预计是 %d,结果是 %d。", 1, 108);
|
||||
|
||||
DataOperationResultException e3 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e3 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedOneRow(108, "预计是 %d,结果是 %d。", 1, 108));
|
||||
assertEquals("预计是 1,结果是 108。", e3.getMessage());
|
||||
|
||||
@@ -986,7 +987,7 @@ public class AssertToolsTests {
|
||||
void testCheckAffectedOneRow_long() {
|
||||
checkAffectedOneRow(1L);
|
||||
|
||||
DataOperationResultException e0 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e0 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedOneRow(108L));
|
||||
assertEquals(String.format("The number of rows affected is expected to be 1, but is: %d", 108L),
|
||||
e0.getMessage());
|
||||
@@ -999,7 +1000,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedOneRow(1L, message);
|
||||
|
||||
DataOperationResultException e1 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e1 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedOneRow(108L, message));
|
||||
assertEquals(message, e1.getMessage());
|
||||
|
||||
@@ -1007,7 +1008,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedOneRow(1L, nullMessage);
|
||||
|
||||
e1 = assertThrows(DataOperationResultException.class,
|
||||
e1 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedOneRow(108L, nullMessage));
|
||||
assertNull(e1.getMessage());
|
||||
}
|
||||
@@ -1018,7 +1019,7 @@ public class AssertToolsTests {
|
||||
|
||||
checkAffectedOneRow(1L, messageSupplier);
|
||||
|
||||
DataOperationResultException e2 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e2 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedOneRow(108L, messageSupplier));
|
||||
assertEquals(messageSupplier.get(), e2.getMessage());
|
||||
|
||||
@@ -1034,7 +1035,7 @@ public class AssertToolsTests {
|
||||
void testCheckAffectedOneRow_long_messageFormat() {
|
||||
checkAffectedOneRow(1L, "预计是 %d,结果是 %d。", 1L, 108L);
|
||||
|
||||
DataOperationResultException e3 = assertThrows(DataOperationResultException.class,
|
||||
JdbcUpdateAffectedIncorrectNumberOfRowsException e3 = assertThrows(JdbcUpdateAffectedIncorrectNumberOfRowsException.class,
|
||||
() -> checkAffectedOneRow(108L, "预计是 %d,结果是 %d。", 1L, 108L));
|
||||
assertEquals("预计是 1,结果是 108。", e3.getMessage());
|
||||
|
||||
|
||||
Reference in New Issue
Block a user