1 /*
   2  * Copyright (c) 2012, 2014, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */
  23 
  24 /*
  25  * @test
  26  * @bug 7157626 8001112
  27  * @summary Test major version for all legal combinations for -source and -target
  28  * @author sgoel
  29  *
  30  */
  31 
  32 import java.io.*;
  33 import java.nio.*;
  34 import java.util.*;
  35 import java.util.regex.*;
  36 
  37 public class ClassVersionChecker {
  38 
  39     int errors;
  40     String[] jdk = {"","1.6","1.7","1.8"};
  41     File javaFile = null;
  42 
  43     public static void main(String[] args) throws Throwable {
  44         new ClassVersionChecker().run();
  45     }
  46 
  47     void run() throws Exception {
  48         writeTestFile();
  49         /* Rules applicable for -source and -target combinations
  50          * 1. If both empty, version num is for the current release
  51          * 2. If source is not empty and target is empty, version is based on source
  52          * 3. If both non-empty, version is based on target
  53          */
  54 
  55         /* -source (0=>empty,1=>1.2,...) X -target (0=>empty,1=>1.2,...)
  56          * ver[0][0] => no -source or -target was given
  57          * -1 => invalid combinations
  58          */
  59         int[][] ver =
  60                 {{52, -1, -1, -1},
  61                  {52, 50, 51, 52},
  62                  {52, -1, 51, 52},
  63                  {52, -1, -1, 52}};
  64 
  65         // Loop to run all possible combinations of source/target values
  66         for (int i = 0; i< ver.length; i++) {
  67             for (int j = 0 ; j< ver[i].length; j++) {
  68                 if(ver[i][j] != -1) {
  69                     logMsg("Index values for i = " + i + " j = " + j);
  70                     logMsg("Running for src = " + jdk[i] + " target = "+jdk[j] +" expected = " + ver[i][j]);
  71                     test(i,j, ver[i][j]);
  72                 }
  73             }
  74         }
  75 
  76         if (errors > 0)
  77             throw new Exception(errors + " errors found");
  78     }
  79 
  80     void test (int i, int j, int expected) {
  81         File classFile = compileTestFile(i, j, javaFile);
  82         short majorVer = getMajorVersion(classFile);
  83         checkVersion(majorVer, expected);
  84     }
  85 
  86     void writeTestFile() throws IOException {
  87         javaFile = new File("Test.java");
  88         try(PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(javaFile)));) {
  89             out.println("class Test { ");
  90             out.println("  public void foo() { }");
  91             out.println("}");
  92         } catch (IOException ioe) {
  93             error("IOException while creating Test.java" + ioe);
  94         }
  95     }
  96 
  97     File compileTestFile(int i , int j, File f) {
  98         int rc = -1;
  99         // Src and target are empty
 100         if (i == 0 && j == 0 ) {
 101             rc = compile("-g", f.getPath());
 102         } else if( j == 0 ) {  // target is empty
 103             rc = compile("-source", jdk[i], "-g", f.getPath());
 104         } else {
 105             rc = compile("-source", jdk[i], "-target", jdk[j], "-g", f.getPath());
 106         }
 107         if (rc != 0)
 108             throw new Error("compilation failed. rc=" + rc);
 109         String path = f.getPath();
 110         return new File(path.substring(0, path.length() - 5) + ".class");
 111     }
 112 
 113     int compile(String... args) {
 114         return com.sun.tools.javac.Main.compile(args);
 115     }
 116 
 117     void logMsg (String str) {
 118         System.out.println(str);
 119     }
 120 
 121     short getMajorVersion(File f) {
 122         List<String> args = new ArrayList<String>();
 123         short majorVer = 0;
 124         try(DataInputStream in = new DataInputStream(new BufferedInputStream(new FileInputStream(f)));) {
 125             in.readInt();
 126             in.readShort();
 127             majorVer = in.readShort();
 128             System.out.println("major version:" +  majorVer);
 129         } catch (IOException e) {
 130             error("IOException while reading Test.class" + e);
 131         }
 132         return majorVer;
 133     }
 134 
 135     void checkVersion(short majorVer, int expected) {
 136         if (majorVer != expected ) {
 137             error("versions did not match, Expected: " + expected + "Got: " + majorVer);
 138         }
 139     }
 140 
 141     void error(String msg) {
 142        System.out.println("error: " + msg);
 143        errors++;
 144     }
 145 }