8181395: Refactor locale related shell tests Charset/default.sh and CheckSJISMappingProp.sh to java
authoramlu
Mon, 19 Jun 2017 09:41:59 +0800
changeset 45569 46642055841c
parent 45568 6a4abb2910ff
child 45570 8d5e343780d5
8181395: Refactor locale related shell tests Charset/default.sh and CheckSJISMappingProp.sh to java Reviewed-by: alanb
jdk/test/TEST.groups
jdk/test/java/nio/charset/Charset/DefaultCharsetTest.java
jdk/test/java/nio/charset/Charset/default.sh
jdk/test/java/nio/charset/coders/CheckSJISMappingProp.sh
jdk/test/java/nio/charset/coders/SJISMappingPropTest.java
jdk/test/java/nio/charset/coders/SJISPropTest.java
--- a/jdk/test/TEST.groups	Mon Jun 19 09:27:17 2017 +0800
+++ b/jdk/test/TEST.groups	Mon Jun 19 09:41:59 2017 +0800
@@ -554,7 +554,6 @@
   java/nio/charset/Charset/NIOCharsetAvailabilityTest.java \
   java/nio/charset/Charset/RegisteredCharsets.java \
   java/nio/charset/CharsetEncoder/Flush.java \
-  java/nio/charset/coders/CheckSJISMappingProp.sh \
   java/nio/charset/coders/ResetISO2022JP.java \
   java/util/Locale/InternationalBAT.java \
   java/util/Locale/LocaleProviders.sh \
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/nio/charset/Charset/DefaultCharsetTest.java	Mon Jun 19 09:41:59 2017 +0800
@@ -0,0 +1,110 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4772857
+ * @summary Unit test for Charset.defaultCharset
+ * @requires (os.family == "linux" | os.family == "solaris")
+ * @library /test/lib
+ * @build jdk.test.lib.Utils
+ *        jdk.test.lib.Asserts
+ *        jdk.test.lib.JDKToolFinder
+ *        jdk.test.lib.JDKToolLauncher
+ *        jdk.test.lib.Platform
+ *        jdk.test.lib.process.*
+ *        Default
+ * @run testng DefaultCharsetTest
+ */
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import jdk.test.lib.Platform;
+import jdk.test.lib.process.ProcessTools;
+
+import org.testng.annotations.BeforeClass;
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertTrue;
+
+public class DefaultCharsetTest {
+
+    private static final ProcessBuilder pb
+            = ProcessTools.createJavaProcessBuilder(true, Default.class.getName());
+    private static final Map<String, String> env = pb.environment();
+    private static String UNSUPPORTED = null;
+
+    @BeforeClass
+    public static void checkSupports() throws Exception {
+        UNSUPPORTED = runWithLocale("nonexist");
+    }
+
+    @DataProvider
+    public static Iterator<Object[]> locales() {
+        List<Object[]> data = new ArrayList<>();
+        data.add(new String[]{"en_US", "iso-8859-1"});
+        data.add(new String[]{"ja_JP.utf8", "utf-8"});
+        data.add(new String[]{"tr_TR", "iso-8859-9"});
+        data.add(new String[]{"C", "us-ascii"});
+        if (Platform.isLinux()) {
+            data.add(new String[]{"ja_JP", "x-euc-jp-linux"});
+            data.add(new String[]{"ja_JP.eucjp", "x-euc-jp-linux"});
+            data.add(new String[]{"ja_JP.ujis", "x-euc-jp-linux"});
+            data.add(new String[]{"ja_JP.utf8", "utf-8"});
+        }
+        if (Platform.isSolaris()) {
+            data.add(new String[]{"ja", "x-eucjp-open"});
+            data.add(new String[]{"ja_JP.eucJP", "x-eucjp-open"});
+            data.add(new String[]{"ja_JP.PCK", "x-PCK"});
+            data.add(new String[]{"ja_JP.UTF-8", "utf-8"});
+        }
+        return data.iterator();
+    }
+
+    @Test(dataProvider = "locales")
+    public void testDefaultCharset(String locale, String expectedCharset)
+            throws Exception {
+        String actual = runWithLocale(locale);
+        if (UNSUPPORTED.equals(actual)) {
+            System.out.println(locale + ": Locale not supported, skipping...");
+        } else {
+            assertTrue(actual.equalsIgnoreCase(expectedCharset),
+                       String.format("LC_ALL = %s, got defaultCharset = %s, "
+                               + "NOT as expected %s",
+                               locale, actual, expectedCharset));
+        }
+    }
+
+    private static String runWithLocale(String locale) throws Exception {
+        env.remove("LC_ALL");
+        env.put("LC_ALL", locale);
+        return ProcessTools.executeProcess(pb)
+                           .shouldHaveExitValue(0)
+                           .getStdout()
+                           .replace(System.lineSeparator(), "");
+    }
+}
--- a/jdk/test/java/nio/charset/Charset/default.sh	Mon Jun 19 09:27:17 2017 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,97 +0,0 @@
-#!/bin/sh
-
-#
-# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
-# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
-#
-# This code is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License version 2 only, as
-# published by the Free Software Foundation.
-#
-# This code is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-# version 2 for more details (a copy is included in the LICENSE file that
-# accompanied this code).
-#
-# You should have received a copy of the GNU General Public License version
-# 2 along with this work; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-#
-# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
-# or visit www.oracle.com if you need additional information or have any
-# questions.
-#
-
-# @test
-# @bug 4772857
-# @summary Unit test for Charset.defaultCharset
-#
-# @build Default
-# @run shell default.sh
-#
-
-# Command-line usage: sh default.sh [/path/to/build]
-
-if [ -z "$TESTJAVA" ]; then
-  if [ $# -lt 1 ]; then exit 1; fi
-  TESTJAVA=$1; shift
-  TESTSRC=`pwd`
-  TESTCLASSES=`pwd`
-fi
-
-s="`uname -s`"
-if [ "$s" != Linux -a "$s" != SunOS ]; then
-  echo "$s: locale command not supported on this system, skipping..."
-  exit 0
-fi
-
-JAVA=$TESTJAVA/bin/java
-
-tolower() {
-  echo "$1" | tr '[A-Z]' '[a-z]'
-}
-
-go() {
-
-  L="$1"
-  shift
-  if [ "x`locale -a | grep \^$L\$`" != "x$L" ]; then
-    echo "$L: Locale not supported, skipping..."
-    return
-  fi
-
-  ecs="$1"; shift
-
-  echo -n "$L: "
-  cs="`LC_ALL=$L $JAVA ${TESTVMOPTS} -cp $TESTCLASSES Default`"
-  if [ $? != 0 ]; then
-    exit 1
-  elif [ "`tolower $cs`" != "`tolower $ecs`" ]; then
-    echo "$cs, expected $ecs -- ERROR"
-    exit 1
-  else
-    echo "$cs, as expected"
-  fi
-
-}
-
-go  en_US       iso-8859-1
-go  ja_JP.utf8  utf-8
-go  tr_TR       iso-8859-9
-go  C           us-ascii
-
-if [ "$s" = Linux ]; then
-  go  ja_JP        x-euc-jp-linux
-  go  ja_JP.eucjp  x-euc-jp-linux
-  go  ja_JP.ujis   x-euc-jp-linux
-  go  ja_JP.utf8   utf-8
-fi
-
-# Solaris
-if [ "$s" = SunOS ]; then
-  go  ja           x-eucjp-open
-  go  ja_JP.eucJP  x-eucjp-open
-  go  ja_JP.PCK    x-PCK
-  go  ja_JP.UTF-8  utf-8
-fi
--- a/jdk/test/java/nio/charset/coders/CheckSJISMappingProp.sh	Mon Jun 19 09:27:17 2017 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,76 +0,0 @@
-#!/bin/sh
-
-#
-# Copyright (c) 2003, 2012, Oracle and/or its affiliates. All rights reserved.
-# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
-#
-# This code is free software; you can redistribute it and/or modify it
-# under the terms of the GNU General Public License version 2 only, as
-# published by the Free Software Foundation.
-#
-# This code is distributed in the hope that it will be useful, but WITHOUT
-# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
-# FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
-# version 2 for more details (a copy is included in the LICENSE file that
-# accompanied this code).
-#
-# You should have received a copy of the GNU General Public License version
-# 2 along with this work; if not, write to the Free Software Foundation,
-# Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
-#
-# Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
-# or visit www.oracle.com if you need additional information or have any
-# questions.
-#
-
-# @test
-# @summary Verify that sun.nio.cs.map property interpreted in ja multibyte locales
-# @bug 4879123
-# @build SJISPropTest
-#
-# @run shell/timeout=300 CheckSJISMappingProp.sh
-
-# set platform-dependent variables
-
-OS=`uname -s`
-case "$OS" in
-  SunOS | Linux | Darwin | AIX ) ;;
-  # Skip locale test for Windows
-  Windows* | CYGWIN* )
-    echo "Passed"; exit 0 ;;
-  * ) echo "Unrecognized system!" ;  exit 1 ;;
-esac
-
-expectPass() {
-  if [ $1 -eq 0 ]
-  then echo "--- passed as expected"
-  else
-    echo "--- failed"
-    exit $1
-  fi
-}
-
-
-JAVA="${TESTJAVA}/bin/java ${TESTVMOPTS} -cp ${TESTCLASSES}"
-runTest() {
-  echo "Testing:" ${1}
-  LC_ALL="$1" ; export LC_ALL
-  locale
-  # Firstly, test with property set
-  # (shift_jis should map to windows-31J charset) 
-  ${JAVA} -Dsun.nio.cs.map="Windows-31J/Shift_JIS" SJISPropTest MS932
-  expectPass $?
-
-  # Next, test without property set - "shift_jis" follows IANA conventions
-  # and should map to the sun.nio.cs.ext.Shift_JIS charset
-  ${JAVA} SJISPropTest Shift_JIS
-  expectPass $?
-}
-
-# Run the test in the common Solaris/Linux/Mac OS locales
-# Tests will simply run in current locale if locale isn't supported
-# on the test machine/platform
-
-for i in "ja" "ja_JP.PCK" "ja_JP.eucJP"  ; do
-  runTest ${i}
-done
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/nio/charset/coders/SJISMappingPropTest.java	Mon Jun 19 09:41:59 2017 +0800
@@ -0,0 +1,91 @@
+/*
+ * Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.
+ * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
+ *
+ * This code is free software; you can redistribute it and/or modify it
+ * under the terms of the GNU General Public License version 2 only, as
+ * published by the Free Software Foundation.
+ *
+ * This code is distributed in the hope that it will be useful, but WITHOUT
+ * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
+ * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * version 2 for more details (a copy is included in the LICENSE file that
+ * accompanied this code).
+ *
+ * You should have received a copy of the GNU General Public License version
+ * 2 along with this work; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
+ *
+ * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
+ * or visit www.oracle.com if you need additional information or have any
+ * questions.
+ */
+
+/*
+ * @test
+ * @bug 4879123
+ * @summary Verify that sun.nio.cs.map property interpreted in ja multibyte locales
+ * @requires (os.family != "windows")
+ * @library /test/lib
+ * @build jdk.test.lib.Utils
+ *        jdk.test.lib.Asserts
+ *        jdk.test.lib.JDKToolFinder
+ *        jdk.test.lib.JDKToolLauncher
+ *        jdk.test.lib.Platform
+ *        jdk.test.lib.process.*
+ *        SJISPropTest
+ * @run testng SJISMappingPropTest
+ */
+
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.Map;
+
+import jdk.test.lib.process.OutputAnalyzer;
+import jdk.test.lib.process.ProcessTools;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import static org.testng.Assert.assertEquals;
+
+public class SJISMappingPropTest {
+
+    @DataProvider
+    public static Iterator<Object[]> locales() {
+        List<Object[]> data = new ArrayList<>();
+        data.add(new String[]{"ja"});
+        data.add(new String[]{"ja_JP.PCK"});
+        data.add(new String[]{"ja_JP.eucJP"});
+        return data.iterator();
+    }
+
+    @Test(dataProvider = "locales")
+    public void testWithProperty(String locale) throws Exception {
+        // with property set, shift_jis should map to windows-31J charset
+        runTest(locale,
+                "-Dsun.nio.cs.map=Windows-31J/Shift_JIS",
+                SJISPropTest.class.getName(),
+                "MS932");
+    }
+
+    @Test(dataProvider = "locales")
+    public void testWithoutProperty(String locale) throws Exception {
+        // without property set - "shift_jis" follows IANA conventions
+        // and should map to the sun.nio.cs.ext.Shift_JIS charset
+        runTest(locale,
+                SJISPropTest.class.getName(),
+                "Shift_JIS");
+    }
+
+    private void runTest(String locale, String... cmd) throws Exception {
+        ProcessBuilder pb = ProcessTools.createJavaProcessBuilder(true, cmd);
+        Map<String, String> env = pb.environment();
+        env.put("LC_ALL", locale);
+        OutputAnalyzer out = ProcessTools.executeProcess(pb)
+                                         .outputTo(System.out)
+                                         .errorTo(System.err);
+        assertEquals(out.getExitValue(), 0);
+    }
+}
--- a/jdk/test/java/nio/charset/coders/SJISPropTest.java	Mon Jun 19 09:27:17 2017 +0800
+++ b/jdk/test/java/nio/charset/coders/SJISPropTest.java	Mon Jun 19 09:41:59 2017 +0800
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2017, Oracle and/or its affiliates. All rights reserved.
  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
  *
  * This code is free software; you can redistribute it and/or modify it
@@ -22,12 +22,9 @@
  */
 
 /*
- *
- *
- * Regression test class run by CheckSJISMappingProp.sh to verify
+ * Regression test class run by SJISMappingPropTest.java to verify
  * that sun.nio.cs.map property is correctly interpreted in
  * multibyte Japanese locales
- *
  */
 
 public class SJISPropTest {