8319567: Update java/lang/invoke tests to support vm flags

8319568: Update java/lang/reflect/exeCallerAccessTest/CallerAccessTest.java to accept vm flags
8319672: Several classloader tests ignore VM flags
8319676: A couple of jdk/modules/incubator/ tests ignore VM flags
8319677: Test jdk/internal/misc/VM/RuntimeArguments.java should be marked as flagless

Reviewed-by: jvernee, lmesnik
This commit is contained in:
Mandy Chung 2023-11-16 22:40:22 +00:00
parent 9727f4bddd
commit 1588dd934c
17 changed files with 219 additions and 434 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2000, 2020, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2000, 2023, 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,6 +24,8 @@
/*
* @test
* @bug 4290640 4785473
* @requires vm.flagless
* @library /test/lib
* @build package1.Class1 package2.Class2 package1.package3.Class3 Assert
* @run main/othervm Assert
* @summary Test the assertion facility
@ -31,12 +33,17 @@
* @key randomness
*/
import jdk.test.lib.process.OutputAnalyzer;
import package1.*;
import package2.*;
import package1.package3.*;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import static jdk.test.lib.process.ProcessTools.*;
public class Assert {
private static Class1 testClass1;
@ -56,7 +63,7 @@ public class Assert {
* off at class load time. Once the class is loaded its assertion status
* does not change.
*/
public static void main(String[] args) throws Exception {
public static void main(String[] args) throws Throwable {
// Switch values: 0=don't touch, 1=off, 2 = on
int[] switches = new int[7];
@ -77,28 +84,17 @@ public class Assert {
}
// Spawn new VM and load classes
String command = System.getProperty("java.home") +
File.separator + "bin" + File.separator + "java Assert";
StringBuffer commandString = new StringBuffer(command);
List<String> commands = new ArrayList<>();
commands.add("Assert");
for(int j=0; j<7; j++)
commandString.append(" "+switches[j]);
Process p = null;
p = Runtime.getRuntime().exec(commandString.toString());
commands.add(Integer.toString(switches[j]));
OutputAnalyzer outputAnalyzer = executeCommand(createLimitedTestJavaProcessBuilder(commands));
if (debug) { // See output of test VMs
BufferedReader blah = new BufferedReader(
new InputStreamReader(p.getInputStream()));
String outString = blah.readLine();
while (outString != null) {
System.out.println("from BufferedReader:"+outString);
outString = blah.readLine();
}
outputAnalyzer.asLines()
.stream()
.forEach(s -> System.out.println(s));
}
p.waitFor();
int result = p.exitValue();
int result = outputAnalyzer.getExitValue();
if (debug) { // See which switch configs failed
if (result == 0) {
for(int k=6; k>=0; k--)

View File

@ -24,6 +24,7 @@
/*
* @test
* @bug 8060130
* @requires vm.flagless
* @library /test/lib
* @build package2.Class2 GetSystemPackage
* @summary Test if getSystemPackage() return consistent values for cases
@ -41,6 +42,8 @@ import java.util.jar.Attributes;
import java.util.jar.JarEntry;
import java.util.jar.JarOutputStream;
import java.util.jar.Manifest;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
public class GetSystemPackage {
@ -118,8 +121,9 @@ public class GetSystemPackage {
private static void runSubProcess(String messageOnError, String ... args)
throws Exception
{
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(args);
int res = pb.directory(tmpFolder).inheritIO().start().waitFor();
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(args)
.directory(tmpFolder);
int res = ProcessTools.executeProcess(pb).getExitValue();
if (res != 0) {
throw new RuntimeException(messageOnError);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2014, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2014, 2023, 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
@ -40,11 +40,9 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import jdk.test.lib.JDKToolFinder;
import static jdk.test.lib.process.ProcessTools.*;
import org.testng.annotations.BeforeTest;
@ -144,26 +142,14 @@ public class GetResource {
private void runTest(Path dir, List<String> options, String expected)
throws Throwable
{
String javapath = JDKToolFinder.getJDKTool("java");
List<String> cmdLine = new ArrayList<>();
cmdLine.add(javapath);
options.forEach(cmdLine::add);
cmdLine.add("GetResource");
cmdLine.add(expected);
System.out.println("Command line: " + cmdLine);
ProcessBuilder pb =
new ProcessBuilder(cmdLine.stream().toArray(String[]::new));
// change working directory
pb.directory(dir.toFile());
// remove CLASSPATH environment variable
Map<String,String> env = pb.environment();
String value = env.remove("CLASSPATH");
ProcessBuilder pb = createTestJavaProcessBuilder(cmdLine);
pb.directory(dir.toFile()); // change working directory
pb.environment().remove("CLASSPATH"); // remove CLASSPATH environment variable
executeCommand(pb).shouldHaveExitValue(0);
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2021, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2021, BELLSOFT. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
@ -38,31 +38,15 @@
*/
import jdk.test.lib.Asserts;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.process.OutputAnalyzer;
import java.lang.ProcessBuilder;
import java.lang.Process;
import java.io.File;
import java.util.*;
import static jdk.test.lib.process.ProcessTools.*;
public class LoadLibraryUnloadTest {
private static String testClassPath = System.getProperty("test.classes");
private static String testLibraryPath = System.getProperty("test.nativepath");
private static Process runJavaCommand(String... command) throws Throwable {
String java = JDKToolFinder.getJDKTool("java");
List<String> commands = new ArrayList<>();
Collections.addAll(commands, java);
Collections.addAll(commands, command);
System.out.println("COMMAND: " + String.join(" ", commands));
return new ProcessBuilder(commands.toArray(new String[0]))
.redirectErrorStream(true)
.directory(new File(testClassPath))
.start();
}
private final static long countLines(OutputAnalyzer output, String string) {
return output.asLines()
.stream()
@ -78,12 +62,10 @@ public class LoadLibraryUnloadTest {
public static void main(String[] args) throws Throwable {
Process process = runJavaCommand(
OutputAnalyzer outputAnalyzer = executeCommand(createTestJavaProcessBuilder(
"-Dtest.classes=" + testClassPath,
"-Djava.library.path=" + testLibraryPath,
"LoadLibraryUnload");
OutputAnalyzer outputAnalyzer = new OutputAnalyzer(process);
"LoadLibraryUnload"));
dump(outputAnalyzer);
Asserts.assertTrue(

View File

@ -44,7 +44,7 @@ import jdk.test.lib.compiler.InMemoryJavaCompiler;
*/
public class CondyNestedResolutionTest {
public static void main(String args[]) throws Throwable {
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder("CondyNestedResolution");
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder("CondyNestedResolution");
OutputAnalyzer oa = new OutputAnalyzer(pb.start());
oa.shouldContain("StackOverflowError");
oa.shouldContain("bsm1arg");

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, 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
@ -37,13 +37,11 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.process.ProcessTools;
import static jdk.test.lib.process.ProcessTools.*;
import org.testng.annotations.Test;
public class FindSpecialTest {
static final String JAVA_LAUNCHER = JDKToolFinder.getJDKTool("java");
static final String TEST_CLASSES = System.getProperty("test.classes", ".");
static final String TEST_CLASS_PATH = System.getProperty("test.class.path");
static final String TEST_MAIN_CLASS = "test.FindSpecial";
@ -59,8 +57,9 @@ public class FindSpecialTest {
throw new Error(m1 + " not exist");
}
String classpath = m1.toString() + File.pathSeparator + TEST_CLASS_PATH;
ProcessTools.executeCommand(JAVA_LAUNCHER, "-cp", classpath, TEST_MAIN_CLASS)
.shouldHaveExitValue(0);
executeCommand(createTestJavaProcessBuilder("-cp", classpath,
TEST_MAIN_CLASS))
.shouldHaveExitValue(0);
}
/*
@ -72,10 +71,9 @@ public class FindSpecialTest {
if (Files.notExists(modules)) {
throw new Error(modules + " not exist");
}
ProcessTools.executeCommand(JAVA_LAUNCHER,
"-cp", TEST_CLASS_PATH,
"-p", modules.toString(),
"-m", TEST_MODULE + "/" + TEST_MAIN_CLASS)
.shouldHaveExitValue(0);
executeCommand(createTestJavaProcessBuilder("-cp", TEST_CLASS_PATH,
"-p", modules.toString(),
"-m", TEST_MODULE + "/" + TEST_MAIN_CLASS))
.shouldHaveExitValue(0);
}
}

View File

@ -1,171 +0,0 @@
/*
* Copyright (c) 2012, 2013, 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.
*/
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
/*
* support infrastructure to invoke a java class from the command line
*/
class LUtils {
static final com.sun.tools.javac.Main javac =
new com.sun.tools.javac.Main();
static final File cwd = new File(".").getAbsoluteFile();
static final String JAVAHOME = System.getProperty("java.home");
static final boolean isWindows =
System.getProperty("os.name", "unknown").startsWith("Windows");
static final File JAVA_BIN_FILE = new File(JAVAHOME, "bin");
static final File JAVA_CMD = new File(JAVA_BIN_FILE,
isWindows ? "java.exe" : "java");
static final File JAR_BIN_FILE = new File(JAVAHOME, "bin");
static final File JAR_CMD = new File(JAR_BIN_FILE,
isWindows ? "jar.exe" : "jar");
protected LUtils() {
}
public static void compile(String... args) {
if (javac.compile(args) != 0) {
throw new RuntimeException("compilation fails");
}
}
static void createFile(File outFile, List<String> content) {
try {
Files.write(outFile.getAbsoluteFile().toPath(), content,
Charset.defaultCharset());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
static File getClassFile(File javaFile) {
return javaFile.getName().endsWith(".java")
? new File(javaFile.getName().replace(".java", ".class"))
: null;
}
static String getSimpleName(File inFile) {
String fname = inFile.getName();
return fname.substring(0, fname.indexOf("."));
}
static TestResult doExec(String... cmds) {
return doExec(null, null, cmds);
}
/*
* A method which executes a java cmd and returns the results in a container
*/
static TestResult doExec(Map<String, String> envToSet,
java.util.Set<String> envToRemove, String... cmds) {
String cmdStr = "";
for (String x : cmds) {
cmdStr = cmdStr.concat(x + " ");
}
ProcessBuilder pb = new ProcessBuilder(cmds);
Map<String, String> env = pb.environment();
if (envToRemove != null) {
for (String key : envToRemove) {
env.remove(key);
}
}
if (envToSet != null) {
env.putAll(envToSet);
}
BufferedReader rdr = null;
try {
List<String> outputList = new ArrayList<>();
pb.redirectErrorStream(true);
Process p = pb.start();
rdr = new BufferedReader(new InputStreamReader(p.getInputStream()));
String in = rdr.readLine();
while (in != null) {
outputList.add(in);
in = rdr.readLine();
}
p.waitFor();
p.destroy();
return new TestResult(cmdStr, p.exitValue(), outputList,
env, new Throwable("current stack of the test"));
} catch (Exception ex) {
ex.printStackTrace();
throw new RuntimeException(ex.getMessage());
}
}
static class TestResult {
String cmd;
int exitValue;
List<String> testOutput;
Map<String, String> env;
Throwable t;
public TestResult(String str, int rv, List<String> oList,
Map<String, String> env, Throwable t) {
cmd = str;
exitValue = rv;
testOutput = oList;
this.env = env;
this.t = t;
}
void assertZero(String message) {
if (exitValue != 0) {
System.err.println(this);
throw new RuntimeException(message);
}
}
@Override
public String toString() {
StringWriter sw = new StringWriter();
PrintWriter status = new PrintWriter(sw);
status.println("Cmd: " + cmd);
status.println("Return code: " + exitValue);
status.println("Environment variable:");
for (String x : env.keySet()) {
status.println("\t" + x + "=" + env.get(x));
}
status.println("Output:");
for (String x : testOutput) {
status.println("\t" + x);
}
status.println("Exception:");
status.println(t.getMessage());
t.printStackTrace(status);
return sw.getBuffer().toString();
}
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2016, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023, 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,19 +24,28 @@
/*
* @test
* @bug 8003881
* @summary tests DoPrivileged action (implemented as lambda expressions) by
* inserting them into the BootClassPath.
* @library /test/lib/
* @modules jdk.compiler
* jdk.zipfs
* @compile -XDignore.symbol.file LambdaAccessControlDoPrivilegedTest.java LUtils.java
* @compile LambdaAccessControlDoPrivilegedTest.java
* @run main/othervm -Djava.security.manager=allow LambdaAccessControlDoPrivilegedTest
* @summary tests DoPrivileged action (implemented as lambda expressions) by
* inserting them into the BootClassPath.
*/
import java.io.File;
import jdk.test.lib.process.OutputAnalyzer;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import java.util.spi.ToolProvider;
public class LambdaAccessControlDoPrivilegedTest extends LUtils {
public static void main(String... args) {
import static jdk.test.lib.process.ProcessTools.*;
public class LambdaAccessControlDoPrivilegedTest {
public static void main(String... args) throws Exception {
final List<String> scratch = new ArrayList();
scratch.clear();
scratch.add("import java.security.*;");
@ -47,9 +56,9 @@ public class LambdaAccessControlDoPrivilegedTest extends LUtils {
scratch.add("});");
scratch.add("}");
scratch.add("}");
File doprivJava = new File("DoPriv.java");
File doprivClass = getClassFile(doprivJava);
createFile(doprivJava, scratch);
Path doprivJava = Path.of("DoPriv.java");
Path doprivClass = Path.of("DoPriv.class");
Files.write(doprivJava, scratch, Charset.defaultCharset());
scratch.clear();
scratch.add("public class Bar {");
@ -59,30 +68,40 @@ public class LambdaAccessControlDoPrivilegedTest extends LUtils {
scratch.add("}");
scratch.add("}");
File barJava = new File("Bar.java");
File barClass = getClassFile(barJava);
createFile(barJava, scratch);
Path barJava = Path.of("Bar.java");
Path barClass = Path.of("Bar.class");
Files.write(barJava, scratch, Charset.defaultCharset());
String[] javacArgs = {barJava.getName(), doprivJava.getName()};
compile(javacArgs);
File jarFile = new File("foo.jar");
String[] jargs = {"cvf", jarFile.getName(), doprivClass.getName()};
TestResult tr = doExec(JAR_CMD.getAbsolutePath(),
"cvf", jarFile.getName(),
doprivClass.getName());
if (tr.exitValue != 0){
throw new RuntimeException(tr.toString());
compile(barJava.toString(), doprivJava.toString());
jar("cvf", "foo.jar", doprivClass.toString());
Files.delete(doprivJava);
Files.delete(doprivClass);
ProcessBuilder pb = createTestJavaProcessBuilder(
"-Xbootclasspath/a:foo.jar",
"-cp", ".",
"-Djava.security.manager=allow",
"Bar");
executeProcess(pb).shouldHaveExitValue(0);
Files.delete(barJava);
Files.delete(barClass);
Files.delete(Path.of("foo.jar"));
}
static final ToolProvider JAR_TOOL = ToolProvider.findFirst("jar").orElseThrow();
static final ToolProvider JAVAC = ToolProvider.findFirst("javac").orElseThrow();
static void compile(String... args) throws IOException {
if (JAVAC.run(System.out, System.err, args) != 0) {
throw new RuntimeException("compilation fails");
}
}
static void jar(String... args) {
int rc = JAR_TOOL.run(System.out, System.err, args);
if (rc != 0){
throw new RuntimeException("fail to create JAR file");
}
doprivJava.delete();
doprivClass.delete();
tr = doExec(JAVA_CMD.getAbsolutePath(),
"-Xbootclasspath/a:foo.jar",
"-cp", ".",
"-Djava.security.manager=allow",
"Bar");
tr.assertZero("testDoPrivileged fails");
barJava.delete();
barClass.delete();
jarFile.delete();
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2023, 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,14 +24,12 @@
/*
* @test
* @bug 8003881
* @summary tests Lambda expression with a security manager at top level
* @modules jdk.compiler
* @compile -XDignore.symbol.file LambdaAccessControlTest.java LUtils.java
*
* @run main/othervm -Djava.security.manager=allow LambdaAccessControlTest
* @summary tests Lambda expression with a security manager at top level
*/
public class LambdaAccessControlTest extends LUtils {
public class LambdaAccessControlTest {
public static void main(String... args) {
System.setSecurityManager(new SecurityManager());
JJ<Integer> iii = (new CC())::impl;

View File

@ -24,13 +24,14 @@
/*
* @test
* @bug 8027232
* @summary ensures that j.l.i.InvokerByteCodeGenerator and ASM visitMethodInsn
* generate bytecodes with correct constant pool references
* @library /test/lib/
* @modules java.base/jdk.internal.org.objectweb.asm
* jdk.jdeps/com.sun.tools.classfile
* jdk.zipfs
* @compile -XDignore.symbol.file LambdaAsm.java LUtils.java
* @compile LambdaAsm.java
* @run main/othervm LambdaAsm
* @summary ensures that j.l.i.InvokerByteCodeGenerator and ASM visitMethodInsn
* generate bytecodes with correct constant pool references
*/
import com.sun.tools.classfile.Attribute;
import com.sun.tools.classfile.ClassFile;
@ -40,34 +41,36 @@ import com.sun.tools.classfile.ConstantPool.CPInfo;
import com.sun.tools.classfile.Instruction;
import com.sun.tools.classfile.Method;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.ArrayList;
import java.nio.file.DirectoryStream;
import java.nio.file.Path;
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.internal.org.objectweb.asm.MethodVisitor;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.process.OutputAnalyzer;
import static java.nio.file.Files.*;
import static jdk.internal.org.objectweb.asm.Opcodes.*;
import static jdk.test.lib.process.ProcessTools.*;
public class LambdaAsm {
static final Path DUMP_LAMBDA_PROXY_CLASS_FILES = Path.of("DUMP_LAMBDA_PROXY_CLASS_FILES");
static final Path SRC = Path.of("src");
static final Path CLASSES = Path.of("classes");
static final File TestFile = new File("A.java");
static void init() {
static void init() throws Exception {
emitCode();
LUtils.compile(TestFile.getName());
LUtils.TestResult tr = LUtils.doExec(LUtils.JAVA_CMD.getAbsolutePath(),
CompilerUtils.compile(SRC, CLASSES);
OutputAnalyzer outputAnalyzer = executeProcess(createTestJavaProcessBuilder(
"-Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles=true",
"-cp", ".", "A");
if (tr.exitValue != 0) {
System.out.println("Error: " + tr.toString());
throw new RuntimeException("could not create proxy classes");
}
"-cp", CLASSES.toString(), "A"));
outputAnalyzer.shouldHaveExitValue(0);
}
static void emitCode() {
static void emitCode() throws IOException {
ArrayList<String> scratch = new ArrayList<>();
scratch.add("import java.util.function.*;");
scratch.add("class A {");
@ -89,7 +92,10 @@ public class LambdaAsm {
scratch.add(" I.d();");
scratch.add(" }");
scratch.add("}");
LUtils.createFile(TestFile, scratch);
Path testFile = SRC.resolve("A.java");
Files.createDirectories(SRC);
Files.write(testFile, scratch, Charset.defaultCharset());
}
static void checkMethod(String cname, String mname, ConstantPool cp,

View File

@ -24,20 +24,23 @@
/*
* @test
* @bug 8025636
* @summary Synthetic frames should be hidden in exceptions
* @library /test/lib/
* @modules java.base/jdk.internal.org.objectweb.asm
* jdk.compiler
* @compile -XDignore.symbol.file LUtils.java LambdaStackTrace.java
* @compile LambdaStackTrace.java
* @run main LambdaStackTrace
* @summary Synthetic frames should be hidden in exceptions
*/
import jdk.internal.org.objectweb.asm.ClassWriter;
import jdk.test.lib.compiler.CompilerUtils;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import static jdk.internal.org.objectweb.asm.Opcodes.ACC_ABSTRACT;
@ -47,7 +50,7 @@ import static jdk.internal.org.objectweb.asm.Opcodes.V1_7;
public class LambdaStackTrace {
static File classes = new File(System.getProperty("test.classes"));
static Path CLASSES = Path.of(System.getProperty("test.classes", "."));
public static void main(String[] args) throws Exception {
testBasic();
@ -121,12 +124,8 @@ public class LambdaStackTrace {
// We can't let javac compile these interfaces because in > 1.8 it will insert
// bridge methods into the interfaces - we want code that looks like <= 1.7,
// so we generate it.
try (FileOutputStream fw = new FileOutputStream(new File(classes, "Maker.class"))) {
fw.write(generateMaker());
}
try (FileOutputStream fw = new FileOutputStream(new File(classes, "StringMaker.class"))) {
fw.write(generateStringMaker());
}
Files.write(CLASSES.resolve("Maker.class"), generateMaker());
Files.write(CLASSES.resolve("StringMaker.class"), generateStringMaker());
}
private static byte[] generateMaker() {
@ -154,7 +153,7 @@ public class LambdaStackTrace {
}
static void emitCode(File f) {
static void emitCode(Path file) throws IOException {
ArrayList<String> scratch = new ArrayList<>();
scratch.add("public class Caller {");
scratch.add(" public static void callStringMaker() {");
@ -166,13 +165,17 @@ public class LambdaStackTrace {
scratch.add(" ((Maker) sm).make();"); // <-- This will call the bridge method
scratch.add(" }");
scratch.add("}");
LUtils.createFile(f, scratch);
Files.write(file, scratch, Charset.defaultCharset());
}
static void compileCaller() {
File caller = new File(classes, "Caller.java");
static void compileCaller() throws IOException {
Path src = Path.of("src");
Files.createDirectories(src);
Path caller = src.resolve("Caller.java");
emitCode(caller);
LUtils.compile("-cp", classes.getAbsolutePath(), "-d", classes.getAbsolutePath(), caller.getAbsolutePath());
CompilerUtils.compile(src, CLASSES, "-cp", CLASSES.toAbsolutePath().toString());
}
private static void verifyFrames(StackTraceElement[] stack, String... patterns) throws Exception {

View File

@ -24,14 +24,16 @@
/*
* @test
* @bug 8023524 8304846
* @summary tests logging generated classes for lambda
* @requires vm.flagless
* @library /test/lib/
* @library /java/nio/file
* @modules jdk.compiler
* jdk.zipfs
* @run testng LogGeneratedClassesTest
* @summary tests logging generated classes for lambda
*/
import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;
@ -40,18 +42,21 @@ import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.PosixFileAttributeView;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.process.OutputAnalyzer;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import org.testng.SkipException;
import static java.nio.file.attribute.PosixFilePermissions.*;
import static jdk.test.lib.process.ProcessTools.*;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertFalse;
import static org.testng.Assert.assertTrue;
public class LogGeneratedClassesTest extends LUtils {
public class LogGeneratedClassesTest {
static final Path DUMP_LAMBDA_PROXY_CLASS_FILES = Path.of("DUMP_LAMBDA_PROXY_CLASS_FILES");
static final Path CLASSES = Path.of("classes").toAbsolutePath();
String longFQCN;
@BeforeClass
@ -74,9 +79,8 @@ public class LogGeneratedClassesTest extends LUtils {
scratch.add(" }");
scratch.add("}");
File test = new File("TestLambda.java");
createFile(test, scratch);
compile("-d", ".", test.getName());
Path testLambda = Path.of("TestLambda.java");
Files.write(testLambda, scratch, Charset.defaultCharset());
scratch.remove(0);
scratch.remove(0);
@ -91,9 +95,10 @@ public class LogGeneratedClassesTest extends LUtils {
sb.append(";");
sb.insert(0, "package ");
scratch.add(0, sb.toString());
test = new File("LongPackageName.java");
createFile(test, scratch);
compile("-d", ".", test.getName());
Path lpnTest = Path.of("LongPackageName.java");
Files.write(lpnTest, scratch, Charset.defaultCharset());
CompilerUtils.compile(Path.of("."), CLASSES);
}
@AfterClass
@ -107,49 +112,49 @@ public class LogGeneratedClassesTest extends LUtils {
}
@Test
public void testNotLogging() {
TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
"-cp", ".",
public void testNotLogging() throws Exception {
ProcessBuilder pb = createLimitedTestJavaProcessBuilder(
"-cp", CLASSES.toString(),
"-Djava.security.manager=allow",
"com.example.TestLambda");
tr.assertZero("Should still return 0");
executeProcess(pb).shouldHaveExitValue(0);
}
@Test
public void testLogging() throws IOException {
public void testLogging() throws Exception {
Path testDir = Path.of("dump");
Path dumpDir = testDir.resolve(DUMP_LAMBDA_PROXY_CLASS_FILES);
Files.createDirectory(testDir);
TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
"-cp", "..",
"-Duser.dir=" + testDir.toAbsolutePath(),
ProcessBuilder pb = createLimitedTestJavaProcessBuilder(
"-cp", CLASSES.toString(),
"-Djava.security.manager=allow",
"-Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles",
"com.example.TestLambda");
"com.example.TestLambda").directory(testDir.toFile());
executeProcess(pb).shouldHaveExitValue(0);
// 2 our own class files. We don't care about the others
assertEquals(Files.find(
dumpDir,
99,
(p, a) -> p.startsWith(dumpDir.resolve("com/example"))
&& a.isRegularFile()).count(),
2, "Two lambda captured");
tr.assertZero("Should still return 0");
2, "Two lambda captured");
}
@Test
public void testDumpDirNotExist() throws IOException {
public void testDumpDirNotExist() throws Exception {
Path testDir = Path.of("NotExist");
Path dumpDir = testDir.resolve(DUMP_LAMBDA_PROXY_CLASS_FILES);
Files.createDirectory(testDir);
TestUtil.removeAll(dumpDir);
assertFalse(Files.exists(dumpDir));
TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
"-cp", "..",
"-Duser.dir=" + testDir.toAbsolutePath(),
"-Djava.security.manager=allow",
"-Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles",
"com.example.TestLambda");
ProcessBuilder pb = createLimitedTestJavaProcessBuilder(
"-cp", CLASSES.toString(),
"-Djava.security.manager=allow",
"-Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles",
"com.example.TestLambda").directory(testDir.toFile());
executeProcess(pb).shouldHaveExitValue(0);
// The dump directory will be created if not exist
assertEquals(Files.find(
@ -157,28 +162,24 @@ public class LogGeneratedClassesTest extends LUtils {
99,
(p, a) -> p.startsWith(dumpDir.resolve("com/example"))
&& a.isRegularFile()).count(),
2, "Two lambda captured");
tr.assertZero("Should still return 0");
2, "Two lambda captured");
}
@Test
public void testDumpDirIsFile() throws IOException {
public void testDumpDirIsFile() throws Exception {
Path testDir = Path.of("notDir");
Path dumpFile = testDir.resolve(DUMP_LAMBDA_PROXY_CLASS_FILES);
Files.createDirectory(testDir);
Files.createFile(dumpFile);
assertTrue(Files.isRegularFile(dumpFile));
TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
"-cp", "..",
"-Duser.dir=" + testDir.toAbsolutePath(),
"-Djava.security.manager=allow",
"-Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles",
"com.example.TestLambda");
assertEquals(tr.testOutput.stream()
.filter(s -> s.contains("DUMP_LAMBDA_PROXY_CLASS_FILES is not a directory"))
.count(),
1, "only show error once");
assertTrue(tr.exitValue != 0);
ProcessBuilder pb = createLimitedTestJavaProcessBuilder(
"-cp", CLASSES.toString(),
"-Djava.security.manager=allow",
"-Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles",
"com.example.TestLambda").directory(testDir.toFile());
executeProcess(pb)
.shouldContain("DUMP_LAMBDA_PROXY_CLASS_FILES is not a directory")
.shouldNotHaveExitValue(0);
}
private static boolean isWriteableDirectory(Path p) {
@ -205,7 +206,7 @@ public class LogGeneratedClassesTest extends LUtils {
}
@Test
public void testDumpDirNotWritable() throws IOException {
public void testDumpDirNotWritable() throws Exception {
if (!Files.getFileStore(Paths.get("."))
.supportsFileAttributeView(PosixFileAttributeView.class)) {
// No easy way to setup readonly directory without POSIX
@ -230,35 +231,33 @@ public class LogGeneratedClassesTest extends LUtils {
return;
}
TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
"-cp", "..",
"-Duser.dir=" + testDir.toAbsolutePath(),
ProcessBuilder pb = createLimitedTestJavaProcessBuilder(
"-cp", CLASSES.toString(),
"-Djava.security.manager=allow",
"-Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles",
"com.example.TestLambda");
assertEquals(tr.testOutput.stream()
.filter(s -> s.contains("DUMP_LAMBDA_PROXY_CLASS_FILES is not writable"))
.count(),
1, "only show error once");
assertTrue(tr.exitValue != 0);
"com.example.TestLambda").directory(testDir.toFile());
executeProcess(pb)
.shouldContain("DUMP_LAMBDA_PROXY_CLASS_FILES is not writable")
.shouldNotHaveExitValue(0);
} finally {
TestUtil.removeAll(testDir);
}
}
@Test
public void testLoggingException() throws IOException {
public void testLoggingException() throws Exception {
Path testDir = Path.of("dumpLong");
Path dumpDir = testDir.resolve(DUMP_LAMBDA_PROXY_CLASS_FILES);
Files.createDirectories(dumpDir.resolve("com/example/nonsense"));
Files.createFile(dumpDir.resolve("com/example/nonsense/nonsense"));
TestResult tr = doExec(JAVA_CMD.getAbsolutePath(),
"-cp", "..",
"-Duser.dir=" + testDir.toAbsolutePath(),
ProcessBuilder pb = createLimitedTestJavaProcessBuilder(
"-cp", CLASSES.toString(),
"-Djava.security.manager=allow",
"-Djdk.invoke.LambdaMetafactory.dumpProxyClassFiles",
longFQCN);
assertEquals(tr.testOutput.stream()
longFQCN).directory(testDir.toFile());
OutputAnalyzer outputAnalyzer = executeProcess(pb);
outputAnalyzer.shouldHaveExitValue(0);
assertEquals(outputAnalyzer.asLines().stream()
.filter(s -> s.startsWith("WARNING: Exception"))
.count(),
2, "show error each capture");
@ -279,6 +278,5 @@ public class LogGeneratedClassesTest extends LUtils {
assertEquals(Files.walk(dumpDir)
.filter(filter)
.count(), 5, "Two lambda captured failed to log");
tr.assertZero("Should still return 0");
}
}

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2019, 2022, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2019, 2023, 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
@ -31,37 +31,15 @@
// Test disabled on AIX since we cannot invoke the JVM on the primordial thread.
import java.io.File;
import java.util.Map;
import jdk.test.lib.Platform;
import jdk.test.lib.process.OutputAnalyzer;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.Paths;
import jdk.test.lib.process.ProcessTools;
public class CallerAccessTest {
public static void main(String[] args) throws IOException {
Path launcher = Paths.get(System.getProperty("test.nativepath"), "CallerAccessTest");
ProcessBuilder pb = new ProcessBuilder(launcher.toString());
Map<String, String> env = pb.environment();
String libDir = Platform.libDir().toString();
String vmDir = Platform.jvmLibDir().toString();
// set up shared library path
String sharedLibraryPathEnvName = Platform.sharedLibraryPathVariableName();
env.compute(sharedLibraryPathEnvName,
(k, v) -> (v == null) ? libDir : v + File.pathSeparator + libDir);
env.compute(sharedLibraryPathEnvName,
(k, v) -> (v == null) ? vmDir : v + File.pathSeparator + vmDir);
System.out.println("Launching: " + launcher + " shared library path: " +
env.get(sharedLibraryPathEnvName));
new OutputAnalyzer(pb.start())
.outputTo(System.out)
.errorTo(System.err)
.shouldHaveExitValue(0);
public static void main(String[] args) throws Exception {
ProcessBuilder pb = ProcessTools.createNativeTestProcessBuilder("CallerAccessTest");
System.out.println("Launching: " + pb.command() + " shared library path: " +
pb.environment().get(Platform.sharedLibraryPathVariableName()));
ProcessTools.executeProcess(pb).shouldHaveExitValue(0);
}
}

View File

@ -25,7 +25,6 @@ import java.io.File;
import java.nio.file.Files;
import java.nio.file.Path;
import jdk.test.lib.JDKToolFinder;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.process.OutputAnalyzer;
import jdk.test.lib.process.ProcessTools;
@ -34,12 +33,12 @@ import jdk.test.lib.util.JarBuilder;
/*
* @test
* @bug 8308184
* @summary Verify that an application can be launched when the classpath contains large number of
* jars and the java.protocol.handler.pkgs system property is set
* @library /test/lib/
* @build jdk.test.lib.util.JarBuilder jdk.test.lib.compiler.CompilerUtils
* jdk.test.lib.process.ProcessTools
* @run driver LargeClasspathWithPkgPrefix
* @summary Verify that an application can be launched when the classpath contains large number of
* jars and the java.protocol.handler.pkgs system property is set
*/
public class LargeClasspathWithPkgPrefix {
@ -126,8 +125,7 @@ public class LargeClasspathWithPkgPrefix {
// java -Djava.protocol.handler.pkgs=foo.bar.some.nonexistent.pkg -cp <classpath> Foo
private static void launchApplication(String classPath) throws Exception {
String java = JDKToolFinder.getJDKTool("java");
ProcessBuilder pb = new ProcessBuilder(java,
ProcessBuilder pb = ProcessTools.createTestJavaProcessBuilder(
"-Djava.protocol.handler.pkgs=foo.bar.some.nonexistent.pkg",
"-cp", classPath,
"Foo");

View File

@ -23,11 +23,12 @@
/**
* @test
* @summary Basic test of VM::getRuntimeArguments
* @requires vm.flagless
* @library /test/lib
* @modules java.base/jdk.internal.misc
* jdk.zipfs
* @run testng RuntimeArguments
* @summary Basic test of VM::getRuntimeArguments
*/
import java.io.IOException;
@ -46,7 +47,6 @@ import org.testng.annotations.Test;
import static org.testng.Assert.*;
public class RuntimeArguments {
static final String TEST_CLASSES = System.getProperty("test.classes");
static final List<String> VM_OPTIONS = getInitialOptions();
/*
@ -112,11 +112,9 @@ public class RuntimeArguments {
@Test(dataProvider = "options")
public void test(List<String> args, List<String> expected) throws Exception {
// launch a test program
// $ java <args> -classpath <cpath> RuntimeArguments <vm_options> <expected>
Stream<String> options = Stream.concat(args.stream(),
Stream.of("-classpath", TEST_CLASSES, "RuntimeArguments"));
// launch a test program with classpath set by ProcessTools::createLimitedTestJavaProcessBuilder
// $ java <args> RuntimeArguments <vm_options> <expected>
Stream<String> options = Stream.concat(args.stream(), Stream.of("RuntimeArguments"));
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(
// The runtime image may be created with jlink --add-options
// The initial VM options will be included in the result

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023, 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,11 +24,12 @@
/*
* @test
* @bug 8170859
* @summary Ensure no incubator modules are resolved by default in the image
* @requires vm.flagless
* @library /test/lib
* @modules jdk.compiler
* @build jdk.test.lib.compiler.CompilerUtils
* @run testng DefaultImage
* @summary Ensure no incubator modules are resolved by default in the image
*/
import java.io.ByteArrayOutputStream;
@ -43,6 +44,7 @@ import java.util.function.Consumer;
import java.util.stream.Stream;
import jdk.test.lib.compiler.CompilerUtils;
import jdk.test.lib.process.ProcessTools;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;
@ -108,10 +110,8 @@ public class DefaultImage {
static ToolResult java(String... opts) throws Throwable {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
PrintStream ps = new PrintStream(baos);
String[] options = Stream.concat(Stream.of(getJava()), Stream.of(opts))
.toArray(String[]::new);
ProcessBuilder pb = new ProcessBuilder(options);
ProcessBuilder pb = ProcessTools.createLimitedTestJavaProcessBuilder(opts);
int exitValue = executeCommand(pb).outputTo(ps)
.errorTo(ps)
.getExitValue();
@ -155,15 +155,6 @@ public class DefaultImage {
}
}
static String getJava() {
Path image = Paths.get(JAVA_HOME);
boolean isWindows = System.getProperty("os.name").startsWith("Windows");
Path java = image.resolve("bin").resolve(isWindows ? "java.exe" : "java");
if (Files.notExists(java))
throw new RuntimeException(java + " not found");
return java.toAbsolutePath().toString();
}
static boolean isExplodedBuild() {
Path modulesPath = Paths.get(JAVA_HOME).resolve("lib").resolve("modules");
return Files.notExists(modulesPath);

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2016, 2018, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2016, 2023, 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,7 +24,7 @@
/*
* @test
* @bug 8170859
* @summary Basic test for incubator modules in jmods and images
* @requires vm.flagless
* @library /test/lib
* @key intermittent
* @modules jdk.compiler jdk.jartool jdk.jlink
@ -32,6 +32,7 @@
* jdk.test.lib.util.FileUtils
* jdk.test.lib.compiler.CompilerUtils
* @run testng/othervm ImageModules
* @summary Basic test for incubator modules in jmods and images
*/
import java.io.ByteArrayOutputStream;