Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
161 views
in Technique[技术] by (71.8m points)

java - Comparing two strings works on windows but fails on linux

I have written a unit test, which controls if there are any encoding problems with german letters( ?,?,?,etc.)

@Test
public void testBodyWithDefaultCharset() throws UnsupportedEncodingException {
    when(backendDefinition.getProperty(BackendDetailsEnum.MAIL_CHARSET.getName())).thenReturn(null);
    Charset defaultCharset  = Charset.defaultCharset();
    when(packet.getPayload()).thenReturn(defaultCharset.encode("??ü??ü?").array());

    final String mailText = classUnderTest.prepareMailText(backendDefinition, packet);

    assertThat(mailText, is(equalTo("??ü??ü?")));
}

This test passes in windows pc but fails on jenkins, which is a linux environment. The error message is as follows;

Expected: is "??ü??ü?"
but: was "???????"

My question is, is it wrong to compare mailText with "??ü??ü?"? I thougt I don't need to state any encoding when I compare two strings.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Might be that your file encoding and stuff is defined differently on Windows then on Linux? It sounds like it is a difference in the file-encoding.

You may try to explicit set encoding in the .bashrc or by using the locale-program in Linux (example with UTF-8):

export LC_ALL=en_US.UTF-8
export LANG=en_US.UTF-8
export LANGUAGE=en_US.UTF-8

What is the output of:

locale?

Any different than the one expected?

Also do check which Charset the defaultCharset() is using. Try outputting the value on both Windows / Linux.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...