TimeStamp.java revision 3883
0N/A/*
553N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
553N/A * published by the Free Software Foundation.
0N/A *
553N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
0N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
0N/A * or visit www.oracle.com if you need additional information or have any
553N/A * questions.
553N/A */
553N/A
0N/Aimport java.io.File;
0N/Aimport java.io.FileOutputStream;
0N/Aimport java.io.IOException;
0N/Aimport java.util.ArrayList;
0N/Aimport java.util.Collections;
0N/Aimport java.util.HashMap;
0N/Aimport java.util.List;
0N/Aimport java.util.TimeZone;
0N/Aimport java.util.jar.JarEntry;
0N/Aimport java.util.jar.JarFile;
0N/Aimport java.util.jar.JarOutputStream;
0N/A
0N/A/*
0N/A * @test
0N/A * @bug 6966740
0N/A * @summary verify identical timestamps, unpacked in any timezone
0N/A * @compile -XDignore.symbol.file Utils.java TimeStamp.java
0N/A * @run main/othervm TimeStamp
0N/A * @author ksrini
0N/A */
0N/A
0N/A/**
0N/A * First we pack the file in some time zone say India, then we unpack the file
0N/A * in the current time zone, and ensure the timestamp recorded in the unpacked
0N/A * jar are the same.
0N/A */
0N/Apublic class TimeStamp {
0N/A static final TimeZone tz = TimeZone.getDefault();
0N/A
0N/A
0N/A public static void main(String... args) throws IOException {
0N/A
0N/A // make a local copy of our test file
0N/A File srcFile = Utils.locateJar("golden.jar");
0N/A File goldenFile = new File("golden.jar");
0N/A Utils.copyFile(srcFile, goldenFile);
0N/A
0N/A JarFile goldenJarFile = new JarFile(goldenFile);
0N/A File packFile = new File("golden.pack");
0N/A
0N/A // set the test timezone and pack the file
0N/A TimeZone.setDefault(TimeZone.getTimeZone("IST"));
0N/A Utils.pack(goldenJarFile, packFile);
0N/A TimeZone.setDefault(tz); // reset the timezone
0N/A
0N/A // unpack in the test timezone
0N/A File istFile = new File("golden.jar.java.IST");
0N/A unpackJava(packFile, istFile);
0N/A verifyJar(goldenFile, istFile);
0N/A istFile.delete();
0N/A
0N/A // unpack in some other timezone
0N/A File pstFile = new File("golden.jar.java.PST");
0N/A unpackJava(packFile, pstFile);
0N/A verifyJar(goldenFile, pstFile);
0N/A pstFile.delete();
0N/A
0N/A // repeat the test for unpack200 tool.
0N/A istFile = new File("golden.jar.native.IST");
0N/A unpackNative(packFile, istFile);
0N/A verifyJar(goldenFile, istFile);
0N/A istFile.delete();
0N/A
0N/A pstFile = new File("golden.jar.native.PST");
0N/A unpackNative(packFile, pstFile);
0N/A verifyJar(goldenFile, pstFile);
0N/A pstFile.delete();
0N/A }
0N/A
0N/A static void unpackNative(File packFile, File outFile) {
0N/A String name = outFile.getName();
0N/A String tzname = name.substring(name.lastIndexOf(".") + 1);
0N/A HashMap<String, String> env = new HashMap<>();
0N/A switch(tzname) {
0N/A case "PST":
0N/A env.put("TZ", "US/Pacific");
0N/A break;
0N/A case "IST":
0N/A env.put("TZ", "Asia/Calcutta");
0N/A break;
0N/A default:
0N/A throw new RuntimeException("not implemented: " + tzname);
0N/A }
0N/A List<String> cmdsList = new ArrayList<>();
cmdsList.add(Utils.getUnpack200Cmd());
cmdsList.add(packFile.getName());
cmdsList.add(outFile.getName());
Utils.runExec(cmdsList, env);
}
static void unpackJava(File packFile, File outFile) throws IOException {
String name = outFile.getName();
String tzname = name.substring(name.lastIndexOf(".") + 1);
JarOutputStream jos = null;
try {
TimeZone.setDefault(TimeZone.getTimeZone(tzname));
jos = new JarOutputStream(new FileOutputStream(outFile));
System.out.println("Using timezone: " + TimeZone.getDefault());
Utils.unpackj(packFile, jos);
} finally {
Utils.close(jos);
TimeZone.setDefault(tz); // always reset
}
}
static void verifyJar(File f1, File f2) throws IOException {
int errors = 0;
JarFile jf1 = null;
JarFile jf2 = null;
try {
jf1 = new JarFile(f1);
jf2 = new JarFile(f2);
System.out.println("Verifying: " + f1 + " and " + f2);
for (JarEntry je1 : Collections.list(jf1.entries())) {
JarEntry je2 = jf2.getJarEntry(je1.getName());
if (je1.getTime() != je2.getTime()) {
System.out.println("Error:");
System.out.println(" expected:" + jf1.getName() + ":"
+ je1.getName() + ":" + je1.getTime());
System.out.println(" obtained:" + jf2.getName() + ":"
+ je2.getName() + ":" + je2.getTime());
errors++;
}
}
} finally {
Utils.close(jf1);
Utils.close(jf2);
}
if (errors > 0) {
throw new RuntimeException("FAIL:" + errors + " error(s) encounted");
}
}
}