Skip to content

Commit

Permalink
[2029] Fallback to default charset if the charset provided by sun.st*…
Browse files Browse the repository at this point in the history
….encoding is invalid
  • Loading branch information
spyrkob authored and remkop committed May 31, 2023
1 parent d20d711 commit 7c7c8b9
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 2 deletions.
12 changes: 10 additions & 2 deletions src/main/java/picocli/CommandLine.java
Expand Up @@ -15045,7 +15045,15 @@ static Charset getStderrEncoding() {
static Charset charsetForName(String encoding) {
if (encoding != null) {
if ("cp65001".equalsIgnoreCase(encoding)) { encoding = "UTF-8"; } // #1474 MS Windows uses code page 65001 for UTF8
return Charset.forName(encoding);
try {
return Charset.forName(encoding);
} catch (Exception e) {
// fallback to default charset if the requested encoding is not available
final Charset defaultCharset = Charset.defaultCharset();
CommandLine.tracer().info("The %s encoding in not available, falling back to %s", encoding,
defaultCharset.name());
return defaultCharset;
}
}
return Charset.defaultCharset();
}
Expand Down Expand Up @@ -18705,7 +18713,7 @@ private List<String> prefixCommandName(List<String> suggestions)
if(commandName == null || commandName.trim().isEmpty()) { return suggestions; }
List<String> prefixedSuggestions = new ArrayList<String>();
for (String s : suggestions) {
prefixedSuggestions.add(commandName + " " + s);
prefixedSuggestions.add(commandName + " " + s);
}
return prefixedSuggestions;
}
Expand Down
55 changes: 55 additions & 0 deletions src/test/java/picocli/Issue2029.java
@@ -0,0 +1,55 @@
package picocli;

import org.junit.Rule;
import org.junit.Test;
import org.junit.contrib.java.lang.system.RestoreSystemProperties;
import org.junit.contrib.java.lang.system.SystemErrRule;
import org.junit.contrib.java.lang.system.SystemOutRule;
import org.junit.rules.TestRule;

import static org.junit.Assert.assertEquals;

public class Issue2029 {
@Rule
public final TestRule restoreSystemProperties = new RestoreSystemProperties();

@Rule
public final SystemOutRule systemOutRule = new SystemOutRule().enableLog();

@Rule
public final SystemErrRule systemErrRule = new SystemErrRule().enableLog();

@CommandLine.Command(name = "test")
static class TestCommand implements Runnable {

@CommandLine.Parameters
String text;

@CommandLine.Spec
CommandLine.Model.CommandSpec spec;

//@Override
public void run() {
spec.commandLine().getOut().print(text);
spec.commandLine().getOut().flush();
spec.commandLine().getErr().print(text);
spec.commandLine().getErr().flush();
}
}

@Test
public void invalidEncodingFallsbackToDefaultEncoding() {
resetLogs();
System.setProperty("sun.stdout.encoding", "cp0");
System.setProperty("sun.stdout.encoding", "cp0");

assertEquals(CommandLine.ExitCode.OK, new CommandLine(new Issue1320.TestCommand()).execute("test"));
assertEquals("test", systemOutRule.getLog());
assertEquals("test", systemErrRule.getLog());
}

private void resetLogs() {
systemOutRule.clearLog();
systemErrRule.clearLog();
}
}

0 comments on commit 7c7c8b9

Please sign in to comment.