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 Default
  31  * @run testng DefaultCharsetTest
  32  */
  33 
  34 import java.util.ArrayList;
  35 import java.util.Iterator;
  36 import java.util.List;
  37 import java.util.Map;
  38 
  39 import jdk.test.lib.Platform;
  40 import jdk.test.lib.process.ProcessTools;
  41 
  42 import org.testng.annotations.BeforeClass;
  43 import org.testng.annotations.DataProvider;
  44 import org.testng.annotations.Test;
  45 
  46 import static org.testng.Assert.assertTrue;
  47 
  48 public class DefaultCharsetTest {
  49 
  50     private static final ProcessBuilder pb
  51             = ProcessTools.createJavaProcessBuilder(Default.class.getName());
  52     private static final Map<String, String> env = pb.environment();
  53     private static String UNSUPPORTED = null;
  54 
  55     @BeforeClass
  56     public static void checkSupports() throws Exception {
  57         UNSUPPORTED = runWithLocale("nonexist");
  58     }
  59 
  60     @DataProvider
  61     public static Iterator<Object[]> locales() {
  62         List<Object[]> data = new ArrayList<>();
  63         data.add(new String[]{"en_US", "iso-8859-1"});
  64         data.add(new String[]{"ja_JP.utf8", "utf-8"});
  65         data.add(new String[]{"tr_TR", "iso-8859-9"});
  66         data.add(new String[]{"C", "us-ascii"});
  67         if (Platform.isLinux()) {
  68             data.add(new String[]{"ja_JP", "x-euc-jp-linux"});
  69             data.add(new String[]{"ja_JP.eucjp", "x-euc-jp-linux"});
  70             data.add(new String[]{"ja_JP.ujis", "x-euc-jp-linux"});
  71             data.add(new String[]{"ja_JP.utf8", "utf-8"});
  72         }
  73         if (Platform.isSolaris()) {
  74             data.add(new String[]{"ja", "x-eucjp-open"});
  75             data.add(new String[]{"ja_JP.eucJP", "x-eucjp-open"});
  76             data.add(new String[]{"ja_JP.PCK", "x-PCK"});
  77             data.add(new String[]{"ja_JP.UTF-8", "utf-8"});
  78         }
  79         return data.iterator();
  80     }
  81 
  82     @Test(dataProvider = "locales")
  83     public void testDefaultCharset(String locale, String expectedCharset)
  84             throws Exception {
  85         String actual = runWithLocale(locale);
  86         if (UNSUPPORTED.equals(actual)) {
  87             System.out.println(locale + ": Locale not supported, skipping...");
  88         } else {
  89             assertTrue(actual.equalsIgnoreCase(expectedCharset),
  90                        String.format("LC_ALL = %s, got defaultCharset = %s, "
  91                                + "NOT as expected %s",
  92                                locale, actual, expectedCharset));
  93         }
  94     }
  95 
  96     private static String runWithLocale(String locale) throws Exception {
  97         env.remove("LC_ALL");
  98         env.put("LC_ALL", locale);
  99         return ProcessTools.executeProcess(pb)
 100                            .shouldHaveExitValue(0)
 101                            .getStdout()
 102                            .replace(System.lineSeparator(), "");
 103     }
 104 }