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 8162746 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 33 import java.io.File; 34 import java.io.FileFilter; 35 import java.util.Map; 36 import java.util.ArrayList; 37 import java.util.HashMap; 38 import java.util.List; 39 40 public 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 "jaotc", 55 "javaw", 56 "javaws", 57 "jcontrol", 58 "jmc", 131 return getVersion0(true, argv); 132 } 133 134 static String getVersion(String... argv) { 135 return getVersion0(false, argv); 136 } 137 138 static String getVersion0(boolean allLines, String... argv) { 139 TestHelper.TestResult tr = doExec(argv); 140 StringBuilder out = new StringBuilder(); 141 // remove the HotSpot line 142 for (String x : tr.testOutput) { 143 if (allLines || !x.matches(".*Client.*VM.*|.*Server.*VM.*")) { 144 out = out.append(x + "\n"); 145 } 146 } 147 return out.toString(); 148 } 149 150 /* 151 * this tests if the tool can take a version string and returns 152 * a 0 exit code, it is not possible to validate the contents 153 * of the -version output as they are inconsistent. 154 */ 155 static boolean testToolVersion() { 156 TestHelper.testExitValue = 0; 157 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) { 158 String x = f.getAbsolutePath(); 159 System.out.println("Testing (-version): " + x); 160 TestResult tr = doExec(x, "-version"); 161 tr.checkPositive(); 162 } 163 return TestHelper.testExitValue == 0; 164 } 165 166 static boolean compareJVersionStrings() { 167 int failcount = 0; 168 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) { 169 String x = f.getAbsolutePath(); 170 System.out.println("Testing (-J-version): " + x); 171 String testStr; 172 173 testStr = getVersion(x, "-J-version"); 174 if (refVersion.compareTo(testStr) != 0) { 175 failcount++; 176 System.out.println("Error: " + x + 177 " fails -J-version comparison"); 178 System.out.println("Expected:"); 179 System.out.print(refVersion); 180 System.out.println("Actual:"); 181 System.out.print(testStr); 182 } 183 184 testStr = getVersion(x, "-J-fullversion"); 185 if (refFullVersion.compareTo(testStr) != 0) { 186 failcount++; 187 System.out.println("Error: " + x + 188 " fails -J-fullversion comparison"); 189 System.out.println("Expected:"); 190 System.out.print(refFullVersion); 191 System.out.println("Actual:"); 192 System.out.print(testStr); 193 } 194 } 195 System.out.println("Version Test: " + failcount); 196 return failcount == 0; 197 } 198 199 static boolean compareInternalStrings() { 200 int failcount = 0; 201 String bStr = refVersion.substring(refVersion.indexOf("build") + 202 "build".length() + 1, 203 refVersion.lastIndexOf(")")); 204 205 String expectedFullVersion = "fullversion:" + bStr; 206 207 Map<String, String> envMap = new HashMap<>(); 208 envMap.put(TestHelper.JLDEBUG_KEY, "true"); 209 TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version"); 210 List<String> alist = new ArrayList<>(); 211 alist.addAll(tr.testOutput); 212 for (String x : tr.testOutput) { 213 alist.add(x.trim()); 214 } 215 216 if (!alist.contains(expectedFullVersion)) { 217 System.out.println("Error: could not find " + expectedFullVersion); 218 failcount++; 219 } 220 System.out.println("Internal Strings Test: " + failcount); 221 return failcount == 0; 222 } 223 224 static boolean testDebugVersion() { 225 String jdkType = System.getProperty("jdk.debug", "release"); 226 String versionLines = getAllVersionLines(javaCmd, "-version"); 227 if ("release".equals(jdkType)) { 228 jdkType = ""; 229 } else { 230 jdkType = jdkType + " "; 231 } 232 String tofind = "(" + jdkType + "build"; 233 int idx = versionLines.indexOf(tofind); 234 if (idx < 0) { 235 System.out.println("Did not find first instance of " + tofind); 236 return false; 237 } 238 idx = versionLines.indexOf(tofind, idx + 1); 239 if (idx < 0) { 240 System.out.println("Did not find first instance of " + tofind); 241 return false; 242 } 243 return true; 244 } 245 246 // Initialize 247 static void init() { 248 refVersion = getVersion(javaCmd, "-version"); 249 refFullVersion = getVersion(javaCmd, "-fullversion"); 250 } 251 252 public static void main(String[] args) { 253 init(); 254 if (compareJVersionStrings() && 255 compareInternalStrings() && 256 testToolVersion() && 257 testDebugVersion()) { 258 System.out.println("All Version string comparisons: PASS"); 259 } else { 260 throw new AssertionError("Some tests failed"); 261 } 262 } 263 264 static class ToolFilter implements FileFilter { 265 final Iterable<String> exclude; 266 protected ToolFilter(String... exclude) { 267 List<String> tlist = new ArrayList<>(); 268 this.exclude = tlist; 269 for (String x : exclude) { 270 String str = x + ((isWindows) ? EXE_FILE_EXT : ""); 271 tlist.add(str.toLowerCase()); 272 } 273 } 274 @Override 275 public boolean accept(File pathname) { 276 if (!pathname.isFile() || !pathname.canExecute()) { 277 return false; 278 } 279 String name = pathname.getName().toLowerCase(); 280 if (isWindows && !name.endsWith(EXE_FILE_EXT)) { | 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 8162746 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 33 import java.io.File; 34 import java.io.FileFilter; 35 import java.util.Map; 36 import java.util.ArrayList; 37 import java.util.HashMap; 38 import java.util.List; 39 import java.util.HashSet; 40 import java.util.Set; 41 42 public class VersionCheck extends TestHelper { 43 44 // tools that do not accept -J-option 45 static 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 "jaotc", 57 "javaw", 58 "javaws", 59 "jcontrol", 60 "jmc", 133 return getVersion0(true, argv); 134 } 135 136 static String getVersion(String... argv) { 137 return getVersion0(false, argv); 138 } 139 140 static String getVersion0(boolean allLines, String... argv) { 141 TestHelper.TestResult tr = doExec(argv); 142 StringBuilder out = new StringBuilder(); 143 // remove the HotSpot line 144 for (String x : tr.testOutput) { 145 if (allLines || !x.matches(".*Client.*VM.*|.*Server.*VM.*")) { 146 out = out.append(x + "\n"); 147 } 148 } 149 return out.toString(); 150 } 151 152 /* 153 * Checks if the tools accept "-version" option (exit code is zero). 154 * The output of the tools run with "-version" is not verified. 155 */ 156 static String testToolVersion() { 157 System.out.println("=== testToolVersion === "); 158 Set<String> failed = new HashSet<>(); 159 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) { 160 String x = f.getAbsolutePath(); 161 TestResult tr = doExec(x, "-version"); 162 System.out.println("Testing " + f.getName()); 163 System.out.println("#> " + x + " -version"); 164 tr.testOutput.forEach(System.out::println); 165 System.out.println("#> echo $?"); 166 System.out.println(tr.exitValue); 167 if (!tr.isOK()) { 168 System.out.println("failed"); 169 failed.add(f.getName()); 170 } 171 } 172 if (failed.isEmpty()) { 173 System.out.println("testToolVersion passed"); 174 return ""; 175 } else { 176 System.out.println("testToolVersion failed"); 177 return "testToolVersion: " + failed + "; "; 178 } 179 180 } 181 182 static String testJVersionStrings() { 183 System.out.println("=== testJVersionStrings === "); 184 Set<String> failed = new HashSet<>(); 185 for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) { 186 System.out.println("Testing " + f.getName()); 187 String x = f.getAbsolutePath(); 188 String testStr = getVersion(x, "-J-version"); 189 if (refVersion.compareTo(testStr) != 0) { 190 failed.add(f.getName()); 191 System.out.println("Error: " + x + 192 " fails -J-version comparison"); 193 System.out.println("Expected:"); 194 System.out.print(refVersion); 195 System.out.println("Actual:"); 196 System.out.print(testStr); 197 } 198 199 testStr = getVersion(x, "-J-fullversion"); 200 if (refFullVersion.compareTo(testStr) != 0) { 201 failed.add(f.getName()); 202 System.out.println("Error: " + x + 203 " fails -J-fullversion comparison"); 204 System.out.println("Expected:"); 205 System.out.print(refFullVersion); 206 System.out.println("Actual:"); 207 System.out.print(testStr); 208 } 209 } 210 if (failed.isEmpty()) { 211 System.out.println("testJVersionStrings passed"); 212 return ""; 213 } else { 214 System.out.println("testJVersionStrings failed"); 215 return "testJVersionStrings: " + failed + "; "; 216 } 217 } 218 219 static String testInternalStrings() { 220 System.out.println("=== testInternalStrings === "); 221 String bStr = refVersion.substring(refVersion.indexOf("build") + 222 "build".length() + 1, 223 refVersion.lastIndexOf(")")); 224 225 String expectedFullVersion = "fullversion:" + bStr; 226 227 Map<String, String> envMap = new HashMap<>(); 228 envMap.put(TestHelper.JLDEBUG_KEY, "true"); 229 TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version"); 230 List<String> alist = new ArrayList<>(); 231 tr.testOutput.stream().map(String::trim).forEach(alist::add); 232 233 if (alist.contains(expectedFullVersion)) { 234 System.out.println("testInternalStrings passed"); 235 return ""; 236 } else { 237 System.out.println("Error: could not find " + expectedFullVersion); 238 tr.testOutput.forEach(System.out::println); 239 System.out.println("testInternalStrings failed"); 240 return "testInternalStrings; "; 241 } 242 } 243 244 static String testDebugVersion() { 245 System.out.println("=== testInternalStrings === "); 246 String jdkType = System.getProperty("jdk.debug", "release"); 247 String versionLines = getAllVersionLines(javaCmd, "-version"); 248 if ("release".equals(jdkType)) { 249 jdkType = ""; 250 } else { 251 jdkType = jdkType + " "; 252 } 253 254 String tofind = "(" + jdkType + "build"; 255 256 int idx = versionLines.indexOf(tofind); 257 if (idx < 0) { 258 System.out.println("versionLines " + versionLines); 259 System.out.println("Did not find first instance of " + tofind); 260 return "testDebugVersion; "; 261 } 262 idx = versionLines.indexOf(tofind, idx + 1); 263 if (idx < 0) { 264 System.out.println("versionLines " + versionLines); 265 System.out.println("Did not find second instance of " + tofind); 266 return "testDebugVersion; "; 267 } 268 System.out.println("testDebugVersion passed"); 269 return ""; 270 } 271 272 // Initialize 273 static void init() { 274 refVersion = getVersion(javaCmd, "-version"); 275 refFullVersion = getVersion(javaCmd, "-fullversion"); 276 } 277 278 public static void main(String[] args) { 279 init(); 280 String errorMessage = ""; 281 errorMessage += testJVersionStrings(); 282 errorMessage += testInternalStrings(); 283 errorMessage += testToolVersion(); 284 errorMessage += testDebugVersion(); 285 if (errorMessage.isEmpty()) { 286 System.out.println("All Version string comparisons: PASS"); 287 } else { 288 throw new AssertionError("VersionCheck failed: " + errorMessage); 289 } 290 } 291 292 static class ToolFilter implements FileFilter { 293 final Iterable<String> exclude; 294 protected ToolFilter(String... exclude) { 295 List<String> tlist = new ArrayList<>(); 296 this.exclude = tlist; 297 for (String x : exclude) { 298 String str = x + ((isWindows) ? EXE_FILE_EXT : ""); 299 tlist.add(str.toLowerCase()); 300 } 301 } 302 @Override 303 public boolean accept(File pathname) { 304 if (!pathname.isFile() || !pathname.canExecute()) { 305 return false; 306 } 307 String name = pathname.getName().toLowerCase(); 308 if (isWindows && !name.endsWith(EXE_FILE_EXT)) { |