This is an automated email from the ASF dual-hosted git repository.
sunnianjun 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 278ec3d6f9c Add parameterized test on mask algorithm (#30589)
278ec3d6f9c is described below
commit 278ec3d6f9cb99a3e5da8719e02c915215d0e70b
Author: Liang Zhang <[email protected]>
AuthorDate: Thu Mar 21 22:00:10 2024 +0800
Add parameterized test on mask algorithm (#30589)
* Refactor ReadwriteSplittingRuleConfiguration
* Add test cases for MaskRuleConfiguration
* Add test cases for MaskRuleConfiguration
* Refactor test cases for mask-core module
* Refactor test cases for mask-core module
* Refactor test cases for mask-core module
* Refactor test cases for mask-core module
* Refactor test cases for mask-core module
* Add parameterized test on mask algorithm
* Add parameterized test on mask algorithm
* Add parameterized test on mask algorithm
---
.../GenericTableRandomReplaceAlgorithm.java | 4 +-
.../cover/KeepFirstNLastMMaskAlgorithmTest.java | 135 ++++++++------------
.../cover/KeepFromXToYMaskAlgorithmTest.java | 142 +++++++++------------
.../cover/MaskAfterSpecialCharsAlgorithmTest.java | 117 +++++++----------
.../cover/MaskBeforeSpecialCharsAlgorithmTest.java | 96 +++++++-------
.../cover/MaskFirstNLastMMaskAlgorithmTest.java | 135 ++++++++------------
.../cover/MaskFromXToYMaskAlgorithmTest.java | 140 ++++++++------------
.../mask/algorithm/hash/MD5MaskAlgorithmTest.java | 48 ++++---
.../MaskAlgorithmArgumentsProvider.java | 39 ++++++
.../parameterized/MaskAlgorithmAssertions.java | 57 +++++++++
.../parameterized/MaskAlgorithmCaseAssert.java | 40 ++++++
11 files changed, 477 insertions(+), 476 deletions(-)
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java
index 39a18aa5d96..768c1a4a428 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/GenericTableRandomReplaceAlgorithm.java
@@ -65,9 +65,9 @@ public final class GenericTableRandomReplaceAlgorithm
implements MaskAlgorithm<O
uppercaseLetterCodes =
splitPropsToList(props.getProperty(UPPERCASE_LETTER_CODES,
DEFAULT_UPPERCASE_LETTER_CODES));
lowercaseLetterCodes =
splitPropsToList(props.getProperty(LOWERCASE_LETTER_CODES,
DEFAULT_LOWERCASE_LETTER_CODES));
digitalCodes = splitPropsToList(props.getProperty(DIGITAL_CODES,
DEFAULT_DIGITAL_CODES));
- ShardingSpherePreconditions.checkState(!digitalCodes.isEmpty(), () ->
new AlgorithmInitializationException(this, String.format("'%s' must be not
empty", DIGITAL_CODES)));
+ ShardingSpherePreconditions.checkState(!digitalCodes.isEmpty(), () ->
new AlgorithmInitializationException(this, "'%s' must be not empty",
DIGITAL_CODES));
specialCodes = splitPropsToList(props.getProperty(SPECIAL_CODES,
DEFAULT_SPECIAL_CODES));
- ShardingSpherePreconditions.checkState(!specialCodes.isEmpty(), () ->
new AlgorithmInitializationException(this, String.format("'%s' must be not
empty", SPECIAL_CODES)));
+ ShardingSpherePreconditions.checkState(!specialCodes.isEmpty(), () ->
new AlgorithmInitializationException(this, "'%s' must be not empty",
SPECIAL_CODES));
}
private List<Character> splitPropsToList(final String props) {
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithmTest.java
index 33a58a61704..a9ff481ae81 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithmTest.java
@@ -17,97 +17,74 @@
package org.apache.shardingsphere.mask.algorithm.cover;
-import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmAssertions;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmCaseAssert;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmArgumentsProvider;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ArgumentsSource;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Properties;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
class KeepFirstNLastMMaskAlgorithmTest {
- private KeepFirstNLastMMaskAlgorithm maskAlgorithm;
-
- private KeepFirstNLastMMaskAlgorithm sameFirstNLastMMaskAlgorithm;
-
- @BeforeEach
- void setUp() {
- maskAlgorithm = (KeepFirstNLastMMaskAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FIRST_N_LAST_M",
- PropertiesBuilder.build(new Property("first-n", "3"), new
Property("last-m", "5"), new Property("replace-char", "*")));
- sameFirstNLastMMaskAlgorithm = (KeepFirstNLastMMaskAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FIRST_N_LAST_M",
- PropertiesBuilder.build(new Property("first-n", "5"), new
Property("last-m", "5"), new Property("replace-char", "*")));
- }
-
- @Test
- void assertMaskWithNullValue() {
- assertNull(maskAlgorithm.mask(null));
- }
-
- @Test
- void assertMask() {
- assertThat(maskAlgorithm.mask("abc123456"), is("abc*23456"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc123456789"),
is("abc12**56789"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthLessThanFirstN() {
- assertThat(maskAlgorithm.mask("ab"), is("ab"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc"), is("abc"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthEqualsFirstN() {
- assertThat(maskAlgorithm.mask("abc"), is("abc"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc12"), is("abc12"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthLessThanLastM() {
- assertThat(maskAlgorithm.mask("abc1"), is("abc1"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc1"), is("abc1"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthEqualsLastM() {
- assertThat(maskAlgorithm.mask("abc12"), is("abc12"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc12"), is("abc12"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthLessThanFirstNPlusLastM() {
- assertThat(maskAlgorithm.mask("abc1234"), is("abc1234"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc123456"),
is("abc123456"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthEqualsFirstNPlusLastM() {
- assertThat(maskAlgorithm.mask("abc12345"), is("abc12345"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc1234567"),
is("abc1234567"));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmInitArgumentsProvider.class)
+ void assertInit(final String type, @SuppressWarnings("unused") final
String name, final Properties props) {
+ MaskAlgorithmAssertions.assertInitFailedWithInvalidProperties(type,
props);
}
- @Test
- void assertInitWhenFirstNIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("first-n",
""), new Property("last-m", "5"), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FIRST_N_LAST_M", props));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmMaskArgumentsProvider.class)
+ void assertMask(final String type, @SuppressWarnings("unused") final
String name, final Properties props, final Object plainValue, final Object
maskedValue) {
+ MaskAlgorithmAssertions.assertMask(type, props, plainValue,
maskedValue);
}
- @Test
- void assertInitWhenLastMIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("first-n",
"2"), new Property("last-m", ""), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FIRST_N_LAST_M", props));
+ private static class AlgorithmInitArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmInitArgumentsProvider() {
+ super("KEEP_FIRST_N_LAST_M");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("empty_first_N",
PropertiesBuilder.build(new Property("first-n", ""), new Property("last-m",
"5"), new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("empty_last_N",
PropertiesBuilder.build(new Property("first-n", "2"), new Property("last-m",
""), new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("empty_replace_char",
PropertiesBuilder.build(new Property("first-n", "2"), new Property("last-m",
"5"), new Property("replace-char", ""))));
+ }
}
- @Test
- void assertInitWhenReplaceCharIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("first-n",
"2"), new Property("last-m", "5"), new Property("replace-char", ""));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FIRST_N_LAST_M", props));
+ private static class AlgorithmMaskArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmMaskArgumentsProvider() {
+ super("KEEP_FIRST_N_LAST_M");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ Properties diffProps = PropertiesBuilder.build(new
Property("first-n", "3"), new Property("last-m", "5"), new
Property("replace-char", "*"));
+ Properties sameProps = PropertiesBuilder.build(new
Property("first-n", "5"), new Property("last-m", "5"), new
Property("replace-char", "*"));
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("null_value", diffProps, null,
null),
+ new MaskAlgorithmCaseAssert("empty_string", diffProps, "",
""),
+ new MaskAlgorithmCaseAssert("normal_with_diff", diffProps,
"abc123456", "abc*23456"),
+ new MaskAlgorithmCaseAssert("normal_with_same", sameProps,
"abc123456789", "abc12**56789"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_less_than_first_n_with_diff",
diffProps, "ab", "ab"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_less_than_first_n_with_same",
sameProps, "abc", "abc"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_first_n_with_diff",
diffProps, "abc", "abc"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_first_n_with_same",
sameProps, "abc12", "abc12"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_less_than_last_m_with_diff",
diffProps, "abc1", "abc1"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_less_than_last_m_with_same",
sameProps, "abc1", "abc1"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_last_m_with_diff",
diffProps, "abc12", "abc12"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_last_m_with_same",
sameProps, "abc12", "abc12"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_less_than_first_n_plus_last_m_with_diff",
diffProps, "abc1234", "abc1234"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_less_than_first_n_plus_last_m_with_same",
sameProps, "abc123456", "abc123456"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_first_n_plus_last_m_with_diff",
diffProps, "abc12345", "abc12345"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_first_n_plus_last_m_with_same",
sameProps, "abc1234567", "abc1234567"));
+ }
}
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithmTest.java
index 3d29a4d8668..4329994bbfd 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithmTest.java
@@ -17,103 +17,75 @@
package org.apache.shardingsphere.mask.algorithm.cover;
-import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmArgumentsProvider;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmAssertions;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmCaseAssert;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ArgumentsSource;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Properties;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
class KeepFromXToYMaskAlgorithmTest {
- private KeepFromXToYMaskAlgorithm maskAlgorithm;
-
- private KeepFromXToYMaskAlgorithm sameFromXToYMaskAlgorithm;
-
- @BeforeEach
- void setUp() {
- maskAlgorithm = (KeepFromXToYMaskAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FROM_X_TO_Y",
- PropertiesBuilder.build(new Property("from-x", "3"), new
Property("to-y", "5"), new Property("replace-char", "*")));
- sameFromXToYMaskAlgorithm = (KeepFromXToYMaskAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FROM_X_TO_Y",
- PropertiesBuilder.build(new Property("from-x", "5"), new
Property("to-y", "5"), new Property("replace-char", "*")));
- }
-
- @Test
- void assertMaskWithNullValue() {
- assertNull(maskAlgorithm.mask(null));
- }
-
- @Test
- void assertMask() {
- assertThat(maskAlgorithm.mask("abc123456"), is("***123***"));
- assertThat(sameFromXToYMaskAlgorithm.mask("abc123456"),
is("*****3***"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthLessThanFromXPlusOne() {
- assertThat(maskAlgorithm.mask("abc"), is("***"));
- assertThat(sameFromXToYMaskAlgorithm.mask("abc"), is("***"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthEqualsFromXPlusOne() {
- assertThat(maskAlgorithm.mask("abc1"), is("***1"));
- assertThat(sameFromXToYMaskAlgorithm.mask("abc123"), is("*****3"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthLessThanToYPlusOne() {
- assertThat(maskAlgorithm.mask("abc12"), is("***12"));
- assertThat(sameFromXToYMaskAlgorithm.mask("abc12"), is("*****"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthEqualsToYPlusOne() {
- assertThat(maskAlgorithm.mask("abc123"), is("***123"));
- assertThat(sameFromXToYMaskAlgorithm.mask("abc123"), is("*****3"));
- }
-
- @Test
- void assertInitWhenFromXIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("from-x", ""),
new Property("to-y", "5"), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FROM_X_TO_Y", props));
- }
-
- @Test
- void assertInitWhenToYIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("from-x",
"2"), new Property("to-y", ""), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FROM_X_TO_Y", props));
- }
-
- @Test
- void assertInitWhenReplaceCharIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("from-x",
"2"), new Property("to-y", "5"), new Property("replace-char", ""));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FROM_X_TO_Y", props));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmInitArgumentsProvider.class)
+ void assertInit(final String type, @SuppressWarnings("unused") final
String name, final Properties props) {
+ MaskAlgorithmAssertions.assertInitFailedWithInvalidProperties(type,
props);
}
- @Test
- void assertInitWhenFromXIsNotPositive() {
- Properties props = PropertiesBuilder.build(new Property("from-x",
"-3"), new Property("to-y", "5"), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FROM_X_TO_Y", props));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmMaskArgumentsProvider.class)
+ void assertMask(final String type, @SuppressWarnings("unused") final
String name, final Properties props, final Object plainValue, final Object
maskedValue) {
+ MaskAlgorithmAssertions.assertMask(type, props, plainValue,
maskedValue);
}
- @Test
- void assertInitWhenToYIsNotPositive() {
- Properties props = PropertiesBuilder.build(new Property("from-x",
"3"), new Property("to-y", "-5"), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FROM_X_TO_Y", props));
+ private static class AlgorithmInitArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmInitArgumentsProvider() {
+ super("KEEP_FROM_X_TO_Y");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("from_x_is_empty",
PropertiesBuilder.build(new Property("from-x", ""), new Property("to-y", "5"),
new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("to_y_is_empty",
PropertiesBuilder.build(new Property("from-x", "2"), new Property("to-y", ""),
new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("replace_char_is_empty",
PropertiesBuilder.build(new Property("from-x", "2"), new Property("to-y", "5"),
new Property("replace-char", ""))),
+ new MaskAlgorithmCaseAssert("from_x_is_not_positive",
PropertiesBuilder.build(new Property("from-x", "-3"), new Property("to-y",
"5"), new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("to_y_is_not_positive",
PropertiesBuilder.build(new Property("from-x", "3"), new Property("to-y",
"-5"), new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("from_x_greater_than_to_y",
PropertiesBuilder.build(new Property("from-x", "5"), new Property("to-y", "2"),
new Property("replace-char", ""))));
+ }
}
- @Test
- void assertInitWhenFromXGreaterThanToY() {
- Properties props = PropertiesBuilder.build(new Property("from-x",
"5"), new Property("to-y", "2"), new Property("replace-char", ""));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "KEEP_FROM_X_TO_Y", props));
+ private static class AlgorithmMaskArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmMaskArgumentsProvider() {
+ super("KEEP_FROM_X_TO_Y");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ Properties diffProps = PropertiesBuilder.build(new
Property("from-x", "3"), new Property("to-y", "5"), new
Property("replace-char", "*"));
+ Properties sameProps = PropertiesBuilder.build(new
Property("from-x", "5"), new Property("to-y", "5"), new
Property("replace-char", "*"));
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("null_value", diffProps, null,
null),
+ new MaskAlgorithmCaseAssert("empty_string", diffProps, "",
""),
+ new MaskAlgorithmCaseAssert("normal_with_diff", diffProps,
"abc123456", "***123***"),
+ new MaskAlgorithmCaseAssert("normal_with_same", sameProps,
"abc123456", "*****3***"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_less_than_from_X_plus_one_with_diff",
diffProps, "abc", "***"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_less_than_from_X_plus_one_with_same",
sameProps, "abc", "***"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_from_X_plus_one_with_diff",
diffProps, "abc1", "***1"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_from_X_plus_one_with_same",
sameProps, "abc123", "*****3"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_from_X_plus_one_with_diff",
diffProps, "abc1", "***1"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_from_X_plus_one_with_same",
sameProps, "abc123", "*****3"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_less_than_to_Y_plus_one_with_diff",
diffProps, "abc12", "***12"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_less_than_to_Y_plus_one_with_same",
sameProps, "abc12", "*****"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_to_Y_plus_one_with_diff",
diffProps, "abc123", "***123"),
+ new
MaskAlgorithmCaseAssert("plain_value_length_equals_to_Y_plus_one_with_same",
sameProps, "abc123", "*****3"));
+ }
}
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithmTest.java
index c522379b166..ae06a185743 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithmTest.java
@@ -17,90 +17,63 @@
package org.apache.shardingsphere.mask.algorithm.cover;
-import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmArgumentsProvider;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmAssertions;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmCaseAssert;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ArgumentsSource;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Properties;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.nullValue;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
class MaskAfterSpecialCharsAlgorithmTest {
- private MaskAfterSpecialCharsAlgorithm maskAlgorithm;
-
- @BeforeEach
- void setUp() {
- maskAlgorithm = (MaskAfterSpecialCharsAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_AFTER_SPECIAL_CHARS",
- PropertiesBuilder.build(new Property("special-chars", "d1"),
new Property("replace-char", "*")));
- }
-
- @Test
- void assertMaskWithNullValue() {
- assertNull(maskAlgorithm.mask(null));
- }
-
- @Test
- void assertMask() {
- assertThat(maskAlgorithm.mask("abcd134"), is("abcd1**"));
- }
-
- @Test
- void assertMaskWhenPlainValueMatchedMultipleSpecialChars() {
- assertThat(maskAlgorithm.mask("abcd1234d1234"), is("abcd1********"));
- }
-
- @Test
- void assertMaskEmptyString() {
- assertThat(maskAlgorithm.mask(""), is(""));
- }
-
- @Test
- void assertMaskNull() {
- assertThat(maskAlgorithm.mask(null), is(nullValue()));
- }
-
- @Test
- void assertMaskWhenPlainValueNotMatchedSpecialChars() {
- assertThat(maskAlgorithm.mask("abcd234"), is("abcd234"));
- }
-
- @Test
- void assertInitWhenSpecialCharsIsEmpty() {
- Properties props = PropertiesBuilder.build(new
Property("special-chars", ""), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_AFTER_SPECIAL_CHARS",
props));
- }
-
- @Test
- void assertInitWhenReplaceCharIsEmpty() {
- Properties props = PropertiesBuilder.build(new
Property("special-chars", "d1"), new Property("replace-char", ""));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_AFTER_SPECIAL_CHARS",
props));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmInitArgumentsProvider.class)
+ void assertInit(final String type, @SuppressWarnings("unused") final
String name, final Properties props) {
+ MaskAlgorithmAssertions.assertInitFailedWithInvalidProperties(type,
props);
}
- @Test
- void assertInitWhenReplaceCharIsMissing() {
- Properties props = PropertiesBuilder.build(new
Property("special-chars", "d1"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_AFTER_SPECIAL_CHARS",
props));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmMaskArgumentsProvider.class)
+ void assertMask(final String type, @SuppressWarnings("unused") final
String name, final Properties props, final Object plainValue, final Object
maskedValue) {
+ MaskAlgorithmAssertions.assertMask(type, props, plainValue,
maskedValue);
}
- @Test
- void assertInitWhenPropertiesAreEmpty() {
- Properties props = new Properties();
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_AFTER_SPECIAL_CHARS",
props));
+ private static class AlgorithmInitArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmInitArgumentsProvider() {
+ super("MASK_AFTER_SPECIAL_CHARS");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("empty_properties", new
Properties()),
+ new MaskAlgorithmCaseAssert("empty_special_char",
PropertiesBuilder.build(new Property("special-chars", ""))),
+ new MaskAlgorithmCaseAssert("empty_replace_char",
PropertiesBuilder.build(new Property("special-chars", "d1"), new
Property("replace-char", ""))),
+ new MaskAlgorithmCaseAssert("missing_replace_char",
PropertiesBuilder.build(new Property("special-chars", "d1"))));
+ }
}
- @Test
- void assertInitWhenValidPropertiesAreSet() {
- MaskBeforeSpecialCharsAlgorithm algorithm = new
MaskBeforeSpecialCharsAlgorithm();
- assertDoesNotThrow(() -> algorithm.init(PropertiesBuilder.build(new
Property("special-chars", "d1"), new Property("replace-char", "*"))));
+ private static class AlgorithmMaskArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmMaskArgumentsProvider() {
+ super("MASK_AFTER_SPECIAL_CHARS");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ Properties props = PropertiesBuilder.build(new
Property("special-chars", "d1"), new Property("replace-char", "*"));
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("null_value", props, null,
null),
+ new MaskAlgorithmCaseAssert("empty_string", props, "", ""),
+ new MaskAlgorithmCaseAssert("normal", props, "abcd134",
"abcd1**"),
+ new
MaskAlgorithmCaseAssert("match_multiple_special_chars", props, "abcd1234d1234",
"abcd1********"),
+ new MaskAlgorithmCaseAssert("not_match_special_chars",
props, "abcd234", "abcd234"));
+ }
}
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithmTest.java
index 0ef60c3ecc6..fb65aa4484d 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithmTest.java
@@ -17,71 +17,61 @@
package org.apache.shardingsphere.mask.algorithm.cover;
-import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmArgumentsProvider;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmAssertions;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmCaseAssert;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ArgumentsSource;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Properties;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.CoreMatchers.nullValue;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
class MaskBeforeSpecialCharsAlgorithmTest {
- private MaskBeforeSpecialCharsAlgorithm maskAlgorithm;
-
- @BeforeEach
- void setUp() {
- maskAlgorithm = (MaskBeforeSpecialCharsAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_BEFORE_SPECIAL_CHARS",
- PropertiesBuilder.build(new Property("special-chars", "d1"),
new Property("replace-char", "*")));
- }
-
- @Test
- void assertMaskWithNullValue() {
- assertNull(maskAlgorithm.mask(null));
- }
-
- @Test
- void assertMask() {
- assertThat(maskAlgorithm.mask("abcd134"), is("***d134"));
- }
-
- @Test
- void assertMaskWhenPlainValueMatchedMultipleSpecialChars() {
- assertThat(maskAlgorithm.mask("abcd1234d1234"), is("***d1234d1234"));
- }
-
- @Test
- void assertMaskEmptyString() {
- assertThat(maskAlgorithm.mask(""), is(""));
- }
-
- @Test
- void assertMaskNull() {
- assertThat(maskAlgorithm.mask(null), is(nullValue()));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmInitArgumentsProvider.class)
+ void assertInit(final String type, @SuppressWarnings("unused") final
String name, final Properties props) {
+ MaskAlgorithmAssertions.assertInitFailedWithInvalidProperties(type,
props);
}
- @Test
- void assertMaskWhenPlainValueNotMatchedSpecialChars() {
- assertThat(maskAlgorithm.mask("abcd234"), is("abcd234"));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmMaskArgumentsProvider.class)
+ void assertMask(final String type, @SuppressWarnings("unused") final
String name, final Properties props, final Object plainValue, final Object
maskedValue) {
+ MaskAlgorithmAssertions.assertMask(type, props, plainValue,
maskedValue);
}
- @Test
- void assertInitWhenSpecialCharsIsEmpty() {
- Properties props = PropertiesBuilder.build(new
Property("special-chars", ""), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_BEFORE_SPECIAL_CHARS",
props));
+ private static class AlgorithmInitArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmInitArgumentsProvider() {
+ super("MASK_BEFORE_SPECIAL_CHARS");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("empty_special_char",
PropertiesBuilder.build(new Property("special-chars", ""))),
+ new MaskAlgorithmCaseAssert("empty_replace_char",
PropertiesBuilder.build(new Property("special-chars", "d1"), new
Property("replace-char", ""))));
+ }
}
- @Test
- void assertInitWhenReplaceCharIsEmpty() {
- Properties props = PropertiesBuilder.build(new
Property("special-chars", "d1"), new Property("replace-char", ""));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_BEFORE_SPECIAL_CHARS",
props));
+ private static class AlgorithmMaskArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmMaskArgumentsProvider() {
+ super("MASK_BEFORE_SPECIAL_CHARS");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ Properties props = PropertiesBuilder.build(new
Property("special-chars", "d1"), new Property("replace-char", "*"));
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("null_value", props, null,
null),
+ new MaskAlgorithmCaseAssert("empty_string", props, "", ""),
+ new MaskAlgorithmCaseAssert("normal", props, "abcd134",
"***d134"),
+ new
MaskAlgorithmCaseAssert("match_multiple_special_chars", props, "abcd1234d1234",
"***d1234d1234"),
+ new MaskAlgorithmCaseAssert("not_match_special_chars",
props, "abcd234", "abcd234"));
+ }
}
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java
index 53bdac7f0f2..ea32beddb40 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithmTest.java
@@ -17,97 +17,74 @@
package org.apache.shardingsphere.mask.algorithm.cover;
-import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmArgumentsProvider;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmAssertions;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmCaseAssert;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ArgumentsSource;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Properties;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
class MaskFirstNLastMMaskAlgorithmTest {
- private MaskFirstNLastMMaskAlgorithm maskAlgorithm;
-
- private MaskFirstNLastMMaskAlgorithm sameFirstNLastMMaskAlgorithm;
-
- @BeforeEach
- void setUp() {
- maskAlgorithm = (MaskFirstNLastMMaskAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FIRST_N_LAST_M",
- PropertiesBuilder.build(new Property("first-n", "3"), new
Property("last-m", "5"), new Property("replace-char", "*")));
- sameFirstNLastMMaskAlgorithm = (MaskFirstNLastMMaskAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FIRST_N_LAST_M",
- PropertiesBuilder.build(new Property("first-n", "5"), new
Property("last-m", "5"), new Property("replace-char", "*")));
- }
-
- @Test
- void assertMaskWithNullValue() {
- assertNull(maskAlgorithm.mask(null));
- }
-
- @Test
- void assertMask() {
- assertThat(maskAlgorithm.mask("abc123456"), is("***1*****"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc123456789"),
is("*****34*****"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthLessThanFirstN() {
- assertThat(maskAlgorithm.mask("ab"), is("**"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc"), is("***"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthEqualsFirstN() {
- assertThat(maskAlgorithm.mask("abc"), is("***"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc12"), is("*****"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthLessThanLastM() {
- assertThat(maskAlgorithm.mask("abc1"), is("****"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc1"), is("****"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthEqualsLastM() {
- assertThat(maskAlgorithm.mask("abc12"), is("*****"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc12"), is("*****"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthLessThanFirstNPlusLastM() {
- assertThat(maskAlgorithm.mask("abc1234"), is("*******"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc123456"),
is("*********"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthEqualsFirstNPlusLastM() {
- assertThat(maskAlgorithm.mask("abc12345"), is("********"));
- assertThat(sameFirstNLastMMaskAlgorithm.mask("abc1234567"),
is("**********"));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmInitArgumentsProvider.class)
+ void assertInit(final String type, @SuppressWarnings("unused") final
String name, final Properties props) {
+ MaskAlgorithmAssertions.assertInitFailedWithInvalidProperties(type,
props);
}
- @Test
- void assertInitWhenFirstNIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("first-n",
""), new Property("last-m", "5"), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FIRST_N_LAST_M", props));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmMaskArgumentsProvider.class)
+ void assertMask(final String type, @SuppressWarnings("unused") final
String name, final Properties props, final Object plainValue, final Object
maskedValue) {
+ MaskAlgorithmAssertions.assertMask(type, props, plainValue,
maskedValue);
}
- @Test
- void assertInitWhenLastMIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("first-n",
"3"), new Property("last-m", ""), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FIRST_N_LAST_M", props));
+ private static class AlgorithmInitArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmInitArgumentsProvider() {
+ super("MASK_FIRST_N_LAST_M");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("empty_first_N",
PropertiesBuilder.build(new Property("first-n", ""), new Property("last-m",
"5"), new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("empty_last_M",
PropertiesBuilder.build(new Property("first-n", "3"), new Property("last-m",
""), new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("empty_replace_char",
PropertiesBuilder.build(new Property("first-n", "3"), new Property("last-m",
"5"), new Property("replace-char", ""))));
+ }
}
- @Test
- void assertInitWhenReplaceCharIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("first-n",
"3"), new Property("last-m", "5"), new Property("replace-char", ""));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FIRST_N_LAST_M", props));
+ private static class AlgorithmMaskArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmMaskArgumentsProvider() {
+ super("MASK_FIRST_N_LAST_M");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ Properties diffProps = PropertiesBuilder.build(new
Property("first-n", "3"), new Property("last-m", "5"), new
Property("replace-char", "*"));
+ Properties sameProps = PropertiesBuilder.build(new
Property("first-n", "5"), new Property("last-m", "5"), new
Property("replace-char", "*"));
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("null_value", diffProps, null,
null),
+ new MaskAlgorithmCaseAssert("empty_string", diffProps, "",
""),
+ new MaskAlgorithmCaseAssert("normal_with_diff", diffProps,
"abc123456", "***1*****"),
+ new MaskAlgorithmCaseAssert("normal_with_same", sameProps,
"abc123456789", "*****34*****"),
+ new
MaskAlgorithmCaseAssert("length_less_than_first_N_with_diff", diffProps, "ab",
"**"),
+ new
MaskAlgorithmCaseAssert("length_less_than_first_N_with_same", sameProps, "abc",
"***"),
+ new
MaskAlgorithmCaseAssert("length_equals_first_N_with_diff", diffProps, "abc",
"***"),
+ new
MaskAlgorithmCaseAssert("length_equals_first_N_with_same", sameProps, "abc12",
"*****"),
+ new
MaskAlgorithmCaseAssert("length_less_than_last_M_with_diff", diffProps, "abc1",
"****"),
+ new
MaskAlgorithmCaseAssert("length_less_than_last_M_with_same", sameProps, "abc1",
"****"),
+ new
MaskAlgorithmCaseAssert("length_equals_last_M_with_diff", diffProps, "abc12",
"*****"),
+ new
MaskAlgorithmCaseAssert("length_equals_last_M_with_same", sameProps, "abc12",
"*****"),
+ new
MaskAlgorithmCaseAssert("length_less_than_first_N_plus_last_M_with_diff",
diffProps, "abc1234", "*******"),
+ new
MaskAlgorithmCaseAssert("length_less_than_first_N_plus_last_M_with_same",
sameProps, "abc123456", "*********"),
+ new
MaskAlgorithmCaseAssert("length_equals_first_N_plus_last_M_with_diff",
diffProps, "abc12345", "********"),
+ new
MaskAlgorithmCaseAssert("length_equals_first_N_plus_last_M_with_same",
sameProps, "abc1234567", "**********"));
+ }
}
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java
index e43d5f3b66f..937717beb83 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithmTest.java
@@ -17,103 +17,73 @@
package org.apache.shardingsphere.mask.algorithm.cover;
-import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmArgumentsProvider;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmAssertions;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmCaseAssert;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.BeforeEach;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ArgumentsSource;
+import java.util.Arrays;
+import java.util.Collection;
import java.util.Properties;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertNull;
-import static org.junit.jupiter.api.Assertions.assertThrows;
-
class MaskFromXToYMaskAlgorithmTest {
- private MaskFromXToYMaskAlgorithm maskAlgorithm;
-
- private MaskFromXToYMaskAlgorithm sameFromXToYMaskAlgorithm;
-
- @BeforeEach
- void setUp() {
- maskAlgorithm = (MaskFromXToYMaskAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FROM_X_TO_Y",
- PropertiesBuilder.build(new Property("from-x", "3"), new
Property("to-y", "5"), new Property("replace-char", "*")));
- sameFromXToYMaskAlgorithm = (MaskFromXToYMaskAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FROM_X_TO_Y",
- PropertiesBuilder.build(new Property("from-x", "5"), new
Property("to-y", "5"), new Property("replace-char", "*")));
- }
-
- @Test
- void assertMaskWithNullValue() {
- assertNull(maskAlgorithm.mask(null));
- }
-
- @Test
- void assertMask() {
- assertThat(maskAlgorithm.mask("abc123456"), is("abc***456"));
- assertThat(sameFromXToYMaskAlgorithm.mask("abc123456"),
is("abc12*456"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthLessThanFromXPlusOne() {
- assertThat(maskAlgorithm.mask("abc"), is("abc"));
- assertThat(sameFromXToYMaskAlgorithm.mask("abc"), is("abc"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthEqualsFromXPlusOne() {
- assertThat(maskAlgorithm.mask("abc1"), is("abc*"));
- assertThat(sameFromXToYMaskAlgorithm.mask("abc123"), is("abc12*"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthLessThanToYPlusOne() {
- assertThat(maskAlgorithm.mask("abc12"), is("abc**"));
- assertThat(sameFromXToYMaskAlgorithm.mask("abc12"), is("abc12"));
- }
-
- @Test
- void assertMaskWhenPlainValueLengthEqualsToYPlusOne() {
- assertThat(maskAlgorithm.mask("abc123"), is("abc***"));
- assertThat(sameFromXToYMaskAlgorithm.mask("abc123"), is("abc12*"));
- }
-
- @Test
- void assertInitWhenFromXIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("from-x", ""),
new Property("to-y", "5"), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FROM_X_TO_Y", props));
- }
-
- @Test
- void assertInitWhenToYIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("from-x",
"3"), new Property("to-y", ""), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FROM_X_TO_Y", props));
- }
-
- @Test
- void assertInitWhenReplaceCharIsEmpty() {
- Properties props = PropertiesBuilder.build(new Property("from-x",
"3"), new Property("to-y", "5"), new Property("replace-char", ""));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FROM_X_TO_Y", props));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmInitArgumentsProvider.class)
+ void assertInit(final String type, @SuppressWarnings("unused") final
String name, final Properties props) {
+ MaskAlgorithmAssertions.assertInitFailedWithInvalidProperties(type,
props);
}
- @Test
- void assertInitWhenFromXIsNotPositive() {
- Properties props = PropertiesBuilder.build(new Property("from-x",
"-3"), new Property("to-y", "5"), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FROM_X_TO_Y", props));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmMaskArgumentsProvider.class)
+ void assertMask(final String type, @SuppressWarnings("unused") final
String name, final Properties props, final Object plainValue, final Object
maskedValue) {
+ MaskAlgorithmAssertions.assertMask(type, props, plainValue,
maskedValue);
}
- @Test
- void assertInitWhenToYIsNotPositive() {
- Properties props = PropertiesBuilder.build(new Property("from-x",
"3"), new Property("to-y", "-5"), new Property("replace-char", "*"));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FROM_X_TO_Y", props));
+ private static class AlgorithmInitArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmInitArgumentsProvider() {
+ super("MASK_FROM_X_TO_Y");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("empty_from_X",
PropertiesBuilder.build(new Property("from-x", ""), new Property("to-y", "5"),
new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("empty_to_Y",
PropertiesBuilder.build(new Property("from-x", "3"), new Property("to-y", ""),
new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("empty_replace_char",
PropertiesBuilder.build(new Property("from-x", "3"), new Property("to-y", "5"),
new Property("replace-char", ""))),
+ new MaskAlgorithmCaseAssert("negative_from_X",
PropertiesBuilder.build(new Property("from-x", "-3"), new Property("to-y",
"5"), new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("negative_to_Y",
PropertiesBuilder.build(new Property("from-x", "3"), new Property("to-y",
"-5"), new Property("replace-char", "*"))),
+ new MaskAlgorithmCaseAssert("from_X_greater_than_to_Y",
PropertiesBuilder.build(new Property("from-x", "5"), new Property("to-y", "2"),
new Property("replace-char", ""))));
+ }
}
- @Test
- void assertInitWhenFromXGreaterThanToY() {
- Properties props = PropertiesBuilder.build(new Property("from-x",
"5"), new Property("to-y", "2"), new Property("replace-char", ""));
- assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, "MASK_FROM_X_TO_Y", props));
+ private static class AlgorithmMaskArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmMaskArgumentsProvider() {
+ super("MASK_FROM_X_TO_Y");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ Properties diffProps = PropertiesBuilder.build(new
Property("from-x", "3"), new Property("to-y", "5"), new
Property("replace-char", "*"));
+ Properties sameProps = PropertiesBuilder.build(new
Property("from-x", "5"), new Property("to-y", "5"), new
Property("replace-char", "*"));
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("null_value", diffProps, null,
null),
+ new MaskAlgorithmCaseAssert("empty_string", diffProps, "",
""),
+ new MaskAlgorithmCaseAssert("normal_with_diff", diffProps,
"abc123456", "abc***456"),
+ new MaskAlgorithmCaseAssert("normal_with_same", sameProps,
"abc123456", "abc12*456"),
+ new
MaskAlgorithmCaseAssert("length_less_than_from_X_plus_one_with_diff",
diffProps, "abc", "abc"),
+ new
MaskAlgorithmCaseAssert("length_less_than_from_X_plus_one_with_same",
sameProps, "abc", "abc"),
+ new
MaskAlgorithmCaseAssert("length_equals_from_X_plus_one_with_diff", diffProps,
"abc1", "abc*"),
+ new
MaskAlgorithmCaseAssert("length_equals_from_X_plus_one_with_same", sameProps,
"abc123", "abc12*"),
+ new
MaskAlgorithmCaseAssert("length_less_than_to_Y_plus_one_with_diff", diffProps,
"abc12", "abc**"),
+ new
MaskAlgorithmCaseAssert("length_less_than_to_Y_plus_one_with_same", sameProps,
"abc12", "abc12"),
+ new
MaskAlgorithmCaseAssert("length_equals_to_Y_plus_one_with_diff", diffProps,
"abc123", "abc***"),
+ new
MaskAlgorithmCaseAssert("length_equals_to_Y_plus_one_with_same", sameProps,
"abc123", "abc12*"));
+ }
}
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/hash/MD5MaskAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/hash/MD5MaskAlgorithmTest.java
index f8fcd50df6a..df45960ff60 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/hash/MD5MaskAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/hash/MD5MaskAlgorithmTest.java
@@ -17,34 +17,40 @@
package org.apache.shardingsphere.mask.algorithm.hash;
-import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
-import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmArgumentsProvider;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmAssertions;
+import
org.apache.shardingsphere.mask.algorithm.parameterized.MaskAlgorithmCaseAssert;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
-import org.junit.jupiter.api.Test;
+import org.junit.jupiter.params.ParameterizedTest;
+import org.junit.jupiter.params.provider.ArgumentsSource;
-import static org.hamcrest.CoreMatchers.is;
-import static org.hamcrest.MatcherAssert.assertThat;
-import static org.junit.jupiter.api.Assertions.assertNull;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.Properties;
class MD5MaskAlgorithmTest {
- @Test
- void assertMask() {
- assertThat(createAlgorithm("").mask("abc123456"),
is("0659c7992e268962384eb17fafe88364"));
+ @ParameterizedTest(name = "{0}: {1}")
+ @ArgumentsSource(AlgorithmMaskArgumentsProvider.class)
+ void assertMask(final String type, @SuppressWarnings("unused") final
String name, final Properties props, final Object plainValue, final Object
maskedValue) {
+ MaskAlgorithmAssertions.assertMask(type, props, plainValue,
maskedValue);
}
- @Test
- void assertMaskWhenPlainValueIsNull() {
- assertNull(createAlgorithm("").mask(null));
- }
-
- @Test
- void assertMaskWhenConfigSalt() {
- assertThat(createAlgorithm("202cb962ac5907").mask("abc123456"),
is("02d44390e9354b72dd2aa78d55016f7f"));
- }
-
- private MD5MaskAlgorithm createAlgorithm(final String salt) {
- return (MD5MaskAlgorithm)
TypedSPILoader.getService(MaskAlgorithm.class, "MD5",
PropertiesBuilder.build(new Property("salt", salt)));
+ private static class AlgorithmMaskArgumentsProvider extends
MaskAlgorithmArgumentsProvider {
+
+ AlgorithmMaskArgumentsProvider() {
+ super("MD5");
+ }
+
+ @Override
+ protected Collection<MaskAlgorithmCaseAssert> getCaseAsserts() {
+ Properties propsWithoutSalt = new Properties();
+ Properties propsWithSalt = PropertiesBuilder.build(new
Property("salt", "202cb962ac5907"));
+ return Arrays.asList(
+ new MaskAlgorithmCaseAssert("null_value",
propsWithoutSalt, null, null),
+ new MaskAlgorithmCaseAssert("without_salt",
propsWithoutSalt, "abc123456", "0659c7992e268962384eb17fafe88364"),
+ new MaskAlgorithmCaseAssert("with_salt", propsWithSalt,
"abc123456", "02d44390e9354b72dd2aa78d55016f7f"));
+ }
}
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/parameterized/MaskAlgorithmArgumentsProvider.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/parameterized/MaskAlgorithmArgumentsProvider.java
new file mode 100644
index 00000000000..85e926c6473
--- /dev/null
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/parameterized/MaskAlgorithmArgumentsProvider.java
@@ -0,0 +1,39 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.apache.shardingsphere.mask.algorithm.parameterized;
+
+import lombok.RequiredArgsConstructor;
+import org.junit.jupiter.api.extension.ExtensionContext;
+import org.junit.jupiter.params.provider.Arguments;
+import org.junit.jupiter.params.provider.ArgumentsProvider;
+
+import java.util.Collection;
+import java.util.stream.Stream;
+
+@RequiredArgsConstructor
+public abstract class MaskAlgorithmArgumentsProvider implements
ArgumentsProvider {
+
+ private final String type;
+
+ @Override
+ public Stream<? extends Arguments> provideArguments(final ExtensionContext
extensionContext) {
+ return getCaseAsserts().stream().map(each -> Arguments.of(type,
each.getName(), each.getProps(), each.getPlainValue(), each.getMaskedValue()));
+ }
+
+ protected abstract Collection<MaskAlgorithmCaseAssert> getCaseAsserts();
+}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/parameterized/MaskAlgorithmAssertions.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/parameterized/MaskAlgorithmAssertions.java
new file mode 100644
index 00000000000..b09003debbe
--- /dev/null
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/parameterized/MaskAlgorithmAssertions.java
@@ -0,0 +1,57 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.apache.shardingsphere.mask.algorithm.parameterized;
+
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import
org.apache.shardingsphere.infra.algorithm.core.exception.AlgorithmInitializationException;
+import org.apache.shardingsphere.infra.spi.type.typed.TypedSPILoader;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Properties;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.Matchers.is;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+public final class MaskAlgorithmAssertions {
+
+ /**
+ * Assert init failed with invalid properties.
+ *
+ * @param type mask algorithm type
+ * @param props mask algorithm props
+ */
+ public static void assertInitFailedWithInvalidProperties(final String
type, final Properties props) {
+ assertThrows(AlgorithmInitializationException.class, () ->
TypedSPILoader.getService(MaskAlgorithm.class, type, props));
+ }
+
+ /**
+ * Assert mask.
+ *
+ * @param type mask algorithm type
+ * @param props mask algorithm props
+ * @param plainValue plain value
+ * @param maskedValue masked value
+ */
+ @SuppressWarnings("unchecked")
+ public static void assertMask(final String type, final Properties props,
final Object plainValue, final Object maskedValue) {
+ assertThat(TypedSPILoader.getService(MaskAlgorithm.class, type,
props).mask(plainValue), is(maskedValue));
+ }
+}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/parameterized/MaskAlgorithmCaseAssert.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/parameterized/MaskAlgorithmCaseAssert.java
new file mode 100644
index 00000000000..387746895e8
--- /dev/null
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/parameterized/MaskAlgorithmCaseAssert.java
@@ -0,0 +1,40 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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
+ *
+ * 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.apache.shardingsphere.mask.algorithm.parameterized;
+
+import lombok.Getter;
+import lombok.RequiredArgsConstructor;
+
+import java.util.Properties;
+
+@RequiredArgsConstructor
+@Getter
+public final class MaskAlgorithmCaseAssert {
+
+ private final String name;
+
+ private final Properties props;
+
+ private final Object plainValue;
+
+ private final Object maskedValue;
+
+ public MaskAlgorithmCaseAssert(final String name, final Properties props) {
+ this(name, props, null, null);
+ }
+}