Fix unicode issue in GetTldCommand (#2108)

This commit is contained in:
Weimin Yu 2023-08-16 12:24:35 -04:00 committed by GitHub
parent 99840488a1
commit 68d35d2d95
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 9 deletions

View file

@ -16,12 +16,15 @@ package google.registry.tools;
import static google.registry.model.tld.TldYamlUtils.getObjectMapper; import static google.registry.model.tld.TldYamlUtils.getObjectMapper;
import static google.registry.model.tld.Tlds.assertTldsExist; import static google.registry.model.tld.Tlds.assertTldsExist;
import static java.nio.charset.StandardCharsets.UTF_8;
import com.beust.jcommander.Parameter; import com.beust.jcommander.Parameter;
import com.beust.jcommander.Parameters; import com.beust.jcommander.Parameters;
import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.ObjectMapper;
import google.registry.model.tld.Tld; import google.registry.model.tld.Tld;
import java.io.PrintStream;
import java.io.UnsupportedEncodingException;
import java.util.List; import java.util.List;
/** Command to show a TLD record. */ /** Command to show a TLD record. */
@ -34,10 +37,12 @@ final class GetTldCommand implements Command {
private List<String> mainParameters; private List<String> mainParameters;
@Override @Override
public void run() throws JsonProcessingException { public void run() throws JsonProcessingException, UnsupportedEncodingException {
ObjectMapper mapper = getObjectMapper(); ObjectMapper mapper = getObjectMapper();
for (String tld : assertTldsExist(mainParameters)) { try (PrintStream printStream = new PrintStream(System.out, false, UTF_8.name())) {
System.out.println(mapper.writeValueAsString(Tld.get(tld))); for (String tld : assertTldsExist(mainParameters)) {
printStream.println(mapper.writeValueAsString(Tld.get(tld)));
}
} }
} }
} }

View file

@ -80,11 +80,12 @@ public abstract class CommandTestCase<C extends Command> {
RegistryToolEnvironment.UNITTEST.setup(systemPropertyExtension); RegistryToolEnvironment.UNITTEST.setup(systemPropertyExtension);
command = newCommandInstance(); command = newCommandInstance();
// Capture standard output/error. // Capture standard output/error. Use a single-byte encoding to emulate platforms where default
// charset is not UTF_8.
oldStdout = System.out; oldStdout = System.out;
System.setOut(new PrintStream(new OutputSplitter(System.out, stdout))); System.setOut(new PrintStream(new OutputSplitter(System.out, stdout), false, "US-ASCII"));
oldStderr = System.err; oldStderr = System.err;
System.setErr(new PrintStream(new OutputSplitter(System.err, stderr))); System.setErr(new PrintStream(new OutputSplitter(System.err, stderr), false, "US-ASCII"));
} }
@AfterEach @AfterEach

View file

@ -20,14 +20,11 @@ import static google.registry.testing.TestDataHelper.loadFile;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import com.beust.jcommander.ParameterException; import com.beust.jcommander.ParameterException;
import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
/** Unit tests for {@link GetTldCommand}. */ /** Unit tests for {@link GetTldCommand}. */
class GetTldCommandTest extends CommandTestCase<GetTldCommand> { class GetTldCommandTest extends CommandTestCase<GetTldCommand> {
// TODO (sarahbot): re-enable this test after we figure out why it fails during RC build.
@Disabled
@Test @Test
void testSuccess() throws Exception { void testSuccess() throws Exception {
createTld("xn--q9jyb4c"); createTld("xn--q9jyb4c");