1/* 2 * Copyright (c) 2013, 2016, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 */ 23 24import java.util.*; 25 26/* 27 * @test 28 * @bug 8011194 29 * @summary Test value of file.encoding for corresponding value of LANG, etc 30 * @library ../../../../tools/launcher/ ../ 31 * @modules jdk.compiler 32 * @build TestHelper TestFileEncoding ExpectedEncoding 33 * @run main TestFileEncoding UTF-8 34 * @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding 35 * @run main TestFileEncoding UTF-8 en_US.UTF-8 36 * @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding en_US.UTF-8 37 * @run main TestFileEncoding US-ASCII C 38 * @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding C 39 * @author Brent Christian 40 */ 41 42/** 43 * Setup the environment and run a sub-test to check the expected value of 44 * file.encoding, based on the value(s) of encoding-related environment vars 45 * (LANG, LC_ALL, LC_CTYPE). 46 * 47 * The first argument (required) is the expected value of the 48 * file.encoding System property. 49 * The second argument (optional) is the value to set to the LANG/etc env vars. 50 */ 51public class TestFileEncoding { 52 private static final String TEST_NAME = "ExpectedEncoding"; 53 54 private String expectedEncoding; // Expected value for file.encoding 55 private String langVar = null; // Value to set for LANG, etc 56 57 private static Set<String> envToRm = new HashSet<>(3); 58 static { 59 // Take these vars out of the test's run environment, possibly adding 60 // our own value back in. 61 envToRm.add("LANG"); 62 envToRm.add("LC_ALL"); 63 envToRm.add("LC_CTYPE"); 64 } 65 66 public TestFileEncoding(String expectedEncoding) { 67 this.expectedEncoding = expectedEncoding; 68 } 69 70 public TestFileEncoding(String expectedEncoding, String langVar) { 71 this.expectedEncoding = expectedEncoding; 72 this.langVar = langVar; 73 } 74 75 /* 76 * Launch ExpectedEncoding with the given parameters, check for the 77 * expected file.encoding. 78 */ 79 private void run() { 80 String testClasses = System.getProperty("test.classes"); 81 82 // Pick up VM opts 83 String vmOptsStr = System.getProperty("test.vm.opts"); 84 System.out.println("test.vm.opts: " + vmOptsStr); 85 String[] vmOpts = new String[0]; 86 if (vmOptsStr != null && !"".equals(vmOptsStr)) { 87 vmOpts = vmOptsStr.split(" "); 88 System.out.println("found vm options:"); 89 for (String opt : vmOpts) { 90 System.out.println(" <" + opt + ">"); 91 } 92 } 93 94 // Build java cmd 95 LinkedList<String> cmdList = new LinkedList<>(); 96 cmdList.add(TestHelper.javaCmd); 97 for (String vmOpt : vmOpts) { 98 if (vmOpt != null && !vmOpt.equals("")) { 99 cmdList.add(vmOpt); 100 } 101 } 102 103 // See if the user specified a file.encoding that we should pass through 104 String userEncoding = System.getProperty("userEncoding"); 105 if (userEncoding != null) { 106 cmdList.add("-Dfile.encoding="+userEncoding); 107 } 108 109 cmdList.add("-cp"); 110 cmdList.add(testClasses); 111 cmdList.add(TEST_NAME); 112 cmdList.add(expectedEncoding); 113 cmdList.add("skip"); // ignore sun.jnu.encoding for this test 114 115 String cmdArray[] = new String[cmdList.size()]; 116 cmdList.toArray(cmdArray); 117 118 // Run the test(s) 119 if (langVar == null) { 120 System.out.println("TestFileEncoding: Running with no envvars set"); 121 TestHelper.TestResult tr = TestHelper.doExec(null, envToRm, 122 cmdArray); 123 checkResult(tr); 124 } else { 125 runWithEnvVar("LANG", cmdArray); 126 runWithEnvVar("LC_ALL", cmdArray); 127 runWithEnvVar("LC_CTYPE", cmdArray); 128 } 129 } 130 131 /* 132 * Run the test, setting the environment named by envVarName to the value 133 * in langVar. 134 */ 135 private void runWithEnvVar(String envVarName, String[] cmdArray) { 136 Map<String, String> envToAdd = new HashMap<>(1); 137 TestHelper.TestResult tr = null; 138 139 System.out.println("TestFileEncoding: Running with " + envVarName + "=" + langVar); 140 envToAdd.put(envVarName, langVar); 141 tr = TestHelper.doExec(envToAdd, envToRm, cmdArray); 142 checkResult(tr); 143 } 144 145 private void checkResult(TestHelper.TestResult tr) { 146 System.out.println(tr); 147 if (!tr.isOK()) { 148 throw new RuntimeException("TEST FAILED: !tr.isOK()"); 149 } 150 } 151 152 public static void main(String[] args) { 153 TestFileEncoding cfe = null; 154 if (!TestHelper.isMacOSX) { 155 System.out.println("Test is currently only for Mac OS X - pass."); 156 return; 157 } 158 if (args.length == 1) { 159 cfe = new TestFileEncoding(args[0]); 160 } else if (args.length == 2) { 161 cfe = new TestFileEncoding(args[0], args[1]); 162 } else { 163 System.out.println("Usage: TestFileEncoding <expected file.encoding>"); 164 System.out.println(" TestFileEncoding <expected file.encoding> <value for LANG/etc env var>"); 165 return; 166 } 167 cfe.run(); 168 } 169} 170