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 4429040 4591027 4814743 27 * @summary Unit test for charset providers 28 * @library /test/lib 29 * @build jdk.test.lib.Utils 30 * jdk.test.lib.Asserts 31 * jdk.test.lib.JDKToolFinder 32 * jdk.test.lib.JDKToolLauncher 33 * jdk.test.lib.Platform 34 * jdk.test.lib.process.* 35 * jdk.test.lib.util.JarUtils 36 * FooCharset FooProvider CharsetTest 37 * @run driver SetupJar 38 * @run testng CharsetProviderBasicTest 39 */ 40 41 import java.io.File; 42 import java.util.ArrayList; 43 import java.util.Iterator; 44 import java.util.List; 45 import java.util.stream.Stream; 46 47 import jdk.test.lib.JDKToolFinder; 48 import jdk.test.lib.Utils; 49 import jdk.test.lib.process.ProcessTools; 50 51 import org.testng.annotations.DataProvider; 52 import org.testng.annotations.Test; 53 54 import static java.util.Arrays.asList; 55 56 public class CharsetProviderBasicTest { 57 58 private static final String TEST_SRC = System.getProperty("test.src"); 59 60 private static final List DEFAULT_CSS = List.of( 61 "US-ASCII", "8859_1", "iso-ir-6", "UTF-16", "windows-1252", "!BAR", "cp1252" 62 ); 63 64 private static final List MINIMAL_POLICY = List.of( 65 "-Djava.security.manager", 66 "-Djava.security.policy=" + TEST_SRC + File.separator + "default-pol" 67 ); 68 69 private static final List CP_POLICY = List.of( 70 "-Djava.security.manager", 71 "-Djava.security.policy=" + TEST_SRC + File.separator + "charsetProvider.sp" 72 ); 73 74 private static boolean checkSupports(String locale) throws Throwable { 75 return ProcessTools.executeProcess("sh", "-c", "LC_ALL=" + locale + " && " 76 + "locale -a | grep " + locale) 77 .getStdout() 78 .replace(System.lineSeparator(), "") 79 .equals(locale); 80 } 81 82 @DataProvider 83 public static Iterator<Object[]> testCases() { 84 return Stream.of("", "ja_JP.eucJP", "tr_TR") 85 .flatMap(locale -> Stream.of( 86 new Object[]{locale, List.of(""), "FOO"}, 87 new Object[]{locale, MINIMAL_POLICY, "!FOO"}, 88 new Object[]{locale, CP_POLICY, "FOO"} 89 )) 90 .iterator(); 91 } 92 93 @Test(dataProvider = "testCases") 94 public void testDefaultCharset(String locale, List opts, String css) throws Throwable { 95 if ((System.getProperty("os.name").startsWith("Windows") || !checkSupports(locale)) 96 && (!locale.isEmpty())) { 97 System.out.println(locale + ": Locale not supported, skipping..."); 98 return; 99 } 100 101 List<String> args = new ArrayList<>(); 102 args.add(JDKToolFinder.getJDKTool("java")); 103 args.addAll(asList(Utils.getTestJavaOpts())); 104 args.add("-cp"); 105 args.add(System.getProperty("test.class.path") + File.pathSeparator + "test.jar"); 106 args.addAll(opts); 107 args.add(CharsetTest.class.getName()); 108 args.addAll(DEFAULT_CSS); 109 args.add(css); 110 args.removeIf(t -> t.isEmpty()); 111 112 ProcessBuilder pb = new ProcessBuilder(args); 113 114 if (!locale.isEmpty()) { 115 pb.environment().put("LC_ALL", locale); 116 } 117 118 ProcessTools.executeCommand(pb) 119 .shouldHaveExitValue(0); 120 } 121 }