test/tools/launcher/TestHelper.java

Print this page




  50 import java.util.List;
  51 import java.util.Locale;
  52 import java.util.Map;
  53 import javax.tools.JavaCompiler;
  54 import javax.tools.ToolProvider;
  55 
  56 import static java.nio.file.StandardCopyOption.*;
  57 import static java.nio.file.StandardOpenOption.*;
  58 
  59 /**
  60  * This class provides some common utilities for the launcher tests.
  61  */
  62 public class TestHelper {
  63     // commonly used jtreg constants
  64     static final File TEST_CLASSES_DIR;
  65     static final File TEST_SOURCES_DIR;
  66 
  67     static final String JAVAHOME = System.getProperty("java.home");
  68     static final String JAVA_BIN;
  69     static final String JAVA_JRE_BIN;


  70     static final boolean isSDK = JAVAHOME.endsWith("jre");
  71     static final String javaCmd;
  72     static final String javawCmd;
  73     static final String javacCmd;
  74     static final String jarCmd;


  75 
  76     static final JavaCompiler compiler;
  77 
  78     static final boolean debug = Boolean.getBoolean("TestHelper.Debug");
  79     static final boolean isWindows =
  80             System.getProperty("os.name", "unknown").startsWith("Windows");
  81     static final boolean isMacOSX =
  82             System.getProperty("os.name", "unknown").contains("OS X");
  83     static final boolean is64Bit =
  84             System.getProperty("sun.arch.data.model").equals("64");
  85     static final boolean is32Bit =
  86             System.getProperty("sun.arch.data.model").equals("32");
  87     static final boolean isSolaris =
  88             System.getProperty("os.name", "unknown").startsWith("SunOS");
  89     static final boolean isLinux =
  90             System.getProperty("os.name", "unknown").startsWith("Linux");

  91 
  92     static final boolean isSparc = System.getProperty("os.arch").startsWith("sparc");
  93 
  94     // make a note of the golden default locale
  95     static final Locale DefaultLocale = Locale.getDefault();
  96 
  97     static final String JAVA_FILE_EXT  = ".java";
  98     static final String CLASS_FILE_EXT = ".class";
  99     static final String JAR_FILE_EXT   = ".jar";
 100     static final String EXE_FILE_EXT   = ".exe";
 101     static final String JLDEBUG_KEY     = "_JAVA_LAUNCHER_DEBUG";
 102     static final String EXPECTED_MARKER = "TRACER_MARKER:About to EXEC";
 103     static final String TEST_PREFIX     = "###TestError###: ";
 104 
 105     static int testExitValue = 0;
 106 
 107     static {
 108         String tmp = System.getProperty("test.classes", null);
 109         if (tmp == null) {
 110             throw new Error("property test.classes not defined ??");
 111         }
 112         TEST_CLASSES_DIR = new File(tmp).getAbsoluteFile();
 113 
 114         tmp = System.getProperty("test.src", null);
 115         if (tmp == null) {
 116             throw new Error("property test.src not defined ??");
 117         }
 118         TEST_SOURCES_DIR = new File(tmp).getAbsoluteFile();
 119 
 120         if (is64Bit && is32Bit) {
 121             throw new RuntimeException("arch model cannot be both 32 and 64 bit");
 122         }
 123         if (!is64Bit && !is32Bit) {
 124             throw new RuntimeException("arch model is not 32 or 64 bit ?");
 125         }
 126         compiler = ToolProvider.getSystemJavaCompiler();

 127         File binDir = (isSDK)
 128                 ? new File((new File(JAVAHOME)).getParentFile(), "bin")
 129                 : new File(JAVAHOME, "bin");
 130         JAVA_BIN = binDir.getAbsolutePath();
 131         JAVA_JRE_BIN = new File((new File(JAVAHOME)).getParentFile(),
 132                         (isSDK) ? "jre/bin" : "bin").getAbsolutePath();








 133         File javaCmdFile = (isWindows)
 134                 ? new File(binDir, "java.exe")
 135                 : new File(binDir, "java");
 136         javaCmd = javaCmdFile.getAbsolutePath();
 137         if (!javaCmdFile.canExecute()) {
 138             throw new RuntimeException("java <" + TestHelper.javaCmd +
 139                     "> must exist and should be executable");
 140         }
 141 
 142         File javacCmdFile = (isWindows)
 143                 ? new File(binDir, "javac.exe")
 144                 : new File(binDir, "javac");
 145         javacCmd = javacCmdFile.getAbsolutePath();
 146 
 147         File jarCmdFile = (isWindows)
 148                 ? new File(binDir, "jar.exe")
 149                 : new File(binDir, "jar");
 150         jarCmd = jarCmdFile.getAbsolutePath();
 151         if (!jarCmdFile.canExecute()) {
 152             throw new RuntimeException("java <" + TestHelper.jarCmd +
 153                     "> must exist and should be executable");
 154         }
 155 
 156         if (isWindows) {
 157             File javawCmdFile = new File(binDir, "javaw.exe");
 158             javawCmd = javawCmdFile.getAbsolutePath();
 159             if (!javawCmdFile.canExecute()) {
 160                 throw new RuntimeException("java <" + javawCmd +
 161                         "> must exist and should be executable");
 162             }
 163         } else {
 164             javawCmd = null;
 165         }
 166 
 167         if (!javacCmdFile.canExecute()) {
 168             throw new RuntimeException("java <" + javacCmd +
 169                     "> must exist and should be executable");
 170         }



 171     }












 172     void run(String[] args) throws Exception {
 173         int passed = 0, failed = 0;
 174         final Pattern p = (args != null && args.length > 0)
 175                 ? Pattern.compile(args[0])
 176                 : null;
 177         for (Method m : this.getClass().getDeclaredMethods()) {
 178             boolean selected = (p == null)
 179                     ? m.isAnnotationPresent(Test.class)
 180                     : p.matcher(m.getName()).matches();
 181             if (selected) {
 182                 try {
 183                     m.invoke(this, (Object[]) null);
 184                     System.out.println(m.getName() + ": OK");
 185                     passed++;
 186                     System.out.printf("Passed: %d, Failed: %d, ExitValue: %d%n",
 187                                       passed, failed, testExitValue);
 188                 } catch (Throwable ex) {
 189                     System.out.printf("Test %s failed: %s %n", m, ex);
 190                     System.out.println("----begin detailed exceptions----");
 191                     ex.printStackTrace(System.out);




  50 import java.util.List;
  51 import java.util.Locale;
  52 import java.util.Map;
  53 import javax.tools.JavaCompiler;
  54 import javax.tools.ToolProvider;
  55 
  56 import static java.nio.file.StandardCopyOption.*;
  57 import static java.nio.file.StandardOpenOption.*;
  58 
  59 /**
  60  * This class provides some common utilities for the launcher tests.
  61  */
  62 public class TestHelper {
  63     // commonly used jtreg constants
  64     static final File TEST_CLASSES_DIR;
  65     static final File TEST_SOURCES_DIR;
  66 
  67     static final String JAVAHOME = System.getProperty("java.home");
  68     static final String JAVA_BIN;
  69     static final String JAVA_JRE_BIN;
  70     static final String JAVA_LIB;
  71     static final String JAVA_JRE_LIB;
  72     static final boolean isSDK = JAVAHOME.endsWith("jre");
  73     static final String javaCmd;
  74     static final String javawCmd;
  75     static final String javacCmd;
  76     static final String jarCmd;
  77     static final boolean haveServerVM;
  78     static final boolean haveClientVM;
  79 
  80     static final JavaCompiler compiler;
  81 
  82     static final boolean debug = Boolean.getBoolean("TestHelper.Debug");
  83     static final boolean isWindows =
  84             System.getProperty("os.name", "unknown").startsWith("Windows");
  85     static final boolean isMacOSX =
  86             System.getProperty("os.name", "unknown").contains("OS X");
  87     static final boolean is64Bit =
  88             System.getProperty("sun.arch.data.model").equals("64");
  89     static final boolean is32Bit =
  90             System.getProperty("sun.arch.data.model").equals("32");
  91     static final boolean isSolaris =
  92             System.getProperty("os.name", "unknown").startsWith("SunOS");
  93     static final boolean isLinux =
  94             System.getProperty("os.name", "unknown").startsWith("Linux");
  95     static final String JVM_DLL = isWindows ? "jvm.dll" : "libjvm.so";
  96 
  97     static final boolean isSparc = System.getProperty("os.arch").startsWith("sparc");
  98 
  99     // make a note of the golden default locale
 100     static final Locale DefaultLocale = Locale.getDefault();
 101 
 102     static final String JAVA_FILE_EXT  = ".java";
 103     static final String CLASS_FILE_EXT = ".class";
 104     static final String JAR_FILE_EXT   = ".jar";
 105     static final String EXE_FILE_EXT   = ".exe";
 106     static final String JLDEBUG_KEY     = "_JAVA_LAUNCHER_DEBUG";
 107     static final String EXPECTED_MARKER = "TRACER_MARKER:About to EXEC";
 108     static final String TEST_PREFIX     = "###TestError###: ";
 109 
 110     static int testExitValue = 0;
 111 
 112     static {
 113         String tmp = System.getProperty("test.classes", null);
 114         if (tmp == null) {
 115             throw new Error("property test.classes not defined ??");
 116         }
 117         TEST_CLASSES_DIR = new File(tmp).getAbsoluteFile();
 118 
 119         tmp = System.getProperty("test.src", null);
 120         if (tmp == null) {
 121             throw new Error("property test.src not defined ??");
 122         }
 123         TEST_SOURCES_DIR = new File(tmp).getAbsoluteFile();
 124 
 125         if (is64Bit && is32Bit) {
 126             throw new RuntimeException("arch model cannot be both 32 and 64 bit");
 127         }
 128         if (!is64Bit && !is32Bit) {
 129             throw new RuntimeException("arch model is not 32 or 64 bit ?");
 130         }
 131         compiler = ToolProvider.getSystemJavaCompiler();
 132 
 133         File binDir = (isSDK)
 134                 ? new File((new File(JAVAHOME)).getParentFile(), "bin")
 135                 : new File(JAVAHOME, "bin");
 136         JAVA_BIN = binDir.getAbsolutePath();
 137         JAVA_JRE_BIN = new File((new File(JAVAHOME)).getParentFile(),
 138                         (isSDK) ? "jre/bin" : "bin").getAbsolutePath();
 139 
 140         File libDir = (isSDK)
 141                 ? new File((new File(JAVAHOME)).getParentFile(), "lib")
 142                 : new File(JAVAHOME, "lib");
 143         JAVA_LIB = libDir.getAbsolutePath();
 144         JAVA_JRE_LIB = new File((new File(JAVAHOME)).getParentFile(),
 145                         (isSDK) ? "jre/lib" : "lib").getAbsolutePath();
 146 
 147         File javaCmdFile = (isWindows)
 148                 ? new File(binDir, "java.exe")
 149                 : new File(binDir, "java");
 150         javaCmd = javaCmdFile.getAbsolutePath();
 151         if (!javaCmdFile.canExecute()) {
 152             throw new RuntimeException("java <" + TestHelper.javaCmd +
 153                     "> must exist and should be executable");
 154         }
 155 
 156         File javacCmdFile = (isWindows)
 157                 ? new File(binDir, "javac.exe")
 158                 : new File(binDir, "javac");
 159         javacCmd = javacCmdFile.getAbsolutePath();
 160 
 161         File jarCmdFile = (isWindows)
 162                 ? new File(binDir, "jar.exe")
 163                 : new File(binDir, "jar");
 164         jarCmd = jarCmdFile.getAbsolutePath();
 165         if (!jarCmdFile.canExecute()) {
 166             throw new RuntimeException("java <" + TestHelper.jarCmd +
 167                     "> must exist and should be executable");
 168         }
 169 
 170         if (isWindows) {
 171             File javawCmdFile = new File(binDir, "javaw.exe");
 172             javawCmd = javawCmdFile.getAbsolutePath();
 173             if (!javawCmdFile.canExecute()) {
 174                 throw new RuntimeException("java <" + javawCmd +
 175                         "> must exist and should be executable");
 176             }
 177         } else {
 178             javawCmd = null;
 179         }
 180 
 181         if (!javacCmdFile.canExecute()) {
 182             throw new RuntimeException("java <" + javacCmd +
 183                     "> must exist and should be executable");
 184         }
 185 
 186         haveClientVM = haveVmVariant("client");
 187         haveServerVM = haveVmVariant("server");
 188     }
 189     private static boolean haveVmVariant(String type) {
 190         if (isWindows) {
 191             File vmDir = new File(JAVA_JRE_BIN, type);
 192             File jvmFile = new File(vmDir, JVM_DLL);
 193             return jvmFile.exists();
 194         } else {
 195             File vmDir = new File(JAVA_JRE_LIB, type);
 196             File vmArchDir = new File(vmDir, getJreArch());
 197             File jvmFile = new File(vmArchDir, JVM_DLL);
 198             return jvmFile.exists();
 199         }
 200     }
 201     void run(String[] args) throws Exception {
 202         int passed = 0, failed = 0;
 203         final Pattern p = (args != null && args.length > 0)
 204                 ? Pattern.compile(args[0])
 205                 : null;
 206         for (Method m : this.getClass().getDeclaredMethods()) {
 207             boolean selected = (p == null)
 208                     ? m.isAnnotationPresent(Test.class)
 209                     : p.matcher(m.getName()).matches();
 210             if (selected) {
 211                 try {
 212                     m.invoke(this, (Object[]) null);
 213                     System.out.println(m.getName() + ": OK");
 214                     passed++;
 215                     System.out.printf("Passed: %d, Failed: %d, ExitValue: %d%n",
 216                                       passed, failed, testExitValue);
 217                 } catch (Throwable ex) {
 218                     System.out.printf("Test %s failed: %s %n", m, ex);
 219                     System.out.println("----begin detailed exceptions----");
 220                     ex.printStackTrace(System.out);