Fri, 18 Sep 2015 08:11:44 -0700
Merge
1 /*
2 * Copyright (c) 2013, 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 */
24 /*
25 * @test
26 * @bug 4241229 4785453
27 * @summary Test -classpath option and classpath defaults.
28 * @library /tools/javac/lib
29 * @build ToolBox
30 * @run main ClassPathTest
31 */
33 import java.nio.file.Paths;
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.List;
37 import java.util.Map;
38 import java.util.TreeMap;
40 //original test: test/tools/javac/ClassPathTest/ClassPathTest.sh
41 public class ClassPathTest {
43 private static final String ClassPathTest1Src =
44 "import pkg.*;\n" +
45 "public class ClassPathTest1 {\n" +
46 " ClassPathTestAux1 x;\n" +
47 "}";
49 private static final String ClassPathTest2Src =
50 "import pkg.*;\n" +
51 "public class ClassPathTest2 {\n" +
52 " ClassPathTestAux2 x;\n" +
53 "}";
55 private static final String ClassPathTest3Src =
56 "import pkg.*;\n" +
57 "public class ClassPathTest3 {\n" +
58 " ClassPathTestAux3 x;\n" +
59 "}";
61 private static final String fooPkgClassPathTestAux1Src =
62 "package pkg;\n" +
63 "public class ClassPathTestAux1 {}";
65 private static final String barPkgClassPathTestAux2Src =
66 "package pkg;\n" +
67 "public class ClassPathTestAux2 {}";
69 private static final String pkgClassPathTestAux3Src =
70 "package pkg;\n" +
71 "public class ClassPathTestAux3 {}";
73 ProcessBuilder pb = null;
75 public static void main(String[] args) throws Exception {
76 new ClassPathTest().test();
77 }
79 public void test() throws Exception {
80 createOutputDirAndSourceFiles();
81 checkCompileCommands();
82 }
84 void createOutputDirAndSourceFiles() throws Exception {
85 //dirs and files creation
86 ToolBox.createJavaFileFromSource(ClassPathTest1Src);
87 ToolBox.createJavaFileFromSource(ClassPathTest2Src);
88 ToolBox.createJavaFileFromSource(ClassPathTest3Src);
89 ToolBox.createJavaFileFromSource(Paths.get("foo"),
90 fooPkgClassPathTestAux1Src);
91 ToolBox.createJavaFileFromSource(Paths.get("bar"),
92 barPkgClassPathTestAux2Src);
93 ToolBox.createJavaFileFromSource(pkgClassPathTestAux3Src);
94 }
96 void checkCompileCommands() throws Exception {
97 // Without the -cp . parameter the command will fail seems like when called
98 // from the command line, the current dir is added to the classpath
99 // automatically but this is not happening when called using ProcessBuilder
101 // testJavac success ClassPathTest3.java
102 List<String> mainArgs = new ArrayList<>();
103 mainArgs.add(ToolBox.javacBinary.toString());
104 if (ToolBox.testToolVMOpts != null) {
105 mainArgs.addAll(ToolBox.testToolVMOpts);
106 }
108 List<String> commonArgs = new ArrayList<>();
109 commonArgs.addAll(mainArgs);
110 commonArgs.addAll(Arrays.asList("-cp", "."));
112 ToolBox.AnyToolArgs successParams = new ToolBox.AnyToolArgs()
113 .appendArgs(commonArgs)
114 .appendArgs("ClassPathTest3.java");
115 ToolBox.executeCommand(successParams);
117 // testJavac failure ClassPathTest1.java
118 ToolBox.AnyToolArgs failParams =
119 new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
120 .appendArgs(commonArgs)
121 .appendArgs("ClassPathTest1.java");
122 ToolBox.executeCommand(failParams);
124 // This is done inside the executeCommand method
125 // CLASSPATH=bar; export CLASSPATH
127 Map<String, String> extVars = new TreeMap<>();
128 extVars.put("CLASSPATH", "bar");
130 // testJavac success ClassPathTest2.java
131 successParams = new ToolBox.AnyToolArgs()
132 .appendArgs(mainArgs)
133 .appendArgs("ClassPathTest2.java")
134 .set(extVars);
135 ToolBox.executeCommand(successParams);
137 // testJavac failure ClassPathTest1.java
138 failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
139 .appendArgs(mainArgs)
140 .appendArgs("ClassPathTest1.java")
141 .set(extVars);
142 ToolBox.executeCommand(failParams);
144 // testJavac failure ClassPathTest3.java
145 failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
146 .appendArgs(mainArgs)
147 .appendArgs("ClassPathTest3.java")
148 .set(extVars);
149 ToolBox.executeCommand(failParams);
151 // testJavac success -classpath foo ClassPathTest1.java
153 commonArgs.clear();
154 commonArgs.addAll(mainArgs);
155 commonArgs.addAll(Arrays.asList("-cp", "foo"));
157 successParams = new ToolBox.AnyToolArgs()
158 .appendArgs(commonArgs)
159 .appendArgs("ClassPathTest1.java")
160 .set(extVars);
161 ToolBox.executeCommand(successParams);
163 // testJavac failure -classpath foo ClassPathTest2.java
164 failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
165 .appendArgs(commonArgs)
166 .appendArgs("ClassPathTest2.java")
167 .set(extVars);
168 ToolBox.executeCommand(failParams);
170 // testJavac failure -classpath foo ClassPathTest3.java
171 failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
172 .appendArgs(commonArgs)
173 .appendArgs("ClassPathTest3.java")
174 .set(extVars);
175 ToolBox.executeCommand(failParams);
176 }
178 }