You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by to...@apache.org on 2022/09/12 03:03:05 UTC
[shardingsphere] branch master updated: Add ShardingSpherePreconditions.checkNotNull (#20933)
This is an automated email from the ASF dual-hosted git repository.
totalo pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/shardingsphere.git
The following commit(s) were added to refs/heads/master by this push:
new bcc23f09bc5 Add ShardingSpherePreconditions.checkNotNull (#20933)
bcc23f09bc5 is described below
commit bcc23f09bc5f5bf46e5bb2bdb2723092928e72d2
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Mon Sep 12 11:02:57 2022 +0800
Add ShardingSpherePreconditions.checkNotNull (#20933)
---
.../exception/ShardingSpherePreconditions.java | 25 +++++++++++++++++
.../exception/ShardingSpherePreconditionsTest.java | 32 ++++++++++++++++++++++
2 files changed, 57 insertions(+)
diff --git a/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.java b/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.java
index 4a004fa96ea..a5d84cf18fb 100644
--- a/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.java
+++ b/shardingsphere-infra/shardingsphere-infra-util/src/main/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditions.java
@@ -67,4 +67,29 @@ public final class ShardingSpherePreconditions {
throw exceptionIfUnexpected;
}
}
+
+ /**
+ * Ensures that an object reference passed as a parameter to the calling method is not null.
+ *
+ * @param reference object reference to be checked
+ * @param exceptionIfUnexpected exception thrown if object is null
+ */
+ public static void checkNotNull(final Object reference, final ShardingSphereExternalException exceptionIfUnexpected) {
+ if (null == reference) {
+ throw exceptionIfUnexpected;
+ }
+ }
+
+ /**
+ * Ensures that an object reference passed as a parameter to the calling method is not null.
+ *
+ * @param reference object reference to be checked
+ * @param exceptionIfUnexpected exception thrown if object is null
+ * @throws ShardingSphereInternalException ShardingSphere internal exception
+ */
+ public static void checkNotNull(final Object reference, final ShardingSphereInternalException exceptionIfUnexpected) throws ShardingSphereInternalException {
+ if (null == reference) {
+ throw exceptionIfUnexpected;
+ }
+ }
}
diff --git a/shardingsphere-infra/shardingsphere-infra-util/src/test/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditionsTest.java b/shardingsphere-infra/shardingsphere-infra-util/src/test/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditionsTest.java
index 06e9f6930e3..48aa4cea7f6 100644
--- a/shardingsphere-infra/shardingsphere-infra-util/src/test/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditionsTest.java
+++ b/shardingsphere-infra/shardingsphere-infra-util/src/test/java/org/apache/shardingsphere/infra/util/exception/ShardingSpherePreconditionsTest.java
@@ -46,4 +46,36 @@ public final class ShardingSpherePreconditionsTest {
public void assertCheckStateToNotThrowInternalException() throws ShardingSphereInternalException {
ShardingSpherePreconditions.checkState(true, new ShardingSphereInternalExceptionFixture("message"));
}
+
+ @Test(expected = SQLException.class)
+ public void assertCheckStateToThrowsSQLException() throws SQLException {
+ ShardingSpherePreconditions.checkState(false, new SQLException("message"));
+ }
+
+ @Test
+ public void assertCheckStateToNotThrowSQLException() throws SQLException {
+ ShardingSpherePreconditions.checkState(true, new SQLException("message"));
+ }
+
+ @Test(expected = ShardingSphereExternalException.class)
+ public void assertCheckNotNullToThrowsExternalException() {
+ ShardingSpherePreconditions.checkNotNull(null, new SQLWrapperException(new SQLException()));
+ }
+
+ @SuppressWarnings("ObviousNullCheck")
+ @Test
+ public void assertCheckNotNullToNotThrowExternalException() {
+ ShardingSpherePreconditions.checkNotNull(new Object(), new SQLWrapperException(new SQLException()));
+ }
+
+ @Test(expected = ShardingSphereInternalException.class)
+ public void assertCheckNotNullToThrowsInternalException() throws ShardingSphereInternalException {
+ ShardingSpherePreconditions.checkNotNull(null, new ShardingSphereInternalExceptionFixture("message"));
+ }
+
+ @SuppressWarnings("ObviousNullCheck")
+ @Test
+ public void assertCheckNotNullToNotThrowInternalException() throws ShardingSphereInternalException {
+ ShardingSpherePreconditions.checkNotNull(new Object(), new ShardingSphereInternalExceptionFixture("message"));
+ }
}