ReleaseOptionClashes.java revision 3779:7d2f8aa366e2
1/*
2 * Copyright (c) 2015, 2016, 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 8072480
27 * @summary Verify option clash between --release and -source is reported correctly.
28 * @modules jdk.compiler/com.sun.tools.javac.util
29 */
30
31import java.io.ByteArrayOutputStream;
32import java.io.File;
33import java.lang.reflect.Field;
34import java.util.ArrayList;
35import java.util.Arrays;
36import java.util.List;
37
38import javax.tools.Tool;
39import javax.tools.ToolProvider;
40
41public class ReleaseOptionClashes {
42    public static void main(String... args) throws Exception {
43        new ReleaseOptionClashes().run();
44    }
45
46    void run() throws Exception {
47        doRunTest("-bootclasspath", "any");
48        doRunTest("-Xbootclasspath:any");
49        doRunTest("-Xbootclasspath/a:any");
50        doRunTest("-Xbootclasspath/p:any");
51        doRunTest("-endorseddirs", "any");
52        doRunTest("-extdirs", "any");
53        doRunTest("-source", "8");
54        doRunTest("-target", "8");
55    }
56
57    void doRunTest(String... args) throws Exception {
58        System.out.println("Testing clashes for arguments: " + Arrays.asList(args));
59        Class<?> log = Class.forName("com.sun.tools.javac.util.Log", true, cl);
60        Field useRawMessages = log.getDeclaredField("useRawMessages");
61        useRawMessages.setAccessible(true);
62        useRawMessages.setBoolean(null, true);
63        ByteArrayOutputStream out = new ByteArrayOutputStream();
64        List<String> options = new ArrayList<>();
65        options.addAll(Arrays.asList("--release", "7"));
66        options.addAll(Arrays.asList(args));
67        options.add(System.getProperty("test.src") + File.separator + "ReleaseOptionClashes.java");
68        compiler.run(null, null, out, options.toArray(new String[0]));
69        useRawMessages.setBoolean(null, false);
70        if (!out.toString().equals(String.format("%s%n%s%n",
71                                                 "javac: javac.err.release.bootclasspath.conflict",
72                                                 "javac.msg.usage")) &&
73            //-Xbootclasspath:any produces two warnings: one for -bootclasspath and one for -Xbootclasspath:
74            !out.toString().equals(String.format("%s%n%s%n%s%n%s%n",
75                                                 "javac: javac.err.release.bootclasspath.conflict",
76                                                 "javac.msg.usage",
77                                                 "javac: javac.err.release.bootclasspath.conflict",
78                                                 "javac.msg.usage"))) {
79            throw new AssertionError(out);
80        }
81        System.out.println("Test PASSED.  Running javac again to see localized output:");
82        compiler.run(null, null, System.out, options.toArray(new String[0]));
83    }
84
85    Tool compiler = ToolProvider.getSystemJavaCompiler();
86    ClassLoader cl = compiler.getClass().getClassLoader();
87}
88