Created
January 22, 2020 11:31
-
-
Save yusufcakal/4402ffae261a162497b84698a7be55f2 to your computer and use it in GitHub Desktop.
It can use text mask to bounds of text certain begin and end indices. Enjoy your data driven test via spock framework :)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public class TextMasker { | |
public static String mask(String text, int begin, int end) { | |
if (Objects.isNull(text)) { | |
return null; | |
} | |
if (areIndexesInBoundOfText(text, begin, end)) { | |
String firstDisplayablePart = text.substring(0, begin); | |
String secondDisplayablePart = text.substring(end); | |
String maskedPart = "*".repeat(end - begin); | |
return firstDisplayablePart + maskedPart + secondDisplayablePart; | |
} | |
return "*".repeat(text.length()); | |
} | |
private static boolean areIndexesInBoundOfText(String text, int begin, int end) { | |
return begin > 0 && begin <= end && end < text.length(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import spock.lang.Specification | |
import spock.lang.Unroll | |
class TextMaskerSpec extends Specification { | |
@Unroll | |
def "should mask text from #begin index to #end index"() { | |
when: | |
def maskedText = StringUtils.mask(text, begin, end) | |
then: | |
maskedText == expectedResult | |
where: | |
text | begin | end | expectedResult | |
"1234123412341234" | 6 | 12 | "123412******1234" | |
"1234123412341234" | 4 | 10 | "1234******341234" | |
null | 4 | 6 | null | |
"" | 4 | 6 | "" | |
"1234" | 6 | 3 | "****" | |
"1234" | 1 | 6 | "****" | |
"1234" | 1 | 2 | "1*34" | |
"1234" | 6 | 10 | "****" | |
"1234" | 0 | 0 | "****" | |
"1234" | -1 | 0 | "****" | |
"1234" | 1 | -1 | "****" | |
"1234" | -1 | -1 | "****" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment