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