jjg@1248: /* jjg@1248: * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. jjg@1248: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. jjg@1248: * jjg@1248: * This code is free software; you can redistribute it and/or modify it jjg@1248: * under the terms of the GNU General Public License version 2 only, as jjg@1248: * published by the Free Software Foundation. jjg@1248: * jjg@1248: * This code is distributed in the hope that it will be useful, but WITHOUT jjg@1248: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or jjg@1248: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License jjg@1248: * version 2 for more details (a copy is included in the LICENSE file that jjg@1248: * accompanied this code). jjg@1248: * jjg@1248: * You should have received a copy of the GNU General Public License version jjg@1248: * 2 along with this work; if not, write to the Free Software Foundation, jjg@1248: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. jjg@1248: * jjg@1248: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA jjg@1248: * or visit www.oracle.com if you need additional information or have any jjg@1248: * questions. jjg@1248: */ jjg@1248: jjg@1248: /* jjg@1248: * @test ksrini@1395: * @bug 7157626 8001112 jjg@1248: * @summary Test major version for all legal combinations for -source and -target jjg@1248: * @author sgoel jjg@1248: * jjg@1248: */ jjg@1248: jjg@1248: import java.io.*; jjg@1248: import java.nio.*; jjg@1248: import java.util.*; jjg@1248: import java.util.regex.*; jjg@1248: jjg@1248: public class ClassVersionChecker { jjg@1248: jjg@1248: int errors; jjg@1248: String[] jdk = {"","1.2","1.3","1.4","1.5","1.6","1.7","1.8"}; jjg@1248: File javaFile = null; jjg@1248: jjg@1248: public static void main(String[] args) throws Throwable { jjg@1248: new ClassVersionChecker().run(); jjg@1248: } jjg@1248: jjg@1248: void run() throws Exception { jjg@1248: writeTestFile(); jjg@1248: /* Rules applicable for -source and -target combinations jjg@1248: * 1. If both empty, version num is for 1.7 jjg@1248: * 2. If source is not empty and target is empty, version is based on source jjg@1248: * 3. If both non-empty, version is based on target jjg@1248: */ jjg@1248: jjg@1248: /* -source (0=>empty,1=>1.2,...) X -target (0=>empty,1=>1.2,...) jjg@1248: * ver[0][0] => no -source or -target was given jjg@1248: * -1 => invalid combinations jjg@1248: */ jjg@1248: int[][] ver = ksrini@1395: {{52, -1, -1, -1, -1, -1, -1, -1}, ksrini@1395: {48, 46, 47, 48, 49, 50, 51, 52}, ksrini@1395: {48, 46, 47, 48, 49, 50, 51, 52}, ksrini@1395: {48, -1, -1, 48, 49, 50, 51, 52}, ksrini@1395: {52, -1, -1, -1, 49, 50, 51, 52}, ksrini@1395: {52, -1, -1, -1, -1, 50, 51, 52}, ksrini@1395: {52, -1, -1, -1, -1, -1, 51, 52}, ksrini@1395: {52, -1, -1, -1, -1, -1, -1, 52}}; jjg@1248: jjg@1248: // Loop to run all possible combinations of source/target values jjg@1248: for (int i = 0; i< ver.length; i++) { jjg@1248: for (int j = 0 ; j< ver[i].length; j++) { jjg@1248: if(ver[i][j] != -1) { jjg@1248: logMsg("Index values for i = " + i + " j = " + j); jjg@1248: logMsg("Running for src = " + jdk[i] + " target = "+jdk[j] +" expected = " + ver[i][j]); jjg@1248: test(i,j, ver[i][j]); jjg@1248: } jjg@1248: } jjg@1248: } jjg@1248: jjg@1248: if (errors > 0) jjg@1248: throw new Exception(errors + " errors found"); jjg@1248: } jjg@1248: jjg@1248: void test (int i, int j, int expected) { jjg@1248: File classFile = compileTestFile(i, j, javaFile); jjg@1248: short majorVer = getMajorVersion(classFile); jjg@1248: checkVersion(majorVer, expected); jjg@1248: } jjg@1248: jjg@1248: void writeTestFile() throws IOException { jjg@1248: javaFile = new File("Test.java"); jjg@1248: try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(javaFile)));) { jjg@1248: out.println("class Test { "); jjg@1248: out.println(" public void foo() { }"); jjg@1248: out.println("}"); jjg@1248: } catch (IOException ioe) { jjg@1248: error("IOException while creating Test.java" + ioe); jjg@1248: } jjg@1248: } jjg@1248: jjg@1248: File compileTestFile(int i , int j, File f) { jjg@1248: int rc = -1; jjg@1248: // Src and target are empty jjg@1248: if (i == 0 && j == 0 ) { jjg@1248: rc = compile("-g", f.getPath()); jjg@1248: } else if( j == 0 ) { // target is empty jjg@1248: rc = compile("-source", jdk[i], "-g", f.getPath()); jjg@1248: } else { jjg@1248: rc = compile("-source", jdk[i], "-target", jdk[j], "-g", f.getPath()); jjg@1248: } jjg@1248: if (rc != 0) jjg@1248: throw new Error("compilation failed. rc=" + rc); jjg@1248: String path = f.getPath(); jjg@1248: return new File(path.substring(0, path.length() - 5) + ".class"); jjg@1248: } jjg@1248: jjg@1248: int compile(String... args) { jjg@1248: return com.sun.tools.javac.Main.compile(args); jjg@1248: } jjg@1248: jjg@1248: void logMsg (String str) { jjg@1248: System.out.println(str); jjg@1248: } jjg@1248: jjg@1248: short getMajorVersion(File f) { jjg@1248: List args = new ArrayList(); jjg@1248: short majorVer = 0; jjg@1248: try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));) { jjg@1248: in.readInt(); jjg@1248: in.readShort(); jjg@1248: majorVer = in.readShort(); jjg@1248: System.out.println("major version:" + majorVer); jjg@1248: } catch (IOException e) { jjg@1248: error("IOException while reading Test.class" + e); jjg@1248: } jjg@1248: return majorVer; jjg@1248: } jjg@1248: jjg@1248: void checkVersion(short majorVer, int expected) { jjg@1248: if (majorVer != expected ) { jjg@1248: error("versions did not match, Expected: " + expected + "Got: " + majorVer); jjg@1248: } jjg@1248: } jjg@1248: jjg@1248: void error(String msg) { jjg@1248: System.out.println("error: " + msg); jjg@1248: errors++; jjg@1248: } jjg@1248: }