This is an automated email from the ASF dual-hosted git repository.
jianglongtao 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 2bd7d0fb4b2 [#24755] Improve property verification of MaskAlgorithm
(#25801)
2bd7d0fb4b2 is described below
commit 2bd7d0fb4b2a35bcd0f2b3f2a52490594f6fc03d
Author: kyooosukedn <[email protected]>
AuthorDate: Wed May 24 08:21:16 2023 +0200
[#24755] Improve property verification of MaskAlgorithm (#25801)
* Improved properies verification in MaskAlgorithm
* Improved properties verification and resolved conflicts
* Removed unnecessary checks
* Removed unnecessary checks
* Removed unnecessary change
---
.../mask/algorithm/MaskAlgorithmPropsChecker.java | 52 +++++++++++++---------
.../cover/KeepFirstNLastMMaskAlgorithm.java | 4 +-
.../algorithm/cover/KeepFromXToYMaskAlgorithm.java | 4 +-
.../cover/MaskFirstNLastMMaskAlgorithm.java | 4 +-
.../algorithm/cover/MaskFromXToYMaskAlgorithm.java | 4 +-
.../GenericTableRandomReplaceAlgorithm.java | 6 +++
...rsonalIdentityNumberRandomReplaceAlgorithm.java | 4 ++
.../algorithm/MaskAlgorithmPropsCheckerTest.java | 8 ++--
.../cover/KeepFromXToYMaskAlgorithmTest.java | 12 +++++
.../cover/MaskAfterSpecialCharsAlgorithmTest.java | 18 ++++++++
.../cover/MaskFromXToYMaskAlgorithmTest.java | 12 +++++
.../replace/LandlineNumberRandomAlgorithmTest.java | 14 ++++++
...ryIdentityNumberRandomReplaceAlgorithmTest.java | 11 +++++
...alIdentityNumberRandomReplaceAlgorithmTest.java | 11 +++++
...nifiedCreditCodeRandomReplaceAlgorithmTest.java | 29 ++++++++++++
15 files changed, 161 insertions(+), 32 deletions(-)
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmPropsChecker.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmPropsChecker.java
index b26417d52c6..5717890d374 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmPropsChecker.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmPropsChecker.java
@@ -19,6 +19,7 @@ package org.apache.shardingsphere.mask.algorithm;
import lombok.AccessLevel;
import lombok.NoArgsConstructor;
+import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
import
org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
import java.util.Properties;
@@ -38,12 +39,10 @@ public final class MaskAlgorithmPropsChecker {
* @throws MaskAlgorithmInitializationException mask algorithm
initialization exception
*/
public static void checkSingleCharConfig(final Properties props, final
String singleCharConfigKey, final String maskType) {
- if (!props.containsKey(singleCharConfigKey)) {
- throw new MaskAlgorithmInitializationException(maskType,
String.format("%s can not be null", singleCharConfigKey));
- }
- if (1 != props.getProperty(singleCharConfigKey).length()) {
- throw new MaskAlgorithmInitializationException(maskType,
String.format("%s's length must be one", singleCharConfigKey));
- }
+
ShardingSpherePreconditions.checkState(props.containsKey(singleCharConfigKey),
+ () -> new MaskAlgorithmInitializationException(maskType,
String.format("%s can not be null", singleCharConfigKey)));
+ ShardingSpherePreconditions.checkState(1 ==
props.getProperty(singleCharConfigKey).length(),
+ () -> new MaskAlgorithmInitializationException(maskType,
String.format("%s's length must be one", singleCharConfigKey)));
}
/**
@@ -55,30 +54,43 @@ public final class MaskAlgorithmPropsChecker {
* @throws MaskAlgorithmInitializationException mask algorithm
initialization exception
*/
public static void checkAtLeastOneCharConfig(final Properties props, final
String atLeastOneCharConfigKey, final String maskType) {
- if (!props.containsKey(atLeastOneCharConfigKey)) {
- throw new MaskAlgorithmInitializationException(maskType,
String.format("%s can not be null", atLeastOneCharConfigKey));
- }
- if (0 == props.getProperty(atLeastOneCharConfigKey).length()) {
- throw new MaskAlgorithmInitializationException(maskType,
String.format("%s's length must be at least one", atLeastOneCharConfigKey));
- }
+
ShardingSpherePreconditions.checkState(props.containsKey(atLeastOneCharConfigKey),
+ () -> new MaskAlgorithmInitializationException(maskType,
String.format("%s can not be null", atLeastOneCharConfigKey)));
+
ShardingSpherePreconditions.checkState(props.getProperty(atLeastOneCharConfigKey).length()
> 0,
+ () -> new MaskAlgorithmInitializationException(maskType,
String.format("%s's length must be at least one", atLeastOneCharConfigKey)));
}
/**
- * Check integer type config.
+ * Check required property config.
*
* @param props props
- * @param integerTypeConfigKey integer type config key
+ * @param requiredPropertyConfigKey required property config key
* @param maskType mask type
* @throws MaskAlgorithmInitializationException mask algorithm
initialization exception
*/
- public static void checkIntegerTypeConfig(final Properties props, final
String integerTypeConfigKey, final String maskType) {
- if (!props.containsKey(integerTypeConfigKey)) {
- throw new MaskAlgorithmInitializationException(maskType,
String.format("%s can not be null", integerTypeConfigKey));
+ public static void checkRequiredPropertyConfig(final Properties props,
final String requiredPropertyConfigKey, final String maskType) {
+ if (!props.containsKey(requiredPropertyConfigKey)) {
+ throw new MaskAlgorithmInitializationException(maskType,
String.format("%s is required", requiredPropertyConfigKey));
}
+ }
+
+ /**
+ * Check required property config.
+ *
+ * @param props props
+ * @param positiveIntegerTypeConfigKey positive integer type config key
+ * @param maskType mask type
+ * @throws MaskAlgorithmInitializationException mask algorithm
initialization exception
+ */
+ public static void checkPositiveIntegerConfig(final Properties props,
final String positiveIntegerTypeConfigKey, final String maskType) {
+
ShardingSpherePreconditions.checkState(props.containsKey(positiveIntegerTypeConfigKey),
+ () -> new MaskAlgorithmInitializationException(maskType,
String.format("%s can not be null", positiveIntegerTypeConfigKey)));
try {
- Integer.parseInt(props.getProperty(integerTypeConfigKey));
- } catch (final NumberFormatException ignored) {
- throw new MaskAlgorithmInitializationException(maskType,
String.format("%s must be a valid integer number", integerTypeConfigKey));
+ int integerValue =
Integer.parseInt(props.getProperty(positiveIntegerTypeConfigKey));
+ ShardingSpherePreconditions.checkState(integerValue > 0,
+ () -> new MaskAlgorithmInitializationException(maskType,
String.format("%s must be a positive integer.", positiveIntegerTypeConfigKey)));
+ } catch (final NumberFormatException ex) {
+ throw new MaskAlgorithmInitializationException(maskType,
String.format("%s must be a valid integer number",
positiveIntegerTypeConfigKey));
}
}
}
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java
index bbfe531cde2..2d6c23cb33a 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFirstNLastMMaskAlgorithm.java
@@ -48,12 +48,12 @@ public final class KeepFirstNLastMMaskAlgorithm implements
MaskAlgorithm<Object,
}
private Integer createFirstN(final Properties props) {
- MaskAlgorithmPropsChecker.checkIntegerTypeConfig(props, FIRST_N,
getType());
+ MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(props, FIRST_N,
getType());
return Integer.parseInt(props.getProperty(FIRST_N));
}
private Integer createLastM(final Properties props) {
- MaskAlgorithmPropsChecker.checkIntegerTypeConfig(props, LAST_M,
getType());
+ MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(props, LAST_M,
getType());
return Integer.parseInt(props.getProperty(LAST_M));
}
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
index d4e936e5d73..29971034309 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/KeepFromXToYMaskAlgorithm.java
@@ -51,12 +51,12 @@ public final class KeepFromXToYMaskAlgorithm implements
MaskAlgorithm<Object, St
}
private Integer createFromX(final Properties props) {
- MaskAlgorithmPropsChecker.checkIntegerTypeConfig(props, FROM_X,
getType());
+ MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(props, FROM_X,
getType());
return Integer.parseInt(props.getProperty(FROM_X));
}
private Integer createToY(final Properties props) {
- MaskAlgorithmPropsChecker.checkIntegerTypeConfig(props, TO_Y,
getType());
+ MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(props, TO_Y,
getType());
return Integer.parseInt(props.getProperty(TO_Y));
}
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithm.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithm.java
index fa78e3dc9d1..b463a39c994 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithm.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFirstNLastMMaskAlgorithm.java
@@ -48,12 +48,12 @@ public final class MaskFirstNLastMMaskAlgorithm implements
MaskAlgorithm<Object,
}
private Integer createFirstN(final Properties props) {
- MaskAlgorithmPropsChecker.checkIntegerTypeConfig(props, FIRST_N,
getType());
+ MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(props, FIRST_N,
getType());
return Integer.parseInt(props.getProperty(FIRST_N));
}
private Integer createLastM(final Properties props) {
- MaskAlgorithmPropsChecker.checkIntegerTypeConfig(props, LAST_M,
getType());
+ MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(props, LAST_M,
getType());
return Integer.parseInt(props.getProperty(LAST_M));
}
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java
index 8b44d910dcd..da9f3ac3408 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskFromXToYMaskAlgorithm.java
@@ -51,12 +51,12 @@ public final class MaskFromXToYMaskAlgorithm implements
MaskAlgorithm<Object, St
}
private Integer createFromX(final Properties props) {
- MaskAlgorithmPropsChecker.checkIntegerTypeConfig(props, FROM_X,
getType());
+ MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(props, FROM_X,
getType());
return Integer.parseInt(props.getProperty(FROM_X));
}
private Integer createToY(final Properties props) {
- MaskAlgorithmPropsChecker.checkIntegerTypeConfig(props, TO_Y,
getType());
+ MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(props, TO_Y,
getType());
return Integer.parseInt(props.getProperty(TO_Y));
}
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 3be42c9acd9..ac2e7ebcc36 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
@@ -19,6 +19,8 @@ package org.apache.shardingsphere.mask.algorithm.replace;
import com.google.common.base.Splitter;
import com.google.common.base.Strings;
+import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
import java.security.SecureRandom;
@@ -63,7 +65,11 @@ 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 MaskAlgorithmInitializationException(getType(),
String.format("'%s' must be not empty", DIGITAL_CODES)));
specialCodes = splitPropsToList(props.getProperty(SPECIAL_CODES,
DEFAULT_SPECIAL_CODES));
+ ShardingSpherePreconditions.checkState(!SPECIAL_CODES.isEmpty(),
+ () -> new MaskAlgorithmInitializationException(getType(),
String.format("'%s' must be not empty", SPECIAL_CODES)));
}
private List<Character> splitPropsToList(final String props) {
diff --git
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithm.java
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithm.java
index a0a87910216..ccdeb2c2f79 100644
---
a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithm.java
+++
b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithm.java
@@ -18,6 +18,8 @@
package org.apache.shardingsphere.mask.algorithm.replace;
import com.google.common.base.Strings;
+import
org.apache.shardingsphere.infra.util.exception.ShardingSpherePreconditions;
+import
org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
import java.security.SecureRandom;
@@ -38,6 +40,8 @@ public final class
PersonalIdentityNumberRandomReplaceAlgorithm implements MaskA
@Override
public void init(final Properties props) {
alphaTwoCountryAreaCode =
props.getProperty(ALPHA_TWO_COUNTRY_AREA_CODE, "CN");
+
ShardingSpherePreconditions.checkState(!Strings.isNullOrEmpty(alphaTwoCountryAreaCode),
+ () -> new MaskAlgorithmInitializationException(getType(),
String.format("%s can not be empty", ALPHA_TWO_COUNTRY_AREA_CODE)));
}
@Override
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmPropsCheckerTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmPropsCheckerTest.java
index e10b946bd88..b7898fe61b1 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmPropsCheckerTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmPropsCheckerTest.java
@@ -83,23 +83,23 @@ class MaskAlgorithmPropsCheckerTest {
@Test
void assertCheckIntegerTypeConfigWithInteger() {
-
MaskAlgorithmPropsChecker.checkIntegerTypeConfig(PropertiesBuilder.build(new
Property("integerTypeConfigKey", "123")), "integerTypeConfigKey", "maskType");
+
MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(PropertiesBuilder.build(new
Property("integerTypeConfigKey", "123")), "integerTypeConfigKey", "maskType");
}
@Test
void assertCheckIntegerTypeConfigWithDifferentKey() {
assertThrows(MaskAlgorithmInitializationException.class,
- () ->
MaskAlgorithmPropsChecker.checkIntegerTypeConfig(PropertiesBuilder.build(new
Property("integerTypeConfigKey", "123")), "integerTypeConfigKey1", "maskType"));
+ () ->
MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(PropertiesBuilder.build(new
Property("integerTypeConfigKey", "123")), "integerTypeConfigKey1",
"maskType"));
}
@Test
void assertCheckIntegerTypeConfigWithNotInteger() {
assertThrows(MaskAlgorithmInitializationException.class,
- () ->
MaskAlgorithmPropsChecker.checkIntegerTypeConfig(PropertiesBuilder.build(new
Property("integerTypeConfigKey", "123abc")), "integerTypeConfigKey",
"maskType"));
+ () ->
MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(PropertiesBuilder.build(new
Property("integerTypeConfigKey", "123abc")), "integerTypeConfigKey",
"maskType"));
}
@Test
void assertCheckIntegerTypeConfigWithNull() {
- assertThrows(MaskAlgorithmInitializationException.class, () ->
MaskAlgorithmPropsChecker.checkIntegerTypeConfig(PropertiesBuilder.build(),
"integerTypeConfigKey", "maskType"));
+ assertThrows(MaskAlgorithmInitializationException.class, () ->
MaskAlgorithmPropsChecker.checkPositiveIntegerConfig(PropertiesBuilder.build(),
"integerTypeConfigKey", "maskType"));
}
}
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 71340210a5c..98ba9c68cb0 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
@@ -89,6 +89,18 @@ class KeepFromXToYMaskAlgorithmTest {
() -> new
KeepFirstNLastMMaskAlgorithm().init(PropertiesBuilder.build(new
Property("from-x", "2"), new Property("to-y", "5"), new
Property("replace-char", ""))));
}
+ @Test
+ void assertInitWhenFromXIsNotPositive() {
+ assertThrows(MaskAlgorithmInitializationException.class,
+ () -> new
MaskFirstNLastMMaskAlgorithm().init(PropertiesBuilder.build(new
Property("from-x", "-3"), new Property("to-y", "5"), new
Property("replace-char", "*"))));
+ }
+
+ @Test
+ void assertInitWhenToYIsNotPositive() {
+ assertThrows(MaskAlgorithmInitializationException.class,
+ () -> new
MaskFirstNLastMMaskAlgorithm().init(PropertiesBuilder.build(new
Property("from-x", "3"), new Property("to-y", "-5"), new
Property("replace-char", "*"))));
+ }
+
@Test
void assertInitWhenFromXGreaterThanToY() {
assertThrows(MaskAlgorithmInitializationException.class,
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 e5109deb691..604196892f4 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
@@ -74,4 +74,22 @@ class MaskAfterSpecialCharsAlgorithmTest {
assertThrows(MaskAlgorithmInitializationException.class,
() -> new
MaskBeforeSpecialCharsAlgorithm().init(PropertiesBuilder.build(new
Property("special-chars", "d1"), new Property("replace-char", ""))));
}
+
+ @Test
+ void assertInitWhenReplaceCharIsMissing() {
+ assertThrows(MaskAlgorithmInitializationException.class,
+ () -> new
MaskBeforeSpecialCharsAlgorithm().init(PropertiesBuilder.build(new
Property("special-chars", "d1"))));
+ }
+
+ @Test
+ void assertInitWhenPropertiesAreEmpty() {
+ assertThrows(MaskAlgorithmInitializationException.class,
+ () -> new
MaskBeforeSpecialCharsAlgorithm().init(PropertiesBuilder.build()));
+ }
+
+ @Test
+ void assertInitWhenValidPropertiesAreSet() {
+ MaskBeforeSpecialCharsAlgorithm algorithm = new
MaskBeforeSpecialCharsAlgorithm();
+ algorithm.init(PropertiesBuilder.build(new Property("special-chars",
"d1"), new Property("replace-char", "*")));
+ }
}
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 7eff89ceb16..1475d990260 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
@@ -89,6 +89,18 @@ class MaskFromXToYMaskAlgorithmTest {
() -> new
MaskFromXToYMaskAlgorithm().init(PropertiesBuilder.build(new Property("from-x",
"3"), new Property("to-y", "5"), new Property("replace-char", ""))));
}
+ @Test
+ void assertInitWhenFromXIsNotPositive() {
+ assertThrows(MaskAlgorithmInitializationException.class,
+ () -> new
MaskFirstNLastMMaskAlgorithm().init(PropertiesBuilder.build(new
Property("from-x", "-3"), new Property("to-y", "5"), new
Property("replace-char", "*"))));
+ }
+
+ @Test
+ void assertInitWhenToYIsNotPositive() {
+ assertThrows(MaskAlgorithmInitializationException.class,
+ () -> new
MaskFirstNLastMMaskAlgorithm().init(PropertiesBuilder.build(new
Property("from-x", "3"), new Property("to-y", "-5"), new
Property("replace-char", "*"))));
+ }
+
@Test
void assertInitWhenFromXGreaterThanToY() {
assertThrows(MaskAlgorithmInitializationException.class,
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithmTest.java
index 698afc63808..35ade1c8975 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/LandlineNumberRandomAlgorithmTest.java
@@ -17,6 +17,7 @@
package org.apache.shardingsphere.mask.algorithm.replace;
+import
org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
import org.apache.shardingsphere.test.util.PropertiesBuilder;
import org.apache.shardingsphere.test.util.PropertiesBuilder.Property;
import org.junit.jupiter.api.BeforeEach;
@@ -25,6 +26,7 @@ import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
+import static org.junit.jupiter.api.Assertions.assertThrows;
class LandlineNumberRandomAlgorithmTest {
@@ -42,4 +44,16 @@ class LandlineNumberRandomAlgorithmTest {
assertThat(maskAlgorithm.mask("0251234567"), not("0251234567"));
assertThat(maskAlgorithm.mask("03101234567"), not("03101234567"));
}
+
+ @Test
+ void assertInitWhenConfigIsEmpty() {
+ assertThrows(MaskAlgorithmInitializationException.class, () ->
maskAlgorithm.init(PropertiesBuilder.build()));
+ }
+
+ @Test
+ void assertMaskWithInvalidConfig() {
+ assertThrows(MaskAlgorithmInitializationException.class,
+ () -> maskAlgorithm.init(PropertiesBuilder.build(new
Property("landline-numbers", ""))));
+ }
+
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithmTest.java
index 718e9e31cd6..7139e9f57c3 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/MilitaryIdentityNumberRandomReplaceAlgorithmTest.java
@@ -17,15 +17,20 @@
package org.apache.shardingsphere.mask.algorithm.replace;
+import
org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
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 java.util.Properties;
+
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
class MilitaryIdentityNumberRandomReplaceAlgorithmTest {
@@ -43,4 +48,10 @@ class MilitaryIdentityNumberRandomReplaceAlgorithmTest {
assertThat(maskAlgorithm.mask(""), is(""));
assertNull(maskAlgorithm.mask(null));
}
+
+ @Test
+ void testMaskWithInvalidProps() {
+ MaskAlgorithm algorithm = new
MilitaryIdentityNumberRandomReplaceAlgorithm();
+ assertThrows(MaskAlgorithmInitializationException.class, () ->
algorithm.init(new Properties()));
+ }
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithmTest.java
index f40d6dff2f4..526253899da 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/PersonalIdentityNumberRandomReplaceAlgorithmTest.java
@@ -24,6 +24,7 @@ import org.junit.jupiter.api.Test;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.not;
+import static org.hamcrest.CoreMatchers.nullValue;
import static org.hamcrest.MatcherAssert.assertThat;
class PersonalIdentityNumberRandomReplaceAlgorithmTest {
@@ -43,5 +44,15 @@ class PersonalIdentityNumberRandomReplaceAlgorithmTest {
assertThat(maskAlgorithm.mask("1234567891011121314"),
is("1234567891011121314"));
assertThat(maskAlgorithm.mask("123456"), is("123456"));
assertThat(maskAlgorithm.mask(""), is(""));
+ assertThat(maskAlgorithm.mask(null), is(nullValue()));
+ }
+
+ @Test
+ void assertMaskWithDifferentCountryCode() {
+ PersonalIdentityNumberRandomReplaceAlgorithm maskAlgorithmCN = new
PersonalIdentityNumberRandomReplaceAlgorithm();
+ maskAlgorithmCN.init(PropertiesBuilder.build(new
Property("alpha-two-country-area-code", "CN")));
+ PersonalIdentityNumberRandomReplaceAlgorithm maskAlgorithmJP = new
PersonalIdentityNumberRandomReplaceAlgorithm();
+ maskAlgorithmJP.init(PropertiesBuilder.build(new
Property("alpha-two-country-area-code", "JP")));
+ assertThat(maskAlgorithmCN.mask("372928198312103215"),
not(maskAlgorithmJP.mask("372928198312103215")));
}
}
diff --git
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithmTest.java
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithmTest.java
index 76d009afc1f..27124e0deee 100644
---
a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithmTest.java
+++
b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/replace/UnifiedCreditCodeRandomReplaceAlgorithmTest.java
@@ -49,4 +49,33 @@ class UnifiedCreditCodeRandomReplaceAlgorithmTest {
void assertInitWhenConfigIsNull() {
assertThrows(MaskAlgorithmInitializationException.class, () ->
maskAlgorithm.init(PropertiesBuilder.build(new
Property("registration-department-codes", "1,2,3,4"))));
}
+
+ @Test
+ void assertInitWhenConfigIsEmpty() {
+ assertThrows(MaskAlgorithmInitializationException.class, () ->
maskAlgorithm.init(PropertiesBuilder.build()));
+ }
+
+ @Test
+ void assertInitWhenRegistrationDepartmentCodesIsEmpty() {
+ assertThrows(MaskAlgorithmInitializationException.class, () ->
maskAlgorithm.init(PropertiesBuilder.build(
+ new Property("registration-department-codes", ""),
+ new Property("category-codes", "1,2,3,4"),
+ new Property("administrative-division-codes",
"100000,200000,300000"))));
+ }
+
+ @Test
+ void assertInitWhenCategoryCodesIsEmpty() {
+ assertThrows(MaskAlgorithmInitializationException.class, () ->
maskAlgorithm.init(PropertiesBuilder.build(
+ new Property("registration-department-codes", "1,2,3,4"),
+ new Property("category-codes", ""),
+ new Property("administrative-division-codes",
"100000,200000,300000"))));
+ }
+
+ @Test
+ void assertInitWhenAdministrativeDivisionCodesIsEmpty() {
+ assertThrows(MaskAlgorithmInitializationException.class, () ->
maskAlgorithm.init(PropertiesBuilder.build(
+ new Property("registration-department-codes", "1,2,3,4"),
+ new Property("category-codes", "1,2,3,4"),
+ new Property("administrative-division-codes", ""))));
+ }
}