jdk/test/tools/pack200/Pack200Test.java
author ohair
Tue, 25 May 2010 15:58:33 -0700
changeset 5506 202f599c92aa
parent 2 90ce3da70b43
child 5799 e0089b2436a4
permissions -rw-r--r--
6943119: Rebrand source copyright notices Reviewed-by: darcy, weijun
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
     2
 * Copyright (c) 2007, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 2
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.util.jar.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.zip.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
/*
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
 * Pack200Test.java
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
 * @author ksrini
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * These tests are very rudimentary smoke tests to ensure that the packing
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * unpacking process works on a select set of JARs.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
public class Pack200Test {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
    private static ArrayList <File> jarList = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
    private static final String PACKEXT = ".pack";
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
    /** Creates a new instance of Pack200Test */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
    private Pack200Test() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
    private static void doPackUnpack() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
        for (File in : jarList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            Pack200.Packer packer = Pack200.newPacker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            Map p = packer.properties();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
            // Take the time optimization vs. space
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            p.put(packer.EFFORT, "1");  // CAUTION: do not use 0.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
            // Make the memory consumption as effective as possible
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
            p.put(packer.SEGMENT_LIMIT,"10000");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
            // throw an error if an attribute is unrecognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
            p.put(packer.UNKNOWN_ATTRIBUTE, packer.ERROR);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
            // ignore all JAR deflation requests to save time
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
            p.put(packer.DEFLATE_HINT, packer.FALSE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
            // save the file ordering of the original JAR
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
            p.put(packer.KEEP_FILE_ORDER, packer.TRUE);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
                JarFile jarFile = new JarFile(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
                // Write out to a jtreg scratch area
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
                FileOutputStream fos = new FileOutputStream(in.getName() + PACKEXT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
                System.out.print("Packing [" + in.toString() + "]...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
                // Call the packer
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
                packer.pack(jarFile, fos);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
                jarFile.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
                fos.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
                System.out.print("Unpacking...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
                File f = new File(in.getName() + PACKEXT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
                // Write out to current directory, jtreg will setup a scratch area
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
                JarOutputStream jostream = new JarOutputStream(new FileOutputStream(in.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
                // Unpack the files
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
                Pack200.Unpacker unpacker = Pack200.newUnpacker();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
                // Call the unpacker
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
                unpacker.unpack(f, jostream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
                // Must explicitly close the output.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                jostream.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                System.out.print("Testing...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
                // Ok we have unpacked the file, lets test it.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                doTest(in);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                System.out.println("Done.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
                System.out.println("ERROR: " + e.getMessage());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
                System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
    private static ArrayList <String> getZipFileEntryNames(ZipFile z) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
        ArrayList <String> out = new ArrayList();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
        for (ZipEntry ze : Collections.list(z.entries())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   101
            out.add(ze.getName());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
        return out;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
    private static void doTest(File in) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
       // make sure all the files in the original jar exists in the other
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
       ArrayList <String> refList = getZipFileEntryNames(new ZipFile(in));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
       ArrayList <String> cmpList = getZipFileEntryNames(new ZipFile(in.getName()));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
       System.out.print(refList.size() + "/" + cmpList.size() + " entries...");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
       if (refList.size() != cmpList.size()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
           throw new Exception("Missing: files ?, entries don't match");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
       for (String ename: refList) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
          if (!cmpList.contains(ename)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
              throw new Exception("Does not contain : " + ename);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
       }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
    private static void doSanity(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
        for (String s: args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
            File f = new File(s);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
            if (f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
                jarList.add(f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
                System.out.println("Warning: The JAR file " + f.toString() + " does not exist,");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
                System.out.println("         this test requires a JDK image, this file will be skipped.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   132
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   135
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
     * @param args the command line arguments
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
    public static void main(String[] args) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
        if (args.length < 1) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
            System.out.println("Usage: jar1 jar2 jar3 .....");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
            System.exit(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
        doSanity(args);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
        doPackUnpack();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
}