1 /*
   2  * Copyright (c) 2010, 2018, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  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 6575373 6969063
  27  * @summary verify default properties of the packer/unpacker and segment limit
  28  * @modules jdk.compiler
  29  *          jdk.zipfs
  30  * @compile -XDignore.symbol.file Utils.java Pack200Props.java
  31  * @run main Pack200Props
  32  * @author ksrini
  33  */
  34 
  35 import java.io.File;
  36 import java.util.ArrayList;
  37 import java.util.HashMap;
  38 import java.util.List;
  39 import java.util.Map;
  40 import java.util.jar.Pack200;
  41 import java.util.jar.Pack200.Packer;
  42 import java.util.logging.Logger;
  43 
  44 /*
  45  * Run this against a large jar file, by default the packer should generate only
  46  * one segment, parse the output of the packer to verify if this is indeed true.
  47  */
  48 
  49 public class Pack200Props {
  50 
  51     final static Logger log = Logger.getLogger("Pack200Props");
  52 
  53     public static void main(String... args) throws Exception {
  54         verifyDefaults();
  55         File out = new File("test" + Utils.PACK_FILE_EXT);
  56         out.delete();
  57         verifySegmentLimit(out);
  58         log.info("cleanup");
  59         Utils.cleanup();
  60     }
  61 
  62     static void verifySegmentLimit(File outFile) throws Exception {
  63         log.info("creating jar");
  64         File testJar = Utils.createRtJar();
  65 
  66         log.info("using pack200: " + Utils.getPack200Cmd());
  67         List<String> cmdsList = new ArrayList<>();
  68         cmdsList.add(Utils.getPack200Cmd());
  69         cmdsList.add("-J-Xshare:off");
  70         cmdsList.add("-J-Xmx1280m");
  71         cmdsList.add("--effort=1");
  72         cmdsList.add("--verbose");
  73         cmdsList.add("--no-gzip");
  74         cmdsList.add(outFile.getName());
  75         cmdsList.add(testJar.getAbsolutePath());
  76         List<String> outList = Utils.runExec(cmdsList);
  77 
  78         log.info("verifying");
  79         int count = 0;
  80         for (String line : outList) {
  81             System.out.println(line);
  82             if (line.matches(".*Transmitted.*files of.*input bytes in a segment of.*bytes")) {
  83                 count++;
  84             }
  85         }
  86         log.info("fini");
  87         if (count == 0) {
  88             throw new RuntimeException("no segments or no output ????");
  89         } else if (count > 1) {
  90             throw new RuntimeException("multiple segments detected, expected 1");
  91         }
  92     }
  93 
  94     private static void verifyDefaults() {
  95         log.info("start");
  96         Map<String, String> expectedDefaults = new HashMap<>();
  97         Packer p = Pack200.newPacker();
  98         expectedDefaults.put("com.sun.java.util.jar.pack.disable.native",
  99                 p.FALSE);
 100         expectedDefaults.put("com.sun.java.util.jar.pack.verbose", "0");
 101         expectedDefaults.put(p.CLASS_ATTRIBUTE_PFX + "CompilationID", "RUH");
 102         expectedDefaults.put(p.CLASS_ATTRIBUTE_PFX + "SourceID", "RUH");
 103         expectedDefaults.put(p.CODE_ATTRIBUTE_PFX + "CharacterRangeTable",
 104                 "NH[PHPOHIIH]");
 105         expectedDefaults.put(p.CODE_ATTRIBUTE_PFX + "CoverageTable",
 106                 "NH[PHHII]");
 107         expectedDefaults.put(p.DEFLATE_HINT, p.KEEP);
 108         expectedDefaults.put(p.EFFORT, "5");
 109         expectedDefaults.put(p.KEEP_FILE_ORDER, p.TRUE);
 110         expectedDefaults.put(p.MODIFICATION_TIME, p.KEEP);
 111         expectedDefaults.put(p.SEGMENT_LIMIT, "-1");
 112         expectedDefaults.put(p.UNKNOWN_ATTRIBUTE, p.PASS);
 113 
 114         Map<String, String> props = p.properties();
 115         int errors = 0;
 116         for (String key : expectedDefaults.keySet()) {
 117             String def = expectedDefaults.get(key);
 118             String x = props.get(key);
 119             if (x == null) {
 120                 System.out.println("Error: key not found:" + key);
 121                 errors++;
 122             } else {
 123                 if (!def.equals(x)) {
 124                     System.out.println("Error: key " + key
 125                             + "\n  value expected: " + def
 126                             + "\n  value obtained: " + x);
 127                     errors++;
 128                 }
 129             }
 130         }
 131         log.info("fini");
 132         if (errors > 0) {
 133             throw new RuntimeException(errors +
 134                     " error(s) encountered in default properties verification");
 135         }
 136     }
 137 }
 138