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