VersionCheck.java revision 10967:e336cbd8b15e
1/*
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.
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 */
23
24/**
25 * @test
26 * @bug 6545058 6611182 8016209
27 * @summary validate and test -version, -fullversion, and internal, as well as
28 *          sanity checks if a tool can be launched.
29 * @compile VersionCheck.java
30 * @run main VersionCheck
31 */
32
33import java.io.File;
34import java.io.FileFilter;
35import java.util.Map;
36import java.util.ArrayList;
37import java.util.HashMap;
38import java.util.List;
39
40public class VersionCheck extends TestHelper {
41
42    // tools that do not accept -J-option
43    static final String[] BLACKLIST_JOPTION = {
44        "controlpanel",
45        "jabswitch",
46        "java-rmi",
47        "java-rmi.cgi",
48        "java",
49        "javacpl",
50        "javaw",
51        "javaws",
52        "jcontrol",
53        "jmc",
54        "jmc.ini",
55        "jp2launcher",
56        "jvisualvm",
57        "packager",
58        "ssvagent",
59        "unpack200",
60        "wsimport"
61    };
62
63    // tools that do not accept -version
64    static final String[] BLACKLIST_VERSION = {
65        "appletviewer",
66        "controlpanel",
67        "jar",
68        "jarsigner",
69        "java-rmi",
70        "java-rmi.cgi",
71        "javadoc",
72        "javaws",
73        "jcmd",
74        "jconsole",
75        "jcontrol",
76        "jdeps",
77        "jimage",
78        "jinfo",
79        "jmap",
80        "jmc",
81        "jmc.ini",
82        "jps",
83        "jrunscript",
84        "jjs",
85        "jp2launcher",
86        "jsadebugd",
87        "jstack",
88        "jstat",
89        "jstatd",
90        "jvisualvm",
91        "keytool",
92        "kinit",
93        "klist",
94        "ktab",
95        "native2ascii",
96        "orbd",
97        "pack200",
98        "packager",
99        "policytool",
100        "rmic",
101        "rmid",
102        "rmiregistry",
103        "schemagen", // returns error code 127
104        "serialver",
105        "servertool",
106        "ssvagent",
107        "tnameserv",
108        "unpack200",
109        "wsgen",
110        "wsimport",
111        "xjc"
112    };
113
114    // expected reference strings
115    static String refVersion;
116    static String refFullVersion;
117
118    static String getVersion(String... argv) {
119        TestHelper.TestResult tr = doExec(argv);
120        StringBuilder out = new StringBuilder();
121        // remove the HotSpot line
122        for (String x : tr.testOutput) {
123            if (!x.matches(".*Client.*VM.*|.*Server.*VM.*")) {
124                out = out.append(x + "\n");
125            }
126        }
127        return out.toString();
128    }
129
130    /*
131     * this tests if the tool can take a version string and returns
132     * a 0 exit code, it is not possible to validate the contents
133     * of the -version output as they are inconsistent.
134     */
135    static boolean testToolVersion() {
136        TestResult tr = null;
137        TestHelper.testExitValue = 0;
138        for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) {
139            String x = f.getAbsolutePath();
140            System.out.println("Testing (-version): " + x);
141            tr = doExec(x, "-version");
142            tr.checkPositive();
143        }
144        return TestHelper.testExitValue == 0;
145    }
146
147    static boolean compareJVersionStrings() {
148        int failcount = 0;
149        for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) {
150            String x = f.getAbsolutePath();
151            System.out.println("Testing (-J-version): " + x);
152            String testStr;
153
154            testStr = getVersion(x, "-J-version");
155            if (refVersion.compareTo(testStr) != 0) {
156                failcount++;
157                System.out.println("Error: " + x +
158                                   " fails -J-version comparison");
159                System.out.println("Expected:");
160                System.out.print(refVersion);
161                System.out.println("Actual:");
162                System.out.print(testStr);
163            }
164
165            testStr = getVersion(x, "-J-fullversion");
166            if (refFullVersion.compareTo(testStr) != 0) {
167                failcount++;
168                System.out.println("Error: " + x +
169                                   " fails -J-fullversion comparison");
170                System.out.println("Expected:");
171                System.out.print(refFullVersion);
172                System.out.println("Actual:");
173                System.out.print(testStr);
174            }
175        }
176        System.out.println("Version Test: " + failcount);
177        return failcount == 0;
178    }
179
180    static boolean compareInternalStrings() {
181        int failcount = 0;
182        String bStr = refVersion.substring(refVersion.lastIndexOf("build") +
183                                           "build".length() + 1,
184                                           refVersion.lastIndexOf(")"));
185
186        String[] vStr = bStr.split("\\.|-|_");
187        String jdkMajor = vStr[0];
188        String jdkMinor = vStr[1];
189        String jdkMicro = vStr[2];
190        String jdkBuild = vStr[vStr.length - 1];
191
192        String expectedDotVersion = "dotversion:" + jdkMajor + "." + jdkMinor;
193        String expectedFullVersion = "fullversion:" + bStr;
194
195        Map<String, String> envMap = new HashMap<>();
196        envMap.put(TestHelper.JLDEBUG_KEY, "true");
197        TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version");
198        List<String> alist = new ArrayList<>();
199        alist.addAll(tr.testOutput);
200        for (String x : tr.testOutput) {
201            alist.add(x.trim());
202        }
203        if (!alist.contains(expectedDotVersion)) {
204            System.out.println("Error: could not find " + expectedDotVersion);
205            failcount++;
206        }
207
208        if (!alist.contains(expectedFullVersion)) {
209            System.out.println("Error: could not find " + expectedFullVersion);
210            failcount++;
211        }
212        System.out.println("Internal Strings Test: " + failcount);
213        return failcount == 0;
214    }
215
216    // Initialize
217    static void init() {
218        refVersion = getVersion(javaCmd, "-version");
219        refFullVersion = getVersion(javaCmd, "-fullversion");
220    }
221
222    public static void main(String[] args) {
223        init();
224        if (compareJVersionStrings() &&
225                compareInternalStrings() &&
226                testToolVersion()) {
227            System.out.println("All Version string comparisons: PASS");
228        } else {
229            throw new AssertionError("Some tests failed");
230        }
231    }
232
233    static class ToolFilter implements FileFilter {
234        final Iterable<String> exclude ;
235        protected ToolFilter(String... exclude) {
236            List<String> tlist = new ArrayList<>();
237            this.exclude = tlist;
238            for (String x : exclude) {
239                String str = x + ((isWindows) ? EXE_FILE_EXT : "");
240                tlist.add(str.toLowerCase());
241            }
242        }
243        @Override
244        public boolean accept(File pathname) {
245            if (!pathname.isFile() || !pathname.canExecute()) {
246                return false;
247            }
248            String name = pathname.getName().toLowerCase();
249            if (isWindows && !name.endsWith(EXE_FILE_EXT)) {
250                return false;
251            }
252            for (String x : exclude) {
253                if (name.endsWith(x)) {
254                    return false;
255                }
256            }
257            return true;
258        }
259    }
260}
261