提交 e192d69f 编写于 作者: K ksrini

7156976: improve java tools testing

Reviewed-by: darcy
Contributed-by: steve.sides@oracle.com
上级 c2bd44e1
......@@ -54,6 +54,7 @@ public class TestHelper {
static final File TEST_SOURCES_DIR;
static final String JAVAHOME = System.getProperty("java.home");
static final String JAVA_BIN;
static final boolean isSDK = JAVAHOME.endsWith("jre");
static final String javaCmd;
static final String javawCmd;
......@@ -83,6 +84,7 @@ public class TestHelper {
static final String JAVA_FILE_EXT = ".java";
static final String CLASS_FILE_EXT = ".class";
static final String JAR_FILE_EXT = ".jar";
static final String EXE_FILE_EXT = ".exe";
static final String JLDEBUG_KEY = "_JAVA_LAUNCHER_DEBUG";
static final String EXPECTED_MARKER = "TRACER_MARKER:About to EXEC";
......@@ -110,6 +112,7 @@ public class TestHelper {
compiler = ToolProvider.getSystemJavaCompiler();
File binDir = (isSDK) ? new File((new File(JAVAHOME)).getParentFile(), "bin")
: new File(JAVAHOME, "bin");
JAVA_BIN = binDir.getAbsolutePath();
File javaCmdFile = (isWindows)
? new File(binDir, "java.exe")
: new File(binDir, "java");
......
/*
* Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
/*
* @test
* @summary Test options patterns for javac,javah,javap and javadoc using
* javac as a test launcher. Create a dummy javac and intercept options to check
* reception of options as passed through the launcher without having to launch
* javac. Only -J and -cp ./* options should be consumed by the launcher.
* @run main ToolsOpts
* @author ssides
*/
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class ToolsOpts extends TestHelper {
static final String JBCP_PREPEND = "-J-Xbootclasspath/p:";
private static File testJar = null;
static String[][] optionPatterns = {
{"-J-Xmx128m"},
{"-J-version"},
{"-J-XshowSettings:vm"},
{"-J-Xdiag"},
{"-J-showversion"},
{"-J-version", "-option"},
{"-option"},
{"-option:sub"},
{"-option:sub-"},
{"-option:sub1,sub2"}, // -option:list
{"-option:{sub1,sub2,sub3}"}, // -option:{list}
{"-option:{{sub1,sub2,sub3}}"},// -option:{{list}}
{"-option/c:/export/date/tmp"},
{"-option=value"},
{"-Dpk1.pk2.pk3"}, // dot in option
{"-Dpk1.pk2=value"}, // dot in option followed by =value
{"@<filename>"},
{"-option", "http://site.com", "http://site.org"},
{"-option", "name", "p1:p2.."},
{"-All these non-options show launchers pass options as is to tool."},
{"-option"},
{"-option:sub"},
{"-option:sub-"},
{"-option", "<path>"},
{"-option", "<file>"},
{"-option", "<dir>"},
{"-option", "http://a/b/c/g;x?y#s"},
{"-option", "<html code>"},
{"-option", "name1:name2"},
{"-option", "3"},
{"option1", "-J-version", "option2"},
{"option1", "-J-version", "-J-XshowSettings:vm", "option2"},};
static void init() throws IOException {
if (testJar != null) {
return;
}
// A tool which simulates com.sun.tools.javac.Main argument processing,
// intercepts options passed via the javac launcher.
final String mainJava = "Main" + JAVA_FILE_EXT;
testJar = new File("test" + JAR_FILE_EXT);
List<String> contents = new ArrayList<>();
contents.add("package com.sun.tools.javac;");
contents.add("public class Main {");
contents.add(" public static void main(String... args) {\n");
contents.add(" for (String x : args) {\n");
contents.add(" if(x.compareTo(\" \")!=0)\n");
contents.add(" System.out.println(x);\n");
contents.add(" }\n");
contents.add(" }\n");
contents.add("}\n");
createFile(new File(mainJava), contents);
// compile and jar Main.java into test.jar
compile("-d", ".", mainJava);
createJar("cvf", testJar.getAbsolutePath(), "com");
}
static void pass(String msg) {
System.out.println("pass: " + msg);
}
static void errout(String msg) {
System.err.println(msg);
}
// Return position of -J option or -1 is does not contain a -J option.
static int indexOfJoption(String[] opts) {
for (int i = 0; i < opts.length; i++) {
if (opts[i].startsWith("-J")) {
return i;
}
}
return -1;
}
/*
* Check that J options a) are not passed to tool, and b) do the right thing,
* that is, they should be passed to java launcher and work as expected.
*/
static void checkJoptionOutput(TestResult tr, String[] opts) throws IOException {
// Check -J-version options are not passed but do what they should.
String jopts = "";
for (String pat : opts) {
jopts = jopts.concat(pat + " ");
if (tr.contains("-J")) {
throw new RuntimeException(
"failed: output should not contain option " + pat);
}
if (pat.compareTo("-J-version") == 0 ||
pat.compareTo("-J-showversion") == 0) {
if (!tr.contains("java version")) {
throw new RuntimeException("failed: " + pat +
" should have display java version.");
}
} else if (pat.compareTo("-J-XshowSettings:VM") == 0) {
if (!tr.contains("VM settings")) {
throw new RuntimeException("failed: " + pat +
" should have display VM settings.");
}
}
}
pass("Joption check: " + jopts);
}
/*
* Feed each option pattern in optionPatterns array to javac launcher with
* checking program preempting javac. Check that option received by 'dummy'
* javac is the one passed on the command line.
*/
static void runTestOptions() throws IOException {
init();
TestResult tr = null;
String sTestJar = testJar.getAbsolutePath();
int jpos = -1;
for (String arg[] : optionPatterns) {
jpos = indexOfJoption(arg);
//Build a cmd string for output in results reporting.
String cmdString = javacCmd + JBCP_PREPEND + sTestJar;
for (String opt : arg) {
cmdString = cmdString.concat(" " + opt);
}
switch (arg.length) {
case 1:
tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,
arg[0]);
break;
case 2:
tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,
arg[0], arg[1]);
break;
case 3:
tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,
arg[0], arg[1], arg[2]);
break;
case 4:
tr = doExec(javacCmd, JBCP_PREPEND + sTestJar,
arg[0], arg[1], arg[2], arg[3]);
break;
default:
tr = null;
break;
}
String[] output = tr.testOutput.toArray(new String[tr.testOutput.size()]);
//-Joptions should not be passed to tool
if (jpos > -1) {
checkJoptionOutput(tr, arg);
if (tr.contains(arg[jpos])) {
throw new RuntimeException(
"failed! Should not have passed -J option to tool.\n"
+ "CMD: " + cmdString);
}
} else {
//check that each non -J option was passed to tool.
for (int i = 0; i < arg.length; i++) {
if (output[i].compareTo(arg[i]) != 0) {
throw new RuntimeException(
"failed! CMD: " + cmdString + "\n case:" +
output[i] + " != " + arg[i]);
} else {
pass("check " + output[i] + " == " + arg[i]);
}
}
}
pass(cmdString);
}
}
public static void main(String... args) throws IOException {
runTestOptions();
}
}
/*
* Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2007, 2012, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
......@@ -24,58 +24,76 @@
/**
* @test
* @bug 6545058 6611182
* @summary validate and test -version, -fullversion, and internal
* @summary validate and test -version, -fullversion, and internal, as well as
* sanity checks if a tool can be launched.
* @compile VersionCheck.java
* @run main VersionCheck
*/
import java.lang.*;
import java.io.File;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.FileFilter;
import java.util.Map;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;
public class VersionCheck {
private static String javaBin;
public class VersionCheck extends TestHelper {
// tools that do not accept -J-option
static final String[] BLACKLIST_JOPTION = {
"controlpanel",
"java-rmi",
"java-rmi.cgi",
"java",
"javaw",
"javaws",
"jcontrol",
"jvisualvm",
"packager",
"unpack200",
"wsimport"
};
// A known set of programs we know for sure will behave correctly.
private static String[] programs = new String[]{
// tools that do not accept -version
static final String[] BLACKLIST_VERSION = {
"appletviewer",
"controlpanel",
"extcheck",
"idlj",
"jar",
"jarsigner",
"javac",
"java-rmi",
"java-rmi.cgi",
"javadoc",
"javah",
"javap",
"javaws",
"jcmd",
"jconsole",
"jdb",
"jhat",
"jcontrol",
"jinfo",
"jmap",
"jps",
"jrunscript",
"jsadebugd",
"jstack",
"jstat",
"jstatd",
"jvisualvm",
"keytool",
"kinit",
"klist",
"ktab",
"native2ascii",
"orbd",
"pack200",
"packager",
"policytool",
"rmic",
"rmid",
"rmiregistry",
"schemagen",
"schemagen", // returns error code 127
"serialver",
"servertool",
"tnameserv",
"unpack200",
"wsgen",
"wsimport",
"xjc"
......@@ -85,53 +103,11 @@ public class VersionCheck {
static String refVersion;
static String refFullVersion;
private static List<String> getProcessStreamAsList(boolean javaDebug,
String... argv) {
List<String> out = new ArrayList<String>();
List<String> javaCmds = new ArrayList<String>();
String prog = javaBin + File.separator + argv[0];
if (System.getProperty("os.name").startsWith("Windows")) {
prog = prog.concat(".exe");
}
javaCmds.add(prog);
for (int i = 1; i < argv.length; i++) {
javaCmds.add(argv[i]);
}
ProcessBuilder pb = new ProcessBuilder(javaCmds);
Map<String, String> env = pb.environment();
if (javaDebug) {
env.put("_JAVA_LAUNCHER_DEBUG", "true");
}
try {
Process p = pb.start();
BufferedReader r = (javaDebug) ?
new BufferedReader(new InputStreamReader(p.getInputStream())) :
new BufferedReader(new InputStreamReader(p.getErrorStream())) ;
String s = r.readLine();
while (s != null) {
out.add(s.trim());
s = r.readLine();
}
p.waitFor();
p.destroy();
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
return out;
}
static String getVersion(String... argv) {
List<String> alist = getProcessStreamAsList(false, argv);
if (alist.size() == 0) {
throw new AssertionError("unexpected process returned null");
}
TestHelper.TestResult tr = doExec(argv);
StringBuilder out = new StringBuilder();
// remove the HotSpot line
for (String x : alist) {
for (String x : tr.testOutput) {
if (!x.matches(".*Client.*VM.*|.*Server.*VM.*")) {
out = out.append(x + "\n");
}
......@@ -139,9 +115,28 @@ public class VersionCheck {
return out.toString();
}
static boolean compareVersionStrings() {
/*
* this tests if the tool can take a version string and returns
* a 0 exit code, it is not possible to validate the contents
* of the -version output as they are inconsistent.
*/
static boolean testToolVersion() {
TestResult tr = null;
TestHelper.testExitValue = 0;
for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_VERSION))) {
String x = f.getAbsolutePath();
System.out.println("Testing (-version): " + x);
tr = doExec(x, "-version");
tr.checkPositive();
}
return TestHelper.testExitValue == 0;
}
static boolean compareJVersionStrings() {
int failcount = 0;
for (String x : programs) {
for (File f : new File(JAVA_BIN).listFiles(new ToolFilter(BLACKLIST_JOPTION))) {
String x = f.getAbsolutePath();
System.out.println("Testing (-J-version): " + x);
String testStr;
testStr = getVersion(x, "-J-version");
......@@ -185,8 +180,14 @@ public class VersionCheck {
String expectedDotVersion = "dotversion:" + jdkMajor + "." + jdkMinor;
String expectedFullVersion = "fullversion:" + bStr;
List<String> alist = getProcessStreamAsList(true, "java", "-version");
Map<String, String> envMap = new HashMap<>();
envMap.put(TestHelper.JLDEBUG_KEY, "true");
TestHelper.TestResult tr = doExec(envMap, javaCmd, "-version");
List<String> alist = new ArrayList<>();
alist.addAll(tr.testOutput);
for (String x : tr.testOutput) {
alist.add(x.trim());
}
if (!alist.contains(expectedDotVersion)) {
System.out.println("Error: could not find " + expectedDotVersion);
failcount++;
......@@ -202,21 +203,46 @@ public class VersionCheck {
// Initialize
static void init() {
String javaHome = System.getProperty("java.home");
if (javaHome.endsWith("jre")) {
javaHome = new File(javaHome).getParent();
}
javaBin = javaHome + File.separator + "bin";
refVersion = getVersion("java", "-version");
refFullVersion = getVersion("java", "-fullversion");
refVersion = getVersion(javaCmd, "-version");
refFullVersion = getVersion(javaCmd, "-fullversion");
}
public static void main(String[] args) {
init();
if (compareVersionStrings() && compareInternalStrings()) {
if (compareJVersionStrings() &&
compareInternalStrings() &&
testToolVersion()) {
System.out.println("All Version string comparisons: PASS");
} else {
throw new AssertionError("Some tests failed");
}
}
static class ToolFilter implements FileFilter {
final Iterable<String> exclude ;
protected ToolFilter(String... exclude) {
List<String> tlist = new ArrayList<>();
this.exclude = tlist;
for (String x : exclude) {
String str = x + ((isWindows) ? EXE_FILE_EXT : "");
tlist.add(str.toLowerCase());
}
}
@Override
public boolean accept(File pathname) {
if (!pathname.isFile() || !pathname.canExecute()) {
return false;
}
String name = pathname.getName().toLowerCase();
if (isWindows && !name.endsWith(EXE_FILE_EXT)) {
return false;
}
for (String x : exclude) {
if (name.endsWith(x)) {
return false;
}
}
return true;
}
}
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册