8030610: replace test/tools/javac/versions/check.sh
authorntoda
Thu, 10 Jul 2014 16:03:45 -0700
changeset 25448 40c5263b551f
parent 25447 90222e25bb57
child 25449 3c14a2e16bd6
8030610: replace test/tools/javac/versions/check.sh Reviewed-by: jjg
langtools/test/tools/javac/versions/CheckClassFileVersion.java
langtools/test/tools/javac/versions/Versions.java
langtools/test/tools/javac/versions/check.sh
--- a/langtools/test/tools/javac/versions/CheckClassFileVersion.java	Thu Jul 10 14:06:04 2014 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,60 +0,0 @@
-/*
- * Copyright (c) 2004, 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.*;
-import java.nio.channels.*;
-
-
-public class CheckClassFileVersion {
-
-    static String get(String fn) throws IOException {
-        ByteBuffer bb = ByteBuffer.allocate(1024);
-        FileChannel fc = new FileInputStream(fn).getChannel();
-        try {
-            bb.clear();
-            if (fc.read(bb) < 0)
-                throw new IOException("Could not read any bytes");
-            bb.flip();
-            int minor = bb.getShort(4);
-            int major = bb.getShort(6);
-            return major + "." + minor;
-        } finally {
-            fc.close();
-        }
-    }
-
-    public static void main(String[] args) throws IOException {
-        String cfv = get(args[0]);
-        if (args.length == 1) {
-            System.out.println(cfv);
-            return;
-        }
-        if (!cfv.equals(args[1])) {
-            System.err.println("Class-file version mismatch: Expected "
-                               + args[1] + ", got " + cfv);
-            System.exit(1);
-        }
-    }
-
-}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/langtools/test/tools/javac/versions/Versions.java	Thu Jul 10 16:03:45 2014 -0700
@@ -0,0 +1,335 @@
+/*
+ * Copyright (c) 2014, 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 4981566 5028634 5094412 6304984 7025786 7025789 8001112 8028545 8000961 8030610
+ * @summary Check interpretation of -target and -source options
+ * @run main Versions
+ */
+
+import java.io.*;
+import java.nio.*;
+import java.nio.channels.*;
+
+import javax.tools.JavaCompiler;
+import javax.tools.ToolProvider;
+import javax.tools.JavaFileObject;
+import javax.tools.StandardJavaFileManager;
+import java.util.List;
+import java.util.ArrayList;
+import java.util.Arrays;
+
+
+public class Versions {
+
+    protected JavaCompiler javacompiler;
+    protected int failedCases;
+
+    public Versions() throws IOException {
+        javacompiler = ToolProvider.getSystemJavaCompiler();
+        genSourceFiles();
+        failedCases = 0;
+    }
+
+    public static void main(String... args) throws IOException {
+        Versions versions = new Versions();
+        versions.run();
+    }
+
+    void run() {
+
+        String jdk9cv = "52.0";  // class version.change when ./dev pushed to 53
+
+        String TC = "";
+        System.out.println("Version.java: Starting");
+
+        check("52.0");
+        check("52.0", "-source 1.6");
+        check("52.0", "-source 1.7");
+        check("52.0", "-source 1.8");
+        check(jdk9cv, "-source 1.9");
+
+        check_source_target("50.0", "6", "6");
+        check_source_target("51.0", "6", "7");
+        check_source_target("51.0", "7", "7");
+        check_source_target("52.0", "6", "8");
+        check_source_target("52.0", "7", "8");
+        check_source_target("52.0", "8", "8");
+        check_source_target(jdk9cv, "6", "9");
+        check_source_target(jdk9cv, "7", "9");
+        check_source_target(jdk9cv, "8", "9");
+        check_source_target(jdk9cv, "9", "9");
+
+        checksrc16("-source 1.6");
+        checksrc16("-source 6");
+        checksrc16("-source 1.6", "-target 1.6");
+        checksrc16("-source 6", "-target 6");
+        checksrc17("-source 1.7");
+        checksrc17("-source 7");
+        checksrc17("-source 1.7", "-target 1.7");
+        checksrc17("-source 7", "-target 7");
+        checksrc18("-source 1.8");
+        checksrc18("-source 8");
+        checksrc18("-source 1.8", "-target 1.8");
+        checksrc18("-source 8", "-target 8");
+        checksrc19();
+        checksrc19("-source 1.9");
+        checksrc19("-source 9");
+        checksrc19("-source 1.9", "-target 1.9");
+        checksrc19("-source 9", "-target 9");
+        checksrc19("-target 1.9");
+        checksrc19("-target 9");
+
+        fail("-source 7", "-target 1.6", "X.java");
+        fail("-source 8", "-target 1.6", "X.java");
+        fail("-source 8", "-target 1.7", "X.java");
+        fail("-source 9", "-target 1.7", "X.java");
+        fail("-source 9", "-target 1.8", "X.java");
+
+        if (failedCases > 0) {
+            System.err.println("failedCases = " + String.valueOf(failedCases));
+            throw new Error("Test failed");
+        }
+
+    }
+
+
+
+    protected void printargs(String fname,String... args) {
+        System.out.printf("test: %s", fname);
+        for (String onearg : args) {
+            System.out.printf(" %s", onearg);
+        }
+        System.out.printf("\n", fname);
+    }
+
+    protected void check_source_target(String... args) {
+        printargs("check_source_target", args);
+        check_target(args[0], args[1], args[2]);
+        check_target(args[0], "1." + args[1], args[2]);
+    }
+
+    protected void check_target(String... args) {
+        check(args[0], "-source " + args[1], "-target " + args[2]);
+        check(args[0], "-source " + args[1], "-target 1." + args[2]);
+    }
+
+    protected void check(String major, String... args) {
+        printargs("check", args);
+        List<String> jcargs = new ArrayList<String>();
+        jcargs.add("-Xlint:-options");
+
+        // add in args conforming to List requrements of JavaCompiler
+        for (String onearg : args) {
+            String[] fields = onearg.split(" ");
+            for (String onefield : fields) {
+                jcargs.add(onefield);
+            }
+        }
+
+        boolean creturn = compile("X.java", jcargs);
+        if (!creturn) {
+            // compilation errors note and return.. assume no class file
+            System.err.println("check: Compilation Failed");
+            System.err.println("\t classVersion:\t" + major);
+            System.err.println("\t arguments:\t" + jcargs);
+            failedCases++;
+
+        } else if (!checkClassFileVersion("X.class", major)) {
+            failedCases++;
+        }
+    }
+
+    protected void checksrc16(String... args) {
+        printargs("checksrc16", args);
+        int asize = args.length;
+        String[] newargs = new String[asize + 1];
+        System.arraycopy(args, 0, newargs, 0, asize);
+        newargs[asize] = "X.java";
+        pass(newargs);
+        newargs[asize] = "Y.java";
+        fail(newargs);
+    }
+
+    protected void checksrc17(String... args) {
+        printargs("checksrc17", args);
+        int asize = args.length;
+        String[] newargs = new String[asize+1];
+        System.arraycopy(args, 0, newargs,0 , asize);
+        newargs[asize] = "X.java";
+        pass(newargs);
+        newargs[asize] = "Y.java";
+        pass(newargs);
+    }
+
+    protected void checksrc18(String... args) {
+        printargs("checksrc18", args);
+        checksrc17(args);
+    }
+
+    protected void checksrc19(String... args) {
+        printargs("checksrc19", args);
+        checksrc17(args);
+    }
+
+    protected void pass(String... args) {
+        printargs("pass", args);
+
+        List<String> jcargs = new ArrayList<String>();
+        jcargs.add("-Xlint:-options");
+
+        // add in args conforming to List requrements of JavaCompiler
+        for (String onearg : args) {
+            String[] fields = onearg.split(" ");
+            for (String onefield : fields) {
+                jcargs.add(onefield);
+            }
+        }
+
+        // empty list is error
+        if (jcargs.isEmpty()) {
+            System.err.println("error: test error in pass() - No arguments");
+            System.err.println("\t arguments:\t" + jcargs);
+            failedCases++;
+            return;
+        }
+
+        // the last argument is the filename *.java
+        String filename = jcargs.get(jcargs.size() - 1);
+        jcargs.remove(jcargs.size() - 1);
+
+        boolean creturn = compile(filename, jcargs);
+        // expect a compilation failure, failure if otherwise
+        if (!creturn) {
+            System.err.println("pass: Compilation erroneously failed");
+            System.err.println("\t arguments:\t" + jcargs);
+            System.err.println("\t file     :\t" + filename);
+            failedCases++;
+
+        }
+
+    }
+
+    protected void fail(String... args) {
+        printargs("fail", args);
+
+        List<String> jcargs = new ArrayList<String>();
+        jcargs.add("-Xlint:-options");
+
+        // add in args conforming to List requrements of JavaCompiler
+        for (String onearg : args) {
+            String[] fields = onearg.split(" ");
+            for (String onefield : fields) {
+                jcargs.add(onefield);
+            }
+        }
+
+        // empty list is error
+        if (jcargs.isEmpty()) {
+            System.err.println("error: test error in fail()- No arguments");
+            System.err.println("\t arguments:\t" + jcargs);
+            failedCases++;
+            return;
+        }
+
+        // the last argument is the filename *.java
+        String filename = jcargs.get(jcargs.size() - 1);
+        jcargs.remove(jcargs.size() - 1);
+
+        boolean creturn = compile(filename, jcargs);
+        // expect a compilation failure, failure if otherwise
+        if (creturn) {
+            System.err.println("fail: Compilation erroneously succeeded");
+            System.err.println("\t arguments:\t" + jcargs);
+            System.err.println("\t file     :\t" + filename);
+            failedCases++;
+        }
+    }
+
+    protected boolean compile(String sourceFile, List<String>options) {
+        JavaCompiler.CompilationTask jctask;
+        StandardJavaFileManager fm = javacompiler.getStandardFileManager(null, null, null);
+        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(sourceFile);
+
+        jctask = javacompiler.getTask(
+            null,    // Writer
+            fm,      // JavaFileManager
+            null,    // DiagnosticListener
+            options, // Iterable<String>
+            null,    // Iterable<String> classes
+            files);  // Iterable<? extends JavaFileObject>
+        return jctask.call();
+    }
+
+
+    protected void genSourceFiles() throws IOException{
+        /* Create a file that executes with all supported versions. */
+        File fsource = new File("X.java");
+        try (Writer fw = new FileWriter(fsource)) {
+            fw.write("public class X { }\n");
+            fw.flush();
+        }
+
+        /* Create a file with feature not supported in deprecated version.
+         * New feature for 1.7, does not exist in 1.6.
+         */
+        fsource = new File("Y.java");
+        try (Writer fw = new FileWriter(fsource)) {
+            fw.write("import java.util.List;\n");
+            fw.write("import java.util.ArrayList;\n");
+            fw.write("class Z { List<String> s = new ArrayList<>(); }\n");
+            fw.flush();
+        }
+    }
+
+    protected boolean checkClassFileVersion
+        (String filename,String classVersionNumber) {
+        ByteBuffer bb = ByteBuffer.allocate(1024);
+        try (FileChannel fc = new FileInputStream(filename).getChannel()) {
+            bb.clear();
+            if (fc.read(bb) < 0)
+                throw new IOException("Could not read from file : " + filename);
+            bb.flip();
+            int minor = bb.getShort(4);
+            int major = bb.getShort(6);
+            String fileVersion = major + "." + minor;
+            if (fileVersion.equals(classVersionNumber)) {
+                return true;
+            } else {
+                System.err.println("checkClassFileVersion : Failed");
+                System.err.println("\tclassfile version mismatch");
+                System.err.println("\texpected : " + classVersionNumber);
+                System.err.println("\tfound    : " + fileVersion);
+                return false;
+            }
+        }
+        catch (IOException e) {
+            System.err.println("checkClassFileVersion : Failed");
+            System.err.println("\terror :\t" + e.getMessage());
+            System.err.println("\tfile:\tfilename");
+        }
+        return false;
+    }
+}
+
--- a/langtools/test/tools/javac/versions/check.sh	Thu Jul 10 14:06:04 2014 -0700
+++ /dev/null	Thu Jan 01 00:00:00 1970 +0000
@@ -1,138 +0,0 @@
-#
-# Copyright (c) 2004, 2014, 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 4981566 5028634 5094412 6304984 7025786 7025789 8001112 8028545 8000961
-# @summary Check interpretation of -target and -source options
-# @build CheckClassFileVersion
-# @run shell check.sh
-
-TESTJAVA=${TESTJAVA:?}
-TC=${TESTCLASSES-.}
-
-J="$TESTJAVA/bin/java"
-JC="$TESTJAVA/bin/javac"
-CFV="${TESTVMOPTS} -cp $TC CheckClassFileVersion"
-
-rm -f $TC/X.java $TC/X.java
-echo 'public class X { }' > $TC/X.java
-echo 'public enum Y { }' > $TC/Y.java
-
-
-# Check class-file versions
-
-check() {
-  V=$1; shift
-  echo "+ javac $* [$V]"
-  "$JC" ${TESTTOOLVMOPTS} -Xlint:-options -d $TC $* $TC/X.java && "$J" $CFV $TC/X.class $V || exit 2
-}
-
-# check for all combinations of target values
-check_target() {
-  check $1 -source $2 -target $3
-  check $1 -source $2 -target 1.${3}
-}
-# check for all combinations of source and target values
-check_source_target() {
-  check_target $1 $2     $3
-  check_target $1 1.${2} $3
-}
-
-
-check_source_target 50.0 6   6
-
-check_source_target 51.0 6   7
-check_source_target 51.0 7   7
-
-check_source_target 52.0 6   8
-check_source_target 52.0 7   8
-check_source_target 52.0 8   8
-
-check_source_target 52.0 8   9
-check_source_target 52.0 9   9
-
-# and finally the default with no options
-check 52.0
-
-# Check source versions
-
-fail() {
-  echo "+ javac $*"
-  if "$JC" ${TESTTOOLVMOPTS} -Xlint:-options -d $TC $*; then
-    echo "-- did not fail as expected"
-    exit 3
-  else
-    echo "-- failed as expected"
-  fi
-}
-
-pass() {
-  echo "+ javac $*"
-  if "$JC" ${TESTTOOLVMOPTS} -Xlint:options -d $TC $*; then
-    echo "-- passed"
-  else
-    echo "-- failed"
-    exit 4
-  fi
-}
-
-# the following need to be updated when -source 7 features are available
-checksrc14() { pass $* $TC/X.java; fail $* $TC/Y.java; }
-checksrc15() { pass $* $TC/X.java; pass $* $TC/Y.java; }
-checksrc16() { checksrc15 $* ; }
-checksrc17() { checksrc15 $* ; }
-checksrc18() { checksrc15 $* ; }
-checksrc19() { checksrc15 $* ; }
-
-
-
-checksrc16 -source 1.6
-checksrc16 -source 6
-checksrc16 -source 1.6 -target 1.6
-checksrc16 -source 6 -target 6
-
-checksrc17 -source 1.7
-checksrc17 -source 7
-checksrc17 -source 1.7 -target 1.7
-checksrc17 -source 7 -target 7
-
-checksrc18 -source 1.8
-checksrc18 -source 8
-checksrc18 -source 1.8 -target 1.8
-checksrc18 -source 8 -target 8
-
-checksrc19
-checksrc19 -source 1.9
-checksrc19 -source 9
-checksrc19 -source 1.9 -target 1.9
-checksrc19 -source 9 -target 9
-checksrc19 -target 1.9
-checksrc19 -target 9
-
-fail -source 1.6 -target 1.5 $TC/X.java
-fail -source 6   -target 1.5 $TC/X.java
-fail -source 7   -target 1.6 $TC/X.java
-fail -source 8   -target 1.6 $TC/X.java
-fail -source 8   -target 1.7 $TC/X.java
-fail -source 9   -target 1.7 $TC/X.java
-fail -source 9   -target 1.8 $TC/X.java