jdk/test/tools/pack200/SegmentLimit.java
changeset 6325 adf468d05745
parent 6240 044d31b99ef5
parent 6324 b624b3a4e6e8
child 6326 047748ce0a45
child 6375 f8eac76fb676
child 6481 78d56f33c3a7
child 6486 85f30f8aa7f3
child 6493 7bbabd9b79e6
child 6523 d1c0054bff1c
child 6788 7b6911d709ed
child 7051 1c545d70a157
equal deleted inserted replaced
6240:044d31b99ef5 6325:adf468d05745
     1 /*
       
     2  * Copyright (c) 2010, 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
       
    27  * @summary verify default segment limit
       
    28  * @compile SegmentLimit.java
       
    29  * @run main SegmentLimit
       
    30  */
       
    31 
       
    32 import java.io.BufferedReader;
       
    33 import java.io.Closeable;
       
    34 import java.io.File;
       
    35 import java.io.FileOutputStream;
       
    36 import java.io.IOException;
       
    37 import java.io.InputStream;
       
    38 import java.io.InputStreamReader;
       
    39 import java.io.PrintStream;
       
    40 
       
    41 /*
       
    42  * Run this against a large jar file, by default the packer should generate only
       
    43  * one segment, parse the output of the packer to verify if this is indeed true.
       
    44  */
       
    45 
       
    46 public class SegmentLimit {
       
    47 
       
    48     private static final File  javaHome = new File(System.getProperty("java.home"));
       
    49 
       
    50     public static void main(String... args) {
       
    51         if (!javaHome.getName().endsWith("jre")) {
       
    52             throw new RuntimeException("Error: requires an SDK to run");
       
    53         }
       
    54 
       
    55         File out = new File("test" + Pack200Test.PACKEXT);
       
    56         out.delete();
       
    57         runPack200(out);
       
    58     }
       
    59 
       
    60     static void close(Closeable c) {
       
    61         if (c == null) {
       
    62             return;
       
    63         }
       
    64         try {
       
    65             c.close();
       
    66         } catch (IOException ignore) {}
       
    67     }
       
    68 
       
    69     static void runPack200(File outFile) {
       
    70         File binDir = new File(javaHome, "bin");
       
    71         File pack200Exe = System.getProperty("os.name").startsWith("Windows")
       
    72                 ? new File(binDir, "pack200.exe")
       
    73                 : new File(binDir, "pack200");
       
    74         File sdkHome = javaHome.getParentFile();
       
    75         File testJar = new File(new File(sdkHome, "lib"), "tools.jar");
       
    76 
       
    77         System.out.println("using pack200: " + pack200Exe.getAbsolutePath());
       
    78 
       
    79         String[] cmds = { pack200Exe.getAbsolutePath(),
       
    80                           "--effort=1",
       
    81                           "--verbose",
       
    82                           "--no-gzip",
       
    83                           outFile.getName(),
       
    84                           testJar.getAbsolutePath()
       
    85         };
       
    86         InputStream is = null;
       
    87         BufferedReader br = null;
       
    88         InputStreamReader ir = null;
       
    89 
       
    90         FileOutputStream fos = null;
       
    91         PrintStream ps = null;
       
    92 
       
    93         try {
       
    94             ProcessBuilder pb = new ProcessBuilder(cmds);
       
    95             pb.redirectErrorStream(true);
       
    96             Process p = pb.start();
       
    97             is = p.getInputStream();
       
    98             ir = new InputStreamReader(is);
       
    99             br = new BufferedReader(ir);
       
   100 
       
   101             File logFile = new File("pack200.log");
       
   102             fos = new FileOutputStream(logFile);
       
   103             ps  = new PrintStream(fos);
       
   104 
       
   105             String line = br.readLine();
       
   106             int count = 0;
       
   107             while (line != null) {
       
   108                 line = line.trim();
       
   109                 if (line.matches(".*Transmitted.*files of.*input bytes in a segment of.*bytes")) {
       
   110                     count++;
       
   111                 }
       
   112                 ps.println(line);
       
   113                 line=br.readLine();
       
   114             }
       
   115             p.waitFor();
       
   116             if (p.exitValue() != 0) {
       
   117                 throw new RuntimeException("pack200 failed");
       
   118             }
       
   119             p.destroy();
       
   120             if (count > 1) {
       
   121                 throw new Error("test fails: check for multiple segments(" +
       
   122                         count + ") in: " + logFile.getAbsolutePath());
       
   123             }
       
   124         } catch (IOException ex) {
       
   125             throw new RuntimeException(ex.getMessage());
       
   126         } catch (InterruptedException ignore){
       
   127         } finally {
       
   128             close(is);
       
   129             close(ps);
       
   130             close(fos);
       
   131         }
       
   132     }
       
   133 }
       
   134