jdk-24/test/jdk/tools/launcher/ToolsOpts.java

224 lines
8.6 KiB
Java
Raw Normal View History

/*
* Copyright (c) 2012, 2017, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @bug 8002091
* @summary Test options patterns for javac,javap and javadoc using
* javac as a test launcher. Create a dummy javac and intercept options to check
* reception of options as passed through the launcher without having to launch
* javac. Only -J and -cp ./* options should be consumed by the launcher.
* @modules jdk.compiler
* jdk.zipfs
* @run main ToolsOpts
* @author ssides
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ToolsOpts extends TestHelper {
static String[][] optionPatterns = {
{"-J-Xmx128m"},
{"-J-version"},
{"-J-XshowSettings:vm"},
{"-J-Xdiag"},
{"-J-showversion"},
{"-J-version", "-option"},
{"-option"},
{"-option:sub"},
{"-option:sub-"},
{"-option:sub1,sub2"}, // -option:list
{"-option:{sub1,sub2,sub3}"}, // -option:{list}
{"-option:{{sub1,sub2,sub3}}"},// -option:{{list}}
{"-option/c:/export/date/tmp"},
{"-option=value"},
{"-Dpk1.pk2.pk3"}, // dot in option
{"-Dpk1.pk2=value"}, // dot in option followed by =value
{"@<filename>"},
{"-option", "http://site.com", "http://site.org"},
{"-option", "name", "p1:p2.."},
{"-All these non-options show launchers pass options as is to tool."},
{"-option"},
{"-option:sub"},
{"-option:sub-"},
{"-option", "<path>"},
{"-option", "<file>"},
{"-option", "<dir>"},
{"-option", "http://a/b/c/g;x?y#s"},
{"-option", "<html code>"},
{"-option", "name1:name2"},
{"-option", "3"},
{"option1", "-J-version", "option2"},
{"option1", "-J-version", "-J-XshowSettings:vm", "option2"},};
static void init() throws IOException {
// A tool which simulates com.sun.tools.javac.Main argument processing,
// intercepts options passed via the javac launcher.
final String mainJava = "Main" + JAVA_FILE_EXT;
List<String> contents = new ArrayList<>();
contents.add("package com.sun.tools.javac;");
contents.add("public class Main {");
contents.add(" public static void main(String... args) {\n");
contents.add(" for (String x : args) {\n");
contents.add(" if(x.compareTo(\" \")!=0)\n");
contents.add(" System.out.println(x);\n");
contents.add(" }\n");
contents.add(" }\n");
contents.add("}\n");
String mainJavaPath = "patch-src/com/sun/tools/javac/" + mainJava;
File mainJavaFile = new File(mainJavaPath.replace('/', File.separatorChar));
mainJavaFile.getParentFile().mkdirs();
createFile(mainJavaFile, contents);
8142968: Module System implementation Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Co-authored-by: Alex Buckley <alex.buckley@oracle.com> Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com> Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com> Co-authored-by: Mandy Chung <mandy.chung@oracle.com> Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com> Co-authored-by: Chris Hegarty <chris.hegarty@oracle.com> Co-authored-by: Alexandr Scherbatiy <alexandr.scherbatiy@oracle.com> Co-authored-by: Amy Lu <amy.lu@oracle.com> Co-authored-by: Calvin Cheung <calvin.cheung@oracle.com> Co-authored-by: Daniel Fuchs <daniel.fuchs@oracle.com> Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com> Co-authored-by: Harold Seigel <harold.seigel@oracle.com> Co-authored-by: Jaroslav Bachorik <jaroslav.bachorik@oracle.com> Co-authored-by: Jean-Francois Denise <jean-francois.denise@oracle.com> Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com> Co-authored-by: James Laskey <james.laskey@oracle.com> Co-authored-by: Lois Foltan <lois.foltan@oracle.com> Co-authored-by: Miroslav Kos <miroslav.kos@oracle.com> Co-authored-by: Huaming Li <huaming.li@oracle.com> Co-authored-by: Sean Mullan <sean.mullan@oracle.com> Co-authored-by: Naoto Sato <naoto.sato@oracle.com> Co-authored-by: Masayoshi Okutsu <masayoshi.okutsu@oracle.com> Co-authored-by: Peter Levart <peter.levart@gmail.com> Co-authored-by: Philip Race <philip.race@oracle.com> Co-authored-by: Claes Redestad <claes.redestad@oracle.com> Co-authored-by: Sergey Bylokhov <sergey.bylokhov@oracle.com> Co-authored-by: Alexandre Iline <alexandre.iline@oracle.com> Co-authored-by: Volker Simonis <volker.simonis@gmail.com> Co-authored-by: Staffan Larsen <staffan.larsen@oracle.com> Co-authored-by: Stuart Marks <stuart.marks@oracle.com> Co-authored-by: Semyon Sadetsky <semyon.sadetsky@oracle.com> Co-authored-by: Serguei Spitsyn <serguei.spitsyn@oracle.com> Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com> Co-authored-by: Valerie Peng <valerie.peng@oracle.com> Co-authored-by: Vincent Ryan <vincent.x.ryan@oracle.com> Co-authored-by: Weijun Wang <weijun.wang@oracle.com> Co-authored-by: Yuri Nesterenko <yuri.nesterenko@oracle.com> Co-authored-by: Yekaterina Kantserova <yekaterina.kantserova@oracle.com> Co-authored-by: Alexander Kulyakthin <alexander.kulyakhtin@oracle.com> Co-authored-by: Felix Yang <felix.yang@oracle.com> Co-authored-by: Andrei Eremeev <andrei.eremeev@oracle.com> Co-authored-by: Frank Yuan <frank.yuan@oracle.com> Co-authored-by: Sergei Pikalev <sergei.pikalev@oracle.com> Co-authored-by: Sibabrata Sahoo <sibabrata.sahoo@oracle.com> Co-authored-by: Tiantian Du <tiantian.du@oracle.com> Co-authored-by: Sha Jiang <sha.jiang@oracle.com> Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt
2016-03-17 19:04:16 +00:00
// compile Main.java into directory to override classes in jdk.compiler
new File("jdk.compiler").mkdir();
compile("--patch-module", "jdk.compiler=patch-src",
"-d", "jdk.compiler",
mainJavaFile.toString());
8142968: Module System implementation Initial integration of JEP 200, JEP 260, JEP 261, and JEP 282 Co-authored-by: Alex Buckley <alex.buckley@oracle.com> Co-authored-by: Jonathan Gibbons <jonathan.gibbons@oracle.com> Co-authored-by: Karen Kinnear <karen.kinnear@oracle.com> Co-authored-by: Mandy Chung <mandy.chung@oracle.com> Co-authored-by: Mark Reinhold <mark.reinhold@oracle.com> Co-authored-by: Chris Hegarty <chris.hegarty@oracle.com> Co-authored-by: Alexandr Scherbatiy <alexandr.scherbatiy@oracle.com> Co-authored-by: Amy Lu <amy.lu@oracle.com> Co-authored-by: Calvin Cheung <calvin.cheung@oracle.com> Co-authored-by: Daniel Fuchs <daniel.fuchs@oracle.com> Co-authored-by: Erik Joelsson <erik.joelsson@oracle.com> Co-authored-by: Harold Seigel <harold.seigel@oracle.com> Co-authored-by: Jaroslav Bachorik <jaroslav.bachorik@oracle.com> Co-authored-by: Jean-Francois Denise <jean-francois.denise@oracle.com> Co-authored-by: Jan Lahoda <jan.lahoda@oracle.com> Co-authored-by: James Laskey <james.laskey@oracle.com> Co-authored-by: Lois Foltan <lois.foltan@oracle.com> Co-authored-by: Miroslav Kos <miroslav.kos@oracle.com> Co-authored-by: Huaming Li <huaming.li@oracle.com> Co-authored-by: Sean Mullan <sean.mullan@oracle.com> Co-authored-by: Naoto Sato <naoto.sato@oracle.com> Co-authored-by: Masayoshi Okutsu <masayoshi.okutsu@oracle.com> Co-authored-by: Peter Levart <peter.levart@gmail.com> Co-authored-by: Philip Race <philip.race@oracle.com> Co-authored-by: Claes Redestad <claes.redestad@oracle.com> Co-authored-by: Sergey Bylokhov <sergey.bylokhov@oracle.com> Co-authored-by: Alexandre Iline <alexandre.iline@oracle.com> Co-authored-by: Volker Simonis <volker.simonis@gmail.com> Co-authored-by: Staffan Larsen <staffan.larsen@oracle.com> Co-authored-by: Stuart Marks <stuart.marks@oracle.com> Co-authored-by: Semyon Sadetsky <semyon.sadetsky@oracle.com> Co-authored-by: Serguei Spitsyn <serguei.spitsyn@oracle.com> Co-authored-by: Sundararajan Athijegannathan <sundararajan.athijegannathan@oracle.com> Co-authored-by: Valerie Peng <valerie.peng@oracle.com> Co-authored-by: Vincent Ryan <vincent.x.ryan@oracle.com> Co-authored-by: Weijun Wang <weijun.wang@oracle.com> Co-authored-by: Yuri Nesterenko <yuri.nesterenko@oracle.com> Co-authored-by: Yekaterina Kantserova <yekaterina.kantserova@oracle.com> Co-authored-by: Alexander Kulyakthin <alexander.kulyakhtin@oracle.com> Co-authored-by: Felix Yang <felix.yang@oracle.com> Co-authored-by: Andrei Eremeev <andrei.eremeev@oracle.com> Co-authored-by: Frank Yuan <frank.yuan@oracle.com> Co-authored-by: Sergei Pikalev <sergei.pikalev@oracle.com> Co-authored-by: Sibabrata Sahoo <sibabrata.sahoo@oracle.com> Co-authored-by: Tiantian Du <tiantian.du@oracle.com> Co-authored-by: Sha Jiang <sha.jiang@oracle.com> Reviewed-by: alanb, mchung, naoto, rriggs, psandoz, plevart, mullan, ascarpino, vinnie, prr, sherman, dfuchs, mhaupt
2016-03-17 19:04:16 +00:00
}
static void pass(String msg) {
System.out.println("pass: " + msg);
}
static void errout(String msg) {
System.err.println(msg);
}
// Return position of -J option or -1 is does not contain a -J option.
static int indexOfJoption(String[] opts) {
for (int i = 0; i < opts.length; i++) {
if (opts[i].startsWith("-J")) {
return i;
}
}
return -1;
}
/*
* Check that J options a) are not passed to tool, and b) do the right thing,
* that is, they should be passed to java launcher and work as expected.
*/
static void checkJoptionOutput(TestResult tr, String[] opts) throws IOException {
// Check -J-version options are not passed but do what they should.
String jopts = "";
for (String pat : opts) {
jopts = jopts.concat(pat + " ");
if (tr.contains("-J")) {
throw new RuntimeException(
"failed: output should not contain option " + pat);
}
if (pat.compareTo("-J-version") == 0 ||
pat.compareTo("-J-showversion") == 0) {
if (!tr.contains("java version") &&
!tr.contains("openjdk version")) {
throw new RuntimeException("failed: " + pat +
" should display a version string.");
}
} else if (pat.compareTo("-J-XshowSettings:VM") == 0) {
if (!tr.contains("VM settings")) {
throw new RuntimeException("failed: " + pat +
" should have display VM settings.");
}
}
}
pass("Joption check: " + jopts);
}
/*
* Feed each option pattern in optionPatterns array to javac launcher with
* checking program preempting javac. Check that option received by 'dummy'
* javac is the one passed on the command line.
*/
static void runTestOptions() throws IOException {
init();
TestResult tr;
int jpos = -1;
String xPatch = "-J--patch-module=jdk.compiler=jdk.compiler";
for (String arg[] : optionPatterns) {
jpos = indexOfJoption(arg);
//Build a cmd string for output in results reporting.
String cmdString = javacCmd + " " + xPatch;
for (String opt : arg) {
cmdString = cmdString.concat(" " + opt);
}
switch (arg.length) {
case 1:
tr = doExec(javacCmd, xPatch, arg[0]);
break;
case 2:
tr = doExec(javacCmd, xPatch, arg[0], arg[1]);
break;
case 3:
tr = doExec(javacCmd, xPatch, arg[0], arg[1], arg[2]);
break;
case 4:
tr = doExec(javacCmd, xPatch, arg[0], arg[1], arg[2], arg[3]);
break;
default:
tr = null;
break;
}
//-Joptions should not be passed to tool
if (jpos > -1) {
checkJoptionOutput(tr, arg);
if (tr.contains(arg[jpos])) {
throw new RuntimeException(
"failed! Should not have passed -J option to tool.\n"
+ "CMD: " + cmdString);
}
} else {
// check that each non -J option was passed to tool. It looks for each arg in the output.
// Irrelevant lines in the output are skipped. Arguments order is checked as well.
int j = 0;
List<String> output = tr.testOutput;
for (int i = 0; i < arg.length; i++) {
boolean found = false;
for (; j < output.size(); j++) {
if (output.get(j).equals(arg[i])) {
pass("check " + output.get(j) + " == " + arg[i]);
found = true;
break;
}
}
if (!found) {
throw new RuntimeException(
"failed! Should have passed non -J option [" + arg[i] + "] to tool.\n"
+ "CMD: " + cmdString);
}
}
}
pass(cmdString);
}
}
public static void main(String... args) throws IOException {
runTestOptions();
}
}