-
Notifications
You must be signed in to change notification settings - Fork 97
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
Showing
2 changed files
with
94 additions
and
6 deletions.
There are no files selected for viewing
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
74 changes: 74 additions & 0 deletions
74
src/test/java/org/codehaus/mojo/exec/LineRedirectOutputStreamTest.java
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
package org.codehaus.mojo.exec; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Assume; | ||
import org.junit.Test; | ||
|
||
import java.io.IOException; | ||
import java.nio.charset.Charset; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.function.Function; | ||
|
||
public class LineRedirectOutputStreamTest { | ||
|
||
@Test | ||
public void givenExtendedUnicodeCharacterOutput_whenRedirectingWithUtf8Charset_thenShouldDecodeProperly() | ||
throws IOException { | ||
internalTestForCharset(StandardCharsets.UTF_8); | ||
} | ||
|
||
@Test | ||
public void givenExtendedUnicodeCharacterOutput_whenRedirectingWithIso8859Charset_thenShouldDecodeProperly() | ||
throws IOException { | ||
internalTestForCharset(StandardCharsets.ISO_8859_1); | ||
} | ||
|
||
@Test | ||
public void givenExtendedUnicodeCharacterOutput_whenRedirectingWithCp1252_thenShouldDecodeProperly() | ||
throws IOException { | ||
Assume.assumeTrue("The JVM does not contain the cp-1252 charset", Charset.availableCharsets().containsKey("windows-1252")); | ||
internalTestForCharset(Charset.forName("windows-1252")); | ||
} | ||
|
||
@Test | ||
public void givenExtendedUnicodeCharacterOutput_whenRedirectingWithDefaultCharset_thenShouldDecodeProperly() | ||
throws IOException { | ||
internalTestForCharset(Charset.defaultCharset()); | ||
} | ||
|
||
@Test | ||
public void givenExtendedUnicodeCharacterOutput_whenRedirectingWithCharsetUnspecified_thenShouldDecodeProperly() | ||
throws IOException { | ||
internalTestForCharset(sb -> new LineRedirectOutputStream(sb::append), Charset.defaultCharset()); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void givenNullCharset_whenInstantiating_thenShouldThrow() { | ||
new LineRedirectOutputStream(new StringBuilder()::append, null); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void givenNullStringConsumer_whenInstantiating_thenShouldThrow() { | ||
new LineRedirectOutputStream(null, Charset.defaultCharset()); | ||
} | ||
|
||
private void internalTestForCharset(Charset charset) throws IOException { | ||
internalTestForCharset(sb -> new LineRedirectOutputStream(sb::append, charset), charset); | ||
} | ||
|
||
private void internalTestForCharset(Function<StringBuilder, LineRedirectOutputStream> lineRedirectOutputStream, | ||
Charset charset) throws IOException { | ||
StringBuilder sb = new StringBuilder(); | ||
String firstLine = "Hello, 你好, नमस्ते, مرحبا, γεια σας, שלום, こんにちは, 안녕하세요!"; | ||
String secondLine = "🌍 Welcome to the world! 🌟✨🎉🔥"; | ||
String expectedString = firstLine + secondLine; | ||
|
||
try (LineRedirectOutputStream os = lineRedirectOutputStream.apply(sb)) { | ||
os.write(String.join("\n", firstLine, secondLine).getBytes(charset)); | ||
} | ||
|
||
// The String to bytes to String is required here because StringCoding uses the Charset.defaultCharset() | ||
// internally so it would make the test fail when testing for different charsets. | ||
Assert.assertEquals(new String(expectedString.getBytes(charset), charset), sb.toString()); | ||
} | ||
} |
d975178
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you ought to also test this with a command output that isn’t a valid string in the current charset.
d975178
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because otherwise that will be my next bugreport.
d975178
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
PR with such test are welcome. This one is already merged.
d975178
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, maybe put
\xFE
into aByteArrayInputStream
and test if that arrives properly on the other side?If, at the same time, you test that
m\xC3\xA4h
is read asm\u00E4h
(or just kept as byte array, not stringified) and ends up asm\xC3\xA4h
in the output again, you’re good.Script output is not bound to a specific string locale, it’s raw octets. Python 3 had to learn this the hard way as well.
d975178
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please create PR with your proposition or at least new issue ... comment on merged commit can be forgotten 😄
d975178
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.