test/tools/javac/ClassPathTest/ClassPathTest.java

changeset 1591
dc8b7aa7cef3
child 1637
2e21ecd7a5ad
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/test/tools/javac/ClassPathTest/ClassPathTest.java	Tue Feb 19 17:53:16 2013 +0000
     1.3 @@ -0,0 +1,148 @@
     1.4 +/*
     1.5 + * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + */
    1.26 +
    1.27 +/*
    1.28 + * @test
    1.29 + * @bug 4241229 4785453
    1.30 + * @summary Test -classpath option and classpath defaults.
    1.31 + * @library /tools/javac/lib
    1.32 + * @build ToolBox
    1.33 + * @run main ClassPathTest
    1.34 + */
    1.35 +
    1.36 +import java.nio.file.Paths;
    1.37 +import java.util.Map;
    1.38 +import java.util.TreeMap;
    1.39 +import com.sun.tools.javac.util.ArrayUtils;
    1.40 +
    1.41 +//original test: test/tools/javac/ClassPathTest/ClassPathTest.sh
    1.42 +public class ClassPathTest {
    1.43 +
    1.44 +    private static final String ClassPathTest1Src =
    1.45 +        "import pkg.*;\n" +
    1.46 +        "public class ClassPathTest1 {\n" +
    1.47 +        "    ClassPathTestAux1 x;\n" +
    1.48 +        "}";
    1.49 +
    1.50 +    private static final String ClassPathTest2Src =
    1.51 +        "import pkg.*;\n" +
    1.52 +        "public class ClassPathTest2 {\n" +
    1.53 +        "    ClassPathTestAux2 x;\n" +
    1.54 +        "}";
    1.55 +
    1.56 +    private static final String ClassPathTest3Src =
    1.57 +        "import pkg.*;\n" +
    1.58 +        "public class ClassPathTest3 {\n" +
    1.59 +        "    ClassPathTestAux3 x;\n" +
    1.60 +        "}";
    1.61 +
    1.62 +    private static final String fooPkgClassPathTestAux1Src =
    1.63 +        "package pkg;\n" +
    1.64 +        "public class ClassPathTestAux1 {}";
    1.65 +
    1.66 +    private static final String barPkgClassPathTestAux2Src =
    1.67 +        "package pkg;\n" +
    1.68 +        "public class ClassPathTestAux2 {}";
    1.69 +
    1.70 +    private static final String pkgClassPathTestAux3Src =
    1.71 +        "package pkg;\n" +
    1.72 +        "public class ClassPathTestAux3 {}";
    1.73 +
    1.74 +    ProcessBuilder pb = null;
    1.75 +
    1.76 +    public static void main(String[] args) throws Exception {
    1.77 +        new ClassPathTest().test();
    1.78 +    }
    1.79 +
    1.80 +    public void test() throws Exception {
    1.81 +        createOutputDirAndSourceFiles();
    1.82 +        checkCompileCommands();
    1.83 +    }
    1.84 +
    1.85 +    void createOutputDirAndSourceFiles() throws Exception {
    1.86 +        //dirs and files creation
    1.87 +        ToolBox.createJavaFileFromSource(ClassPathTest1Src);
    1.88 +        ToolBox.createJavaFileFromSource(ClassPathTest2Src);
    1.89 +        ToolBox.createJavaFileFromSource(ClassPathTest3Src);
    1.90 +        ToolBox.createJavaFileFromSource(Paths.get("foo"),
    1.91 +                fooPkgClassPathTestAux1Src);
    1.92 +        ToolBox.createJavaFileFromSource(Paths.get("bar"),
    1.93 +                barPkgClassPathTestAux2Src);
    1.94 +        ToolBox.createJavaFileFromSource(pkgClassPathTestAux3Src);
    1.95 +    }
    1.96 +
    1.97 +    void checkCompileCommands() throws Exception {
    1.98 +        String[] mainArgs = ToolBox.getJavacBin();
    1.99 +
   1.100 +//        Without the -cp . parameter the command will fail seems like when called
   1.101 +//        from the command line, the current dir is added to the classpath
   1.102 +//        automatically but this is not happening when called using ProcessBuilder
   1.103 +
   1.104 +//        testJavac success ClassPathTest3.java
   1.105 +        String[] commonArgs = ArrayUtils.concatOpen(mainArgs, "-cp", ".");
   1.106 +
   1.107 +        ToolBox.AnyToolArgs successParams =
   1.108 +                new ToolBox.AnyToolArgs()
   1.109 +                .setAllArgs(ArrayUtils.concatOpen(commonArgs, "ClassPathTest3.java"));
   1.110 +        ToolBox.executeCommand(successParams);
   1.111 +
   1.112 +//        testJavac failure ClassPathTest1.java
   1.113 +        ToolBox.AnyToolArgs failParams =
   1.114 +                new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
   1.115 +                .setAllArgs(ArrayUtils.concatOpen(commonArgs, "ClassPathTest1.java"));
   1.116 +        ToolBox.executeCommand(failParams);
   1.117 +
   1.118 +//        This is done inside the executeCommand method
   1.119 +//        CLASSPATH=bar; export CLASSPATH
   1.120 +
   1.121 +        Map<String, String> extVars = new TreeMap<>();
   1.122 +        extVars.put("CLASSPATH", "bar");
   1.123 +
   1.124 +//        testJavac success ClassPathTest2.java
   1.125 +        successParams.setAllArgs(ArrayUtils.concatOpen(mainArgs, "ClassPathTest2.java")).set(extVars);
   1.126 +        ToolBox.executeCommand(successParams);
   1.127 +
   1.128 +//        testJavac failure ClassPathTest1.java
   1.129 +        failParams.setAllArgs(ArrayUtils.concatOpen(mainArgs, "ClassPathTest1.java")).set(extVars);
   1.130 +        ToolBox.executeCommand(failParams);
   1.131 +
   1.132 +//        testJavac failure ClassPathTest3.java
   1.133 +        failParams.setAllArgs(ArrayUtils.concatOpen(mainArgs, "ClassPathTest3.java"));
   1.134 +        ToolBox.executeCommand(failParams);
   1.135 +
   1.136 +//        testJavac success -classpath foo ClassPathTest1.java
   1.137 +
   1.138 +        commonArgs = ArrayUtils.concatOpen(mainArgs, "-cp", "foo");
   1.139 +        successParams.setAllArgs(ArrayUtils.concatOpen(commonArgs, "ClassPathTest1.java"));
   1.140 +        ToolBox.executeCommand(successParams);
   1.141 +
   1.142 +//        testJavac failure -classpath foo ClassPathTest2.java
   1.143 +        failParams.setAllArgs(ArrayUtils.concatOpen(commonArgs, "ClassPathTest2.java"));
   1.144 +        ToolBox.executeCommand(failParams);
   1.145 +
   1.146 +//        testJavac failure -classpath foo ClassPathTest3.java
   1.147 +        failParams.setAllArgs(ArrayUtils.concatOpen(commonArgs, "ClassPathTest3.java"));
   1.148 +        ToolBox.executeCommand(failParams);
   1.149 +    }
   1.150 +
   1.151 +}

mercurial