You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by su...@apache.org on 2022/12/29 02:04:27 UTC

[shardingsphere] branch master updated: Optimize mask algorithm logic and add MaskAlgorithmInitializationException (#23145)

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 92f74f008ac Optimize mask algorithm logic and add MaskAlgorithmInitializationException (#23145)
92f74f008ac is described below

commit 92f74f008acfe207cee7674a49acb4699173dd5d
Author: Zhengqiang Duan <du...@apache.org>
AuthorDate: Thu Dec 29 10:04:20 2022 +0800

    Optimize mask algorithm logic and add MaskAlgorithmInitializationException (#23145)
---
 .../mask/algorithm/MaskAlgorithmUtil.java          | 81 ++++++++++++++++++++++
 .../cover/KeepFirstNLastMMaskAlgorithm.java        |  9 ++-
 .../algorithm/cover/KeepFromXToYMaskAlgorithm.java |  9 ++-
 ...hm.java => MaskAfterSpecialCharsAlgorithm.java} | 35 ++++++----
 ...m.java => MaskBeforeSpecialCharsAlgorithm.java} | 35 ++++++----
 .../cover/MaskFirstNLastMMaskAlgorithm.java        |  9 ++-
 .../algorithm/cover/MaskFromXToYMaskAlgorithm.java |  9 ++-
 .../mask/exception/MaskSQLException.java           | 35 ++++++++++
 .../MaskAlgorithmInitializationException.java      | 33 +++++++++
 ...rg.apache.shardingsphere.mask.spi.MaskAlgorithm |  4 +-
 .../cover/KeepFirstNLastMMaskAlgorithmTest.java    |  3 +-
 .../cover/KeepFromXToYMaskAlgorithmTest.java       |  3 +-
 ...ava => MaskAfterSpecialCharsAlgorithmTest.java} | 20 +++---
 ...va => MaskBeforeSpecialCharsAlgorithmTest.java} | 20 +++---
 .../cover/MaskFirstNLastMMaskAlgorithmTest.java    |  3 +-
 .../cover/MaskFromXToYMaskAlgorithmTest.java       |  3 +-
 16 files changed, 241 insertions(+), 70 deletions(-)

diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmUtil.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmUtil.java
new file mode 100644
index 00000000000..8964a6e993f
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/MaskAlgorithmUtil.java
@@ -0,0 +1,81 @@
+/*
+ * 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;
+
+import com.google.common.primitives.Ints;
+import lombok.AccessLevel;
+import lombok.NoArgsConstructor;
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
+
+import java.util.Properties;
+
+/**
+ * Mask algorithm util.
+ */
+@NoArgsConstructor(access = AccessLevel.PRIVATE)
+@SuppressWarnings("UnstableApiUsage")
+public final class MaskAlgorithmUtil {
+    
+    /**
+     * Check single char config.
+     *
+     * @param props props
+     * @param singleCharConfigKey single char config key
+     * @param maskType mask type
+     */
+    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));
+        }
+    }
+    
+    /**
+     * Check at least one char config.
+     *
+     * @param props props
+     * @param atLeastOneCharConfigKey at least one char config key
+     * @param maskType mask type
+     */
+    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));
+        }
+    }
+    
+    /**
+     * Check integer type config.
+     *
+     * @param props props
+     * @param integerTypeConfigKey integer type config key
+     * @param maskType mask type
+     */
+    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));
+        }
+        if (null == Ints.tryParse(props.getProperty(integerTypeConfigKey))) {
+            throw new MaskAlgorithmInitializationException(maskType, String.format("%s must be a valid integer number.", integerTypeConfigKey));
+        }
+    }
+}
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 3b1baad5c23..f1ad57ad859 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
@@ -17,9 +17,9 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
-import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
 import java.util.Properties;
@@ -53,18 +53,17 @@ public final class KeepFirstNLastMMaskAlgorithm implements MaskAlgorithm<Object,
     }
     
     private Integer createFirstN(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(FIRST_N), "%s can not be null.", FIRST_N);
+        MaskAlgorithmUtil.checkIntegerTypeConfig(props, FIRST_N, getType());
         return Integer.parseInt(props.getProperty(FIRST_N));
     }
     
     private Integer createLastM(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(LAST_M), "%s can not be null.", LAST_M);
+        MaskAlgorithmUtil.checkIntegerTypeConfig(props, LAST_M, getType());
         return Integer.parseInt(props.getProperty(LAST_M));
     }
     
     private Character createReplaceChar(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(REPLACE_CHAR), "%s can not be null.", REPLACE_CHAR);
-        Preconditions.checkArgument(1 == props.getProperty(REPLACE_CHAR).length(), "%s's length must be one.", REPLACE_CHAR);
+        MaskAlgorithmUtil.checkSingleCharConfig(props, REPLACE_CHAR, getType());
         return props.getProperty(REPLACE_CHAR).charAt(0);
     }
     
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 bf0c8623604..66d1c818388 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
@@ -17,9 +17,9 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
-import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
 import java.util.Properties;
@@ -53,18 +53,17 @@ public final class KeepFromXToYMaskAlgorithm implements MaskAlgorithm<Object, St
     }
     
     private Integer createFromX(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(FROM_X), "%s can not be null.", FROM_X);
+        MaskAlgorithmUtil.checkIntegerTypeConfig(props, FROM_X, getType());
         return Integer.parseInt(props.getProperty(FROM_X));
     }
     
     private Integer createToY(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(TO_Y), "%s can not be null.", TO_Y);
+        MaskAlgorithmUtil.checkIntegerTypeConfig(props, TO_Y, getType());
         return Integer.parseInt(props.getProperty(TO_Y));
     }
     
     private Character createReplaceChar(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(REPLACE_CHAR), "%s can not be null.", REPLACE_CHAR);
-        Preconditions.checkArgument(props.getProperty(REPLACE_CHAR).length() == 1, "%s's length must be one.", REPLACE_CHAR);
+        MaskAlgorithmUtil.checkSingleCharConfig(props, REPLACE_CHAR, getType());
         return props.getProperty(REPLACE_CHAR).charAt(0);
     }
     
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharAlgorithm.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithm.java
similarity index 59%
rename from features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharAlgorithm.java
rename to features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithm.java
index b60b7c59f5d..af49a26d9e7 100644
--- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharAlgorithm.java
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithm.java
@@ -17,21 +17,25 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
-import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
 import java.util.Properties;
 
 /**
- * Mask after special char algorithm.
+ * Mask after special-chars algorithm.
  */
-public final class MaskAfterSpecialCharAlgorithm implements MaskAlgorithm<Object, String> {
+public final class MaskAfterSpecialCharsAlgorithm implements MaskAlgorithm<Object, String> {
     
-    private static final String SPECIAL_CHARACTERS = "special-characters";
+    private static final String SPECIAL_CHARS = "special-chars";
     
-    private String specialCharacters;
+    private static final String REPLACE_CHAR = "replace-char";
+    
+    private String specialChars;
+    
+    private Character replaceChar;
     
     @Getter
     private Properties props;
@@ -39,13 +43,18 @@ public final class MaskAfterSpecialCharAlgorithm implements MaskAlgorithm<Object
     @Override
     public void init(final Properties props) {
         this.props = props;
-        this.specialCharacters = createSpecialCharacters(props);
+        this.specialChars = createSpecialChars(props);
+        this.replaceChar = createReplaceChar(props);
+    }
+    
+    private String createSpecialChars(final Properties props) {
+        MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, SPECIAL_CHARS, getType());
+        return props.getProperty(SPECIAL_CHARS);
     }
     
-    private String createSpecialCharacters(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(SPECIAL_CHARACTERS), "%s can not be null.", SPECIAL_CHARACTERS);
-        Preconditions.checkArgument(props.getProperty(SPECIAL_CHARACTERS).length() > 0, "%s is not empty.", SPECIAL_CHARACTERS);
-        return props.getProperty(SPECIAL_CHARACTERS);
+    private Character createReplaceChar(final Properties props) {
+        MaskAlgorithmUtil.checkSingleCharConfig(props, REPLACE_CHAR, getType());
+        return props.getProperty(REPLACE_CHAR).charAt(0);
     }
     
     @Override
@@ -54,16 +63,16 @@ public final class MaskAfterSpecialCharAlgorithm implements MaskAlgorithm<Object
         if (Strings.isNullOrEmpty(result)) {
             return result;
         }
-        int index = result.indexOf(specialCharacters) == -1 ? -1 : result.indexOf(specialCharacters) + specialCharacters.length();
+        int index = result.contains(specialChars) ? result.indexOf(specialChars) + specialChars.length() : -1;
         char[] chars = result.toCharArray();
         for (int i = index; i != -1 && i < chars.length; i++) {
-            chars[i] = '*';
+            chars[i] = replaceChar;
         }
         return new String(chars);
     }
     
     @Override
     public String getType() {
-        return "MASK_BEFORE_SPECIAL_CHAR";
+        return "MASK_AFTER_SPECIAL_CHARS";
     }
 }
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharAlgorithm.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithm.java
similarity index 60%
rename from features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharAlgorithm.java
rename to features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithm.java
index 3af8d1bd865..2a5fd28ef19 100644
--- a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharAlgorithm.java
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithm.java
@@ -17,21 +17,25 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
-import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
 import java.util.Properties;
 
 /**
- * Mask before special char algorithm.
+ * Mask before special-chars algorithm.
  */
-public final class MaskBeforeSpecialCharAlgorithm implements MaskAlgorithm<Object, String> {
+public final class MaskBeforeSpecialCharsAlgorithm implements MaskAlgorithm<Object, String> {
     
-    private static final String SPECIAL_CHARACTERS = "special-characters";
+    private static final String SPECIAL_CHARS = "special-chars";
     
-    private String specialCharacters;
+    private static final String REPLACE_CHAR = "replace-char";
+    
+    private String specialChars;
+    
+    private Character replaceChar;
     
     @Getter
     private Properties props;
@@ -39,13 +43,18 @@ public final class MaskBeforeSpecialCharAlgorithm implements MaskAlgorithm<Objec
     @Override
     public void init(final Properties props) {
         this.props = props;
-        this.specialCharacters = createSpecialCharacters(props);
+        this.specialChars = createSpecialChars(props);
+        this.replaceChar = createReplaceChar(props);
+    }
+    
+    private String createSpecialChars(final Properties props) {
+        MaskAlgorithmUtil.checkAtLeastOneCharConfig(props, SPECIAL_CHARS, getType());
+        return props.getProperty(SPECIAL_CHARS);
     }
     
-    private String createSpecialCharacters(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(SPECIAL_CHARACTERS), "%s can not be null.", SPECIAL_CHARACTERS);
-        Preconditions.checkArgument(props.getProperty(SPECIAL_CHARACTERS).length() > 0, "%s is not empty.", SPECIAL_CHARACTERS);
-        return props.getProperty(SPECIAL_CHARACTERS);
+    private Character createReplaceChar(final Properties props) {
+        MaskAlgorithmUtil.checkSingleCharConfig(props, REPLACE_CHAR, getType());
+        return props.getProperty(REPLACE_CHAR).charAt(0);
     }
     
     @Override
@@ -54,16 +63,16 @@ public final class MaskBeforeSpecialCharAlgorithm implements MaskAlgorithm<Objec
         if (Strings.isNullOrEmpty(result)) {
             return result;
         }
-        int index = result.indexOf(specialCharacters);
+        int index = result.indexOf(specialChars);
         char[] chars = result.toCharArray();
         for (int i = 0; i < index; i++) {
-            chars[i] = '*';
+            chars[i] = replaceChar;
         }
         return new String(chars);
     }
     
     @Override
     public String getType() {
-        return "MASK_BEFORE_SPECIAL_CHAR";
+        return "MASK_BEFORE_SPECIAL_CHARS";
     }
 }
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 7730580bf03..df90ce4d630 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
@@ -17,9 +17,9 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
-import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
 import java.util.Properties;
@@ -53,18 +53,17 @@ public final class MaskFirstNLastMMaskAlgorithm implements MaskAlgorithm<Object,
     }
     
     private Integer createFirstN(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(FIRST_N), "%s can not be null.", FIRST_N);
+        MaskAlgorithmUtil.checkIntegerTypeConfig(props, FIRST_N, getType());
         return Integer.parseInt(props.getProperty(FIRST_N));
     }
     
     private Integer createLastM(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(LAST_M), "%s can not be null.", LAST_M);
+        MaskAlgorithmUtil.checkIntegerTypeConfig(props, LAST_M, getType());
         return Integer.parseInt(props.getProperty(LAST_M));
     }
     
     private Character createReplaceChar(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(REPLACE_CHAR), "%s can not be null.", REPLACE_CHAR);
-        Preconditions.checkArgument(1 == props.getProperty(REPLACE_CHAR).length(), "%s's length must be one.", REPLACE_CHAR);
+        MaskAlgorithmUtil.checkSingleCharConfig(props, REPLACE_CHAR, getType());
         return props.getProperty(REPLACE_CHAR).charAt(0);
     }
     
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 4d4f7f02521..d4e13894aa0 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
@@ -17,9 +17,9 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
-import com.google.common.base.Preconditions;
 import com.google.common.base.Strings;
 import lombok.Getter;
+import org.apache.shardingsphere.mask.algorithm.MaskAlgorithmUtil;
 import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
 
 import java.util.Properties;
@@ -53,18 +53,17 @@ public final class MaskFromXToYMaskAlgorithm implements MaskAlgorithm<Object, St
     }
     
     private Integer createFromX(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(FROM_X), "%s can not be null.", FROM_X);
+        MaskAlgorithmUtil.checkIntegerTypeConfig(props, FROM_X, getType());
         return Integer.parseInt(props.getProperty(FROM_X));
     }
     
     private Integer createToY(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(TO_Y), "%s can not be null.", TO_Y);
+        MaskAlgorithmUtil.checkIntegerTypeConfig(props, TO_Y, getType());
         return Integer.parseInt(props.getProperty(TO_Y));
     }
     
     private Character createReplaceChar(final Properties props) {
-        Preconditions.checkArgument(props.containsKey(REPLACE_CHAR), "%s can not be null.", REPLACE_CHAR);
-        Preconditions.checkArgument(1 == props.getProperty(REPLACE_CHAR).length(), "%s's length must be one.", REPLACE_CHAR);
+        MaskAlgorithmUtil.checkSingleCharConfig(props, REPLACE_CHAR, getType());
         return props.getProperty(REPLACE_CHAR).charAt(0);
     }
     
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/exception/MaskSQLException.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/exception/MaskSQLException.java
new file mode 100644
index 00000000000..6fd76cf9da5
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/exception/MaskSQLException.java
@@ -0,0 +1,35 @@
+/*
+ * 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.exception;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.SQLState;
+import org.apache.shardingsphere.infra.util.exception.external.sql.type.feature.FeatureSQLException;
+
+/**
+ * Mask SQL exception.
+ */
+public abstract class MaskSQLException extends FeatureSQLException {
+    
+    private static final long serialVersionUID = 7788917372368241543L;
+    
+    private static final int FEATURE_CODE = 9;
+    
+    public MaskSQLException(final SQLState sqlState, final int errorCode, final String reason, final Object... messageArgs) {
+        super(sqlState, FEATURE_CODE, errorCode, reason, messageArgs);
+    }
+}
diff --git a/features/mask/core/src/main/java/org/apache/shardingsphere/mask/exception/algorithm/MaskAlgorithmInitializationException.java b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/exception/algorithm/MaskAlgorithmInitializationException.java
new file mode 100644
index 00000000000..70e22710117
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/exception/algorithm/MaskAlgorithmInitializationException.java
@@ -0,0 +1,33 @@
+/*
+ * 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.exception.algorithm;
+
+import org.apache.shardingsphere.infra.util.exception.external.sql.sqlstate.XOpenSQLState;
+import org.apache.shardingsphere.mask.exception.MaskSQLException;
+
+/**
+ * Mask algorithm initialization exception.
+ */
+public final class MaskAlgorithmInitializationException extends MaskSQLException {
+    
+    private static final long serialVersionUID = -2004166948563207100L;
+    
+    public MaskAlgorithmInitializationException(final String maskType, final String reason) {
+        super(XOpenSQLState.GENERAL_ERROR, 80, "Mask algorithm `%s` initialization failed, reason is: %s", maskType, reason);
+    }
+}
diff --git a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
index c54ea5caaed..fc69572429b 100644
--- a/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
+++ b/features/mask/core/src/main/resources/META-INF/services/org.apache.shardingsphere.mask.spi.MaskAlgorithm
@@ -18,7 +18,7 @@
 org.apache.shardingsphere.mask.algorithm.hash.MD5MaskAlgorithm
 org.apache.shardingsphere.mask.algorithm.cover.KeepFirstNLastMMaskAlgorithm
 org.apache.shardingsphere.mask.algorithm.cover.KeepFromXToYMaskAlgorithm
-org.apache.shardingsphere.mask.algorithm.cover.MaskAfterSpecialCharAlgorithm
-org.apache.shardingsphere.mask.algorithm.cover.MaskBeforeSpecialCharAlgorithm
+org.apache.shardingsphere.mask.algorithm.cover.MaskAfterSpecialCharsAlgorithm
+org.apache.shardingsphere.mask.algorithm.cover.MaskBeforeSpecialCharsAlgorithm
 org.apache.shardingsphere.mask.algorithm.cover.MaskFirstNLastMMaskAlgorithm
 org.apache.shardingsphere.mask.algorithm.cover.MaskFromXToYMaskAlgorithm
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 08cc5797571..d17aa7a366f 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,6 +17,7 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -55,7 +56,7 @@ public final class KeepFirstNLastMMaskAlgorithmTest {
         assertThat(actual, is("abc"));
     }
     
-    @Test(expected = IllegalArgumentException.class)
+    @Test(expected = MaskAlgorithmInitializationException.class)
     public void assertInitWhenConfigWrongProps() {
         KeepFirstNLastMMaskAlgorithm maskAlgorithm = new KeepFirstNLastMMaskAlgorithm();
         maskAlgorithm.init(createProperties("", "3", "+"));
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 7d274dda08a..148aba3e1c4 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,6 +17,7 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -61,7 +62,7 @@ public final class KeepFromXToYMaskAlgorithmTest {
         assertThat(actual, is("a"));
     }
     
-    @Test(expected = IllegalArgumentException.class)
+    @Test(expected = MaskAlgorithmInitializationException.class)
     public void assertInitWhenConfigWrongProps() {
         KeepFirstNLastMMaskAlgorithm maskAlgorithm = new KeepFirstNLastMMaskAlgorithm();
         maskAlgorithm.init(createProperties("", "3", "+"));
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithmTest.java
similarity index 78%
rename from features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharAlgorithmTest.java
rename to features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithmTest.java
index e2585a3c59c..072d5db4b3b 100644
--- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharAlgorithmTest.java
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharsAlgorithmTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -26,19 +27,20 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
 
-public final class MaskAfterSpecialCharAlgorithmTest {
+public final class MaskAfterSpecialCharsAlgorithmTest {
     
-    private MaskAfterSpecialCharAlgorithm maskAlgorithm;
+    private MaskAfterSpecialCharsAlgorithm maskAlgorithm;
     
     @Before
     public void setUp() {
-        maskAlgorithm = new MaskAfterSpecialCharAlgorithm();
+        maskAlgorithm = new MaskAfterSpecialCharsAlgorithm();
         maskAlgorithm.init(createProperties("d1"));
     }
     
-    private Properties createProperties(final String specialCharacters) {
+    private Properties createProperties(final String specialChars) {
         Properties result = new Properties();
-        result.setProperty("special-characters", specialCharacters);
+        result.setProperty("special-chars", specialChars);
+        result.setProperty("replace-char", "*");
         return result;
     }
     
@@ -49,7 +51,7 @@ public final class MaskAfterSpecialCharAlgorithmTest {
     }
     
     @Test
-    public void assertMaskWhenPlainValueMatchedMultipleSpecialCharacters() {
+    public void assertMaskWhenPlainValueMatchedMultipleSpecialChars() {
         String actual = maskAlgorithm.mask("abcd1234d1234");
         assertThat(actual, is("abcd1********"));
     }
@@ -67,14 +69,14 @@ public final class MaskAfterSpecialCharAlgorithmTest {
     }
     
     @Test
-    public void assertMaskWhenPlainValueNotMatchedSpecialCharacters() {
+    public void assertMaskWhenPlainValueNotMatchedSpecialChars() {
         String actual = maskAlgorithm.mask("abcd234");
         assertThat(actual, is("abcd234"));
     }
     
-    @Test(expected = IllegalArgumentException.class)
+    @Test(expected = MaskAlgorithmInitializationException.class)
     public void assertInitWhenConfigWrongProps() {
-        MaskBeforeSpecialCharAlgorithm maskAlgorithm = new MaskBeforeSpecialCharAlgorithm();
+        MaskBeforeSpecialCharsAlgorithm maskAlgorithm = new MaskBeforeSpecialCharsAlgorithm();
         maskAlgorithm.init(createProperties(""));
     }
 }
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithmTest.java
similarity index 78%
rename from features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharAlgorithmTest.java
rename to features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithmTest.java
index 1de77373364..d39de0a0441 100644
--- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharAlgorithmTest.java
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharsAlgorithmTest.java
@@ -17,6 +17,7 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -26,19 +27,20 @@ import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
 
-public final class MaskBeforeSpecialCharAlgorithmTest {
+public final class MaskBeforeSpecialCharsAlgorithmTest {
     
-    private MaskBeforeSpecialCharAlgorithm maskAlgorithm;
+    private MaskBeforeSpecialCharsAlgorithm maskAlgorithm;
     
     @Before
     public void setUp() {
-        maskAlgorithm = new MaskBeforeSpecialCharAlgorithm();
+        maskAlgorithm = new MaskBeforeSpecialCharsAlgorithm();
         maskAlgorithm.init(createProperties("d1"));
     }
     
-    private Properties createProperties(final String specialCharacters) {
+    private Properties createProperties(final String specialChars) {
         Properties result = new Properties();
-        result.setProperty("special-characters", specialCharacters);
+        result.setProperty("special-chars", specialChars);
+        result.setProperty("replace-char", "*");
         return result;
     }
     
@@ -49,7 +51,7 @@ public final class MaskBeforeSpecialCharAlgorithmTest {
     }
     
     @Test
-    public void assertMaskWhenPlainValueMatchedMultipleSpecialCharacters() {
+    public void assertMaskWhenPlainValueMatchedMultipleSpecialChars() {
         String actual = maskAlgorithm.mask("abcd1234d1234");
         assertThat(actual, is("***d1234d1234"));
     }
@@ -67,14 +69,14 @@ public final class MaskBeforeSpecialCharAlgorithmTest {
     }
     
     @Test
-    public void assertMaskWhenPlainValueNotMatchedSpecialCharacters() {
+    public void assertMaskWhenPlainValueNotMatchedSpecialChars() {
         String actual = maskAlgorithm.mask("abcd234");
         assertThat(actual, is("abcd234"));
     }
     
-    @Test(expected = IllegalArgumentException.class)
+    @Test(expected = MaskAlgorithmInitializationException.class)
     public void assertInitWhenConfigWrongProps() {
-        MaskBeforeSpecialCharAlgorithm maskAlgorithm = new MaskBeforeSpecialCharAlgorithm();
+        MaskBeforeSpecialCharsAlgorithm maskAlgorithm = new MaskBeforeSpecialCharsAlgorithm();
         maskAlgorithm.init(createProperties(""));
     }
 }
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 91bd8d0c6cb..df71fc8db2a 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,6 +17,7 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -45,7 +46,7 @@ public final class MaskFirstNLastMMaskAlgorithmTest {
         assertThat(maskAlgorithm.mask("ab"), is("**"));
     }
     
-    @Test(expected = IllegalArgumentException.class)
+    @Test(expected = MaskAlgorithmInitializationException.class)
     public void assertInitWhenConfigWrongProps() {
         maskAlgorithm.init(createProperties("", "3", "+"));
     }
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 558c8175cb5..a7643177297 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,6 +17,7 @@
 
 package org.apache.shardingsphere.mask.algorithm.cover;
 
+import org.apache.shardingsphere.mask.exception.algorithm.MaskAlgorithmInitializationException;
 import org.junit.Before;
 import org.junit.Test;
 
@@ -50,7 +51,7 @@ public final class MaskFromXToYMaskAlgorithmTest {
         assertThat(maskAlgorithm.mask("abc1"), is("abc*"));
     }
     
-    @Test(expected = IllegalArgumentException.class)
+    @Test(expected = MaskAlgorithmInitializationException.class)
     public void assertInitWhenConfigWrongProps() {
         maskAlgorithm.init(createProperties("5", "", "+"));
     }