ClassPathTest.java revision 3314:97ec97671022
1/*
2 * Copyright (c) 2013, 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 4241229 4785453
27 * @summary Test -classpath option and classpath defaults.
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 ClassPathTest
33 */
34
35import java.nio.file.Paths;
36
37import toolbox.JavacTask;
38import toolbox.Task;
39import toolbox.ToolBox;
40
41// Original test: test/tools/javac/ClassPathTest/ClassPathTest.sh
42public class ClassPathTest {
43
44    private static final String ClassPathTest1Src =
45        "import pkg.*;\n" +
46        "public class ClassPathTest1 {\n" +
47        "    ClassPathTestAux1 x;\n" +
48        "}";
49
50    private static final String ClassPathTest2Src =
51        "import pkg.*;\n" +
52        "public class ClassPathTest2 {\n" +
53        "    ClassPathTestAux2 x;\n" +
54        "}";
55
56    private static final String ClassPathTest3Src =
57        "import pkg.*;\n" +
58        "public class ClassPathTest3 {\n" +
59        "    ClassPathTestAux3 x;\n" +
60        "}";
61
62    private static final String fooPkgClassPathTestAux1Src =
63        "package pkg;\n" +
64        "public class ClassPathTestAux1 {}";
65
66    private static final String barPkgClassPathTestAux2Src =
67        "package pkg;\n" +
68        "public class ClassPathTestAux2 {}";
69
70    private static final String pkgClassPathTestAux3Src =
71        "package pkg;\n" +
72        "public class ClassPathTestAux3 {}";
73
74    public static void main(String[] args) throws Exception {
75        new ClassPathTest().test();
76    }
77
78    ToolBox tb = new ToolBox();
79
80    public void test() throws Exception {
81        createOutputDirAndSourceFiles();
82        checkCompileCommands();
83    }
84
85    void createOutputDirAndSourceFiles() throws Exception {
86        //dirs and files creation
87        tb.writeJavaFiles(Paths.get("."),
88                ClassPathTest1Src,
89                ClassPathTest2Src,
90                ClassPathTest3Src);
91        tb.writeJavaFiles(Paths.get("foo"),
92                fooPkgClassPathTestAux1Src);
93        tb.writeJavaFiles(Paths.get("bar"),
94                barPkgClassPathTestAux2Src);
95        tb.writeJavaFiles(Paths.get("."),
96                pkgClassPathTestAux3Src);
97    }
98
99    void checkCompileCommands() throws Exception {
100//        Without the -cp . parameter the command will fail seems like when called
101//        from the command line, the current dir is added to the classpath
102//        automatically but this is not happening when called using ProcessBuilder
103
104//        testJavac success ClassPathTest3.java
105        new JavacTask(tb, Task.Mode.EXEC)
106                .classpath(".")
107                .files("ClassPathTest3.java")
108                .run();
109
110//        testJavac failure ClassPathTest1.java
111        new JavacTask(tb, Task.Mode.EXEC)
112                .classpath(".")
113                .files("ClassPathTest1.java")
114                .run(Task.Expect.FAIL);
115
116//        testJavac success ClassPathTest2.java
117        new JavacTask(tb, Task.Mode.EXEC)
118                .envVar("CLASSPATH", "bar")
119                .files("ClassPathTest2.java")
120                .run();
121
122//        testJavac failure ClassPathTest1.java
123        new JavacTask(tb, Task.Mode.EXEC)
124                .envVar("CLASSPATH", "bar")
125                .files("ClassPathTest1.java")
126                .run(Task.Expect.FAIL);
127
128//        testJavac failure ClassPathTest3.java
129        new JavacTask(tb, Task.Mode.EXEC)
130                .envVar("CLASSPATH", "bar")
131                .files("ClassPathTest3.java")
132                .run(Task.Expect.FAIL);
133
134//        testJavac success -classpath foo ClassPathTest1.java
135        new JavacTask(tb, Task.Mode.EXEC)
136                .envVar("CLASSPATH", "bar")
137                .classpath("foo")
138                .files("ClassPathTest1.java")
139                .run();
140
141//        testJavac failure -classpath foo ClassPathTest2.java
142        new JavacTask(tb, Task.Mode.EXEC)
143                .envVar("CLASSPATH", "bar")
144                .classpath("foo")
145                .files("ClassPathTest2.java")
146                .run(Task.Expect.FAIL);
147
148//        testJavac failure -classpath foo ClassPathTest3.java
149        new JavacTask(tb, Task.Mode.EXEC)
150                .envVar("CLASSPATH", "bar")
151                .classpath("foo")
152                .files("ClassPathTest3.java")
153                .run(Task.Expect.FAIL);
154    }
155
156}
157