1 /*
   2  * Copyright (c) 2017, 2020, 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.createTestJvm(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         data.add(new String[]{"ja_JP", "x-euc-jp-linux"});
  74         data.add(new String[]{"ja_JP.eucjp", "x-euc-jp-linux"});
  75         data.add(new String[]{"ja_JP.ujis", "x-euc-jp-linux"});
  76         data.add(new String[]{"ja_JP.utf8", "utf-8"});
  77         return data.iterator();
  78     }
  79 
  80     @Test(dataProvider = "locales")
  81     public void testDefaultCharset(String locale, String expectedCharset)
  82             throws Exception {
  83         String actual = runWithLocale(locale);
  84         if (UNSUPPORTED.equals(actual)) {
  85             System.out.println(locale + ": Locale not supported, skipping...");
  86         } else {
  87             assertTrue(actual.equalsIgnoreCase(expectedCharset),
  88                        String.format("LC_ALL = %s, got defaultCharset = %s, "
  89                                + "NOT as expected %s",
  90                                locale, actual, expectedCharset));
  91         }
  92     }
  93 
  94     private static String runWithLocale(String locale) throws Exception {
  95         env.remove("LC_ALL");
  96         env.put("LC_ALL", locale);
  97         return ProcessTools.executeProcess(pb)
  98                            .shouldHaveExitValue(0)
  99                            .getStdout()
 100                            .replace(System.lineSeparator(), "");
 101     }
 102 }