jdk/test/tools/pack200/Utils.java
changeset 27565 729f9700483a
parent 18594 b6a3c9f71ac8
child 28255 ddb0157601ed
equal deleted inserted replaced
27564:eaaa79b68cd5 27565:729f9700483a
     1 /*
     1 /*
     2  * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 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.
    52  *
    52  *
    53  * @author ksrini
    53  * @author ksrini
    54  */
    54  */
    55 
    55 
    56 /*
    56 /*
    57  * This class contains all the commonly used utilities used by various tests
    57  * This class contains the commonly used utilities.
    58  * in this directory.
       
    59  */
    58  */
    60 class Utils {
    59 class Utils {
    61     static final String JavaHome = System.getProperty("test.java",
    60     static final String JavaHome = System.getProperty("test.java",
    62             System.getProperty("java.home"));
    61             System.getProperty("java.home"));
    63     static final boolean IsWindows =
    62     static final boolean IsWindows =
    76     static final String VERIFIER_DIR_NAME = "pack200-verifier";
    75     static final String VERIFIER_DIR_NAME = "pack200-verifier";
    77     static final File   VerifierJar = new File(VERIFIER_DIR_NAME + JAR_FILE_EXT);
    76     static final File   VerifierJar = new File(VERIFIER_DIR_NAME + JAR_FILE_EXT);
    78     static final File   XCLASSES = new File("xclasses");
    77     static final File   XCLASSES = new File("xclasses");
    79 
    78 
    80     private Utils() {} // all static
    79     private Utils() {} // all static
    81 
       
    82     static {
       
    83         if (!JavaHome.endsWith("jre")) {
       
    84             throw new RuntimeException("Error: requires an SDK to run");
       
    85         }
       
    86     }
       
    87 
    80 
    88     private static void init() throws IOException {
    81     private static void init() throws IOException {
    89         if (VerifierJar.exists()) {
    82         if (VerifierJar.exists()) {
    90             return;
    83             return;
    91         }
    84         }
   135     static void doCompareVerify(File reference, File specimen) throws IOException {
   128     static void doCompareVerify(File reference, File specimen) throws IOException {
   136         init();
   129         init();
   137         List<String> cmds = new ArrayList<String>();
   130         List<String> cmds = new ArrayList<String>();
   138         cmds.add(getJavaCmd());
   131         cmds.add(getJavaCmd());
   139         cmds.add("-cp");
   132         cmds.add("-cp");
   140         cmds.add(Utils.locateJar("tools.jar") +
   133         cmds.add(VerifierJar.getName());
   141                 System.getProperty("path.separator") + VerifierJar.getName());
       
   142         cmds.add("sun.tools.pack.verify.Main");
   134         cmds.add("sun.tools.pack.verify.Main");
   143         cmds.add(reference.getAbsolutePath());
   135         cmds.add(reference.getAbsolutePath());
   144         cmds.add(specimen.getAbsolutePath());
   136         cmds.add(specimen.getAbsolutePath());
   145         cmds.add("-O");
   137         cmds.add("-O");
   146         runExec(cmds);
   138         runExec(cmds);
   150             throws IOException {
   142             throws IOException {
   151         init();
   143         init();
   152         List<String> cmds = new ArrayList<String>();
   144         List<String> cmds = new ArrayList<String>();
   153         cmds.add(getJavaCmd());
   145         cmds.add(getJavaCmd());
   154         cmds.add("-cp");
   146         cmds.add("-cp");
   155         cmds.add(Utils.locateJar("tools.jar")
   147         cmds.add(VerifierJar.getName());
   156                 + System.getProperty("path.separator") + VerifierJar.getName());
       
   157         cmds.add("sun.tools.pack.verify.Main");
   148         cmds.add("sun.tools.pack.verify.Main");
   158         cmds.add(reference.getName());
   149         cmds.add(reference.getName());
   159         cmds.add(specimen.getName());
   150         cmds.add(specimen.getName());
   160         cmds.add("-O");
   151         cmds.add("-O");
   161         cmds.add("-b");
   152         cmds.add("-b");
   341         } catch (IOException ignore) {
   332         } catch (IOException ignore) {
   342         }
   333         }
   343     }
   334     }
   344 
   335 
   345     static void compiler(String... javacCmds) {
   336     static void compiler(String... javacCmds) {
   346         if (com.sun.tools.javac.Main.compile(javacCmds) != 0) {
   337         List<String> cmdList = new ArrayList<>();
   347             throw new RuntimeException("compilation failed");
   338         cmdList.add(getJavacCmd());
   348         }
   339         for (String x : javacCmds) {
       
   340             cmdList.add(x);
       
   341         }
       
   342         runExec(cmdList);
   349     }
   343     }
   350 
   344 
   351     static void jar(String... jargs) {
   345     static void jar(String... jargs) {
   352         sun.tools.jar.Main jarTool =
   346         List<String> cmdList = new ArrayList<>();
   353                 new sun.tools.jar.Main(System.out, System.err, "jartool");
   347         cmdList.add(getJarCmd());
   354         if (!jarTool.run(jargs)) {
   348         for (String x : jargs) {
   355             throw new RuntimeException("jar command failed");
   349             cmdList.add(x);
   356         }
   350         }
       
   351         runExec(cmdList);
   357     }
   352     }
   358 
   353 
   359     static void testWithRepack(File inFile, String... repackOpts) throws IOException {
   354     static void testWithRepack(File inFile, String... repackOpts) throws IOException {
   360         File cwd = new File(".");
   355         File cwd = new File(".");
   361         // pack using --repack in native mode
   356         // pack using --repack in native mode
   526 
   521 
   527     static String getJavaCmd() {
   522     static String getJavaCmd() {
   528         return getAjavaCmd("java");
   523         return getAjavaCmd("java");
   529     }
   524     }
   530 
   525 
       
   526     static String getJavacCmd() {
       
   527         return getAjavaCmd("javac");
       
   528     }
       
   529 
       
   530     static String getJarCmd() {
       
   531         return getAjavaCmd("jar");
       
   532     }
       
   533 
       
   534     static String getJimageCmd() {
       
   535         return getAjavaCmd("jimage");
       
   536     }
       
   537 
   531     static String getAjavaCmd(String cmdStr) {
   538     static String getAjavaCmd(String cmdStr) {
   532         File binDir = new File(JavaHome, "bin");
   539         File binDir = new File(JavaHome, "bin");
   533         File unpack200File = IsWindows
   540         File unpack200File = IsWindows
   534                 ? new File(binDir, cmdStr + ".exe")
   541                 ? new File(binDir, cmdStr + ".exe")
   535                 : new File(binDir, cmdStr);
   542                 : new File(binDir, cmdStr);
   540                     cmd + " exists and is executable");
   547                     cmd + " exists and is executable");
   541         }
   548         }
   542         return cmd;
   549         return cmd;
   543     }
   550     }
   544 
   551 
       
   552     static File createRtJar() throws IOException {
       
   553         File LibDir = new File(JavaHome, "lib");
       
   554         File ModuleDir = new File(LibDir, "modules");
       
   555         File BootModules = new File(ModuleDir, "bootmodules.jimage");
       
   556         List<String> cmdList = new ArrayList<>();
       
   557         cmdList.add(getJimageCmd());
       
   558         cmdList.add("extract");
       
   559         cmdList.add(BootModules.getAbsolutePath());
       
   560         cmdList.add("--dir");
       
   561         cmdList.add("out");
       
   562         runExec(cmdList);
       
   563 
       
   564         File rtJar = new File("rt.jar");
       
   565         cmdList.clear();
       
   566         cmdList.add(getJarCmd());
       
   567         cmdList.add("cvf");
       
   568         cmdList.add(rtJar.getName());
       
   569         cmdList.add("-C");
       
   570         cmdList.add("out");
       
   571         cmdList.add(".");
       
   572         runExec(cmdList);
       
   573 
       
   574         recursiveDelete(new File("out"));
       
   575         return rtJar;
       
   576     }
   545     private static List<File> locaterCache = null;
   577     private static List<File> locaterCache = null;
   546     // search the source dir and jdk dir for requested file and returns
   578     // search the source dir and jdk dir for requested file and returns
   547     // the first location it finds.
   579     // the first location it finds.
   548     static File locateJar(String name) {
   580     static File locateJar(String name) {
   549         try {
   581         try {