jdk/test/tools/pack200/TimeStamp.java
author alanb
Mon, 21 Jan 2013 23:23:12 -0500
changeset 15684 e5b41bd22ec9
parent 14342 8435a30053c1
child 18594 b6a3c9f71ac8
permissions -rw-r--r--
8004931: add/removePropertyChangeListener should not exist in subset Profiles of Java SE Reviewed-by: dholmes, mchung, ksrini
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
6323
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
     1
/*
14342
8435a30053c1 7197491: update copyright year to match last edit in jdk8 jdk repository
alanb
parents: 9009
diff changeset
     2
 * Copyright (c) 2010, 2011, Oracle and/or its affiliates. All rights reserved.
6323
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
     4
 *
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
     7
 * published by the Free Software Foundation.
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
     8
 *
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    13
 * accompanied this code).
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    14
 *
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    18
 *
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    21
 * questions.
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    22
 */
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    23
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    24
import java.io.File;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    25
import java.io.FileOutputStream;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    26
import java.io.IOException;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    27
import java.util.ArrayList;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    28
import java.util.Collections;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    29
import java.util.HashMap;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    30
import java.util.List;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    31
import java.util.TimeZone;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    32
import java.util.jar.JarEntry;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    33
import java.util.jar.JarFile;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    34
import java.util.jar.JarOutputStream;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    35
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    36
/*
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    37
 * @test
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    38
 * @bug 6966740
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    39
 * @summary verify identical timestamps, unpacked in any timezone
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    40
 * @compile -XDignore.symbol.file Utils.java TimeStamp.java
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    41
 * @run main/othervm TimeStamp
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    42
 * @author ksrini
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    43
 */
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    44
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    45
/**
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    46
 * First we pack the file in some time zone say India, then we unpack the  file
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    47
 * in the current time zone, and ensure the timestamp recorded in the unpacked
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    48
 * jar are the same.
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    49
 */
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    50
public class TimeStamp {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    51
    static final TimeZone tz = TimeZone.getDefault();
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    52
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    53
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    54
    public static void main(String... args) throws IOException {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    55
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    56
        // make a local copy of our test file
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    57
        File srcFile = Utils.locateJar("golden.jar");
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    58
        File goldenFile = new File("golden.jar");
9009
e7036c254324 7031166: (pack200) tools/pack200/CommandLineTests.java fail with testsdk on RO filesystem
ksrini
parents: 6323
diff changeset
    59
        Utils.copyFile(srcFile, goldenFile);
6323
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    60
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    61
        JarFile goldenJarFile = new JarFile(goldenFile);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    62
        File packFile = new File("golden.pack");
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    63
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    64
        // set the test timezone and pack the file
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    65
        TimeZone.setDefault(TimeZone.getTimeZone("IST"));
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    66
        Utils.pack(goldenJarFile, packFile);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    67
        TimeZone.setDefault(tz);   // reset the timezone
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    68
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    69
        // unpack in the  test timezone
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    70
        File istFile = new File("golden.jar.java.IST");
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    71
        unpackJava(packFile, istFile);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    72
        verifyJar(goldenFile, istFile);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    73
        istFile.delete();
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    74
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    75
        // unpack in some other timezone
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    76
        File pstFile = new File("golden.jar.java.PST");
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    77
        unpackJava(packFile, pstFile);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    78
        verifyJar(goldenFile, pstFile);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    79
        pstFile.delete();
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    80
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    81
        // repeat the test for unpack200 tool.
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    82
        istFile = new File("golden.jar.native.IST");
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    83
        unpackNative(packFile, istFile);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    84
        verifyJar(goldenFile, istFile);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    85
        istFile.delete();
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    86
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    87
        pstFile = new File("golden.jar.native.PST");
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    88
        unpackNative(packFile, pstFile);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    89
        verifyJar(goldenFile, pstFile);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    90
        pstFile.delete();
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    91
    }
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    92
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    93
    static void unpackNative(File packFile, File outFile) {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    94
        String name = outFile.getName();
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    95
        String tzname = name.substring(name.lastIndexOf(".") + 1);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    96
        HashMap<String, String> env = new HashMap<>();
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    97
        switch(tzname) {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    98
            case "PST":
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
    99
                env.put("TZ", "US/Pacific");
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   100
                break;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   101
            case "IST":
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   102
                env.put("TZ", "Asia/Calcutta");
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   103
                break;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   104
            default:
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   105
                throw new RuntimeException("not implemented: " + tzname);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   106
        }
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   107
        List<String> cmdsList = new ArrayList<>();
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   108
        cmdsList.add(Utils.getUnpack200Cmd());
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   109
        cmdsList.add(packFile.getName());
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   110
        cmdsList.add(outFile.getName());
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   111
        Utils.runExec(cmdsList, env);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   112
    }
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   113
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   114
    static void unpackJava(File packFile, File outFile) throws IOException {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   115
        String name = outFile.getName();
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   116
        String tzname = name.substring(name.lastIndexOf(".") + 1);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   117
        JarOutputStream jos = null;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   118
        try {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   119
            TimeZone.setDefault(TimeZone.getTimeZone(tzname));
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   120
            jos = new JarOutputStream(new FileOutputStream(outFile));
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   121
            System.out.println("Using timezone: " + TimeZone.getDefault());
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   122
            Utils.unpackj(packFile, jos);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   123
        } finally {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   124
            Utils.close(jos);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   125
            TimeZone.setDefault(tz); // always reset
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   126
        }
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   127
    }
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   128
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   129
    static void verifyJar(File f1, File f2) throws IOException {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   130
        int errors = 0;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   131
        JarFile jf1 = null;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   132
        JarFile jf2 = null;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   133
        try {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   134
            jf1 = new JarFile(f1);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   135
            jf2 = new JarFile(f2);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   136
            System.out.println("Verifying: " + f1 + " and " + f2);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   137
            for (JarEntry je1 : Collections.list(jf1.entries())) {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   138
                JarEntry je2 = jf2.getJarEntry(je1.getName());
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   139
                if (je1.getTime() != je2.getTime()) {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   140
                    System.out.println("Error:");
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   141
                    System.out.println("  expected:" + jf1.getName() + ":"
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   142
                            + je1.getName() + ":" + je1.getTime());
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   143
                    System.out.println("  obtained:" + jf2.getName() + ":"
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   144
                            + je2.getName() + ":" + je2.getTime());
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   145
                    errors++;
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   146
                }
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   147
            }
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   148
        } finally {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   149
            Utils.close(jf1);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   150
            Utils.close(jf2);
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   151
        }
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   152
        if (errors > 0) {
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   153
            throw new RuntimeException("FAIL:" + errors + " error(s) encounted");
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   154
        }
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   155
    }
1cdec3dff0a3 6966740: (pack200) need to add the timezone regression test
ksrini
parents:
diff changeset
   156
}