EnvVariableTest.java revision 4002:4a4fd9ecca20
1/* 2 * Copyright (c) 2017, 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 8173308 27 * @summary Check JDK_JAVA_OPTIONS parsing behavior 28 * @library /tools/lib 29 * @modules jdk.compiler/com.sun.tools.javac.main 30 * @build toolbox.ToolBox toolbox.TestRunner 31 * @run main EnvVariableTest 32 */ 33 34import java.io.ByteArrayOutputStream; 35import java.io.IOException; 36import java.io.PrintStream; 37import java.nio.file.Path; 38 39import toolbox.*; 40 41import com.sun.tools.javac.main.CommandLine; 42 43public class EnvVariableTest extends TestRunner { 44 final String testClasses; 45 final ToolBox tb; 46 final Path javaExePath; 47 final ExecTask task; 48 final PrintStream ostream; 49 final ByteArrayOutputStream baos; 50 51 public EnvVariableTest() { 52 super(System.err); 53 ostream = System.err; 54 baos = new ByteArrayOutputStream(); 55 testClasses = System.getProperty("test.classes"); 56 tb = new ToolBox(); 57 javaExePath = tb.getJDKTool("java"); 58 task = new ExecTask(tb, javaExePath); 59 } 60 61 public static void main(String... args) throws Exception { 62 EnvVariableTest t = new EnvVariableTest(); 63 t.runTests(); 64 } 65 66 @Test 67 public void testDoubleQuote() throws Exception { 68 // white space quoted with double quotes 69 test("-version -cp \"c:\\\\java libs\\\\one.jar\" \n", 70 "-version", "-cp", "c:\\\\java libs\\\\one.jar"); 71 } 72 73 @Test 74 public void testSingleQuote() throws Exception { 75 // white space quoted with single quotes 76 test("-version -cp \'c:\\\\java libs\\\\one.jar\' \n", 77 "-version", "-cp", "c:\\\\java libs\\\\one.jar"); 78 } 79 80 @Test 81 public void testEscapeCharacters() throws Exception { 82 // escaped characters 83 test("escaped chars testing \"\\a\\b\\c\\f\\n\\r\\t\\v\\9\\6\\23\\82\\28\\377\\477\\278\\287\"", 84 "escaped", "chars", "testing", "\\a\\b\\c\\f\\n\\r\\t\\v\\9\\6\\23\\82\\28\\377\\477\\278\\287"); 85 } 86 87 @Test 88 public void testMixedQuotes() throws Exception { 89 // more mixing of quote types 90 test("\"mix 'single quote' in double\" 'mix \"double quote\" in single' partial\"quote me\"this", 91 "mix 'single quote' in double", "mix \"double quote\" in single", "partialquote methis"); 92 } 93 94 @Test 95 public void testWhiteSpaces() throws Exception { 96 // whitespace tests 97 test("line one #comment\n'line #2' #rest are comment\r\n#comment on line 3\fline 4 #comment to eof", 98 "line", "one", "#comment", "line #2", "#rest", "are", "comment", "#comment", "on", "line", 99 "3", "line", "4", "#comment", "to", "eof"); 100 } 101 102 @Test 103 public void testMismatchedDoubleQuote() throws Exception { 104 // mismatched quote 105 test("This is an \"open quote \n across line\n\t, note for WS.", 106 "Exception: JDK_JAVAC_OPTIONS"); 107 } 108 109 @Test 110 public void testMismatchedSingleQuote() throws Exception { 111 // mismatched quote 112 test("This is an \'open quote \n across line\n\t, note for WS.", 113 "Exception: JDK_JAVAC_OPTIONS"); 114 } 115 116 void test(String full, String... expectedArgs) throws Exception { 117 task.envVar("JDK_JAVAC_OPTIONS", full); 118 task.args("--add-exports", "jdk.compiler/com.sun.tools.javac.main=ALL-UNNAMED", 119 "-cp", testClasses, "EnvVariableTest$Tester"); 120 Task.Result tr = task.run(Task.Expect.SUCCESS); 121 String expected = Tester.arrayToString(expectedArgs); 122 String in = tr.getOutput(Task.OutputKind.STDOUT); 123 System.err.println("Matching..."); 124 System.err.println("Obtained: " + in); 125 System.err.println("Expected: " + expected); 126 if (in.contains(expected)) { 127 System.err.println("....OK"); 128 return; 129 } 130 throw new Exception("Expected strings not found"); 131 } 132 133 /** 134 * A tester class that is invoked to invoke the CommandLine class, and 135 * print the result. 136 */ 137 public static class Tester { 138 private static final String[] EMPTY_ARRAY = new String[0]; 139 static String arrayToString(String... args) { 140 return String.join(", ", args); 141 } 142 public static void main(String... args) throws IOException { 143 try { 144 String[] argv = CommandLine.parse("JDK_JAVAC_OPTIONS", EMPTY_ARRAY); 145 System.out.print(arrayToString(argv)); 146 } catch (CommandLine.UnmatchedQuote ex) { 147 System.out.print("Exception: " + ex.variableName); 148 } 149 } 150 } 151} 152