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" | os.family == "solaris")
  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         if (Platform.isSolaris()) {
  80             data.add(new String[]{"ja", "x-eucjp-open"});
  81             data.add(new String[]{"ja_JP.eucJP", "x-eucjp-open"});
  82             data.add(new String[]{"ja_JP.PCK", "x-PCK"});
  83             data.add(new String[]{"ja_JP.UTF-8", "utf-8"});
  84         }
  85         return data.iterator();
  86     }
  87 
  88     @Test(dataProvider = "locales")
  89     public void testDefaultCharset(String locale, String expectedCharset)
  90             throws Exception {
  91         String actual = runWithLocale(locale);
  92         if (UNSUPPORTED.equals(actual)) {
  93             System.out.println(locale + ": Locale not supported, skipping...");
  94         } else {
  95             assertTrue(actual.equalsIgnoreCase(expectedCharset),
  96                        String.format("LC_ALL = %s, got defaultCharset = %s, "
  97                                + "NOT as expected %s",
  98                                locale, actual, expectedCharset));
  99         }
 100     }
 101 
 102     private static String runWithLocale(String locale) throws Exception {
 103         env.remove("LC_ALL");
 104         env.put("LC_ALL", locale);
 105         return ProcessTools.executeProcess(pb)
 106                            .shouldHaveExitValue(0)
 107                            .getStdout()
 108                            .replace(System.lineSeparator(), "");
 109     }
 110 }