ClassPathWithDoubleQuotesTest.java revision 3734:11ab0f581f11
1/*
2 * Copyright (c) 2016, 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 8132562
27 * @summary javac fails with CLASSPATH with double-quotes as an environment variable
28 * @library /tools/lib
29 * @modules jdk.compiler/com.sun.tools.javac.api
30 *          jdk.compiler/com.sun.tools.javac.main
31 * @build toolbox.ToolBox toolbox.JavacTask
32 * @run main ClassPathWithDoubleQuotesTest
33*/
34
35import java.io.File;
36import java.nio.file.Path;
37import java.nio.file.Paths;
38
39import toolbox.TestRunner;
40import toolbox.JarTask;
41import toolbox.JavacTask;
42import toolbox.Task;
43import toolbox.ToolBox;
44
45public class ClassPathWithDoubleQuotesTest extends TestRunner {
46
47    ToolBox tb;
48
49    private static final String ASrc = "public class A {}";
50    private static final String JarSrc = "public class J {}";
51    private static final String[] jarArgs = {"cf", "test/J.jar", "-C", "test", "J.java"};
52
53    public static void main(String... args) throws Exception {
54        new ClassPathWithDoubleQuotesTest().runTests();
55    }
56
57    ClassPathWithDoubleQuotesTest() {
58        super(System.err);
59        tb = new ToolBox();
60    }
61
62    public void runTests() throws Exception {
63        runTests(m -> new Object[] { Paths.get(m.getName()) });
64    }
65
66    @Test
67    public void test(Path base) throws Exception {
68        Path current = base.resolve(".");
69        tb.writeJavaFiles(current, ASrc, JarSrc);
70        new JarTask(tb).run(jarArgs).writeAll();
71
72        executeTask(new JavacTask(tb, Task.Mode.EXEC)
73                    .envVar("CLASSPATH", "\"test/J.jar" + File.pathSeparator + "test\"")
74                    .files("test/A.java"));
75
76        executeTask(new JavacTask(tb)
77                    .classpath("\"test/J.jar" + File.pathSeparator + "test\"")
78                    .files("test/A.java"));
79    }
80
81    void executeTask(JavacTask task) {
82        boolean isWindows = System.getProperty("os.name").startsWith("Windows");
83        Task.Expect whatToExpect = isWindows ? Task.Expect.FAIL : Task.Expect.SUCCESS;
84        try {
85            task.run(whatToExpect);
86            if (isWindows) {
87                throw new AssertionError("exception must be thrown");
88            }
89        } catch (IllegalArgumentException iae) {
90            if (!isWindows) {
91                throw new AssertionError("exception unexpectedly thrown");
92            }
93        } catch (Throwable t) {
94            throw new AssertionError("unexpected exception thrown");
95        }
96    }
97}
98