Versions.java revision 2599:50b448c5be54
1/*
2 * Copyright (c) 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 4981566 5028634 5094412 6304984 7025786 7025789 8001112 8028545 8000961 8030610
27 * @summary Check interpretation of -target and -source options
28 * @run main Versions
29 */
30
31import java.io.*;
32import java.nio.*;
33import java.nio.channels.*;
34
35import javax.tools.JavaCompiler;
36import javax.tools.ToolProvider;
37import javax.tools.JavaFileObject;
38import javax.tools.StandardJavaFileManager;
39import java.util.List;
40import java.util.ArrayList;
41import java.util.Arrays;
42
43
44public class Versions {
45
46    protected JavaCompiler javacompiler;
47    protected int failedCases;
48
49    public Versions() throws IOException {
50        javacompiler = ToolProvider.getSystemJavaCompiler();
51        genSourceFiles();
52        failedCases = 0;
53    }
54
55    public static void main(String... args) throws IOException {
56        Versions versions = new Versions();
57        versions.run();
58    }
59
60    void run() {
61
62        String jdk9cv = "52.0";  // class version.change when ./dev pushed to 53
63
64        String TC = "";
65        System.out.println("Version.java: Starting");
66
67        check("52.0");
68        check("52.0", "-source 1.6");
69        check("52.0", "-source 1.7");
70        check("52.0", "-source 1.8");
71        check(jdk9cv, "-source 1.9");
72
73        check_source_target("50.0", "6", "6");
74        check_source_target("51.0", "6", "7");
75        check_source_target("51.0", "7", "7");
76        check_source_target("52.0", "6", "8");
77        check_source_target("52.0", "7", "8");
78        check_source_target("52.0", "8", "8");
79        check_source_target(jdk9cv, "6", "9");
80        check_source_target(jdk9cv, "7", "9");
81        check_source_target(jdk9cv, "8", "9");
82        check_source_target(jdk9cv, "9", "9");
83
84        checksrc16("-source 1.6");
85        checksrc16("-source 6");
86        checksrc16("-source 1.6", "-target 1.6");
87        checksrc16("-source 6", "-target 6");
88        checksrc17("-source 1.7");
89        checksrc17("-source 7");
90        checksrc17("-source 1.7", "-target 1.7");
91        checksrc17("-source 7", "-target 7");
92        checksrc18("-source 1.8");
93        checksrc18("-source 8");
94        checksrc18("-source 1.8", "-target 1.8");
95        checksrc18("-source 8", "-target 8");
96        checksrc19();
97        checksrc19("-source 1.9");
98        checksrc19("-source 9");
99        checksrc19("-source 1.9", "-target 1.9");
100        checksrc19("-source 9", "-target 9");
101        checksrc19("-target 1.9");
102        checksrc19("-target 9");
103
104        fail("-source 7", "-target 1.6", "X.java");
105        fail("-source 8", "-target 1.6", "X.java");
106        fail("-source 8", "-target 1.7", "X.java");
107        fail("-source 9", "-target 1.7", "X.java");
108        fail("-source 9", "-target 1.8", "X.java");
109
110        if (failedCases > 0) {
111            System.err.println("failedCases = " + String.valueOf(failedCases));
112            throw new Error("Test failed");
113        }
114
115    }
116
117
118
119    protected void printargs(String fname,String... args) {
120        System.out.printf("test: %s", fname);
121        for (String onearg : args) {
122            System.out.printf(" %s", onearg);
123        }
124        System.out.printf("\n", fname);
125    }
126
127    protected void check_source_target(String... args) {
128        printargs("check_source_target", args);
129        check_target(args[0], args[1], args[2]);
130        check_target(args[0], "1." + args[1], args[2]);
131    }
132
133    protected void check_target(String... args) {
134        check(args[0], "-source " + args[1], "-target " + args[2]);
135        check(args[0], "-source " + args[1], "-target 1." + args[2]);
136    }
137
138    protected void check(String major, String... args) {
139        printargs("check", args);
140        List<String> jcargs = new ArrayList<String>();
141        jcargs.add("-Xlint:-options");
142
143        // add in args conforming to List requrements of JavaCompiler
144        for (String onearg : args) {
145            String[] fields = onearg.split(" ");
146            for (String onefield : fields) {
147                jcargs.add(onefield);
148            }
149        }
150
151        boolean creturn = compile("X.java", jcargs);
152        if (!creturn) {
153            // compilation errors note and return.. assume no class file
154            System.err.println("check: Compilation Failed");
155            System.err.println("\t classVersion:\t" + major);
156            System.err.println("\t arguments:\t" + jcargs);
157            failedCases++;
158
159        } else if (!checkClassFileVersion("X.class", major)) {
160            failedCases++;
161        }
162    }
163
164    protected void checksrc16(String... args) {
165        printargs("checksrc16", args);
166        int asize = args.length;
167        String[] newargs = new String[asize + 1];
168        System.arraycopy(args, 0, newargs, 0, asize);
169        newargs[asize] = "X.java";
170        pass(newargs);
171        newargs[asize] = "Y.java";
172        fail(newargs);
173    }
174
175    protected void checksrc17(String... args) {
176        printargs("checksrc17", args);
177        int asize = args.length;
178        String[] newargs = new String[asize+1];
179        System.arraycopy(args, 0, newargs,0 , asize);
180        newargs[asize] = "X.java";
181        pass(newargs);
182        newargs[asize] = "Y.java";
183        pass(newargs);
184    }
185
186    protected void checksrc18(String... args) {
187        printargs("checksrc18", args);
188        checksrc17(args);
189    }
190
191    protected void checksrc19(String... args) {
192        printargs("checksrc19", args);
193        checksrc17(args);
194    }
195
196    protected void pass(String... args) {
197        printargs("pass", args);
198
199        List<String> jcargs = new ArrayList<String>();
200        jcargs.add("-Xlint:-options");
201
202        // add in args conforming to List requrements of JavaCompiler
203        for (String onearg : args) {
204            String[] fields = onearg.split(" ");
205            for (String onefield : fields) {
206                jcargs.add(onefield);
207            }
208        }
209
210        // empty list is error
211        if (jcargs.isEmpty()) {
212            System.err.println("error: test error in pass() - No arguments");
213            System.err.println("\t arguments:\t" + jcargs);
214            failedCases++;
215            return;
216        }
217
218        // the last argument is the filename *.java
219        String filename = jcargs.get(jcargs.size() - 1);
220        jcargs.remove(jcargs.size() - 1);
221
222        boolean creturn = compile(filename, jcargs);
223        // expect a compilation failure, failure if otherwise
224        if (!creturn) {
225            System.err.println("pass: Compilation erroneously failed");
226            System.err.println("\t arguments:\t" + jcargs);
227            System.err.println("\t file     :\t" + filename);
228            failedCases++;
229
230        }
231
232    }
233
234    protected void fail(String... args) {
235        printargs("fail", args);
236
237        List<String> jcargs = new ArrayList<String>();
238        jcargs.add("-Xlint:-options");
239
240        // add in args conforming to List requrements of JavaCompiler
241        for (String onearg : args) {
242            String[] fields = onearg.split(" ");
243            for (String onefield : fields) {
244                jcargs.add(onefield);
245            }
246        }
247
248        // empty list is error
249        if (jcargs.isEmpty()) {
250            System.err.println("error: test error in fail()- No arguments");
251            System.err.println("\t arguments:\t" + jcargs);
252            failedCases++;
253            return;
254        }
255
256        // the last argument is the filename *.java
257        String filename = jcargs.get(jcargs.size() - 1);
258        jcargs.remove(jcargs.size() - 1);
259
260        boolean creturn = compile(filename, jcargs);
261        // expect a compilation failure, failure if otherwise
262        if (creturn) {
263            System.err.println("fail: Compilation erroneously succeeded");
264            System.err.println("\t arguments:\t" + jcargs);
265            System.err.println("\t file     :\t" + filename);
266            failedCases++;
267        }
268    }
269
270    protected boolean compile(String sourceFile, List<String>options) {
271        JavaCompiler.CompilationTask jctask;
272        StandardJavaFileManager fm = javacompiler.getStandardFileManager(null, null, null);
273        Iterable<? extends JavaFileObject> files = fm.getJavaFileObjects(sourceFile);
274
275        jctask = javacompiler.getTask(
276            null,    // Writer
277            fm,      // JavaFileManager
278            null,    // DiagnosticListener
279            options, // Iterable<String>
280            null,    // Iterable<String> classes
281            files);  // Iterable<? extends JavaFileObject>
282
283        try {
284            return jctask.call();
285        } catch (IllegalStateException e) {
286            System.err.println(e);
287            return false;
288        }
289    }
290
291
292    protected void genSourceFiles() throws IOException{
293        /* Create a file that executes with all supported versions. */
294        File fsource = new File("X.java");
295        try (Writer fw = new FileWriter(fsource)) {
296            fw.write("public class X { }\n");
297            fw.flush();
298        }
299
300        /* Create a file with feature not supported in deprecated version.
301         * New feature for 1.7, does not exist in 1.6.
302         */
303        fsource = new File("Y.java");
304        try (Writer fw = new FileWriter(fsource)) {
305            fw.write("import java.util.List;\n");
306            fw.write("import java.util.ArrayList;\n");
307            fw.write("class Z { List<String> s = new ArrayList<>(); }\n");
308            fw.flush();
309        }
310    }
311
312    protected boolean checkClassFileVersion
313        (String filename,String classVersionNumber) {
314        ByteBuffer bb = ByteBuffer.allocate(1024);
315        try (FileChannel fc = new FileInputStream(filename).getChannel()) {
316            bb.clear();
317            if (fc.read(bb) < 0)
318                throw new IOException("Could not read from file : " + filename);
319            bb.flip();
320            int minor = bb.getShort(4);
321            int major = bb.getShort(6);
322            String fileVersion = major + "." + minor;
323            if (fileVersion.equals(classVersionNumber)) {
324                return true;
325            } else {
326                System.err.println("checkClassFileVersion : Failed");
327                System.err.println("\tclassfile version mismatch");
328                System.err.println("\texpected : " + classVersionNumber);
329                System.err.println("\tfound    : " + fileVersion);
330                return false;
331            }
332        }
333        catch (IOException e) {
334            System.err.println("checkClassFileVersion : Failed");
335            System.err.println("\terror :\t" + e.getMessage());
336            System.err.println("\tfile:\tfilename");
337        }
338        return false;
339    }
340}
341
342