ToolsOpts.java revision 16712:96748d4b1204
1/*
2 * Copyright (c) 2012, 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 8002091
27 * @summary Test options patterns for javac,javah,javap and javadoc using
28 * javac as a test launcher. Create a dummy javac and intercept options to check
29 * reception of options as passed through the launcher without having to launch
30 * javac. Only -J and -cp ./* options should be consumed by the launcher.
31 * @run main ToolsOpts
32 * @author ssides
33 */
34
35import java.io.File;
36import java.io.IOException;
37import java.util.ArrayList;
38import java.util.List;
39
40public class ToolsOpts extends TestHelper {
41    static String[][] optionPatterns = {
42        {"-J-Xmx128m"},
43        {"-J-version"},
44        {"-J-XshowSettings:vm"},
45        {"-J-Xdiag"},
46        {"-J-showversion"},
47        {"-J-version", "-option"},
48        {"-option"},
49        {"-option:sub"},
50        {"-option:sub-"},
51        {"-option:sub1,sub2"}, // -option:list
52        {"-option:{sub1,sub2,sub3}"}, // -option:{list}
53        {"-option:{{sub1,sub2,sub3}}"},// -option:{{list}}
54        {"-option/c:/export/date/tmp"},
55        {"-option=value"},
56        {"-Dpk1.pk2.pk3"}, // dot in option
57        {"-Dpk1.pk2=value"}, // dot in option followed by =value
58        {"@<filename>"},
59        {"-option", "http://site.com", "http://site.org"},
60        {"-option", "name", "p1:p2.."},
61        {"-All these non-options show launchers pass options as is to tool."},
62        {"-option"},
63        {"-option:sub"},
64        {"-option:sub-"},
65        {"-option", "<path>"},
66        {"-option", "<file>"},
67        {"-option", "<dir>"},
68        {"-option", "http://a/b/c/g;x?y#s"},
69        {"-option", "<html code>"},
70        {"-option", "name1:name2"},
71        {"-option", "3"},
72        {"option1", "-J-version", "option2"},
73        {"option1", "-J-version", "-J-XshowSettings:vm", "option2"},};
74
75    static void init() throws IOException {
76
77        // A tool which simulates com.sun.tools.javac.Main argument processing,
78        // intercepts options passed via the javac launcher.
79        final String mainJava = "Main" + JAVA_FILE_EXT;
80        List<String> contents = new ArrayList<>();
81        contents.add("package com.sun.tools.javac;");
82        contents.add("public class Main {");
83        contents.add("    public static void main(String... args) {\n");
84        contents.add("       for (String x : args) {\n");
85        contents.add("           if(x.compareTo(\" \")!=0)\n");
86        contents.add("               System.out.println(x);\n");
87        contents.add("       }\n");
88        contents.add("    }\n");
89        contents.add("}\n");
90        String mainJavaPath = "patch-src/com/sun/tools/javac/" + mainJava;
91        File mainJavaFile = new File(mainJavaPath.replace('/', File.separatorChar));
92        mainJavaFile.getParentFile().mkdirs();
93        createFile(mainJavaFile, contents);
94
95        // compile Main.java into directory to override classes in jdk.compiler
96        new File("jdk.compiler").mkdir();
97        compile("--patch-module", "jdk.compiler=patch-src",
98                "-d", "jdk.compiler",
99                mainJavaFile.toString());
100    }
101
102    static void pass(String msg) {
103        System.out.println("pass: " + msg);
104    }
105
106    static void errout(String msg) {
107        System.err.println(msg);
108    }
109
110    // Return position of -J option or -1 is does not contain a -J option.
111    static int indexOfJoption(String[] opts) {
112        for (int i = 0; i < opts.length; i++) {
113            if (opts[i].startsWith("-J")) {
114                return i;
115            }
116        }
117        return -1;
118    }
119
120    /*
121     * Check that J options a) are not passed to tool, and b) do the right thing,
122     * that is, they should be passed to java launcher and work as expected.
123     */
124    static void checkJoptionOutput(TestResult tr, String[] opts) throws IOException {
125        // Check -J-version options are not passed but do what they should.
126        String jopts = "";
127        for (String pat : opts) {
128            jopts = jopts.concat(pat + " ");
129            if (tr.contains("-J")) {
130                throw new RuntimeException(
131                        "failed: output should not contain option " + pat);
132            }
133            if (pat.compareTo("-J-version") == 0 ||
134                    pat.compareTo("-J-showversion") == 0) {
135                if (!tr.contains("java version") &&
136                        !tr.contains("openjdk version")) {
137                    throw new RuntimeException("failed: " + pat +
138                            " should display a version string.");
139                }
140            } else if (pat.compareTo("-J-XshowSettings:VM") == 0) {
141                if (!tr.contains("VM settings")) {
142                    throw new RuntimeException("failed: " + pat +
143                            " should have display VM settings.");
144                }
145            }
146        }
147        pass("Joption check: " + jopts);
148    }
149
150    /*
151     * Feed each option pattern in optionPatterns array to javac launcher with
152     * checking program preempting javac. Check that option received by 'dummy'
153     * javac is the one passed on the command line.
154     */
155    static void runTestOptions() throws IOException {
156        init();
157        TestResult tr;
158        int jpos = -1;
159        String xPatch = "-J--patch-module=jdk.compiler=jdk.compiler";
160        for (String arg[] : optionPatterns) {
161            jpos = indexOfJoption(arg);
162            //Build a cmd string for output in results reporting.
163            String cmdString = javacCmd + " " + xPatch;
164            for (String opt : arg) {
165                cmdString = cmdString.concat(" " + opt);
166            }
167            switch (arg.length) {
168                case 1:
169                    tr = doExec(javacCmd, xPatch, arg[0]);
170                    break;
171                case 2:
172                    tr = doExec(javacCmd, xPatch, arg[0], arg[1]);
173                    break;
174                case 3:
175                    tr = doExec(javacCmd, xPatch, arg[0], arg[1], arg[2]);
176                    break;
177                case 4:
178                    tr = doExec(javacCmd, xPatch, arg[0], arg[1], arg[2], arg[3]);
179                    break;
180                default:
181                    tr = null;
182                    break;
183            }
184
185            String[] output = tr.testOutput.toArray(new String[tr.testOutput.size()]);
186            //-Joptions should not be passed to tool
187            if (jpos > -1) {
188                checkJoptionOutput(tr, arg);
189                if (tr.contains(arg[jpos])) {
190                    throw new RuntimeException(
191                            "failed! Should not have passed -J option to tool.\n"
192                            + "CMD: " + cmdString);
193                }
194            } else {
195                //check that each non -J option was passed to tool.
196                for (int i = 0; i < arg.length; i++) {
197                    if (output[i].compareTo(arg[i]) != 0) {
198                        throw new RuntimeException(
199                                "failed! CMD: " + cmdString + "\n   case:" +
200                                output[i] + " != " + arg[i]);
201                    } else {
202                        pass("check " + output[i] + " == " + arg[i]);
203                    }
204                }
205            }
206            pass(cmdString);
207        }
208    }
209
210    public static void main(String... args) throws IOException {
211        runTestOptions();
212    }
213}
214