|
3 | 3 | import static org.junit.jupiter.api.Assertions.*; |
4 | 4 | import io.github.easy4j.opencli.OpenCliExecutionTarget; |
5 | 5 | import io.github.easy4j.opencli.OpenCliProperties; |
| 6 | +import java.util.Arrays; |
6 | 7 | import org.apache.commons.exec.CommandLine; |
7 | 8 | import org.junit.jupiter.api.Test; |
8 | 9 |
|
@@ -31,6 +32,23 @@ void shouldAppendQuotedKeyValueWithTrailingEquals() { |
31 | 32 | assertTrue(args[args.length - 1].contains("--key=val")); |
32 | 33 | } |
33 | 34 |
|
| 35 | + @Test |
| 36 | + void shouldDecodeUtf8OutputRegardlessOfPlatformCharset() { |
| 37 | + OpenCliProperties props = new OpenCliProperties(); |
| 38 | + props.setExecutable("/bin/sh"); |
| 39 | + props.setCommandTimeoutMillis(30_000L); |
| 40 | + OpenCliExecutor executor = new OpenCliExecutor(props); |
| 41 | + |
| 42 | + // 你 = \344\275\240, 好 = \345\245\275(POSIX printf 八进制转义)。 |
| 43 | + // Java 源码里反斜杠必须双写,否则编译器会把 \344 当成八进制转义吃掉。 |
| 44 | + OpenCliResult result = executor.invoke(Arrays.asList( |
| 45 | + "-c", "printf '\\344\\275\\240\\345\\245\\275'")); |
| 46 | + |
| 47 | + assertTrue(result.isSuccess(), "stderr=" + result.getStderr()); |
| 48 | + assertEquals("你好", result.getStdout().trim(), |
| 49 | + "UTF-8 bytes must decode with explicit UTF-8, not the platform charset"); |
| 50 | + } |
| 51 | + |
34 | 52 | @Test |
35 | 53 | void shouldConfigureRemoteMode() { |
36 | 54 | OpenCliProperties props = new OpenCliProperties(); |
|
0 commit comments