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