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