Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/ToolsOpts.java
38833 views
/*1* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 800209126* @summary Test options patterns for javac,javah,javap and javadoc using27* javac as a test launcher. Create a dummy javac and intercept options to check28* reception of options as passed through the launcher without having to launch29* javac. Only -J and -cp ./* options should be consumed by the launcher.30* @run main ToolsOpts31* @author ssides32*/3334import java.io.File;35import java.io.IOException;36import java.util.ArrayList;37import java.util.List;3839public class ToolsOpts extends TestHelper {40static final String JBCP_PREPEND = "-J-Xbootclasspath/p:";41private static File testJar = null;42static 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:list53{"-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 option58{"-Dpk1.pk2=value"}, // dot in option followed by =value59{"@<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"},};7576static void init() throws IOException {77if (testJar != null) {78return;79}8081// A tool which simulates com.sun.tools.javac.Main argument processing,82// intercepts options passed via the javac launcher.83final String mainJava = "Main" + JAVA_FILE_EXT;84testJar = new File("test" + JAR_FILE_EXT);85List<String> contents = new ArrayList<>();86contents.add("package com.sun.tools.javac;");87contents.add("public class Main {");88contents.add(" public static void main(String... args) {\n");89contents.add(" for (String x : args) {\n");90contents.add(" if(x.compareTo(\" \")!=0)\n");91contents.add(" System.out.println(x);\n");92contents.add(" }\n");93contents.add(" }\n");94contents.add("}\n");95createFile(new File(mainJava), contents);9697// compile and jar Main.java into test.jar98compile("-d", ".", mainJava);99createJar("cvf", testJar.getAbsolutePath(), "com");100}101102static void pass(String msg) {103System.out.println("pass: " + msg);104}105106static void errout(String msg) {107System.err.println(msg);108}109110// Return position of -J option or -1 is does not contain a -J option.111static int indexOfJoption(String[] opts) {112for (int i = 0; i < opts.length; i++) {113if (opts[i].startsWith("-J")) {114return i;115}116}117return -1;118}119120/*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*/124static void checkJoptionOutput(TestResult tr, String[] opts) throws IOException {125// Check -J-version options are not passed but do what they should.126String jopts = "";127for (String pat : opts) {128jopts = jopts.concat(pat + " ");129if (tr.contains("-J")) {130throw new RuntimeException(131"failed: output should not contain option " + pat);132}133if (pat.compareTo("-J-version") == 0 ||134pat.compareTo("-J-showversion") == 0) {135if (!tr.contains("java version") &&136!tr.contains("openjdk version")) {137throw new RuntimeException("failed: " + pat +138" should display a version string.");139}140} else if (pat.compareTo("-J-XshowSettings:VM") == 0) {141if (!tr.contains("VM settings")) {142throw new RuntimeException("failed: " + pat +143" should have display VM settings.");144}145}146}147pass("Joption check: " + jopts);148}149150/*151* Feed each option pattern in optionPatterns array to javac launcher with152* checking program preempting javac. Check that option received by 'dummy'153* javac is the one passed on the command line.154*/155static void runTestOptions() throws IOException {156init();157TestResult tr = null;158String sTestJar = testJar.getAbsolutePath();159int jpos = -1;160for (String arg[] : optionPatterns) {161jpos = indexOfJoption(arg);162//Build a cmd string for output in results reporting.163String cmdString = javacCmd + " " + JBCP_PREPEND + sTestJar;164for (String opt : arg) {165cmdString = cmdString.concat(" " + opt);166}167switch (arg.length) {168case 1:169tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,170arg[0]);171break;172case 2:173tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,174arg[0], arg[1]);175break;176case 3:177tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,178arg[0], arg[1], arg[2]);179break;180case 4:181tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,182arg[0], arg[1], arg[2], arg[3]);183break;184default:185tr = null;186break;187}188189String[] output = tr.testOutput.toArray(new String[tr.testOutput.size()]);190//-Joptions should not be passed to tool191if (jpos > -1) {192checkJoptionOutput(tr, arg);193if (tr.contains(arg[jpos])) {194throw new RuntimeException(195"failed! Should not have passed -J option to tool.\n"196+ "CMD: " + cmdString);197}198} else {199//check that each non -J option was passed to tool.200for (int i = 0; i < arg.length; i++) {201if (output[i].compareTo(arg[i]) != 0) {202throw new RuntimeException(203"failed! CMD: " + cmdString + "\n case:" +204output[i] + " != " + arg[i]);205} else {206pass("check " + output[i] + " == " + arg[i]);207}208}209}210pass(cmdString);211}212}213214public static void main(String... args) throws IOException {215runTestOptions();216}217}218219220