langtools/test/tools/javac/ClassPathTest/ClassPathTest.java
changeset 26100 bb7dd001d190
parent 16549 1bdeedb5446c
child 30730 d3ce7619db2c
equal deleted inserted replaced
26099:c425126bfadf 26100:bb7dd001d190
     1 /*
     1 /*
     2  * Copyright (c) 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2013, 2014, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     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
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     7  * published by the Free Software Foundation.
    23 
    23 
    24 /*
    24 /*
    25  * @test
    25  * @test
    26  * @bug 4241229 4785453
    26  * @bug 4241229 4785453
    27  * @summary Test -classpath option and classpath defaults.
    27  * @summary Test -classpath option and classpath defaults.
    28  * @library /tools/javac/lib
    28  * @library /tools/lib
    29  * @build ToolBox
    29  * @build ToolBox
    30  * @run main ClassPathTest
    30  * @run main ClassPathTest
    31  */
    31  */
    32 
    32 
    33 import java.nio.file.Paths;
    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;
       
    39 
    34 
    40 //original test: test/tools/javac/ClassPathTest/ClassPathTest.sh
    35 // Original test: test/tools/javac/ClassPathTest/ClassPathTest.sh
    41 public class ClassPathTest {
    36 public class ClassPathTest {
    42 
    37 
    43     private static final String ClassPathTest1Src =
    38     private static final String ClassPathTest1Src =
    44         "import pkg.*;\n" +
    39         "import pkg.*;\n" +
    45         "public class ClassPathTest1 {\n" +
    40         "public class ClassPathTest1 {\n" +
    68 
    63 
    69     private static final String pkgClassPathTestAux3Src =
    64     private static final String pkgClassPathTestAux3Src =
    70         "package pkg;\n" +
    65         "package pkg;\n" +
    71         "public class ClassPathTestAux3 {}";
    66         "public class ClassPathTestAux3 {}";
    72 
    67 
    73     ProcessBuilder pb = null;
       
    74 
       
    75     public static void main(String[] args) throws Exception {
    68     public static void main(String[] args) throws Exception {
    76         new ClassPathTest().test();
    69         new ClassPathTest().test();
    77     }
    70     }
       
    71 
       
    72     ToolBox tb = new ToolBox();
    78 
    73 
    79     public void test() throws Exception {
    74     public void test() throws Exception {
    80         createOutputDirAndSourceFiles();
    75         createOutputDirAndSourceFiles();
    81         checkCompileCommands();
    76         checkCompileCommands();
    82     }
    77     }
    83 
    78 
    84     void createOutputDirAndSourceFiles() throws Exception {
    79     void createOutputDirAndSourceFiles() throws Exception {
    85         //dirs and files creation
    80         //dirs and files creation
    86         ToolBox.createJavaFileFromSource(ClassPathTest1Src);
    81         tb.writeJavaFiles(Paths.get("."),
    87         ToolBox.createJavaFileFromSource(ClassPathTest2Src);
    82                 ClassPathTest1Src,
    88         ToolBox.createJavaFileFromSource(ClassPathTest3Src);
    83                 ClassPathTest2Src,
    89         ToolBox.createJavaFileFromSource(Paths.get("foo"),
    84                 ClassPathTest3Src);
       
    85         tb.writeJavaFiles(Paths.get("foo"),
    90                 fooPkgClassPathTestAux1Src);
    86                 fooPkgClassPathTestAux1Src);
    91         ToolBox.createJavaFileFromSource(Paths.get("bar"),
    87         tb.writeJavaFiles(Paths.get("bar"),
    92                 barPkgClassPathTestAux2Src);
    88                 barPkgClassPathTestAux2Src);
    93         ToolBox.createJavaFileFromSource(pkgClassPathTestAux3Src);
    89         tb.writeJavaFiles(Paths.get("."),
       
    90                 pkgClassPathTestAux3Src);
    94     }
    91     }
    95 
    92 
    96     void checkCompileCommands() throws Exception {
    93     void checkCompileCommands() throws Exception {
    97 //        Without the -cp . parameter the command will fail seems like when called
    94 //        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
    95 //        from the command line, the current dir is added to the classpath
    99 //        automatically but this is not happening when called using ProcessBuilder
    96 //        automatically but this is not happening when called using ProcessBuilder
   100 
    97 
   101 //        testJavac success ClassPathTest3.java
    98 //        testJavac success ClassPathTest3.java
   102         List<String> mainArgs = new ArrayList<>();
    99         tb.new JavacTask(ToolBox.Mode.EXEC)
   103         mainArgs.add(ToolBox.javacBinary.toString());
   100                 .classpath(".")
   104         if (ToolBox.testToolVMOpts != null) {
   101                 .files("ClassPathTest3.java")
   105             mainArgs.addAll(ToolBox.testToolVMOpts);
   102                 .run();
   106         }
       
   107 
       
   108         List<String> commonArgs = new ArrayList<>();
       
   109         commonArgs.addAll(mainArgs);
       
   110         commonArgs.addAll(Arrays.asList("-cp", "."));
       
   111 
       
   112         ToolBox.AnyToolArgs successParams = new ToolBox.AnyToolArgs()
       
   113                 .appendArgs(commonArgs)
       
   114                 .appendArgs("ClassPathTest3.java");
       
   115         ToolBox.executeCommand(successParams);
       
   116 
   103 
   117 //        testJavac failure ClassPathTest1.java
   104 //        testJavac failure ClassPathTest1.java
   118         ToolBox.AnyToolArgs failParams =
   105         tb.new JavacTask(ToolBox.Mode.EXEC)
   119                 new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
   106                 .classpath(".")
   120                 .appendArgs(commonArgs)
   107                 .files("ClassPathTest1.java")
   121                 .appendArgs("ClassPathTest1.java");
   108                 .run(ToolBox.Expect.FAIL);
   122         ToolBox.executeCommand(failParams);
       
   123 
       
   124 //        This is done inside the executeCommand method
       
   125 //        CLASSPATH=bar; export CLASSPATH
       
   126 
       
   127         Map<String, String> extVars = new TreeMap<>();
       
   128         extVars.put("CLASSPATH", "bar");
       
   129 
   109 
   130 //        testJavac success ClassPathTest2.java
   110 //        testJavac success ClassPathTest2.java
   131         successParams = new ToolBox.AnyToolArgs()
   111         tb.new JavacTask(ToolBox.Mode.EXEC)
   132                 .appendArgs(mainArgs)
   112                 .envVar("CLASSPATH", "bar")
   133                 .appendArgs("ClassPathTest2.java")
   113                 .files("ClassPathTest2.java")
   134                 .set(extVars);
   114                 .run();
   135         ToolBox.executeCommand(successParams);
       
   136 
   115 
   137 //        testJavac failure ClassPathTest1.java
   116 //        testJavac failure ClassPathTest1.java
   138         failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
   117         tb.new JavacTask(ToolBox.Mode.EXEC)
   139                 .appendArgs(mainArgs)
   118                 .envVar("CLASSPATH", "bar")
   140                 .appendArgs("ClassPathTest1.java")
   119                 .files("ClassPathTest1.java")
   141                 .set(extVars);
   120                 .run(ToolBox.Expect.FAIL);
   142         ToolBox.executeCommand(failParams);
       
   143 
   121 
   144 //        testJavac failure ClassPathTest3.java
   122 //        testJavac failure ClassPathTest3.java
   145         failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
   123         tb.new JavacTask(ToolBox.Mode.EXEC)
   146                 .appendArgs(mainArgs)
   124                 .envVar("CLASSPATH", "bar")
   147                 .appendArgs("ClassPathTest3.java")
   125                 .files("ClassPathTest3.java")
   148                 .set(extVars);
   126                 .run(ToolBox.Expect.FAIL);
   149         ToolBox.executeCommand(failParams);
       
   150 
   127 
   151 //        testJavac success -classpath foo ClassPathTest1.java
   128 //        testJavac success -classpath foo ClassPathTest1.java
   152 
   129         tb.new JavacTask(ToolBox.Mode.EXEC)
   153         commonArgs.clear();
   130                 .envVar("CLASSPATH", "bar")
   154         commonArgs.addAll(mainArgs);
   131                 .classpath("foo")
   155         commonArgs.addAll(Arrays.asList("-cp", "foo"));
   132                 .files("ClassPathTest1.java")
   156 
   133                 .run();
   157         successParams = new ToolBox.AnyToolArgs()
       
   158                 .appendArgs(commonArgs)
       
   159                 .appendArgs("ClassPathTest1.java")
       
   160                 .set(extVars);
       
   161         ToolBox.executeCommand(successParams);
       
   162 
   134 
   163 //        testJavac failure -classpath foo ClassPathTest2.java
   135 //        testJavac failure -classpath foo ClassPathTest2.java
   164         failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
   136         tb.new JavacTask(ToolBox.Mode.EXEC)
   165                 .appendArgs(commonArgs)
   137                 .envVar("CLASSPATH", "bar")
   166                 .appendArgs("ClassPathTest2.java")
   138                 .classpath("foo")
   167                 .set(extVars);
   139                 .files("ClassPathTest2.java")
   168         ToolBox.executeCommand(failParams);
   140                 .run(ToolBox.Expect.FAIL);
   169 
   141 
   170 //        testJavac failure -classpath foo ClassPathTest3.java
   142 //        testJavac failure -classpath foo ClassPathTest3.java
   171         failParams = new ToolBox.AnyToolArgs(ToolBox.Expect.FAIL)
   143         tb.new JavacTask(ToolBox.Mode.EXEC)
   172                 .appendArgs(commonArgs)
   144                 .envVar("CLASSPATH", "bar")
   173                 .appendArgs("ClassPathTest3.java")
   145                 .classpath("foo")
   174                 .set(extVars);
   146                 .files("ClassPathTest3.java")
   175         ToolBox.executeCommand(failParams);
   147                 .run(ToolBox.Expect.FAIL);
   176     }
   148     }
   177 
   149 
   178 }
   150 }