In my previous post about building a regex to check a text without special characters but allow German and French. I met a problem that the unit test works fine on my machine using Eclipse, but it was fail when running on Jenkins' build job.
Here is my test:
Production code:
The result likes the following:
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:
And, modified the test case also:
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
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
By using Jenkins job, the Unicode character codes should be lower case all characters in Java code. for example: use '\u00e0' instead of '\u00E0'
ReplyDeleteDelete please, i already posted
DeleteThanks a lot. but, I won't. hehe
Deleteit just work when You use lower case for all special character. for ex: \u00E0 will not work
ReplyDeleteYou can use this tool to convert from unicode to hex:
ReplyDeletehttp://www.endmemo.com/unicode/unicodeconverter.php