Path: blob/master/test/jdk/java/nio/charset/Charset/DefaultCharsetTest.java
41155 views
/*1* Copyright (c) 2017, 2020, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 477285726* @summary Unit test for Charset.defaultCharset27* @requires os.family == "linux"28* @library /test/lib29* @build jdk.test.lib.Utils30* jdk.test.lib.Asserts31* jdk.test.lib.JDKToolFinder32* jdk.test.lib.JDKToolLauncher33* jdk.test.lib.Platform34* jdk.test.lib.process.*35* Default36* @run testng DefaultCharsetTest37*/3839import java.util.ArrayList;40import java.util.Iterator;41import java.util.List;42import java.util.Map;4344import jdk.test.lib.Platform;45import jdk.test.lib.process.ProcessTools;4647import org.testng.annotations.BeforeClass;48import org.testng.annotations.DataProvider;49import org.testng.annotations.Test;5051import static org.testng.Assert.assertTrue;5253public class DefaultCharsetTest {5455private static final ProcessBuilder pb56= ProcessTools.createTestJvm(Default.class.getName());57private static final Map<String, String> env = pb.environment();58private static String UNSUPPORTED = null;5960@BeforeClass61public static void checkSupports() throws Exception {62UNSUPPORTED = runWithLocale("nonexist");63}6465@DataProvider66public static Iterator<Object[]> locales() {67List<Object[]> data = new ArrayList<>();68data.add(new String[]{"en_US", "iso-8859-1"});69data.add(new String[]{"ja_JP.utf8", "utf-8"});70data.add(new String[]{"tr_TR", "iso-8859-9"});71data.add(new String[]{"C", "us-ascii"});72data.add(new String[]{"ja_JP", "x-euc-jp-linux"});73data.add(new String[]{"ja_JP.eucjp", "x-euc-jp-linux"});74data.add(new String[]{"ja_JP.ujis", "x-euc-jp-linux"});75data.add(new String[]{"ja_JP.utf8", "utf-8"});76return data.iterator();77}7879@Test(dataProvider = "locales")80public void testDefaultCharset(String locale, String expectedCharset)81throws Exception {82String actual = runWithLocale(locale);83if (UNSUPPORTED.equals(actual)) {84System.out.println(locale + ": Locale not supported, skipping...");85} else {86assertTrue(actual.equalsIgnoreCase(expectedCharset),87String.format("LC_ALL = %s, got defaultCharset = %s, "88+ "NOT as expected %s",89locale, actual, expectedCharset));90}91}9293private static String runWithLocale(String locale) throws Exception {94env.remove("LC_ALL");95env.put("LC_ALL", locale);96return ProcessTools.executeProcess(pb)97.shouldHaveExitValue(0)98.getStdout()99.replace(System.lineSeparator(), "");100}101}102103104