jdk/test/tools/launcher/VersionCheck.java
changeset 12301 201cef0a3f12
parent 10053 43930b0d0564
child 14998 b9a99aac309e
equal deleted inserted replaced
12300:c795ca195227 12301:201cef0a3f12
     1 /*
     1 /*
     2  * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2007, 2012, 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.
    22  */
    22  */
    23 
    23 
    24 /**
    24 /**
    25  * @test
    25  * @test
    26  * @bug 6545058 6611182
    26  * @bug 6545058 6611182
    27  * @summary validate and test -version, -fullversion, and internal
    27  * @summary validate and test -version, -fullversion, and internal, as well as
       
    28  *          sanity checks if a tool can be launched.
    28  * @compile VersionCheck.java
    29  * @compile VersionCheck.java
    29  * @run main VersionCheck
    30  * @run main VersionCheck
    30  */
    31  */
    31 
    32 
    32 import java.lang.*;
       
    33 import java.io.File;
    33 import java.io.File;
    34 import java.io.BufferedReader;
    34 import java.io.FileFilter;
    35 import java.io.InputStream;
       
    36 import java.io.InputStreamReader;
       
    37 import java.io.Reader;
       
    38 import java.util.Map;
    35 import java.util.Map;
    39 import java.util.ArrayList;
    36 import java.util.ArrayList;
       
    37 import java.util.HashMap;
    40 import java.util.List;
    38 import java.util.List;
    41 import java.util.StringTokenizer;
    39 
    42 
    40 public class VersionCheck extends TestHelper {
    43 public class VersionCheck {
    41 
    44 
    42     // tools that do not accept -J-option
    45     private static String javaBin;
    43     static final String[] BLACKLIST_JOPTION = {
    46 
    44         "controlpanel",
    47     // A known set of programs we know for sure will behave correctly.
    45         "java-rmi",
    48     private static String[] programs = new String[]{
    46         "java-rmi.cgi",
       
    47         "java",
       
    48         "javaw",
       
    49         "javaws",
       
    50         "jcontrol",
       
    51         "jvisualvm",
       
    52         "packager",
       
    53         "unpack200",
       
    54         "wsimport"
       
    55     };
       
    56 
       
    57     // tools that do not accept -version
       
    58     static final String[] BLACKLIST_VERSION = {
    49         "appletviewer",
    59         "appletviewer",
       
    60         "controlpanel",
    50         "extcheck",
    61         "extcheck",
    51         "idlj",
       
    52         "jar",
    62         "jar",
    53         "jarsigner",
    63         "jarsigner",
    54         "javac",
    64         "java-rmi",
       
    65         "java-rmi.cgi",
    55         "javadoc",
    66         "javadoc",
    56         "javah",
    67         "javaws",
    57         "javap",
    68         "jcmd",
    58         "jconsole",
    69         "jconsole",
    59         "jdb",
    70         "jcontrol",
    60         "jhat",
       
    61         "jinfo",
    71         "jinfo",
    62         "jmap",
    72         "jmap",
    63         "jps",
    73         "jps",
       
    74         "jrunscript",
       
    75         "jsadebugd",
    64         "jstack",
    76         "jstack",
    65         "jstat",
    77         "jstat",
    66         "jstatd",
    78         "jstatd",
       
    79         "jvisualvm",
    67         "keytool",
    80         "keytool",
       
    81         "kinit",
       
    82         "klist",
       
    83         "ktab",
    68         "native2ascii",
    84         "native2ascii",
    69         "orbd",
    85         "orbd",
    70         "pack200",
    86         "pack200",
       
    87         "packager",
    71         "policytool",
    88         "policytool",
    72         "rmic",
    89         "rmic",
    73         "rmid",
    90         "rmid",
    74         "rmiregistry",
    91         "rmiregistry",
    75         "schemagen",
    92         "schemagen", // returns error code 127
    76         "serialver",
    93         "serialver",
    77         "servertool",
    94         "servertool",
    78         "tnameserv",
    95         "tnameserv",
       
    96         "unpack200",
    79         "wsgen",
    97         "wsgen",
    80         "wsimport",
    98         "wsimport",
    81         "xjc"
    99         "xjc"
    82     };
   100     };
    83 
   101 
    84     // expected reference strings
   102     // expected reference strings
    85     static String refVersion;
   103     static String refVersion;
    86     static String refFullVersion;
   104     static String refFullVersion;
    87 
   105 
    88     private static List<String> getProcessStreamAsList(boolean javaDebug,
       
    89                                                        String... argv) {
       
    90         List<String> out = new ArrayList<String>();
       
    91         List<String> javaCmds = new ArrayList<String>();
       
    92 
       
    93         String prog = javaBin + File.separator + argv[0];
       
    94         if (System.getProperty("os.name").startsWith("Windows")) {
       
    95             prog = prog.concat(".exe");
       
    96         }
       
    97         javaCmds.add(prog);
       
    98         for (int i = 1; i < argv.length; i++) {
       
    99             javaCmds.add(argv[i]);
       
   100         }
       
   101 
       
   102         ProcessBuilder pb = new ProcessBuilder(javaCmds);
       
   103         Map<String, String> env = pb.environment();
       
   104         if (javaDebug) {
       
   105             env.put("_JAVA_LAUNCHER_DEBUG", "true");
       
   106         }
       
   107         try {
       
   108             Process p = pb.start();
       
   109             BufferedReader r = (javaDebug) ?
       
   110                 new BufferedReader(new InputStreamReader(p.getInputStream())) :
       
   111                 new BufferedReader(new InputStreamReader(p.getErrorStream())) ;
       
   112 
       
   113             String s = r.readLine();
       
   114             while (s != null) {
       
   115                 out.add(s.trim());
       
   116                 s = r.readLine();
       
   117             }
       
   118             p.waitFor();
       
   119             p.destroy();
       
   120         } catch (Exception ex) {
       
   121             ex.printStackTrace();
       
   122             throw new RuntimeException(ex.getMessage());
       
   123         }
       
   124         return out;
       
   125     }
       
   126 
       
   127     static String getVersion(String... argv) {
   106     static String getVersion(String... argv) {
   128         List<String> alist = getProcessStreamAsList(false, argv);
   107         TestHelper.TestResult tr = doExec(argv);
   129         if (alist.size() == 0) {
       
   130             throw new AssertionError("unexpected process returned null");
       
   131         }
       
   132         StringBuilder out = new StringBuilder();
   108         StringBuilder out = new StringBuilder();
   133         // remove the HotSpot line
   109         // remove the HotSpot line
   134         for (String x : alist) {
   110         for (String x : tr.testOutput) {
   135             if (!x.matches(".*Client.*VM.*|.*Server.*VM.*")) {
   111             if (!x.matches(".*Client.*VM.*|.*Server.*VM.*")) {
   136                 out = out.append(x + "\n");
   112                 out = out.append(x + "\n");
   137             }
   113             }
   138         }
   114         }
   139         return out.toString();
   115         return out.toString();
   140     }
   116     }
   141 
   117 
   142     static boolean compareVersionStrings() {
   118     /*
       
   119      * this tests if the tool can take a version string and returns
       
   120      * a 0 exit code, it is not possible to validate the contents
       
   121      * of the -version output as they are inconsistent.
       
   122      */
       
   123     static boolean testToolVersion() {
       
   124         TestResult tr = null;
       
   125         TestHelper.testExitValue = 0;
       
   126         for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) {
       
   127             String x = f.getAbsolutePath();
       
   128             System.out.println("Testing (-version): " + x);
       
   129             tr = doExec(x, "-version");
       
   130             tr.checkPositive();
       
   131         }
       
   132         return TestHelper.testExitValue == 0;
       
   133     }
       
   134 
       
   135     static boolean compareJVersionStrings() {
   143         int failcount = 0;
   136         int failcount = 0;
   144         for (String x : programs) {
   137         for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) {
       
   138             String x = f.getAbsolutePath();
       
   139             System.out.println("Testing (-J-version): " + x);
   145             String testStr;
   140             String testStr;
   146 
   141 
   147             testStr = getVersion(x, "-J-version");
   142             testStr = getVersion(x, "-J-version");
   148             if (refVersion.compareTo(testStr) != 0) {
   143             if (refVersion.compareTo(testStr) != 0) {
   149                 failcount++;
   144                 failcount++;
   183         String jdkBuild = vStr[vStr.length - 1];
   178         String jdkBuild = vStr[vStr.length - 1];
   184 
   179 
   185         String expectedDotVersion = "dotversion:" + jdkMajor + "." + jdkMinor;
   180         String expectedDotVersion = "dotversion:" + jdkMajor + "." + jdkMinor;
   186         String expectedFullVersion = "fullversion:" + bStr;
   181         String expectedFullVersion = "fullversion:" + bStr;
   187 
   182 
   188         List<String> alist = getProcessStreamAsList(true, "java", "-version");
   183         Map<String, String> envMap = new HashMap<>();
   189 
   184         envMap.put(TestHelper.JLDEBUG_KEY, "true");
       
   185         TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version");
       
   186         List<String> alist = new ArrayList<>();
       
   187         alist.addAll(tr.testOutput);
       
   188         for (String x : tr.testOutput) {
       
   189             alist.add(x.trim());
       
   190         }
   190         if (!alist.contains(expectedDotVersion)) {
   191         if (!alist.contains(expectedDotVersion)) {
   191             System.out.println("Error: could not find " + expectedDotVersion);
   192             System.out.println("Error: could not find " + expectedDotVersion);
   192             failcount++;
   193             failcount++;
   193         }
   194         }
   194 
   195 
   200         return failcount == 0;
   201         return failcount == 0;
   201     }
   202     }
   202 
   203 
   203     // Initialize
   204     // Initialize
   204     static void init() {
   205     static void init() {
   205         String javaHome = System.getProperty("java.home");
   206         refVersion = getVersion(javaCmd, "-version");
   206         if (javaHome.endsWith("jre")) {
   207         refFullVersion = getVersion(javaCmd, "-fullversion");
   207             javaHome = new File(javaHome).getParent();
       
   208         }
       
   209         javaBin = javaHome + File.separator + "bin";
       
   210         refVersion = getVersion("java", "-version");
       
   211         refFullVersion = getVersion("java", "-fullversion");
       
   212     }
   208     }
   213 
   209 
   214     public static void main(String[] args) {
   210     public static void main(String[] args) {
   215         init();
   211         init();
   216         if (compareVersionStrings() && compareInternalStrings()) {
   212         if (compareJVersionStrings() &&
       
   213                 compareInternalStrings() &&
       
   214                 testToolVersion()) {
   217             System.out.println("All Version string comparisons: PASS");
   215             System.out.println("All Version string comparisons: PASS");
   218         } else {
   216         } else {
   219             throw new AssertionError("Some tests failed");
   217             throw new AssertionError("Some tests failed");
   220         }
   218         }
   221     }
   219     }
       
   220 
       
   221     static class ToolFilter implements FileFilter {
       
   222         final Iterable<String> exclude ;
       
   223         protected ToolFilter(String... exclude) {
       
   224             List<String> tlist = new ArrayList<>();
       
   225             this.exclude = tlist;
       
   226             for (String x : exclude) {
       
   227                 String str = x + ((isWindows) ? EXE_FILE_EXT : "");
       
   228                 tlist.add(str.toLowerCase());
       
   229             }
       
   230         }
       
   231         @Override
       
   232         public boolean accept(File pathname) {
       
   233             if (!pathname.isFile() || !pathname.canExecute()) {
       
   234                 return false;
       
   235             }
       
   236             String name = pathname.getName().toLowerCase();
       
   237             if (isWindows && !name.endsWith(EXE_FILE_EXT)) {
       
   238                 return false;
       
   239             }
       
   240             for (String x : exclude) {
       
   241                 if (name.endsWith(x)) {
       
   242                     return false;
       
   243                 }
       
   244             }
       
   245             return true;
       
   246         }
       
   247     }
   222 }
   248 }