VersionCheck.java revision 13901:b2a69d66dc65
1/* 2 * Copyright (c) 2007, 2015, 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 8139986 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 "jaccessinspector", 51 "jaccessinspector-32", 52 "jaccesswalker", 53 "jaccesswalker-32", 54 "javaw", 55 "javaws", 56 "jcontrol", 57 "jmc", 58 "jmc.ini", 59 "jp2launcher", 60 "jvisualvm", 61 "packager", 62 "ssvagent", 63 "unpack200", 64 "wsimport" 65 }; 66 67 // tools that do not accept -version 68 static final String[] BLACKLIST_VERSION = { 69 "appletviewer", 70 "controlpanel", 71 "jaccessinspector", 72 "jaccessinspector-32", 73 "jaccesswalker", 74 "jaccesswalker-32", 75 "jar", 76 "jarsigner", 77 "java-rmi", 78 "java-rmi.cgi", 79 "javadoc", 80 "javacpl", 81 "javaws", 82 "jcmd", 83 "jconsole", 84 "jcontrol", 85 "jdeps", 86 "jimage", 87 "jinfo", 88 "jlink", 89 "jmap", 90 "jmod", 91 "jmc", 92 "jmc.ini", 93 "jps", 94 "jrunscript", 95 "jjs", 96 "jp2launcher", 97 "jsadebugd", 98 "jstack", 99 "jstat", 100 "jstatd", 101 "jvisualvm", 102 "keytool", 103 "kinit", 104 "klist", 105 "ktab", 106 "orbd", 107 "pack200", 108 "packager", 109 "policytool", 110 "rmic", 111 "rmid", 112 "rmiregistry", 113 "schemagen", // returns error code 127 114 "serialver", 115 "servertool", 116 "ssvagent", 117 "tnameserv", 118 "unpack200", 119 "wsgen", 120 "wsimport", 121 "xjc" 122 }; 123 124 // expected reference strings 125 static String refVersion; 126 static String refFullVersion; 127 128 static String getAllVersionLines(String... argv) { 129 return getVersion0(true, argv); 130 } 131 132 static String getVersion(String... argv) { 133 return getVersion0(false, argv); 134 } 135 136 static String getVersion0(boolean allLines, String... argv) { 137 TestHelper.TestResult tr = doExec(argv); 138 StringBuilder out = new StringBuilder(); 139 // remove the HotSpot line 140 for (String x : tr.testOutput) { 141 if (allLines || !x.matches(".*Client.*VM.*|.*Server.*VM.*")) { 142 out = out.append(x + "\n"); 143 } 144 } 145 return out.toString(); 146 } 147 148 /* 149 * this tests if the tool can take a version string and returns 150 * a 0 exit code, it is not possible to validate the contents 151 * of the -version output as they are inconsistent. 152 */ 153 static boolean testToolVersion() { 154 TestResult tr = null; 155 TestHelper.testExitValue = 0; 156 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) { 157 String x = f.getAbsolutePath(); 158 System.out.println("Testing (-version): " + x); 159 tr = doExec(x, "-version"); 160 tr.checkPositive(); 161 } 162 return TestHelper.testExitValue == 0; 163 } 164 165 static boolean compareJVersionStrings() { 166 int failcount = 0; 167 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) { 168 String x = f.getAbsolutePath(); 169 System.out.println("Testing (-J-version): " + x); 170 String testStr; 171 172 testStr = getVersion(x, "-J-version"); 173 if (refVersion.compareTo(testStr) != 0) { 174 failcount++; 175 System.out.println("Error: " + x + 176 " fails -J-version comparison"); 177 System.out.println("Expected:"); 178 System.out.print(refVersion); 179 System.out.println("Actual:"); 180 System.out.print(testStr); 181 } 182 183 testStr = getVersion(x, "-J-fullversion"); 184 if (refFullVersion.compareTo(testStr) != 0) { 185 failcount++; 186 System.out.println("Error: " + x + 187 " fails -J-fullversion comparison"); 188 System.out.println("Expected:"); 189 System.out.print(refFullVersion); 190 System.out.println("Actual:"); 191 System.out.print(testStr); 192 } 193 } 194 System.out.println("Version Test: " + failcount); 195 return failcount == 0; 196 } 197 198 static boolean compareInternalStrings() { 199 int failcount = 0; 200 String bStr = refVersion.substring(refVersion.indexOf("build") + 201 "build".length() + 1, 202 refVersion.lastIndexOf(")")); 203 204 String expectedFullVersion = "fullversion:" + bStr; 205 206 Map<String, String> envMap = new HashMap<>(); 207 envMap.put(TestHelper.JLDEBUG_KEY, "true"); 208 TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version"); 209 List<String> alist = new ArrayList<>(); 210 alist.addAll(tr.testOutput); 211 for (String x : tr.testOutput) { 212 alist.add(x.trim()); 213 } 214 215 if (!alist.contains(expectedFullVersion)) { 216 System.out.println("Error: could not find " + expectedFullVersion); 217 failcount++; 218 } 219 System.out.println("Internal Strings Test: " + failcount); 220 return failcount == 0; 221 } 222 223 static boolean testDebugVersion() { 224 String jdkType = System.getProperty("jdk.debug", "release"); 225 String versionLines = getAllVersionLines(javaCmd, "-version"); 226 if ("release".equals(jdkType)) { 227 jdkType = ""; 228 } else { 229 jdkType = jdkType + " "; 230 } 231 String tofind = "(" + jdkType + "build"; 232 int idx = versionLines.indexOf(tofind); 233 if (idx < 0) { 234 System.out.println("Did not find first instance of " + tofind); 235 return false; 236 } 237 idx = versionLines.indexOf(tofind, idx + 1); 238 if (idx < 0) { 239 System.out.println("Did not find first instance of " + tofind); 240 return false; 241 } 242 return true; 243 } 244 245 // Initialize 246 static void init() { 247 refVersion = getVersion(javaCmd, "-version"); 248 refFullVersion = getVersion(javaCmd, "-fullversion"); 249 } 250 251 public static void main(String[] args) { 252 init(); 253 if (compareJVersionStrings() && 254 compareInternalStrings() && 255 testToolVersion() && 256 testDebugVersion()) { 257 System.out.println("All Version string comparisons: PASS"); 258 } else { 259 throw new AssertionError("Some tests failed"); 260 } 261 } 262 263 static class ToolFilter implements FileFilter { 264 final Iterable<String> exclude; 265 protected ToolFilter(String... exclude) { 266 List<String> tlist = new ArrayList<>(); 267 this.exclude = tlist; 268 for (String x : exclude) { 269 String str = x + ((isWindows) ? EXE_FILE_EXT : ""); 270 tlist.add(str.toLowerCase()); 271 } 272 } 273 @Override 274 public boolean accept(File pathname) { 275 if (!pathname.isFile() || !pathname.canExecute()) { 276 return false; 277 } 278 String name = pathname.getName().toLowerCase(); 279 if (isWindows && !name.endsWith(EXE_FILE_EXT)) { 280 return false; 281 } 282 for (String x : exclude) { 283 if (name.endsWith(x)) { 284 return false; 285 } 286 } 287 return true; 288 } 289 } 290} 291