< prev index next >

test/jdk/tools/launcher/modules/classpath/JavaClassPathTest.java

Print this page
rev 51638 : [mq]: 8210112


  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 import java.io.BufferedWriter;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.spi.ToolProvider;

  32 


  33 import jdk.test.lib.compiler.CompilerUtils;
  34 import jdk.testlibrary.OutputAnalyzer;
  35 import org.testng.annotations.BeforeTest;
  36 import org.testng.annotations.DataProvider;
  37 import org.testng.annotations.Test;
  38 
  39 import static org.testng.Assert.assertTrue;
  40 import static jdk.testlibrary.ProcessTools.*;
  41 
  42 /**
  43  * @test
  44  * @bug 8168205
  45  * @summary Test the default class path if -Djava.class.path is set
  46  * @library /lib/testlibrary /test/lib
  47  * @modules jdk.compiler
  48  *          jdk.jartool
  49  * @build jdk.test.lib.compiler.CompilerUtils jdk.testlibrary.*
  50  * @run testng JavaClassPathTest
  51  */
  52 
  53 public class JavaClassPathTest {
  54     private static final Path SRC_DIR = Paths.get(System.getProperty("test.src"),
  55                                                   "src");
  56     private static final Path MODS_DIR = Paths.get("mods");
  57     private static final Path LIB_DIR = Paths.get("lib");
  58     private static final String TEST_MODULE = "m";
  59     private static final String TEST_MAIN = "jdk.test.Main";
  60 
  61     @BeforeTest
  62     public void setup() throws Exception {
  63         boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
  64                                                  MODS_DIR.resolve(TEST_MODULE));
  65         assertTrue(compiled, "module " + TEST_MODULE + " did not compile");
  66 
  67         // add the class and a resource to the current working directory
  68         Path file = Paths.get("jdk/test/Main.class");
  69         Files.createDirectories(file.getParent());


 180 
 181         List<String> args = new ArrayList<>();
 182         args.add("-jar");
 183         args.add(jarfile);
 184         args.add(Boolean.toString(false));
 185         args.add(jarfile);
 186 
 187         assertTrue(execute(args).getExitValue() == 0);
 188 
 189         args.clear();
 190         args.add("-cp");
 191         args.add(jarfile);
 192         args.add(TEST_MAIN);
 193         args.add(Boolean.toString(false));
 194         args.add(jarfile);
 195 
 196         assertTrue(execute(args).getExitValue() == 0);
 197     }
 198 
 199     private OutputAnalyzer execute(List<String> options) throws Throwable {
 200         ProcessBuilder pb = createJavaProcessBuilder(


 201             options.stream()
 202                    .map(this::autoQuote)
 203                    .toArray(String[]::new)
 204         );
 205 
 206         Map<String,String> env = pb.environment();
 207         // remove CLASSPATH environment variable
 208         String value = env.remove("CLASSPATH");
 209         return executeCommand(pb)
 210                     .outputTo(System.out)
 211                     .errorTo(System.out);
 212     }
 213 
 214     private static final boolean IS_WINDOWS
 215         = System.getProperty("os.name").startsWith("Windows");
 216 
 217     /*
 218      * Autoquote empty string argument on Windows
 219      */
 220     private String autoQuote(String arg) {
 221         if (IS_WINDOWS && arg.isEmpty()) {
 222             return "\"\"";
 223         }
 224         return arg;
 225     }
 226 }


  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 import java.io.BufferedWriter;
  25 import java.nio.file.Files;
  26 import java.nio.file.Path;
  27 import java.nio.file.Paths;
  28 import java.util.ArrayList;
  29 import java.util.List;
  30 import java.util.Map;
  31 import java.util.spi.ToolProvider;
  32 import java.util.stream.Stream;
  33 
  34 import jdk.test.lib.JDKToolFinder;
  35 import jdk.test.lib.Platform;
  36 import jdk.test.lib.compiler.CompilerUtils;
  37 import jdk.test.lib.process.OutputAnalyzer;
  38 import org.testng.annotations.BeforeTest;
  39 import org.testng.annotations.DataProvider;
  40 import org.testng.annotations.Test;
  41 
  42 import static org.testng.Assert.assertTrue;
  43 import static jdk.test.lib.process.ProcessTools.*;
  44 
  45 /**
  46  * @test
  47  * @bug 8168205
  48  * @summary Test the default class path if -Djava.class.path is set
  49  * @library /test/lib
  50  * @modules jdk.compiler
  51  *          jdk.jartool
  52  * @build jdk.test.lib.compiler.CompilerUtils
  53  * @run testng JavaClassPathTest
  54  */
  55 
  56 public class JavaClassPathTest {
  57     private static final Path SRC_DIR = Paths.get(System.getProperty("test.src"),
  58                                                   "src");
  59     private static final Path MODS_DIR = Paths.get("mods");
  60     private static final Path LIB_DIR = Paths.get("lib");
  61     private static final String TEST_MODULE = "m";
  62     private static final String TEST_MAIN = "jdk.test.Main";
  63 
  64     @BeforeTest
  65     public void setup() throws Exception {
  66         boolean compiled = CompilerUtils.compile(SRC_DIR.resolve(TEST_MODULE),
  67                                                  MODS_DIR.resolve(TEST_MODULE));
  68         assertTrue(compiled, "module " + TEST_MODULE + " did not compile");
  69 
  70         // add the class and a resource to the current working directory
  71         Path file = Paths.get("jdk/test/Main.class");
  72         Files.createDirectories(file.getParent());


 183 
 184         List<String> args = new ArrayList<>();
 185         args.add("-jar");
 186         args.add(jarfile);
 187         args.add(Boolean.toString(false));
 188         args.add(jarfile);
 189 
 190         assertTrue(execute(args).getExitValue() == 0);
 191 
 192         args.clear();
 193         args.add("-cp");
 194         args.add(jarfile);
 195         args.add(TEST_MAIN);
 196         args.add(Boolean.toString(false));
 197         args.add(jarfile);
 198 
 199         assertTrue(execute(args).getExitValue() == 0);
 200     }
 201 
 202     private OutputAnalyzer execute(List<String> options) throws Throwable {
 203         // can't use ProcessTools.createJavaProcessBuilder as it always adds -cp
 204         ProcessBuilder pb = new ProcessBuilder(
 205                 Stream.concat(Stream.of(JDKToolFinder.getTestJDKTool("java")),
 206                               options.stream()
 207                                      .map(this::autoQuote))
 208                       .toArray(String[]::new)
 209         );
 210 
 211         Map<String,String> env = pb.environment();
 212         // remove CLASSPATH environment variable
 213         env.remove("CLASSPATH");
 214         return executeCommand(pb)
 215                     .outputTo(System.out)
 216                     .errorTo(System.out);
 217     }
 218 



 219     /*
 220      * Autoquote empty string argument on Windows
 221      */
 222     private String autoQuote(String arg) {
 223         if (Platform.isWindows() && arg.isEmpty()) {
 224             return "\"\"";
 225         }
 226         return arg;
 227     }
 228 }
< prev index next >