test/tools/launcher/TestHelper.java

Print this page


   1 /*
   2  * Copyright (c) 2008, 2012, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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  */


  49 import java.util.ArrayList;
  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 boolean isSDK = JAVAHOME.endsWith("jre");
  70     static final String javaCmd;
  71     static final String javawCmd;
  72     static final String java64Cmd;
  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     static final boolean isDualMode = isSolaris;
  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) ? new File((new File(JAVAHOME)).getParentFile(), "bin")

 128             : new File(JAVAHOME, "bin");
 129         JAVA_BIN = binDir.getAbsolutePath();


 130         File javaCmdFile = (isWindows)
 131                 ? new File(binDir, "java.exe")
 132                 : new File(binDir, "java");
 133         javaCmd = javaCmdFile.getAbsolutePath();
 134         if (!javaCmdFile.canExecute()) {
 135             throw new RuntimeException("java <" + TestHelper.javaCmd +
 136                     "> must exist and should be executable");
 137         }
 138 
 139         File javacCmdFile = (isWindows)
 140                 ? new File(binDir, "javac.exe")
 141                 : new File(binDir, "javac");
 142         javacCmd = javacCmdFile.getAbsolutePath();
 143 
 144         File jarCmdFile = (isWindows)
 145                 ? new File(binDir, "jar.exe")
 146                 : new File(binDir, "jar");
 147         jarCmd = jarCmdFile.getAbsolutePath();
 148         if (!jarCmdFile.canExecute()) {
 149             throw new RuntimeException("java <" + TestHelper.jarCmd +
 150                     "> must exist and should be executable");
 151         }
 152 
 153         if (isWindows) {
 154             File javawCmdFile = new File(binDir, "javaw.exe");
 155             javawCmd = javawCmdFile.getAbsolutePath();
 156             if (!javawCmdFile.canExecute()) {
 157                 throw new RuntimeException("java <" + javawCmd +
 158                         "> must exist and should be executable");
 159             }
 160         } else {
 161             javawCmd = null;
 162         }
 163 
 164         if (!javacCmdFile.canExecute()) {
 165             throw new RuntimeException("java <" + javacCmd +
 166                     "> must exist and should be executable");
 167         }
 168         if (isSolaris) {
 169             File sparc64BinDir = new File(binDir,isSparc ? "sparcv9" : "amd64");
 170             File java64CmdFile= new File(sparc64BinDir, "java");
 171             if (java64CmdFile.exists() && java64CmdFile.canExecute()) {
 172                 java64Cmd = java64CmdFile.getAbsolutePath();
 173             } else {
 174                 java64Cmd = null;
 175             }
 176         } else {
 177             java64Cmd = null;
 178         }
 179     }
 180     void run(String[] args) throws Exception {
 181         int passed = 0, failed = 0;
 182         final Pattern p = (args != null && args.length > 0)
 183                 ? Pattern.compile(args[0])
 184                 : null;
 185         for (Method m : this.getClass().getDeclaredMethods()) {
 186             boolean selected = (p == null)
 187                     ? m.isAnnotationPresent(Test.class)
 188                     : p.matcher(m.getName()).matches();
 189             if (selected) {
 190                 try {
 191                     m.invoke(this, (Object[]) null);
 192                     System.out.println(m.getName() + ": OK");
 193                     passed++;
 194                     System.out.printf("Passed: %d, Failed: %d, ExitValue: %d%n",
 195                                       passed, failed, testExitValue);
 196                 } catch (Throwable ex) {
 197                     System.out.printf("Test %s failed: %s %n", m, ex.getCause());






 198                     failed++;
 199                 }
 200             }
 201         }
 202         System.out.printf("Total: Passed: %d, Failed %d%n", passed, failed);
 203         if (failed > 0) {
 204             throw new RuntimeException("Tests failed: " + failed);
 205         }
 206         if (passed == 0 && failed == 0) {
 207             throw new AssertionError("No test(s) selected: passed = " +
 208                     passed + ", failed = " + failed + " ??????????");
 209         }
 210     }
 211 
 212     /*
 213      * is a dual mode available in the test jdk
 214      */
 215     static boolean dualModePresent() {
 216         return isDualMode && java64Cmd != null;
 217     }
 218 
 219     /*
 220      * usually the jre/lib/arch-name is the same as os.arch, except for x86.
 221      */
 222     static String getJreArch() {
 223         String arch = System.getProperty("os.arch");
 224         return arch.equals("x86") ? "i386" : arch;
 225     }
 226 
 227     /*
 228      * get the complementary jre arch ie. if sparc then return sparcv9 and
 229      * vice-versa.
 230      */
 231     static String getComplementaryJreArch() {
 232         String arch = System.getProperty("os.arch");
 233         if (arch != null) {
 234             switch (arch) {
 235                 case "sparc":
 236                     return "sparcv9";
 237                 case "sparcv9":
 238                     return "sparc";
 239                 case "x86":
 240                     return "amd64";
 241                 case "amd64":
 242                     return "i386";
 243             }
 244         }
 245         return null;
 246     }
 247 
 248     static File getClassFile(File javaFile) {
 249         String s = javaFile.getAbsolutePath().replace(JAVA_FILE_EXT, CLASS_FILE_EXT);
 250         return new File(s);
 251     }
 252 
 253     static File getJavaFile(File classFile) {
 254         String s = classFile.getAbsolutePath().replace(CLASS_FILE_EXT, JAVA_FILE_EXT);
 255         return new File(s);
 256     }
 257 
 258     static String baseName(File f) {
 259         String s = f.getName();
 260         return s.substring(0, s.indexOf("."));
 261     }
 262 
 263     /*
 264      * A convenience method to create a jar with jar file name and defs
 265      */
 266     static void createJar(File jarName, String... mainDefs)
 267             throws FileNotFoundException{


 606 
 607         boolean notContains(String str) {
 608              for (String x : testOutput) {
 609                 if (x.contains(str)) {
 610                     appendError("string <" + str + "> found");
 611                     return false;
 612                 }
 613             }
 614             return true;
 615         }
 616 
 617         boolean matches(String stringToMatch) {
 618           for (String x : testOutput) {
 619                 if (x.matches(stringToMatch)) {
 620                     return true;
 621                 }
 622             }
 623             appendError("string <" + stringToMatch + "> not found");
 624             return false;
 625         }










 626     }
 627     /**
 628     * Indicates that the annotated method is a test method.
 629     */
 630     @Retention(RetentionPolicy.RUNTIME)
 631     @Target(ElementType.METHOD)
 632     public @interface Test {}
 633 }
   1 /*
   2  * Copyright (c) 2008, 2013, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   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
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  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  */


  49 import java.util.ArrayList;
  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);
 192                     for (Throwable t : ex.getSuppressed()) {
 193                         t.printStackTrace(System.out);
 194                     }
 195                     System.out.println("----end detailed exceptions----");
 196                     failed++;
 197                 }
 198             }
 199         }
 200         System.out.printf("Total: Passed: %d, Failed %d%n", passed, failed);
 201         if (failed > 0) {
 202             throw new RuntimeException("Tests failed: " + failed);
 203         }
 204         if (passed == 0 && failed == 0) {
 205             throw new AssertionError("No test(s) selected: passed = " +
 206                     passed + ", failed = " + failed + " ??????????");
 207         }
 208     }
 209 
 210     /*







 211      * usually the jre/lib/arch-name is the same as os.arch, except for x86.
 212      */
 213     static String getJreArch() {
 214         String arch = System.getProperty("os.arch");
 215         return arch.equals("x86") ? "i386" : arch;
 216     }
 217     static String getArch() {
 218         return System.getProperty("os.arch");















 219     }




 220     static File getClassFile(File javaFile) {
 221         String s = javaFile.getAbsolutePath().replace(JAVA_FILE_EXT, CLASS_FILE_EXT);
 222         return new File(s);
 223     }
 224 
 225     static File getJavaFile(File classFile) {
 226         String s = classFile.getAbsolutePath().replace(CLASS_FILE_EXT, JAVA_FILE_EXT);
 227         return new File(s);
 228     }
 229 
 230     static String baseName(File f) {
 231         String s = f.getName();
 232         return s.substring(0, s.indexOf("."));
 233     }
 234 
 235     /*
 236      * A convenience method to create a jar with jar file name and defs
 237      */
 238     static void createJar(File jarName, String... mainDefs)
 239             throws FileNotFoundException{


 578 
 579         boolean notContains(String str) {
 580              for (String x : testOutput) {
 581                 if (x.contains(str)) {
 582                     appendError("string <" + str + "> found");
 583                     return false;
 584                 }
 585             }
 586             return true;
 587         }
 588 
 589         boolean matches(String stringToMatch) {
 590           for (String x : testOutput) {
 591                 if (x.matches(stringToMatch)) {
 592                     return true;
 593                 }
 594             }
 595             appendError("string <" + stringToMatch + "> not found");
 596             return false;
 597         }
 598 
 599         boolean notMatches(String stringToMatch) {
 600             for (String x : testOutput) {
 601                 if (!x.matches(stringToMatch)) {
 602                     return true;
 603                 }
 604             }
 605             appendError("string <" + stringToMatch + "> found");
 606             return false;
 607         }
 608     }
 609     /**
 610     * Indicates that the annotated method is a test method.
 611     */
 612     @Retention(RetentionPolicy.RUNTIME)
 613     @Target(ElementType.METHOD)
 614     public @interface Test {}
 615 }