Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/java/lang/System/MacEncoding/TestFileEncoding.java
46901 views
/*1* Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.util.*;2425/*26* @test27* @bug 801119428* @summary Test value of file.encoding for corresponding value of LANG, etc29* @library ../../../../tools/launcher/ ../30* @build TestHelper TestFileEncoding ExpectedEncoding31* @run main TestFileEncoding UTF-832* @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding33* @run main TestFileEncoding UTF-8 en_US.UTF-834* @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding en_US.UTF-835* @run main TestFileEncoding US-ASCII C36* @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding C37* @author Brent Christian38*/3940/**41* Setup the environment and run a sub-test to check the expected value of42* file.encoding, based on the value(s) of encoding-related environment vars43* (LANG, LC_ALL, LC_CTYPE).44*45* The first argument (required) is the expected value of the46* file.encoding System property.47* The second argument (optional) is the value to set to the LANG/etc env vars.48*/49public class TestFileEncoding {50private static final String TEST_NAME = "ExpectedEncoding";5152private String expectedEncoding; // Expected value for file.encoding53private String langVar = null; // Value to set for LANG, etc5455private static Set<String> envToRm = new HashSet<>(3);56static {57// Take these vars out of the test's run environment, possibly adding58// our own value back in.59envToRm.add("LANG");60envToRm.add("LC_ALL");61envToRm.add("LC_CTYPE");62}6364public TestFileEncoding(String expectedEncoding) {65this.expectedEncoding = expectedEncoding;66}6768public TestFileEncoding(String expectedEncoding, String langVar) {69this.expectedEncoding = expectedEncoding;70this.langVar = langVar;71}7273/*74* Launch ExpectedEncoding with the given parameters, check for the75* expected file.encoding.76*/77private void run() {78String testClasses = System.getProperty("test.classes");7980// Pick up VM opts81String vmOptsStr = System.getProperty("test.vm.opts");82System.out.println("test.vm.opts: " + vmOptsStr);83String[] vmOpts = new String[0];84if (vmOptsStr != null && !"".equals(vmOptsStr)) {85vmOpts = vmOptsStr.split(" ");86System.out.println("found vm options:");87for (String opt : vmOpts) {88System.out.println(" <" + opt + ">");89}90}9192// Build java cmd93LinkedList<String> cmdList = new LinkedList<>();94cmdList.add(TestHelper.javaCmd);95for (String vmOpt : vmOpts) {96if (vmOpt != null && !vmOpt.equals("")) {97cmdList.add(vmOpt);98}99}100101// See if the user specified a file.encoding that we should pass through102String userEncoding = System.getProperty("userEncoding");103if (userEncoding != null) {104cmdList.add("-Dfile.encoding="+userEncoding);105}106107cmdList.add("-cp");108cmdList.add(testClasses);109cmdList.add(TEST_NAME);110cmdList.add(expectedEncoding);111cmdList.add("skip"); // ignore sun.jnu.encoding for this test112113String cmdArray[] = new String[cmdList.size()];114cmdList.toArray(cmdArray);115116// Run the test(s)117if (langVar == null) {118System.out.println("TestFileEncoding: Running with no envvars set");119TestHelper.TestResult tr = TestHelper.doExec(null, envToRm,120cmdArray);121checkResult(tr);122} else {123runWithEnvVar("LANG", cmdArray);124runWithEnvVar("LC_ALL", cmdArray);125runWithEnvVar("LC_CTYPE", cmdArray);126}127}128129/*130* Run the test, setting the environment named by envVarName to the value131* in langVar.132*/133private void runWithEnvVar(String envVarName, String[] cmdArray) {134Map<String, String> envToAdd = new HashMap<>(1);135TestHelper.TestResult tr = null;136137System.out.println("TestFileEncoding: Running with " + envVarName + "=" + langVar);138envToAdd.put(envVarName, langVar);139tr = TestHelper.doExec(envToAdd, envToRm, cmdArray);140checkResult(tr);141}142143private void checkResult(TestHelper.TestResult tr) {144System.out.println(tr);145if (!tr.isOK()) {146throw new RuntimeException("TEST FAILED: !tr.isOK()");147}148}149150public static void main(String[] args) {151TestFileEncoding cfe = null;152if (!TestHelper.isMacOSX) {153System.out.println("Test is currently only for Mac OS X - pass.");154return;155}156if (args.length == 1) {157cfe = new TestFileEncoding(args[0]);158} else if (args.length == 2) {159cfe = new TestFileEncoding(args[0], args[1]);160} else {161System.out.println("Usage: TestFileEncoding <expected file.encoding>");162System.out.println(" TestFileEncoding <expected file.encoding> <value for LANG/etc env var>");163return;164}165cfe.run();166}167}168169170