Path: blob/master/test/jdk/tools/launcher/ToolsOpts.java
41144 views
/*1* Copyright (c) 2012, 2017, 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,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* @modules jdk.compiler31* jdk.zipfs32* @run main ToolsOpts33* @author ssides34*/3536import java.io.File;37import java.io.IOException;38import java.util.ArrayList;39import java.util.List;4041public class ToolsOpts extends TestHelper {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 {7778// A tool which simulates com.sun.tools.javac.Main argument processing,79// intercepts options passed via the javac launcher.80final String mainJava = "Main" + JAVA_FILE_EXT;81List<String> contents = new ArrayList<>();82contents.add("package com.sun.tools.javac;");83contents.add("public class Main {");84contents.add(" public static void main(String... args) {\n");85contents.add(" for (String x : args) {\n");86contents.add(" if(x.compareTo(\" \")!=0)\n");87contents.add(" System.out.println(x);\n");88contents.add(" }\n");89contents.add(" }\n");90contents.add("}\n");91String mainJavaPath = "patch-src/com/sun/tools/javac/" + mainJava;92File mainJavaFile = new File(mainJavaPath.replace('/', File.separatorChar));93mainJavaFile.getParentFile().mkdirs();94createFile(mainJavaFile, contents);9596// compile Main.java into directory to override classes in jdk.compiler97new File("jdk.compiler").mkdir();98compile("--patch-module", "jdk.compiler=patch-src",99"-d", "jdk.compiler",100mainJavaFile.toString());101}102103static void pass(String msg) {104System.out.println("pass: " + msg);105}106107static void errout(String msg) {108System.err.println(msg);109}110111// Return position of -J option or -1 is does not contain a -J option.112static int indexOfJoption(String[] opts) {113for (int i = 0; i < opts.length; i++) {114if (opts[i].startsWith("-J")) {115return i;116}117}118return -1;119}120121/*122* Check that J options a) are not passed to tool, and b) do the right thing,123* that is, they should be passed to java launcher and work as expected.124*/125static void checkJoptionOutput(TestResult tr, String[] opts) throws IOException {126// Check -J-version options are not passed but do what they should.127String jopts = "";128for (String pat : opts) {129jopts = jopts.concat(pat + " ");130if (tr.contains("-J")) {131throw new RuntimeException(132"failed: output should not contain option " + pat);133}134if (pat.compareTo("-J-version") == 0 ||135pat.compareTo("-J-showversion") == 0) {136if (!tr.contains("java version") &&137!tr.contains("openjdk version")) {138throw new RuntimeException("failed: " + pat +139" should display a version string.");140}141} else if (pat.compareTo("-J-XshowSettings:VM") == 0) {142if (!tr.contains("VM settings")) {143throw new RuntimeException("failed: " + pat +144" should have display VM settings.");145}146}147}148pass("Joption check: " + jopts);149}150151/*152* Feed each option pattern in optionPatterns array to javac launcher with153* checking program preempting javac. Check that option received by 'dummy'154* javac is the one passed on the command line.155*/156static void runTestOptions() throws IOException {157init();158TestResult tr;159int jpos = -1;160String xPatch = "-J--patch-module=jdk.compiler=jdk.compiler";161for (String arg[] : optionPatterns) {162jpos = indexOfJoption(arg);163//Build a cmd string for output in results reporting.164String cmdString = javacCmd + " " + xPatch;165for (String opt : arg) {166cmdString = cmdString.concat(" " + opt);167}168switch (arg.length) {169case 1:170tr = doExec(javacCmd, xPatch, arg[0]);171break;172case 2:173tr = doExec(javacCmd, xPatch, arg[0], arg[1]);174break;175case 3:176tr = doExec(javacCmd, xPatch, arg[0], arg[1], arg[2]);177break;178case 4:179tr = doExec(javacCmd, xPatch, arg[0], arg[1], arg[2], arg[3]);180break;181default:182tr = null;183break;184}185186//-Joptions should not be passed to tool187if (jpos > -1) {188checkJoptionOutput(tr, arg);189if (tr.contains(arg[jpos])) {190throw 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. It looks for each arg in the output.196// Irrelevant lines in the output are skipped. Arguments order is checked as well.197int j = 0;198List<String> output = tr.testOutput;199for (int i = 0; i < arg.length; i++) {200boolean found = false;201for (; j < output.size(); j++) {202if (output.get(j).equals(arg[i])) {203pass("check " + output.get(j) + " == " + arg[i]);204found = true;205break;206}207}208if (!found) {209throw new RuntimeException(210"failed! Should have passed non -J option [" + arg[i] + "] to tool.\n"211+ "CMD: " + cmdString);212}213}214}215pass(cmdString);216}217}218219public static void main(String... args) throws IOException {220runTestOptions();221}222}223224225