1 /* 2 * Copyright (c) 2017, 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 /* 25 * @test 26 * @bug 4772857 27 * @summary Unit test for Charset.defaultCharset 28 * @requires os.family == "linux" 29 * @library /test/lib 30 * @build jdk.test.lib.Utils 31 * jdk.test.lib.Asserts 32 * jdk.test.lib.JDKToolFinder 33 * jdk.test.lib.JDKToolLauncher 34 * jdk.test.lib.Platform 35 * jdk.test.lib.process.* 36 * Default 37 * @run testng DefaultCharsetTest 38 */ 39 40 import java.util.ArrayList; 41 import java.util.Iterator; 42 import java.util.List; 43 import java.util.Map; 44 45 import jdk.test.lib.Platform; 46 import jdk.test.lib.process.ProcessTools; 47 48 import org.testng.annotations.BeforeClass; 49 import org.testng.annotations.DataProvider; 50 import org.testng.annotations.Test; 51 52 import static org.testng.Assert.assertTrue; 53 54 public class DefaultCharsetTest { 55 56 private static final ProcessBuilder pb 57 = ProcessTools.createJavaProcessBuilder(true, Default.class.getName()); 58 private static final Map<String, String> env = pb.environment(); 59 private static String UNSUPPORTED = null; 60 61 @BeforeClass 62 public static void checkSupports() throws Exception { 63 UNSUPPORTED = runWithLocale("nonexist"); 64 } 65 66 @DataProvider 67 public static Iterator<Object[]> locales() { 68 List<Object[]> data = new ArrayList<>(); 69 data.add(new String[]{"en_US", "iso-8859-1"}); 70 data.add(new String[]{"ja_JP.utf8", "utf-8"}); 71 data.add(new String[]{"tr_TR", "iso-8859-9"}); 72 data.add(new String[]{"C", "us-ascii"}); 73 if (Platform.isLinux()) { 74 data.add(new String[]{"ja_JP", "x-euc-jp-linux"}); 75 data.add(new String[]{"ja_JP.eucjp", "x-euc-jp-linux"}); 76 data.add(new String[]{"ja_JP.ujis", "x-euc-jp-linux"}); 77 data.add(new String[]{"ja_JP.utf8", "utf-8"}); 78 } 79 return data.iterator(); 80 } 81 82 @Test(dataProvider = "locales") 83 public void testDefaultCharset(String locale, String expectedCharset) 84 throws Exception { 85 String actual = runWithLocale(locale); 86 if (UNSUPPORTED.equals(actual)) { 87 System.out.println(locale + ": Locale not supported, skipping..."); 88 } else { 89 assertTrue(actual.equalsIgnoreCase(expectedCharset), 90 String.format("LC_ALL = %s, got defaultCharset = %s, " 91 + "NOT as expected %s", 92 locale, actual, expectedCharset)); 93 } 94 } 95 96 private static String runWithLocale(String locale) throws Exception { 97 env.remove("LC_ALL"); 98 env.put("LC_ALL", locale); 99 return ProcessTools.executeProcess(pb) 100 .shouldHaveExitValue(0) 101 .getStdout() 102 .replace(System.lineSeparator(), ""); 103 } 104 }