< prev index next >

test/jdk/java/lang/invoke/lambda/LogGeneratedClassesTest.java

Print this page
@@ -22,38 +22,43 @@
   */
  
  /*
   * @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;
  import java.nio.file.Files;
  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
      public void setup() throws IOException {
          final List<String> scratch = new ArrayList<>();

@@ -72,13 +77,12 @@
          scratch.add("        r.run();");
          scratch.add("        System.out.println(\"Finish\");");
          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);
          scratch.add(0, "public class LongPackageName {");
          StringBuilder sb = new StringBuilder("com.example.");

@@ -89,13 +93,14 @@
          sb.append("enough");
          longFQCN = sb.toString() + ".LongPackageName";
          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
      public void cleanup() throws IOException {
          Files.delete(Paths.get("TestLambda.java"));

@@ -105,82 +110,78 @@
          TestUtil.removeAll(Paths.get("dump"));
          TestUtil.removeAll(Paths.get("dumpLong"));
      }
  
      @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(
                          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 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) {
          if (!Files.isDirectory(p)) {
              return false;

@@ -203,11 +204,11 @@
              }
          }
      }
  
      @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
              // We would like to skip the test with a cause with
              //     throw new SkipException("Posix not supported");

@@ -228,39 +229,37 @@
                  // (e.g. current user is super user).
                  System.out.println("WARNING: The dump directory is writeable. Skipping testDumpDirNotWritable test.");
                  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");
          // dumpLong/DUMP_LAMBDA_PROXY_CLASS_FILES/com/example/nonsense/nonsense
          Path dumpPath = dumpDir.resolve("com/example/nonsense");

@@ -277,8 +276,7 @@
                   });
          }
          assertEquals(Files.walk(dumpDir)
                  .filter(filter)
                  .count(), 5, "Two lambda captured failed to log");
-         tr.assertZero("Should still return 0");
      }
  }
< prev index next >