8152622: tools/pack200/Pack200Props.java timed out
authorksrini
Wed, 06 Apr 2016 09:38:39 -0700
changeset 36948 8727362c1c78
parent 36947 83319fc2c7ac
child 36949 7c9b86aeee10
8152622: tools/pack200/Pack200Props.java timed out Reviewed-by: alanb, sundar
jdk/test/ProblemList.txt
jdk/test/tools/pack200/ModuleAttributes.java
jdk/test/tools/pack200/Pack200Props.java
jdk/test/tools/pack200/Utils.java
--- a/jdk/test/ProblemList.txt	Wed Apr 06 21:31:48 2016 +0100
+++ b/jdk/test/ProblemList.txt	Wed Apr 06 09:38:39 2016 -0700
@@ -315,8 +315,6 @@
 
 tools/pack200/Pack200Test.java                                  8059906,8151901 generic-all
 
-tools/pack200/Pack200Props.java                                 8152622 macosx-all
-
 tools/launcher/FXLauncherTest.java                              8068049 linux-all,macosx-all
 
 ############################################################################
--- a/jdk/test/tools/pack200/ModuleAttributes.java	Wed Apr 06 21:31:48 2016 +0100
+++ b/jdk/test/tools/pack200/ModuleAttributes.java	Wed Apr 06 09:38:39 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2014, 2016, 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
@@ -39,38 +39,10 @@
     }
 
     public void run() throws Exception {
-        File file = createModuleJar();
+        File file = Utils.createRtJar(".*module-info\\.class");
         Utils.testWithRepack(file,
                 "--effort=1",
                 "--unknown-attribute=error");
-    }
-
-    File createModuleJar() throws IOException {
-        File libDir = new File(Utils.JavaHome, "lib");
-        File modules = new File(libDir, "modules");
-        File outDir = new File("out");
-
-        List<String> cmdList = new ArrayList<>();
-        cmdList.add(Utils.getJimageCmd());
-        cmdList.add("extract");
-        cmdList.add(modules.getAbsolutePath());
-        cmdList.add("--dir");
-        cmdList.add(outDir.getName());
-        Utils.runExec(cmdList);
-
-        FileFilter filter = (File file) -> file.getName().equals("module-info.class");
-        List<File> mfiles = Utils.findFiles(outDir, filter);
-
-        List<String> contents = new ArrayList<>(mfiles.size());
-        mfiles.stream().forEach((f) -> {
-            contents.add(f.getAbsolutePath());
-        });
-
-        File listFile = new File("mfiles.list");
-        Utils.createFile(listFile, contents);
-        File testFile = new File("test.jar");
-        Utils.jar("cvf", testFile.getName(), "@" + listFile.getName());
-        Utils.recursiveDelete(outDir);
-        return testFile;
+        Utils.cleanup();
     }
 }
--- a/jdk/test/tools/pack200/Pack200Props.java	Wed Apr 06 21:31:48 2016 +0100
+++ b/jdk/test/tools/pack200/Pack200Props.java	Wed Apr 06 09:38:39 2016 -0700
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2010, 2015, Oracle and/or its affiliates. All rights reserved.
+ * Copyright (c) 2010, 2016, 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,13 +31,13 @@
  */
 
 import java.io.File;
-import java.io.IOException;
 import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
 import java.util.jar.Pack200;
 import java.util.jar.Pack200.Packer;
+import java.util.logging.Logger;
 
 /*
  * Run this against a large jar file, by default the packer should generate only
@@ -46,20 +46,22 @@
 
 public class Pack200Props {
 
-    public static void main(String... args) throws IOException {
+    final static Logger log = Logger.getLogger("Pack200Props");
+
+    public static void main(String... args) throws Exception {
         verifyDefaults();
         File out = new File("test" + Utils.PACK_FILE_EXT);
         out.delete();
         verifySegmentLimit(out);
+        log.info("cleanup");
         Utils.cleanup();
     }
 
-    static void verifySegmentLimit(File outFile) throws IOException {
-        File sdkHome = Utils.JavaSDK;
+    static void verifySegmentLimit(File outFile) throws Exception {
+        log.info("creating jar");
         File testJar = Utils.createRtJar();
 
-        System.out.println("using pack200: " + Utils.getPack200Cmd());
-
+        log.info("using pack200: " + Utils.getPack200Cmd());
         List<String> cmdsList = new ArrayList<>();
         cmdsList.add(Utils.getPack200Cmd());
         cmdsList.add("-J-Xshare:off");
@@ -71,6 +73,7 @@
         cmdsList.add(testJar.getAbsolutePath());
         List<String> outList = Utils.runExec(cmdsList);
 
+        log.info("verifying");
         int count = 0;
         for (String line : outList) {
             System.out.println(line);
@@ -78,6 +81,7 @@
                 count++;
             }
         }
+        log.info("fini");
         if (count == 0) {
             throw new RuntimeException("no segments or no output ????");
         } else if (count > 1) {
@@ -86,6 +90,7 @@
     }
 
     private static void verifyDefaults() {
+        log.info("start");
         Map<String, String> expectedDefaults = new HashMap<>();
         Packer p = Pack200.newPacker();
         expectedDefaults.put("com.sun.java.util.jar.pack.disable.native",
@@ -121,6 +126,7 @@
                 }
             }
         }
+        log.info("fini");
         if (errors > 0) {
             throw new RuntimeException(errors +
                     " error(s) encountered in default properties verification");
--- a/jdk/test/tools/pack200/Utils.java	Wed Apr 06 21:31:48 2016 +0100
+++ b/jdk/test/tools/pack200/Utils.java	Wed Apr 06 09:38:39 2016 -0700
@@ -32,22 +32,33 @@
 import java.io.InputStream;
 import java.io.InputStreamReader;
 import java.io.PrintStream;
+import java.net.URI;
+import java.net.URL;
 import java.nio.charset.Charset;
+import java.nio.file.attribute.BasicFileAttributes;
+import java.nio.file.FileSystem;
+import java.nio.file.FileSystems;
+import java.nio.file.FileVisitResult;
+import java.nio.file.FileVisitor;
 import java.nio.file.Files;
 import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Map;
+import java.util.HashMap;
 import java.util.jar.JarFile;
 import java.util.jar.JarOutputStream;
 import java.util.jar.Pack200;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 import java.util.zip.ZipEntry;
 import java.util.zip.ZipFile;
 
 import static java.nio.file.StandardCopyOption.*;
 import static java.nio.file.StandardOpenOption.*;
 
+
 /**
  *
  * @author ksrini
@@ -544,10 +555,6 @@
         return getAjavaCmd("jar");
     }
 
-    static String getJimageCmd() {
-        return getAjavaCmd("jimage");
-    }
-
     static String getAjavaCmd(String cmdStr) {
         File binDir = new File(JavaHome, "bin");
         File unpack200File = IsWindows
@@ -562,29 +569,88 @@
         return cmd;
     }
 
-    static File createRtJar() throws IOException {
-        File libDir = new File(JavaHome, "lib");
-        File modules = new File(libDir, "modules");
-        List<String> cmdList = new ArrayList<>();
-        cmdList.add(getJimageCmd());
-        cmdList.add("extract");
-        cmdList.add(modules.getAbsolutePath());
-        cmdList.add("--dir");
-        cmdList.add("out");
-        runExec(cmdList);
-
+    // used to get all classes
+    static File createRtJar() throws Exception {
         File rtJar = new File("rt.jar");
-        cmdList.clear();
-        cmdList.add(getJarCmd());
-        // cmdList.add("cvf"); too noisy
-        cmdList.add("cf");
-        cmdList.add(rtJar.getName());
-        cmdList.add("-C");
-        cmdList.add("out");
-        cmdList.add(".");
-        runExec(cmdList);
+        new JrtToZip(".*\\.class", rtJar).run();
+        return rtJar;
+    }
 
-        recursiveDelete(new File("out"));
+    // used to select the contents
+    static File createRtJar(String pattern) throws Exception {
+        File rtJar = new File("rt.jar");
+        new JrtToZip(pattern, rtJar).run();
         return rtJar;
     }
+
+    /*
+     * A helper class to create a pseudo rt.jar.
+     */
+    static class JrtToZip {
+
+        final File outFile;
+        final Pattern pattern;
+
+        public static void main(String[] args) throws Exception {
+            new JrtToZip(args[0], new File(args[1])).run();
+        }
+
+        JrtToZip(String pattern, File outFile) throws Exception {
+            this.pattern = Pattern.compile(pattern);
+            this.outFile = outFile;
+        }
+
+        void run() throws Exception {
+            URI uri = URI.create("jar:" + outFile.toURI());
+            Map<String, String> env = new HashMap<>();
+            env.put("create", "true");
+            try (FileSystem zipfs = FileSystems.newFileSystem(uri, env)) {
+                toZipfs(zipfs);
+            }
+        }
+
+        void toZipfs(FileSystem zipfs) throws Exception {
+            FileSystem jrtfs = FileSystems.getFileSystem(URI.create("jrt:/"));
+            for (Path root : jrtfs.getRootDirectories()) {
+                Files.walkFileTree(root, new FileVisitor<Path>() {
+                    @Override
+                    public FileVisitResult preVisitDirectory(Path dir,
+                            BasicFileAttributes attrs) throws IOException {
+                        // ignore unneeded directory
+                        if (dir.startsWith("/packages"))
+                            return FileVisitResult.SKIP_SUBTREE;
+
+                        // pre-create required directories
+                        Path zpath = zipfs.getPath(dir.toString());
+                        Files.createDirectories(zpath);
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    @Override
+                    public FileVisitResult visitFile(Path file,
+                            BasicFileAttributes attrs) throws IOException {
+                        Matcher matcher = pattern.matcher(file.toString());
+                        if (matcher.matches()) {
+                            // System.out.println("x: " + file);
+                            Path zpath = zipfs.getPath(file.toString());
+                            Files.copy(file, zpath, REPLACE_EXISTING);
+                        }
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    @Override
+                    public FileVisitResult visitFileFailed(Path file,
+                            IOException exc) throws IOException {
+                        return FileVisitResult.CONTINUE;
+                    }
+
+                    @Override
+                    public FileVisitResult postVisitDirectory(Path dir,
+                            IOException exc) throws IOException {
+                        return FileVisitResult.CONTINUE;
+                    }
+                });
+            }
+        }
+    }
 }