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 
  35 import java.io.File;
  36 import java.io.IOException;
  37 import java.util.ArrayList;
  38 import java.util.List;
  39 
  40 public 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         createFile(new File(mainJava), contents);
  91 
  92         // compile Main.java into directory to override classes in jdk.compiler
  93         new File("jdk.compiler").mkdir();
  94         compile("-Xmodule:jdk.compiler", "-d", "jdk.compiler", mainJava);
  95     }
  96 
  97     static void pass(String msg) {
  98         System.out.println("pass: " + msg);
  99     }
 100 
 101     static void errout(String msg) {
 102         System.err.println(msg);
 103     }
 104 
 105     // Return position of -J option or -1 is does not contain a -J option.
 106     static int indexOfJoption(String[] opts) {
 107         for (int i = 0; i < opts.length; i++) {
 108             if (opts[i].startsWith("-J")) {
 109                 return i;
 110             }
 111         }
 112         return -1;
 113     }
 114 
 115     /*
 116      * Check that J options a) are not passed to tool, and b) do the right thing,
 117      * that is, they should be passed to java launcher and work as expected.
 118      */
 119     static void checkJoptionOutput(TestResult tr, String[] opts) throws IOException {
 120         // Check -J-version options are not passed but do what they should.
 121         String jopts = "";
 122         for (String pat : opts) {
 123             jopts = jopts.concat(pat + " ");
 124             if (tr.contains("-J")) {
 125                 throw new RuntimeException(
 126                         "failed: output should not contain option " + pat);
 127             }
 128             if (pat.compareTo("-J-version") == 0 ||
 129                     pat.compareTo("-J-showversion") == 0) {
 130                 if (!tr.contains("java version") &&
 131                         !tr.contains("openjdk version")) {
 132                     throw new RuntimeException("failed: " + pat +
 133                             " should display a version string.");
 134                 }
 135             } else if (pat.compareTo("-J-XshowSettings:VM") == 0) {
 136                 if (!tr.contains("VM settings")) {
 137                     throw new RuntimeException("failed: " + pat +
 138                             " should have display VM settings.");
 139                 }
 140             }
 141         }
 142         pass("Joption check: " + jopts);
 143     }
 144 
 145     /*
 146      * Feed each option pattern in optionPatterns array to javac launcher with
 147      * checking program preempting javac. Check that option received by 'dummy'
 148      * javac is the one passed on the command line.
 149      */
 150     static void runTestOptions() throws IOException {
 151         init();
 152         TestResult tr;
 153         int jpos = -1;
 154         String xPatch = "-J-Xpatch:jdk.compiler=jdk.compiler";
 155         for (String arg[] : optionPatterns) {
 156             jpos = indexOfJoption(arg);
 157             //Build a cmd string for output in results reporting.
 158             String cmdString = javacCmd + " " + xPatch;
 159             for (String opt : arg) {
 160                 cmdString = cmdString.concat(" " + opt);
 161             }
 162             switch (arg.length) {
 163                 case 1:
 164                     tr = doExec(javacCmd, xPatch, arg[0]);
 165                     break;
 166                 case 2:
 167                     tr = doExec(javacCmd, xPatch, arg[0], arg[1]);
 168                     break;
 169                 case 3:
 170                     tr = doExec(javacCmd, xPatch, arg[0], arg[1], arg[2]);
 171                     break;
 172                 case 4:
 173                     tr = doExec(javacCmd, xPatch, arg[0], arg[1], arg[2], arg[3]);
 174                     break;
 175                 default:
 176                     tr = null;
 177                     break;
 178             }
 179 
 180             String[] output = tr.testOutput.toArray(new String[tr.testOutput.size()]);
 181             //-Joptions should not be passed to tool
 182             if (jpos > -1) {
 183                 checkJoptionOutput(tr, arg);
 184                 if (tr.contains(arg[jpos])) {
 185                     throw new RuntimeException(
 186                             "failed! Should not have passed -J option to tool.\n"
 187                             + "CMD: " + cmdString);
 188                 }
 189             } else {
 190                 //check that each non -J option was passed to tool.
 191                 for (int i = 0; i < arg.length; i++) {
 192                     if (output[i].compareTo(arg[i]) != 0) {
 193                         throw new RuntimeException(
 194                                 "failed! CMD: " + cmdString + "\n   case:" +
 195                                 output[i] + " != " + arg[i]);
 196                     } else {
 197                         pass("check " + output[i] + " == " + arg[i]);
 198                     }
 199                 }
 200             }
 201             pass(cmdString);
 202         }
 203     }
 204 
 205     public static void main(String... args) throws IOException {
 206         runTestOptions();
 207     }
 208 }