From c48a9a184a6ce982c4cca19d88e9bb0b06e21ea7 Mon Sep 17 00:00:00 2001 From: Amy Lu Date: Wed, 21 Jun 2017 12:09:25 +0800 Subject: [PATCH] 8181575: Refactor locale related shell test java/nio/charset/spi/basic.sh to java Reviewed-by: psandoz --- .../charset/spi/CharsetProviderBasicTest.java | 122 ++++++++++++++++ .../spi/{Test.java => CharsetTest.java} | 14 +- .../java/nio/charset/spi/FooProvider.java | 10 +- jdk/test/java/nio/charset/spi/SetupJar.java | 52 +++++++ jdk/test/java/nio/charset/spi/basic.sh | 130 ------------------ 5 files changed, 187 insertions(+), 141 deletions(-) create mode 100644 jdk/test/java/nio/charset/spi/CharsetProviderBasicTest.java rename jdk/test/java/nio/charset/spi/{Test.java => CharsetTest.java} (90%) create mode 100644 jdk/test/java/nio/charset/spi/SetupJar.java delete mode 100644 jdk/test/java/nio/charset/spi/basic.sh diff --git a/jdk/test/java/nio/charset/spi/CharsetProviderBasicTest.java b/jdk/test/java/nio/charset/spi/CharsetProviderBasicTest.java new file mode 100644 index 00000000000..f67b14cfb33 --- /dev/null +++ b/jdk/test/java/nio/charset/spi/CharsetProviderBasicTest.java @@ -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 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 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); + } +} diff --git a/jdk/test/java/nio/charset/spi/Test.java b/jdk/test/java/nio/charset/spi/CharsetTest.java similarity index 90% rename from jdk/test/java/nio/charset/spi/Test.java rename to jdk/test/java/nio/charset/spi/CharsetTest.java index 646e041dcbb..4ac3d14b1d3 100644 --- a/jdk/test/java/nio/charset/spi/Test.java +++ b/jdk/test/java/nio/charset/spi/CharsetTest.java @@ -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,12 +21,14 @@ * questions. */ -import java.io.*; -import java.nio.charset.*; -import java.util.*; +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 Test { +public class CharsetTest { private static PrintStream out = System.err; private static final SortedMap available = Charset.availableCharsets(); diff --git a/jdk/test/java/nio/charset/spi/FooProvider.java b/jdk/test/java/nio/charset/spi/FooProvider.java index 16bfb4f0308..31a321764c4 100644 --- a/jdk/test/java/nio/charset/spi/FooProvider.java +++ b/jdk/test/java/nio/charset/spi/FooProvider.java @@ -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 diff --git a/jdk/test/java/nio/charset/spi/SetupJar.java b/jdk/test/java/nio/charset/spi/SetupJar.java new file mode 100644 index 00000000000..73751f87133 --- /dev/null +++ b/jdk/test/java/nio/charset/spi/SetupJar.java @@ -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); + } +} diff --git a/jdk/test/java/nio/charset/spi/basic.sh b/jdk/test/java/nio/charset/spi/basic.sh deleted file mode 100644 index 4e1c6df891b..00000000000 --- a/jdk/test/java/nio/charset/spi/basic.sh +++ /dev/null @@ -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