Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[2029] Fallback to default charset if the charset provided by sun.st*… #2034

Merged
merged 3 commits into from May 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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();
}
}