8181575: Refactor locale related shell test java/nio/charset/spi/basic.sh to java
authoramlu
Wed, 21 Jun 2017 12:09:25 +0800
changeset 45577 ed8a5fd6fe33
parent 45576 b6be034b9e29
child 45578 cd8f28842351
8181575: Refactor locale related shell test java/nio/charset/spi/basic.sh to java Reviewed-by: psandoz
jdk/test/java/nio/charset/spi/CharsetProviderBasicTest.java
jdk/test/java/nio/charset/spi/CharsetTest.java
jdk/test/java/nio/charset/spi/FooProvider.java
jdk/test/java/nio/charset/spi/SetupJar.java
jdk/test/java/nio/charset/spi/Test.java
jdk/test/java/nio/charset/spi/basic.sh
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/nio/charset/spi/CharsetProviderBasicTest.java	Wed Jun 21 12:09:25 2017 +0800
@@ -0,0 +1,122 @@
+/*
+ * 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 4429040 4591027 4814743
+ * @summary Unit test for charset providers
+ * @library /test/lib
+ *          /lib/testlibrary
+ * @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.*
+ *        JarUtils
+ *        FooCharset FooProvider CharsetTest
+ * @run driver SetupJar
+ * @run testng CharsetProviderBasicTest
+ */
+
+import java.io.File;
+import java.util.ArrayList;
+import java.util.Iterator;
+import java.util.List;
+import java.util.stream.Stream;
+
+import jdk.test.lib.JDKToolFinder;
+import jdk.test.lib.Utils;
+import jdk.test.lib.process.ProcessTools;
+
+import org.testng.annotations.DataProvider;
+import org.testng.annotations.Test;
+
+import static java.util.Arrays.asList;
+
+public class CharsetProviderBasicTest {
+
+    private static final String TEST_SRC = System.getProperty("test.src");
+
+    private static final List DEFAULT_CSS = List.of(
+        "US-ASCII", "8859_1", "iso-ir-6", "UTF-16", "windows-1252", "!BAR", "cp1252"
+    );
+
+    private static final List MINIMAL_POLICY = List.of(
+        "-Djava.security.manager",
+        "-Djava.security.policy=" + TEST_SRC + File.separator + "default-pol"
+    );
+
+    private static final List CP_POLICY = List.of(
+        "-Djava.security.manager",
+        "-Djava.security.policy=" + TEST_SRC + File.separator + "charsetProvider.sp"
+    );
+
+    private static boolean checkSupports(String locale) throws Throwable {
+        return ProcessTools.executeProcess("sh", "-c", "LC_ALL=" + locale + " && "
+                                           + "locale -a | grep " + locale)
+                           .getStdout()
+                           .replace(System.lineSeparator(), "")
+                           .equals(locale);
+    }
+
+    @DataProvider
+    public static Iterator<Object[]> testCases() {
+        return Stream.of("", "ja_JP.eucJP", "tr_TR")
+                     .flatMap(locale -> Stream.of(
+                             new Object[]{locale, List.of(""), "FOO"},
+                             new Object[]{locale, MINIMAL_POLICY, "!FOO"},
+                             new Object[]{locale, CP_POLICY, "FOO"}
+                     ))
+                     .iterator();
+    }
+
+    @Test(dataProvider = "testCases")
+    public void testDefaultCharset(String locale, List opts, String css) throws Throwable {
+        if ((System.getProperty("os.name").startsWith("Windows") || !checkSupports(locale))
+                && (!locale.isEmpty())) {
+            System.out.println(locale + ": Locale not supported, skipping...");
+            return;
+        }
+
+        List<String> args = new ArrayList<>();
+        args.add(JDKToolFinder.getJDKTool("java"));
+        args.addAll(asList(Utils.getTestJavaOpts()));
+        args.add("-cp");
+        args.add(System.getProperty("test.class.path") + File.pathSeparator + "test.jar");
+        args.addAll(opts);
+        args.add(CharsetTest.class.getName());
+        args.addAll(DEFAULT_CSS);
+        args.add(css);
+        args.removeIf(t -> t.isEmpty());
+
+        ProcessBuilder pb = new ProcessBuilder(args);
+
+        if (!locale.isEmpty()) {
+            pb.environment().put("LC_ALL", locale);
+        }
+
+        ProcessTools.executeCommand(pb)
+                    .shouldHaveExitValue(0);
+    }
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/nio/charset/spi/CharsetTest.java	Wed Jun 21 12:09:25 2017 +0800
@@ -0,0 +1,94 @@
+/*
+ * 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
+ * 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.
+ */
+
+import java.io.InputStreamReader;
+import java.io.PrintStream;
+import java.nio.charset.Charset;
+import java.nio.charset.UnsupportedCharsetException;
+import java.util.Iterator;
+import java.util.SortedMap;
+
+public class CharsetTest {
+
+    private static PrintStream out = System.err;
+    private static final SortedMap available = Charset.availableCharsets();
+
+    private static void fail(String csn, String msg) {
+        throw new RuntimeException(csn + ": " + msg);
+    }
+
+    private static void testPositive(String csn) {
+        if (!Charset.isSupported(csn))
+            fail(csn, "Not supported");
+
+        Charset cs = Charset.forName(csn);
+        out.println(csn + " --> " + cs.getClass().getName());
+        out.println("  " + cs.name() + " " + cs.aliases());
+
+        if (!available.containsKey(cs.name()))
+            fail(csn, "Not in available charsets: " + available.keySet());
+        if (!((Charset)available.get(cs.name())).equals(cs))
+            fail(csn, "Available charset != looked-up charset");
+
+        if (csn.equalsIgnoreCase("FOO")) {
+            if (!(cs instanceof FooCharset))
+                fail(csn, "instanceof failed");
+        }
+    }
+
+    private static void testNegative(String csn) {
+        if (Charset.isSupported(csn))
+            fail(csn, "Supported");
+        if (available.containsKey(csn))
+            fail(csn, "Available");
+        try {
+            Charset.forName(csn);
+        } catch (UnsupportedCharsetException x) {
+            out.println(csn + " not supported, as expected");
+            return;
+        }
+        fail(csn, "Lookup succeeded");
+    }
+
+    public static void main(String [] args) {
+
+        out.println("Default: "
+                    + new InputStreamReader(System.in).getEncoding());
+
+        out.print("Available:");
+        for (Iterator i = available.keySet().iterator(); i.hasNext();)
+            out.print(" " + (String)i.next());
+        out.println();
+
+        for (int i = 0; i < args.length; i++) {
+            String a = args[i];
+            boolean not = a.startsWith("!");
+            String csn = (not ? a.substring(1) : a);
+            if (not)
+                testNegative(csn);
+            else
+                testPositive(csn);
+        }
+    }
+
+}
--- a/jdk/test/java/nio/charset/spi/FooProvider.java	Wed Jun 21 10:30:03 2017 +0800
+++ b/jdk/test/java/nio/charset/spi/FooProvider.java	Wed Jun 21 12:09:25 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
@@ -21,10 +21,10 @@
  * questions.
  */
 
-import java.util.*;
-import java.nio.charset.*;
-import java.nio.charset.spi.*;
-
+import java.nio.charset.Charset;
+import java.nio.charset.spi.CharsetProvider;
+import java.util.Collections;
+import java.util.Iterator;
 
 public class FooProvider
     extends CharsetProvider
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/jdk/test/java/nio/charset/spi/SetupJar.java	Wed Jun 21 12:09:25 2017 +0800
@@ -0,0 +1,52 @@
+/*
+ * 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.
+ */
+
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+
+import static java.nio.file.StandardCopyOption.REPLACE_EXISTING;
+import static java.nio.file.StandardOpenOption.CREATE;
+
+public class SetupJar {
+
+    private static final String PROVIDER
+            = "META-INF/services/java.nio.charset.spi.CharsetProvider";
+    private static final String TEST_DIR = System.getProperty("test.dir", ".");
+
+    public static void main(String args[]) throws Exception {
+        Path xdir = Files.createDirectories(Paths.get(TEST_DIR, "xdir"));
+        Path provider = xdir.resolve(PROVIDER);
+        Files.createDirectories(provider.getParent());
+        Files.write(provider, "FooProvider".getBytes(), CREATE);
+
+        String[] files = {"FooCharset.class", "FooProvider.class"};
+        for (String f : files) {
+            Path source = Paths.get(System.getProperty("test.classes")).resolve(f);
+            Path target = xdir.resolve(source.getFileName());
+            Files.copy(source, target, REPLACE_EXISTING);
+        }
+
+        JarUtils.createJarFile(Paths.get("test.jar"), xdir);
+    }
+}
--- a/jdk/test/java/nio/charset/spi/Test.java	Wed Jun 21 10:30:03 2017 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,92 +0,0 @@
-/*
- * 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.
- */
-
-import java.io.*;
-import java.nio.charset.*;
-import java.util.*;
-
-
-public class Test {
-
-    private static PrintStream out = System.err;
-    private static final SortedMap available = Charset.availableCharsets();
-
-    private static void fail(String csn, String msg) {
-        throw new RuntimeException(csn + ": " + msg);
-    }
-
-    private static void testPositive(String csn) {
-        if (!Charset.isSupported(csn))
-            fail(csn, "Not supported");
-
-        Charset cs = Charset.forName(csn);
-        out.println(csn + " --> " + cs.getClass().getName());
-        out.println("  " + cs.name() + " " + cs.aliases());
-
-        if (!available.containsKey(cs.name()))
-            fail(csn, "Not in available charsets: " + available.keySet());
-        if (!((Charset)available.get(cs.name())).equals(cs))
-            fail(csn, "Available charset != looked-up charset");
-
-        if (csn.equalsIgnoreCase("FOO")) {
-            if (!(cs instanceof FooCharset))
-                fail(csn, "instanceof failed");
-        }
-    }
-
-    private static void testNegative(String csn) {
-        if (Charset.isSupported(csn))
-            fail(csn, "Supported");
-        if (available.containsKey(csn))
-            fail(csn, "Available");
-        try {
-            Charset.forName(csn);
-        } catch (UnsupportedCharsetException x) {
-            out.println(csn + " not supported, as expected");
-            return;
-        }
-        fail(csn, "Lookup succeeded");
-    }
-
-    public static void main(String [] args) {
-
-        out.println("Default: "
-                    + new InputStreamReader(System.in).getEncoding());
-
-        out.print("Available:");
-        for (Iterator i = available.keySet().iterator(); i.hasNext();)
-            out.print(" " + (String)i.next());
-        out.println();
-
-        for (int i = 0; i < args.length; i++) {
-            String a = args[i];
-            boolean not = a.startsWith("!");
-            String csn = (not ? a.substring(1) : a);
-            if (not)
-                testNegative(csn);
-            else
-                testPositive(csn);
-        }
-    }
-
-}
--- a/jdk/test/java/nio/charset/spi/basic.sh	Wed Jun 21 10:30:03 2017 +0800
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,130 +0,0 @@
-#!/bin/sh
-
-#
-# Copyright (c) 2010, 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
-# @bug 4429040 4591027 4814743
-# @summary Unit test for charset providers
-#
-# @build Test FooCharset FooProvider
-# @run shell basic.sh
-# @run shell basic.sh ja_JP.eucJP
-# @run shell basic.sh tr_TR
-#
-
-# Command-line usage: sh basic.sh /path/to/build [locale]
-
-if [ -z "$TESTJAVA" ]; then
-  if [ $# -lt 1 ]; then exit 1; fi
-  TESTJAVA=$1; shift
-  COMPILEJDK="${TESTJAVA}"
-  TESTSRC=`pwd`
-  TESTCLASSES=`pwd`
-fi
-
-JAVA=$TESTJAVA/bin/java
-JAR=$COMPILEJAVA/bin/jar
-
-DIR=`pwd`
-case `uname` in
-  SunOS | Linux | Darwin | AIX ) CPS=':' ;;
-  Windows* )      CPS=';' ;;
-  CYGWIN*  )
-    DIR=`/usr/bin/cygpath -a -s -m $DIR`
-    CPS=";";;
-  *)              echo "Unknown platform: `uname`"; exit 1 ;;
-esac
-
-JARD=$DIR/x.jar
-APPD=$DIR/x.ext
-TESTD=$DIR/x.test
-
-CSS='US-ASCII 8859_1 iso-ir-6 UTF-16 windows-1252 !BAR cp1252'
-
-
-if [ \! -d $APPD ]; then
-    # Initialize
-    echo Initializing...
-    rm -rf $JARD $APPD $TESTD
-    mkdir -p $JARD/META-INF/services x.ext
-    echo FooProvider \
-      >$JARD/META-INF/services/java.nio.charset.spi.CharsetProvider
-    cp $TESTCLASSES/FooProvider.class $TESTCLASSES/FooCharset.class $JARD
-    mkdir $TESTD
-    cp $TESTCLASSES/Test.class $TESTD
-    (cd $JARD; $JAR ${TESTTOOLVMOPTS} -cf $APPD/test.jar *)
-fi
-
-if [ $# -gt 0 ]; then
-    # Use locale specified on command line, if it's supported
-    L="$1"
-    shift
-    s=`uname -s`
-    if [ $s != Linux -a $s != SunOS -a $s != Darwin -a $s != AIX ]; then
-      echo "$L: Locales not supported on this system, skipping..."
-      exit 0
-    fi
-    if [ "x`locale -a | grep $L`" != "x$L" ]; then
-      echo "$L: Locale not supported, skipping..."
-      exit 0
-    fi
-    LC_ALL=$L; export LC_ALL
-fi
-
-TMP=${TMP:-$TEMP}; TMP=${TMP:-/tmp}
-cd $TMP
-
-failures=0
-for where in app; do
-  for security in none minimal-policy cp-policy; do
-    echo '';
-    echo "LC_ALL=$LC_ALL where=$where security=$security"
-    av=''
-    if [ $where = app ]; then
-      av="$av -cp $TESTD$CPS$APPD/test.jar";
-    fi
-    case $security in
-      none)          css="$CSS FOO";;
-      # Minimal policy in this case is more or less carbon copy of jre default
-      # security policy and doesn't give explicit runtime permission
-      # for user provided runtime loadable charsets
-      minimal-policy)  css="$CSS !FOO";
-		     av="$av -Djava.security.manager -Djava.security.policy==$TESTSRC/default-pol";;
-      cp-policy)     css="$CSS FOO";
-		     av="$av -Djava.security.manager
-		         -Djava.security.policy=$TESTSRC/charsetProvider.sp";;
-    esac
-    if (set -x; $JAVA ${TESTVMOPTS} $av Test $css) 2>&1; then
-      continue;
-    else
-      failures=`expr $failures + 1`
-    fi
-  done
-done
-
-echo ''
-if [ $failures -gt 0 ];
-  then echo "$failures cases failed";
-  else echo "All cases passed"; fi
-exit $failures