Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/launcher/VersionCheck.java
38833 views
/*1* Copyright (c) 2007, 2014, 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 801620926* @summary validate and test -version, -fullversion, and internal, as well as27* sanity checks if a tool can be launched.28* @compile VersionCheck.java29* @run main VersionCheck30*/3132import java.io.File;33import java.io.FileFilter;34import java.util.Map;35import java.util.ArrayList;36import java.util.HashMap;37import java.util.List;3839public class VersionCheck extends TestHelper {4041// tools that do not accept -J-option42static final String[] BLACKLIST_JOPTION = {43"controlpanel",44"jabswitch",45"java-rmi",46"java-rmi.cgi",47"java",48"javaw",49"javaws",50"jcontrol",51"jmc",52"jmc.ini",53"jvisualvm",54"packager",55"unpack200",56"wsimport"57};5859// tools that do not accept -version60static final String[] BLACKLIST_VERSION = {61"appletviewer",62"controlpanel",63"clhsdb",64"extcheck",65"hsdb",66"jar",67"jarsigner",68"java-rmi",69"java-rmi.cgi",70"javadoc",71"javaws",72"jcmd",73"jconsole",74"jcontrol",75"jdeps",76"jfr",77"jinfo",78"jmap",79"jmc",80"jmc.ini",81"jps",82"jrunscript",83"jjs",84"jsadebugd",85"jstack",86"jstat",87"jstatd",88"jvisualvm",89"keytool",90"kinit",91"klist",92"ktab",93"native2ascii",94"orbd",95"pack200",96"packager",97"policytool",98"rmic",99"rmid",100"rmiregistry",101"schemagen", // returns error code 127102"serialver",103"servertool",104"tnameserv",105"unpack200",106"wsgen",107"wsimport",108"xjc"109};110111// expected reference strings112static String refVersion;113static String refFullVersion;114115static String getVersion(String... argv) {116TestHelper.TestResult tr = doExec(argv);117StringBuilder out = new StringBuilder();118// remove the HotSpot line119for (String x : tr.testOutput) {120if (!x.matches(".*Client.*VM.*|.*Server.*VM.*")) {121out = out.append(x + "\n");122}123}124return out.toString();125}126127/*128* this tests if the tool can take a version string and returns129* a 0 exit code, it is not possible to validate the contents130* of the -version output as they are inconsistent.131*/132static boolean testToolVersion() {133TestResult tr = null;134TestHelper.testExitValue = 0;135for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) {136String x = f.getAbsolutePath();137System.out.println("Testing (-version): " + x);138tr = doExec(x, "-version");139tr.checkPositive();140}141return TestHelper.testExitValue == 0;142}143144static boolean compareJVersionStrings() {145int failcount = 0;146for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) {147String x = f.getAbsolutePath();148System.out.println("Testing (-J-version): " + x);149String testStr;150151testStr = getVersion(x, "-J-version");152if (refVersion.compareTo(testStr) != 0) {153failcount++;154System.out.println("Error: " + x +155" fails -J-version comparison");156System.out.println("Expected:");157System.out.print(refVersion);158System.out.println("Actual:");159System.out.print(testStr);160}161162testStr = getVersion(x, "-J-fullversion");163if (refFullVersion.compareTo(testStr) != 0) {164failcount++;165System.out.println("Error: " + x +166" fails -J-fullversion comparison");167System.out.println("Expected:");168System.out.print(refFullVersion);169System.out.println("Actual:");170System.out.print(testStr);171}172}173System.out.println("Version Test: " + failcount);174return failcount == 0;175}176177static boolean compareInternalStrings() {178int failcount = 0;179String bStr = refVersion.substring(refVersion.lastIndexOf("build") +180"build".length() + 1,181refVersion.lastIndexOf(")"));182183String[] vStr = bStr.split("\\.|-|_");184String jdkMajor = vStr[0];185String jdkMinor = vStr[1];186String jdkMicro = vStr[2];187String jdkBuild = vStr[vStr.length - 1];188189String expectedDotVersion = "dotversion:" + jdkMajor + "." + jdkMinor;190String expectedFullVersion = "fullversion:" + bStr;191192Map<String, String> envMap = new HashMap<>();193envMap.put(TestHelper.JLDEBUG_KEY, "true");194TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version");195List<String> alist = new ArrayList<>();196alist.addAll(tr.testOutput);197for (String x : tr.testOutput) {198alist.add(x.trim());199}200if (!alist.contains(expectedDotVersion)) {201System.out.println("Error: could not find " + expectedDotVersion);202failcount++;203}204205if (!alist.contains(expectedFullVersion)) {206System.out.println("Error: could not find " + expectedFullVersion);207failcount++;208}209System.out.println("Internal Strings Test: " + failcount);210return failcount == 0;211}212213// Initialize214static void init() {215refVersion = getVersion(javaCmd, "-version");216refFullVersion = getVersion(javaCmd, "-fullversion");217}218219public static void main(String[] args) {220init();221if (compareJVersionStrings() &&222compareInternalStrings() &&223testToolVersion()) {224System.out.println("All Version string comparisons: PASS");225} else {226throw new AssertionError("Some tests failed");227}228}229230static class ToolFilter implements FileFilter {231final Iterable<String> exclude ;232protected ToolFilter(String... exclude) {233List<String> tlist = new ArrayList<>();234this.exclude = tlist;235for (String x : exclude) {236String str = x + ((isWindows) ? EXE_FILE_EXT : "");237tlist.add(str.toLowerCase());238}239}240@Override241public boolean accept(File pathname) {242if (!pathname.isFile() || !pathname.canExecute()) {243return false;244}245String name = pathname.getName().toLowerCase();246if (isWindows && !name.endsWith(EXE_FILE_EXT)) {247return false;248}249for (String x : exclude) {250if (name.endsWith(x)) {251return false;252}253}254return true;255}256}257}258259260