You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by yx...@apache.org on 2024/08/18 07:15:39 UTC
(shardingsphere) branch master updated: Fix sonar issue on SQLHintUtilsTest (#32578)
This is an automated email from the ASF dual-hosted git repository.
yx9o 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 759fe5c9349 Fix sonar issue on SQLHintUtilsTest (#32578)
759fe5c9349 is described below
commit 759fe5c934967438b4601184b0e67c2846b9d7c3
Author: Liang Zhang <zh...@apache.org>
AuthorDate: Sun Aug 18 15:15:33 2024 +0800
Fix sonar issue on SQLHintUtilsTest (#32578)
---
.../infra/hint/SQLHintUtilsTest.java | 67 ++++++++++++++--------
1 file changed, 42 insertions(+), 25 deletions(-)
diff --git a/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java b/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java
index 3c32ea0dc33..0adba0a2c90 100644
--- a/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java
+++ b/infra/common/src/test/java/org/apache/shardingsphere/infra/hint/SQLHintUtilsTest.java
@@ -18,13 +18,20 @@
package org.apache.shardingsphere.infra.hint;
import org.junit.jupiter.api.Test;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.ArgumentsProvider;
+import org.junit.jupiter.params.provider.ArgumentsSource;
import java.math.BigInteger;
import java.util.Arrays;
import java.util.Collections;
+import java.util.stream.Stream;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;
class SQLHintUtilsTest {
@@ -90,36 +97,46 @@ class SQLHintUtilsTest {
assertTrue(actual.isShadow());
}
- @Test
- void assertFindHintDataSourceNameExist() {
- HintValueContext actual = SQLHintUtils.extractHint("/* SHARDINGSPHERE_HINT: DATA_SOURCE_NAME=ds_1 */");
- assertTrue(actual.findHintDataSourceName().isPresent());
- assertThat(actual.findHintDataSourceName().get(), is("ds_1"));
+ @ParameterizedTest(name = "extractHintFormat:{0}")
+ @ArgumentsSource(ExtractHintTestCaseArgumentsProvider.class)
+ void assertExtractHintFormat(@SuppressWarnings("unused") final String name, final String actualSQL, final boolean found) {
+ HintValueContext actual = SQLHintUtils.extractHint(actualSQL);
+ if (found) {
+ assertTrue(actual.findHintDataSourceName().isPresent());
+ assertThat(actual.findHintDataSourceName().get(), is("foo_ds"));
+ } else {
+ assertFalse(actual.findHintDataSourceName().isPresent());
+ }
}
- @Test
- void assertFindHintDataSourceNameAliasExist() {
- HintValueContext actual = SQLHintUtils.extractHint("/* ShardingSphere hint: dataSourceName=ds_1 */");
- assertTrue(actual.findHintDataSourceName().isPresent());
- assertThat(actual.findHintDataSourceName().get(), is("ds_1"));
+ @ParameterizedTest(name = "extractHintFormat:{0}")
+ @ArgumentsSource(RemoveHintTestCaseArgumentsProvider.class)
+ void assertRemoveHint(@SuppressWarnings("unused") final String name, final String actualSQL, final String expectedSQL) {
+ assertThat(SQLHintUtils.removeHint(actualSQL), is(expectedSQL));
}
- @Test
- void assertFindHintDataSourceNameWithDBeaverHint() {
- HintValueContext actual = SQLHintUtils.extractHint("/* ApplicationName=DBeaver 24.1.0 - SQLEditor <Script-84.sql> */ /* SHARDINGSPHERE_HINT: DATA_SOURCE_NAME=ds_1*/ SELECT * FROM t_order");
- assertTrue(actual.findHintDataSourceName().isPresent());
- assertThat(actual.findHintDataSourceName().get(), is("ds_1"));
+ private static final class ExtractHintTestCaseArgumentsProvider implements ArgumentsProvider {
+
+ @Override
+ public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
+ return Stream.of(
+ Arguments.of("PrefixNotFound", "/* FOO_HINT: xxx=xxx */", false),
+ Arguments.of("ContentNotMatch", "/* SHARDINGSPHERE_HINT: xxx=xxx */", false),
+ Arguments.of("UnderlineMode", "/* SHARDINGSPHERE_HINT: DATA_SOURCE_NAME=foo_ds */", true),
+ Arguments.of("SpaceMode", "/* ShardingSphere hint: dataSourceName=foo_ds */", true),
+ Arguments.of("DBeaverHint", "/* ApplicationName=DBeaver 24.1.0 - SQLEditor <Script-84.sql> */ /* SHARDINGSPHERE_HINT: DATA_SOURCE_NAME=foo_ds*/ SELECT * FROM t_order", true));
+ }
}
- @Test
- void assertRemoveHint() {
- String actual = SQLHintUtils.removeHint("/* SHARDINGSPHERE_HINT: DATA_SOURCE_NAME=ds_1*/ SELECT * FROM t_order");
- assertThat(actual, is("SELECT * FROM t_order"));
- }
-
- @Test
- void assertRemoveHintWithDBeaverHint() {
- String actual = SQLHintUtils.removeHint("/* ApplicationName=DBeaver 24.1.0 - SQLEditor <Script-84.sql> */ /* SHARDINGSPHERE_HINT: DATA_SOURCE_NAME=ds_1*/ SELECT * FROM t_order");
- assertThat(actual, is("/* ApplicationName=DBeaver 24.1.0 - SQLEditor <Script-84.sql> */ SELECT * FROM t_order"));
+ private static final class RemoveHintTestCaseArgumentsProvider implements ArgumentsProvider {
+
+ @Override
+ public Stream<? extends Arguments> provideArguments(final ExtensionContext extensionContext) {
+ return Stream.of(
+ Arguments.of("UnderlineMode", "/* SHARDINGSPHERE_HINT: DATA_SOURCE_NAME=foo_ds*/ SELECT * FROM t_order", "SELECT * FROM t_order"),
+ Arguments.of("SpaceMode", "/* ShardingSphere hint: DATA_SOURCE_NAME=foo_ds*/ SELECT * FROM t_order", "SELECT * FROM t_order"),
+ Arguments.of("DBeaverHint", "/* ApplicationName=DBeaver 24.1.0 - SQLEditor <Script-84.sql> */ /* SHARDINGSPHERE_HINT: DATA_SOURCE_NAME=foo_ds*/ SELECT * FROM t_order",
+ "/* ApplicationName=DBeaver 24.1.0 - SQLEditor <Script-84.sql> */ SELECT * FROM t_order"));
+ }
}
}