Path: blob/master/test/jdk/java/lang/System/MacEncoding/TestFileEncoding.java
41153 views
/*1* Copyright (c) 2013, 2016, 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* @modules jdk.compiler31* @build TestHelper TestFileEncoding ExpectedEncoding32* @run main TestFileEncoding UTF-833* @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding34* @run main TestFileEncoding UTF-8 en_US.UTF-835* @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding en_US.UTF-836* @run main TestFileEncoding US-ASCII C37* @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding C38* @author Brent Christian39*/4041/**42* Setup the environment and run a sub-test to check the expected value of43* file.encoding, based on the value(s) of encoding-related environment vars44* (LANG, LC_ALL, LC_CTYPE).45*46* The first argument (required) is the expected value of the47* file.encoding System property.48* The second argument (optional) is the value to set to the LANG/etc env vars.49*/50public class TestFileEncoding {51private static final String TEST_NAME = "ExpectedEncoding";5253private String expectedEncoding; // Expected value for file.encoding54private String langVar = null; // Value to set for LANG, etc5556private static Set<String> envToRm = new HashSet<>(3);57static {58// Take these vars out of the test's run environment, possibly adding59// our own value back in.60envToRm.add("LANG");61envToRm.add("LC_ALL");62envToRm.add("LC_CTYPE");63}6465public TestFileEncoding(String expectedEncoding) {66this.expectedEncoding = expectedEncoding;67}6869public TestFileEncoding(String expectedEncoding, String langVar) {70this.expectedEncoding = expectedEncoding;71this.langVar = langVar;72}7374/*75* Launch ExpectedEncoding with the given parameters, check for the76* expected file.encoding.77*/78private void run() {79String testClasses = System.getProperty("test.classes");8081// Pick up VM opts82String vmOptsStr = System.getProperty("test.vm.opts");83System.out.println("test.vm.opts: " + vmOptsStr);84String[] vmOpts = new String[0];85if (vmOptsStr != null && !"".equals(vmOptsStr)) {86vmOpts = vmOptsStr.split(" ");87System.out.println("found vm options:");88for (String opt : vmOpts) {89System.out.println(" <" + opt + ">");90}91}9293// Build java cmd94LinkedList<String> cmdList = new LinkedList<>();95cmdList.add(TestHelper.javaCmd);96for (String vmOpt : vmOpts) {97if (vmOpt != null && !vmOpt.equals("")) {98cmdList.add(vmOpt);99}100}101102// See if the user specified a file.encoding that we should pass through103String userEncoding = System.getProperty("userEncoding");104if (userEncoding != null) {105cmdList.add("-Dfile.encoding="+userEncoding);106}107108cmdList.add("-cp");109cmdList.add(testClasses);110cmdList.add(TEST_NAME);111cmdList.add(expectedEncoding);112cmdList.add("skip"); // ignore sun.jnu.encoding for this test113114String cmdArray[] = new String[cmdList.size()];115cmdList.toArray(cmdArray);116117// Run the test(s)118if (langVar == null) {119System.out.println("TestFileEncoding: Running with no envvars set");120TestHelper.TestResult tr = TestHelper.doExec(null, envToRm,121cmdArray);122checkResult(tr);123} else {124runWithEnvVar("LANG", cmdArray);125runWithEnvVar("LC_ALL", cmdArray);126runWithEnvVar("LC_CTYPE", cmdArray);127}128}129130/*131* Run the test, setting the environment named by envVarName to the value132* in langVar.133*/134private void runWithEnvVar(String envVarName, String[] cmdArray) {135Map<String, String> envToAdd = new HashMap<>(1);136TestHelper.TestResult tr = null;137138System.out.println("TestFileEncoding: Running with " + envVarName + "=" + langVar);139envToAdd.put(envVarName, langVar);140tr = TestHelper.doExec(envToAdd, envToRm, cmdArray);141checkResult(tr);142}143144private void checkResult(TestHelper.TestResult tr) {145System.out.println(tr);146if (!tr.isOK()) {147throw new RuntimeException("TEST FAILED: !tr.isOK()");148}149}150151public static void main(String[] args) {152TestFileEncoding cfe = null;153if (!TestHelper.isMacOSX) {154System.out.println("Test is currently only for Mac OS X - pass.");155return;156}157if (args.length == 1) {158cfe = new TestFileEncoding(args[0]);159} else if (args.length == 2) {160cfe = new TestFileEncoding(args[0], args[1]);161} else {162System.out.println("Usage: TestFileEncoding <expected file.encoding>");163System.out.println(" TestFileEncoding <expected file.encoding> <value for LANG/etc env var>");164return;165}166cfe.run();167}168}169170171