Path: blob/master/test/jdk/tools/launcher/VersionCheck.java
41145 views
/*1* Copyright (c) 2007, 2021, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/**24* @test25* @bug 6545058 6611182 8016209 8139986 816274626* @summary validate and test -version, -fullversion, and internal, as well as27* sanity checks if a tool can be launched.28* @modules jdk.compiler29* jdk.zipfs30* @compile VersionCheck.java31* @run main VersionCheck32*/3334import java.io.File;35import java.util.ArrayList;36import java.util.HashMap;37import java.util.HashSet;38import java.util.List;39import java.util.Map;40import java.util.Set;4142public class VersionCheck extends TestHelper {4344// tools that do not accept -J-option45static final String[] BLACKLIST_JOPTION = {46"controlpanel",47"jabswitch",48"java-rmi",49"java-rmi.cgi",50"java",51"javacpl",52"jaccessinspector",53"jaccessinspector-32",54"jaccesswalker",55"jaccesswalker-32",56"javaw",57"javaws",58"jcontrol",59"jmc",60"jmc.ini",61"jweblauncher",62"jpackage",63"ssvagent"64};6566// tools that do not accept -version67static final String[] BLACKLIST_VERSION = {68"appletviewer",69"controlpanel",70"jaccessinspector",71"jaccessinspector-32",72"jaccesswalker",73"jaccesswalker-32",74"jar",75"jarsigner",76"java-rmi",77"java-rmi.cgi",78"javadoc",79"javacpl",80"javaws",81"jcmd",82"jconsole",83"jcontrol",84"jdeprscan",85"jdeps",86"jfr",87"jimage",88"jinfo",89"jlink",90"jmap",91"jmod",92"jmc",93"jmc.ini",94"jps",95"jrunscript",96"jjs",97"jstack",98"jstat",99"jstatd",100"jweblauncher",101"keytool",102"kinit",103"klist",104"ktab",105"jpackage",106"rmiregistry",107"serialver",108"servertool",109"ssvagent"110};111112// expected reference strings113static String refVersion;114static String refFullVersion;115116static String getAllVersionLines(String... argv) {117return getVersion0(true, argv);118}119120static String getVersion(String... argv) {121return getVersion0(false, argv);122}123124static String getVersion0(boolean allLines, String... argv) {125TestHelper.TestResult tr = doExec(argv);126StringBuilder out = new StringBuilder();127// remove the HotSpot line128for (String x : tr.testOutput) {129if (allLines || !x.matches(".*Client.*VM.*|.*Server.*VM.*")) {130out = out.append(x + "\n");131}132}133return out.toString();134}135136/*137* Checks if the tools accept "-version" option (exit code is zero).138* The output of the tools run with "-version" is not verified.139*/140static String testToolVersion() {141System.out.println("=== testToolVersion === ");142Set<String> failed = new HashSet<>();143for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) {144String x = f.getAbsolutePath();145TestResult tr = doExec(x, "-version");146System.out.println("Testing " + f.getName());147System.out.println("#> " + x + " -version");148tr.testOutput.forEach(System.out::println);149System.out.println("#> echo $?");150System.out.println(tr.exitValue);151if (!tr.isOK()) {152System.out.println("failed");153failed.add(f.getName());154}155}156if (failed.isEmpty()) {157System.out.println("testToolVersion passed");158return "";159} else {160System.out.println("testToolVersion failed");161return "testToolVersion: " + failed + "; ";162}163164}165166static String testJVersionStrings() {167System.out.println("=== testJVersionStrings === ");168Set<String> failed = new HashSet<>();169for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) {170System.out.println("Testing " + f.getName());171String x = f.getAbsolutePath();172String testStr = getVersion(x, "-J-version");173if (refVersion.compareTo(testStr) != 0) {174failed.add(f.getName());175System.out.println("Error: " + x +176" fails -J-version comparison");177System.out.println("Expected:");178System.out.print(refVersion);179System.out.println("Actual:");180System.out.print(testStr);181}182183testStr = getVersion(x, "-J-fullversion");184if (refFullVersion.compareTo(testStr) != 0) {185failed.add(f.getName());186System.out.println("Error: " + x +187" fails -J-fullversion comparison");188System.out.println("Expected:");189System.out.print(refFullVersion);190System.out.println("Actual:");191System.out.print(testStr);192}193}194if (failed.isEmpty()) {195System.out.println("testJVersionStrings passed");196return "";197} else {198System.out.println("testJVersionStrings failed");199return "testJVersionStrings: " + failed + "; ";200}201}202203static String testInternalStrings() {204System.out.println("=== testInternalStrings === ");205String bStr = refVersion.substring(refVersion.indexOf("build") +206"build".length() + 1,207refVersion.lastIndexOf(")"));208209String expectedFullVersion = "fullversion:" + bStr;210211Map<String, String> envMap = new HashMap<>();212envMap.put(TestHelper.JLDEBUG_KEY, "true");213TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version");214List<String> alist = new ArrayList<>();215tr.testOutput.stream().map(String::trim).forEach(alist::add);216217if (alist.contains(expectedFullVersion)) {218System.out.println("testInternalStrings passed");219return "";220} else {221System.out.println("Error: could not find " + expectedFullVersion);222tr.testOutput.forEach(System.out::println);223System.out.println("testInternalStrings failed");224return "testInternalStrings; ";225}226}227228static String testDebugVersion() {229System.out.println("=== testInternalStrings === ");230String jdkType = System.getProperty("jdk.debug", "release");231String versionLines = getAllVersionLines(javaCmd, "-version");232if ("release".equals(jdkType)) {233jdkType = "";234} else {235jdkType = jdkType + " ";236}237238String tofind = "(" + jdkType + "build";239240int idx = versionLines.indexOf(tofind);241if (idx < 0) {242System.out.println("versionLines " + versionLines);243System.out.println("Did not find first instance of " + tofind);244return "testDebugVersion; ";245}246idx = versionLines.indexOf(tofind, idx + 1);247if (idx < 0) {248System.out.println("versionLines " + versionLines);249System.out.println("Did not find second instance of " + tofind);250return "testDebugVersion; ";251}252System.out.println("testDebugVersion passed");253return "";254}255256// Initialize257static void init() {258refVersion = getVersion(javaCmd, "-version");259refFullVersion = getVersion(javaCmd, "-fullversion");260}261262public static void main(String[] args) {263init();264String errorMessage = "";265errorMessage += testJVersionStrings();266errorMessage += testInternalStrings();267errorMessage += testToolVersion();268errorMessage += testDebugVersion();269if (errorMessage.isEmpty()) {270System.out.println("All Version string comparisons: PASS");271} else {272throw new AssertionError("VersionCheck failed: " + errorMessage);273}274}275}276277278