Here is my test:
@Test
public void shouldAllowFrenchAndGermanCharacters(){
String source = "ÄäÖöÜüß áÁàÀâÂéÉèÈêÊîÎçÇ";
assertFalse(SpecialCharactersUtils.isExistSpecialCharater(source));
}
Production code:
public static boolean isExistNotAllowedCharacters(String source){
Pattern regex = Pattern.compile("^[a-zA-Z_0-9_ÄäÖöÜüß áÁàÀâÂéÉèÈêÊîÎçÇ]*$");
Matcher matcher = regex.matcher(source);
return !matcher.matches();
}
The result likes the following:
Failed tests:
SpecialCharactersUtilsTest.shouldAllowFrenchAndGermanCharacters:32 null
A guy from stackoverflow.com says:
"This is probably due to the default encoding used for your Java source files. The ö in the string literal in the JUnit source code is probably being converted to something else when the test is compiled. To avoid this, use Unicode escapes (\uxxxx) in the string literals in your JUnit source code"
So, I tried to find what and where exactly the \uxxxx is. The answer they are Unicode character codes, and they could be easy to find. The following is an example:
https://en.wikipedia.org/wiki/List_of_Unicode_characters
I changed the function to use Unicode characters instead:
public static boolean isExistSpecialCharater(String source){
Pattern regex = Pattern.compile("^[a-zA-Z_0-9_\u00c4\u00e4\u00d6\u00f6\u00dc\u00fc\u00df\u00e0\u00c0\u00e1\u00c1\u00e2\u00c2\u00e9\u00c9\u00e8\u00c8\u00ea\u00ca\u00ee\u00ce\u00e7\u00c7\u0020\u0027]*$");
Matcher matcher = regex.matcher(source);
return !matcher.matches();
}
And, modified the test case also:
@Test
public void shouldAllowFrenchCharacters(){
String source = "\u00e0\u00c0\u00e1\u00c1\u00e2\u00c2\u00e9\u00c9\u00e8\u00c8\u00ea\u00ca\u00ee\u00ce\u00e7\u00c7\u0020\u0027";
assertFalse(SpecialCharactersUtils.isExistSpecialCharater(source));
}
Yeah, it works. Besides, I have already made it by writing an automation test with Selenium to make sure that it can also work on GUI as my expectation.
References:
[1]. http://stackoverflow.com/questions/4237581/comparing-unicode-characters-in-junit
[2]. http://www.widecodes.com/0zxqPkPkej/junit-fails-on-french-string-assertion.html