You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@shardingsphere.apache.org by du...@apache.org on 2022/12/16 01:07:41 UTC

[shardingsphere] branch master updated: Implement MASK_AFTER_SPECIAL_CHAR Algorithm (#22894)

This is an automated email from the ASF dual-hosted git repository.

duanzhengqiang 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 565927fad0c Implement MASK_AFTER_SPECIAL_CHAR Algorithm (#22894)
565927fad0c is described below

commit 565927fad0ca70a9c0b4c806ca35071439054c04
Author: gxxiong <xi...@foxmail.com>
AuthorDate: Fri Dec 16 09:07:26 2022 +0800

    Implement MASK_AFTER_SPECIAL_CHAR Algorithm (#22894)
---
 .../cover/MaskAfterSpecialCharAlgorithm.java       | 69 ++++++++++++++++++++++
 .../MaskAfterSpecialCharAlgorithmTest.java}        | 20 ++++---
 .../MaskBeforeSpecialCharAlgorithmTest.java        | 10 +++-
 3 files changed, 90 insertions(+), 9 deletions(-)

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/MaskAfterSpecialCharAlgorithm.java
new file mode 100644
index 00000000000..b60b7c59f5d
--- /dev/null
+++ b/features/mask/core/src/main/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharAlgorithm.java
@@ -0,0 +1,69 @@
+/*
+ * 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.cover;
+
+import com.google.common.base.Preconditions;
+import com.google.common.base.Strings;
+import lombok.Getter;
+import org.apache.shardingsphere.mask.spi.MaskAlgorithm;
+
+import java.util.Properties;
+
+/**
+ * Mask after special char algorithm.
+ */
+public final class MaskAfterSpecialCharAlgorithm implements MaskAlgorithm<Object, String> {
+    
+    private static final String SPECIAL_CHARACTERS = "special-characters";
+    
+    private String specialCharacters;
+    
+    @Getter
+    private Properties props;
+    
+    @Override
+    public void init(final Properties props) {
+        this.props = props;
+        this.specialCharacters = createSpecialCharacters(props);
+    }
+    
+    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);
+    }
+    
+    @Override
+    public String mask(final Object plainValue) {
+        String result = null == plainValue ? null : String.valueOf(plainValue);
+        if (Strings.isNullOrEmpty(result)) {
+            return result;
+        }
+        int index = result.indexOf(specialCharacters) == -1 ? -1 : result.indexOf(specialCharacters) + specialCharacters.length();
+        char[] chars = result.toCharArray();
+        for (int i = index; i != -1 && i < chars.length; i++) {
+            chars[i] = '*';
+        }
+        return new String(chars);
+    }
+    
+    @Override
+    public String getType() {
+        return "MASK_BEFORE_SPECIAL_CHAR";
+    }
+}
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/MaskBeforeSpecialCharAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharAlgorithmTest.java
similarity index 80%
copy from features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/MaskBeforeSpecialCharAlgorithmTest.java
copy to features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharAlgorithmTest.java
index 2d150d950a2..e2585a3c59c 100644
--- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/MaskBeforeSpecialCharAlgorithmTest.java
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskAfterSpecialCharAlgorithmTest.java
@@ -15,24 +15,24 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.mask.algorithm;
+package org.apache.shardingsphere.mask.algorithm.cover;
 
-import org.apache.shardingsphere.mask.algorithm.cover.MaskBeforeSpecialCharAlgorithm;
 import org.junit.Before;
 import org.junit.Test;
 
 import java.util.Properties;
 
 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 MaskAfterSpecialCharAlgorithmTest {
     
-    private MaskBeforeSpecialCharAlgorithm maskAlgorithm;
+    private MaskAfterSpecialCharAlgorithm maskAlgorithm;
     
     @Before
     public void setUp() {
-        maskAlgorithm = new MaskBeforeSpecialCharAlgorithm();
+        maskAlgorithm = new MaskAfterSpecialCharAlgorithm();
         maskAlgorithm.init(createProperties("d1"));
     }
     
@@ -45,13 +45,13 @@ public final class MaskBeforeSpecialCharAlgorithmTest {
     @Test
     public void assertMask() {
         String actual = maskAlgorithm.mask("abcd134");
-        assertThat(actual, is("***d134"));
+        assertThat(actual, is("abcd1**"));
     }
     
     @Test
     public void assertMaskWhenPlainValueMatchedMultipleSpecialCharacters() {
         String actual = maskAlgorithm.mask("abcd1234d1234");
-        assertThat(actual, is("***d1234d1234"));
+        assertThat(actual, is("abcd1********"));
     }
     
     @Test
@@ -60,6 +60,12 @@ public final class MaskBeforeSpecialCharAlgorithmTest {
         assertThat(actual, is(""));
     }
     
+    @Test
+    public void assertMaskNull() {
+        String actual = maskAlgorithm.mask(null);
+        assertThat(actual, is(nullValue()));
+    }
+    
     @Test
     public void assertMaskWhenPlainValueNotMatchedSpecialCharacters() {
         String actual = maskAlgorithm.mask("abcd234");
diff --git a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/MaskBeforeSpecialCharAlgorithmTest.java b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharAlgorithmTest.java
similarity index 90%
rename from features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/MaskBeforeSpecialCharAlgorithmTest.java
rename to features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharAlgorithmTest.java
index 2d150d950a2..1de77373364 100644
--- a/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/MaskBeforeSpecialCharAlgorithmTest.java
+++ b/features/mask/core/src/test/java/org/apache/shardingsphere/mask/algorithm/cover/MaskBeforeSpecialCharAlgorithmTest.java
@@ -15,15 +15,15 @@
  * limitations under the License.
  */
 
-package org.apache.shardingsphere.mask.algorithm;
+package org.apache.shardingsphere.mask.algorithm.cover;
 
-import org.apache.shardingsphere.mask.algorithm.cover.MaskBeforeSpecialCharAlgorithm;
 import org.junit.Before;
 import org.junit.Test;
 
 import java.util.Properties;
 
 import static org.hamcrest.CoreMatchers.is;
+import static org.hamcrest.CoreMatchers.nullValue;
 import static org.hamcrest.MatcherAssert.assertThat;
 
 public final class MaskBeforeSpecialCharAlgorithmTest {
@@ -60,6 +60,12 @@ public final class MaskBeforeSpecialCharAlgorithmTest {
         assertThat(actual, is(""));
     }
     
+    @Test
+    public void assertMaskNull() {
+        String actual = maskAlgorithm.mask(null);
+        assertThat(actual, is(nullValue()));
+    }
+    
     @Test
     public void assertMaskWhenPlainValueNotMatchedSpecialCharacters() {
         String actual = maskAlgorithm.mask("abcd234");