jdk/test/tools/jar/normalize/TestNormal.java
changeset 27114 cc60649130bb
parent 27113 28c10766cfac
parent 27109 ef8c10faaccd
child 27116 64a78dd93766
equal deleted inserted replaced
27113:28c10766cfac 27114:cc60649130bb
     1 /*
       
     2  * Copyright (c) 2013, 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 /*
       
    26  * @test
       
    27  * @run main/timeout=600 TestNormal
       
    28  * @bug 8020802
       
    29  * @summary Need an ability to create jar files that are invariant to the pack200 packing/unpacking
       
    30  * @author Alexander Zuev
       
    31  */
       
    32 
       
    33 import java.io.*;
       
    34 import java.util.Collections;
       
    35 import java.util.Properties;
       
    36 import java.util.jar.JarEntry;
       
    37 import java.util.jar.JarFile;
       
    38 
       
    39 public class TestNormal {
       
    40     private static String FS = File.separator;
       
    41 
       
    42     public static void main(String args[]) throws Exception {
       
    43         Properties p = System.getProperties();
       
    44         String java_home = p.getProperty("test.jdk");
       
    45         String dtjar = java_home + File.separator + "lib"
       
    46                 + File.separator + "dt.jar";
       
    47 
       
    48         File folder = new File("dt");
       
    49         if (folder.exists()) {
       
    50             delete(folder);
       
    51         }
       
    52         folder.mkdir();
       
    53 
       
    54         try {
       
    55             extractJar(new JarFile(dtjar), folder);
       
    56             execJavaCommand(java_home, "jar cnf normalized.jar -C dt .");
       
    57             execJavaCommand(java_home, "jar cf original.jar -C dt .");
       
    58             execJavaCommand(java_home, "pack200 -r repacked.jar original.jar");
       
    59             compareJars(new JarFile("normalized.jar"), new JarFile("repacked.jar"));
       
    60         } finally {
       
    61             String[] cleanupList = {"dt", "normalized.jar", "original.jar", "repacked.jar"};
       
    62             for (String s : cleanupList) {
       
    63                 delete(new File(s));
       
    64             }
       
    65         }
       
    66     }
       
    67 
       
    68     public static void execJavaCommand(String java_home, String cmd) throws Exception {
       
    69         Process proc = Runtime.getRuntime().exec(java_home + FS + "bin" + FS + cmd);
       
    70         String s;
       
    71         BufferedReader stdInput =
       
    72                 new BufferedReader(new InputStreamReader(proc.getInputStream()));
       
    73         BufferedReader stdError =
       
    74                 new BufferedReader(new InputStreamReader(proc.getErrorStream()));
       
    75         while ((s = stdInput.readLine()) != null) {
       
    76             System.out.println(s);
       
    77         }
       
    78         while ((s = stdError.readLine()) != null) {
       
    79             System.err.println(s);
       
    80         }
       
    81     }
       
    82 
       
    83     public static void compareJars(JarFile jf1, JarFile jf2) throws Exception {
       
    84         try {
       
    85             if (jf1.size() != jf2.size()) {
       
    86                 throw new Exception("Jars " + jf1.getName() + " and " + jf2.getName()
       
    87                         + " have different number of entries");
       
    88             }
       
    89             for (JarEntry elem1 : Collections.list(jf1.entries())) {
       
    90                 JarEntry elem2 = jf2.getJarEntry(elem1.getName());
       
    91                 if (elem2 == null) {
       
    92                     throw new Exception("Element " + elem1.getName() + " is missing from " + jf2.getName());
       
    93                 }
       
    94                 if (!elem1.isDirectory() && elem1.getCrc() != elem2.getCrc()) {
       
    95                     throw new Exception("The crc of " + elem1.getName() + " is different.");
       
    96                 }
       
    97             }
       
    98         } finally {
       
    99             jf1.close();
       
   100             jf2.close();
       
   101         }
       
   102     }
       
   103 
       
   104     public static void extractJar(JarFile jf, File where) throws Exception {
       
   105         for (JarEntry file : Collections.list(jf.entries())) {
       
   106             File out = new File(where, file.getName());
       
   107             if (file.isDirectory()) {
       
   108                 out.mkdirs();
       
   109                 continue;
       
   110             }
       
   111             File parent = out.getParentFile();
       
   112             if (parent != null && !parent.exists()) {
       
   113                 parent.mkdirs();
       
   114             }
       
   115             InputStream is = null;
       
   116             OutputStream os = null;
       
   117             try {
       
   118                 is = jf.getInputStream(file);
       
   119                 os = new FileOutputStream(out);
       
   120                 while (is.available() > 0) {
       
   121                     os.write(is.read());
       
   122                 }
       
   123             } finally {
       
   124                 if (is != null) {
       
   125                     is.close();
       
   126                 }
       
   127                 if (os != null) {
       
   128                     os.close();
       
   129                 }
       
   130             }
       
   131         }
       
   132     }
       
   133 
       
   134     static void delete(File f) throws IOException {
       
   135         if (!f.exists()) {
       
   136             return;
       
   137         }
       
   138         if (f.isDirectory()) {
       
   139             for (File c : f.listFiles()) {
       
   140                 delete(c);
       
   141             }
       
   142         }
       
   143         if (!f.delete()) {
       
   144             throw new FileNotFoundException("Failed to delete file: " + f);
       
   145         }
       
   146     }
       
   147 }