1 /* 2 * Copyright (c) 2013, 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 24 import 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 * @build TestHelper TestFileEncoding ExpectedEncoding 32 * @run main TestFileEncoding UTF-8 33 * @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding 34 * @run main TestFileEncoding UTF-8 en_US.UTF-8 35 * @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding en_US.UTF-8 36 * @run main TestFileEncoding US-ASCII C 37 * @run main/othervm -Dfile.encoding=MyEncoding -DuserEncoding=MyEncoding TestFileEncoding MyEncoding C 38 * @author Brent Christian 39 */ 40 41 /** 42 * Setup the environment and run a sub-test to check the expected value of 43 * file.encoding, based on the value(s) of encoding-related environment vars 44 * (LANG, LC_ALL, LC_CTYPE). 45 * 46 * The first argument (required) is the expected value of the 47 * file.encoding System property. 48 * The second argument (optional) is the value to set to the LANG/etc env vars. 49 */ 50 public class TestFileEncoding { 51 private static final String TEST_NAME = "ExpectedEncoding"; 52 53 private String expectedEncoding; // Expected value for file.encoding 54 private String langVar = null; // Value to set for LANG, etc 55 56 private static Set<String> envToRm = new HashSet<>(3); 57 static { 58 // Take these vars out of the test's run environment, possibly adding 59 // our own value back in. 60 envToRm.add("LANG"); 61 envToRm.add("LC_ALL"); 62 envToRm.add("LC_CTYPE"); 63 } 64 65 public TestFileEncoding(String expectedEncoding) { 66 this.expectedEncoding = expectedEncoding; 67 } 68 69 public TestFileEncoding(String expectedEncoding, String langVar) { 70 this.expectedEncoding = expectedEncoding; 71 this.langVar = langVar; 72 } 73 74 /* 75 * Launch ExpectedEncoding with the given parameters, check for the 76 * expected file.encoding. 77 */ 78 private void run() { 79 String testClasses = System.getProperty("test.classes"); 80 81 // Pick up VM opts 82 String vmOptsStr = System.getProperty("test.vm.opts"); 83 System.out.println("test.vm.opts: " + vmOptsStr); 84 String[] vmOpts = new String[0]; 85 if (vmOptsStr != null && !"".equals(vmOptsStr)) { 86 vmOpts = vmOptsStr.split(" "); 87 System.out.println("found vm options:"); 88 for (String opt : vmOpts) { 89 System.out.println(" <" + opt + ">"); 90 } 91 } 92 93 // Build java cmd 94 LinkedList<String> cmdList = new LinkedList<>(); 95 cmdList.add(TestHelper.javaCmd); 96 for (String vmOpt : vmOpts) { 97 if (vmOpt != null && !vmOpt.equals("")) { 98 cmdList.add(vmOpt); 99 } 100 } 101 102 // See if the user specified a file.encoding that we should pass through 103 String userEncoding = System.getProperty("userEncoding"); 104 if (userEncoding != null) { 105 cmdList.add("-Dfile.encoding="+userEncoding); 106 } 107 108 cmdList.add("-cp"); 109 cmdList.add(testClasses); 110 cmdList.add(TEST_NAME); 111 cmdList.add(expectedEncoding); 112 cmdList.add("skip"); // ignore sun.jnu.encoding for this test 113 114 String cmdArray[] = new String[cmdList.size()]; 115 cmdList.toArray(cmdArray); 116 117 // Run the test(s) 118 if (langVar == null) { 119 System.out.println("TestFileEncoding: Running with no envvars set"); 120 TestHelper.TestResult tr = TestHelper.doExec(null, envToRm, 121 cmdArray); 122 checkResult(tr); 123 } else { 124 runWithEnvVar("LANG", cmdArray); 125 runWithEnvVar("LC_ALL", cmdArray); 126 runWithEnvVar("LC_CTYPE", cmdArray); 127 } 128 } 129 130 /* 131 * Run the test, setting the environment named by envVarName to the value 132 * in langVar. 133 */ 134 private void runWithEnvVar(String envVarName, String[] cmdArray) { 135 Map<String, String> envToAdd = new HashMap<>(1); 136 TestHelper.TestResult tr = null; 137 138 System.out.println("TestFileEncoding: Running with " + envVarName + "=" + langVar); 139 envToAdd.put(envVarName, langVar); 140 tr = TestHelper.doExec(envToAdd, envToRm, cmdArray); 141 checkResult(tr); 142 } 143 144 private void checkResult(TestHelper.TestResult tr) { 145 System.out.println(tr); 146 if (!tr.isOK()) { 147 throw new RuntimeException("TEST FAILED: !tr.isOK()"); 148 } 149 } 150 151 public static void main(String[] args) { 152 TestFileEncoding cfe = null; 153 if (!TestHelper.isMacOSX) { 154 System.out.println("Test is currently only for Mac OS X - pass."); 155 return; 156 } 157 if (args.length == 1) { 158 cfe = new TestFileEncoding(args[0]); 159 } else if (args.length == 2) { 160 cfe = new TestFileEncoding(args[0], args[1]); 161 } else { 162 System.out.println("Usage: TestFileEncoding <expected file.encoding>"); 163 System.out.println(" TestFileEncoding <expected file.encoding> <value for LANG/etc env var>"); 164 return; 165 } 166 cfe.run(); 167 } 168 } 169