Wed, 27 Apr 2016 01:34:52 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/langtools/
changeset: 2573:53ca196be1ae
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | */ |
aoqi@0 | 23 | |
aoqi@0 | 24 | /* |
aoqi@0 | 25 | * @test |
aoqi@0 | 26 | * @bug 4241229 4785453 |
aoqi@0 | 27 | * @summary Test -classpath option and classpath defaults. |
aoqi@0 | 28 | * @library /tools/javac/lib |
aoqi@0 | 29 | * @build ToolBox |
aoqi@0 | 30 | * @run main ClassPathTest |
aoqi@0 | 31 | */ |
aoqi@0 | 32 | |
aoqi@0 | 33 | import java.nio.file.Paths; |
aoqi@0 | 34 | import java.util.ArrayList; |
aoqi@0 | 35 | import java.util.Arrays; |
aoqi@0 | 36 | import java.util.List; |
aoqi@0 | 37 | import java.util.Map; |
aoqi@0 | 38 | import java.util.TreeMap; |
aoqi@0 | 39 | |
aoqi@0 | 40 | //original test: test/tools/javac/ClassPathTest/ClassPathTest.sh |
aoqi@0 | 41 | public class ClassPathTest { |
aoqi@0 | 42 | |
aoqi@0 | 43 | private static final String ClassPathTest1Src = |
aoqi@0 | 44 | "import pkg.*;\n" + |
aoqi@0 | 45 | "public class ClassPathTest1 {\n" + |
aoqi@0 | 46 | " ClassPathTestAux1 x;\n" + |
aoqi@0 | 47 | "}"; |
aoqi@0 | 48 | |
aoqi@0 | 49 | private static final String ClassPathTest2Src = |
aoqi@0 | 50 | "import pkg.*;\n" + |
aoqi@0 | 51 | "public class ClassPathTest2 {\n" + |
aoqi@0 | 52 | " ClassPathTestAux2 x;\n" + |
aoqi@0 | 53 | "}"; |
aoqi@0 | 54 | |
aoqi@0 | 55 | private static final String ClassPathTest3Src = |
aoqi@0 | 56 | "import pkg.*;\n" + |
aoqi@0 | 57 | "public class ClassPathTest3 {\n" + |
aoqi@0 | 58 | " ClassPathTestAux3 x;\n" + |
aoqi@0 | 59 | "}"; |
aoqi@0 | 60 | |
aoqi@0 | 61 | private static final String fooPkgClassPathTestAux1Src = |
aoqi@0 | 62 | "package pkg;\n" + |
aoqi@0 | 63 | "public class ClassPathTestAux1 {}"; |
aoqi@0 | 64 | |
aoqi@0 | 65 | private static final String barPkgClassPathTestAux2Src = |
aoqi@0 | 66 | "package pkg;\n" + |
aoqi@0 | 67 | "public class ClassPathTestAux2 {}"; |
aoqi@0 | 68 | |
aoqi@0 | 69 | private static final String pkgClassPathTestAux3Src = |
aoqi@0 | 70 | "package pkg;\n" + |
aoqi@0 | 71 | "public class ClassPathTestAux3 {}"; |
aoqi@0 | 72 | |
aoqi@0 | 73 | ProcessBuilder pb = null; |
aoqi@0 | 74 | |
aoqi@0 | 75 | public static void main(String[] args) throws Exception { |
aoqi@0 | 76 | new ClassPathTest().test(); |
aoqi@0 | 77 | } |
aoqi@0 | 78 | |
aoqi@0 | 79 | public void test() throws Exception { |
aoqi@0 | 80 | createOutputDirAndSourceFiles(); |
aoqi@0 | 81 | checkCompileCommands(); |
aoqi@0 | 82 | } |
aoqi@0 | 83 | |
aoqi@0 | 84 | void createOutputDirAndSourceFiles() throws Exception { |
aoqi@0 | 85 | //dirs and files creation |
aoqi@0 | 86 | ToolBox.createJavaFileFromSource(ClassPathTest1Src); |
aoqi@0 | 87 | ToolBox.createJavaFileFromSource(ClassPathTest2Src); |
aoqi@0 | 88 | ToolBox.createJavaFileFromSource(ClassPathTest3Src); |
aoqi@0 | 89 | ToolBox.createJavaFileFromSource(Paths.get("foo"), |
aoqi@0 | 90 | fooPkgClassPathTestAux1Src); |
aoqi@0 | 91 | ToolBox.createJavaFileFromSource(Paths.get("bar"), |
aoqi@0 | 92 | barPkgClassPathTestAux2Src); |
aoqi@0 | 93 | ToolBox.createJavaFileFromSource(pkgClassPathTestAux3Src); |
aoqi@0 | 94 | } |
aoqi@0 | 95 | |
aoqi@0 | 96 | void checkCompileCommands() throws Exception { |
aoqi@0 | 97 | // Without the -cp . parameter the command will fail seems like when called |
aoqi@0 | 98 | // from the command line, the current dir is added to the classpath |
aoqi@0 | 99 | // automatically but this is not happening when called using ProcessBuilder |
aoqi@0 | 100 | |
aoqi@0 | 101 | // testJavac success ClassPathTest3.java |
aoqi@0 | 102 | List<String> mainArgs = new ArrayList<>(); |
aoqi@0 | 103 | mainArgs.add(ToolBox.javacBinary.toString()); |
aoqi@0 | 104 | if (ToolBox.testToolVMOpts != null) { |
aoqi@0 | 105 | mainArgs.addAll(ToolBox.testToolVMOpts); |
aoqi@0 | 106 | } |
aoqi@0 | 107 | |
aoqi@0 | 108 | List<String> commonArgs = new ArrayList<>(); |
aoqi@0 | 109 | commonArgs.addAll(mainArgs); |
aoqi@0 | 110 | commonArgs.addAll(Arrays.asList("-cp", ".")); |
aoqi@0 | 111 | |
aoqi@0 | 112 | ToolBox.AnyToolArgs successParams = new ToolBox.AnyToolArgs() |
aoqi@0 | 113 | .appendArgs(commonArgs) |
aoqi@0 | 114 | .appendArgs("ClassPathTest3.java"); |
aoqi@0 | 115 | ToolBox.executeCommand(successParams); |
aoqi@0 | 116 | |
aoqi@0 | 117 | // testJavac failure ClassPathTest1.java |
aoqi@0 | 118 | ToolBox.AnyToolArgs failParams = |
aoqi@0 | 119 | new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL) |
aoqi@0 | 120 | .appendArgs(commonArgs) |
aoqi@0 | 121 | .appendArgs("ClassPathTest1.java"); |
aoqi@0 | 122 | ToolBox.executeCommand(failParams); |
aoqi@0 | 123 | |
aoqi@0 | 124 | // This is done inside the executeCommand method |
aoqi@0 | 125 | // CLASSPATH=bar; export CLASSPATH |
aoqi@0 | 126 | |
aoqi@0 | 127 | Map<String, String> extVars = new TreeMap<>(); |
aoqi@0 | 128 | extVars.put("CLASSPATH", "bar"); |
aoqi@0 | 129 | |
aoqi@0 | 130 | // testJavac success ClassPathTest2.java |
aoqi@0 | 131 | successParams = new ToolBox.AnyToolArgs() |
aoqi@0 | 132 | .appendArgs(mainArgs) |
aoqi@0 | 133 | .appendArgs("ClassPathTest2.java") |
aoqi@0 | 134 | .set(extVars); |
aoqi@0 | 135 | ToolBox.executeCommand(successParams); |
aoqi@0 | 136 | |
aoqi@0 | 137 | // testJavac failure ClassPathTest1.java |
aoqi@0 | 138 | failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL) |
aoqi@0 | 139 | .appendArgs(mainArgs) |
aoqi@0 | 140 | .appendArgs("ClassPathTest1.java") |
aoqi@0 | 141 | .set(extVars); |
aoqi@0 | 142 | ToolBox.executeCommand(failParams); |
aoqi@0 | 143 | |
aoqi@0 | 144 | // testJavac failure ClassPathTest3.java |
aoqi@0 | 145 | failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL) |
aoqi@0 | 146 | .appendArgs(mainArgs) |
aoqi@0 | 147 | .appendArgs("ClassPathTest3.java") |
aoqi@0 | 148 | .set(extVars); |
aoqi@0 | 149 | ToolBox.executeCommand(failParams); |
aoqi@0 | 150 | |
aoqi@0 | 151 | // testJavac success -classpath foo ClassPathTest1.java |
aoqi@0 | 152 | |
aoqi@0 | 153 | commonArgs.clear(); |
aoqi@0 | 154 | commonArgs.addAll(mainArgs); |
aoqi@0 | 155 | commonArgs.addAll(Arrays.asList("-cp", "foo")); |
aoqi@0 | 156 | |
aoqi@0 | 157 | successParams = new ToolBox.AnyToolArgs() |
aoqi@0 | 158 | .appendArgs(commonArgs) |
aoqi@0 | 159 | .appendArgs("ClassPathTest1.java") |
aoqi@0 | 160 | .set(extVars); |
aoqi@0 | 161 | ToolBox.executeCommand(successParams); |
aoqi@0 | 162 | |
aoqi@0 | 163 | // testJavac failure -classpath foo ClassPathTest2.java |
aoqi@0 | 164 | failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL) |
aoqi@0 | 165 | .appendArgs(commonArgs) |
aoqi@0 | 166 | .appendArgs("ClassPathTest2.java") |
aoqi@0 | 167 | .set(extVars); |
aoqi@0 | 168 | ToolBox.executeCommand(failParams); |
aoqi@0 | 169 | |
aoqi@0 | 170 | // testJavac failure -classpath foo ClassPathTest3.java |
aoqi@0 | 171 | failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL) |
aoqi@0 | 172 | .appendArgs(commonArgs) |
aoqi@0 | 173 | .appendArgs("ClassPathTest3.java") |
aoqi@0 | 174 | .set(extVars); |
aoqi@0 | 175 | ToolBox.executeCommand(failParams); |
aoqi@0 | 176 | } |
aoqi@0 | 177 | |
aoqi@0 | 178 | } |