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