Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/pack200/TimeStamp.java
38833 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223import java.io.File;24import java.io.FileOutputStream;25import java.io.IOException;26import java.util.ArrayList;27import java.util.Collections;28import java.util.HashMap;29import java.util.List;30import java.util.TimeZone;31import java.util.jar.JarEntry;32import java.util.jar.JarFile;33import java.util.jar.JarOutputStream;3435/*36* @test37* @bug 696674038* @summary verify identical timestamps, unpacked in any timezone39* @compile -XDignore.symbol.file Utils.java TimeStamp.java40* @run main/othervm TimeStamp41* @author ksrini42*/4344/**45* First we pack the file in some time zone say India, then we unpack the file46* in the current time zone, and ensure the timestamp recorded in the unpacked47* jar are the same.48*/49public class TimeStamp {50static final TimeZone tz = TimeZone.getDefault();515253public static void main(String... args) throws IOException {5455// make a local copy of our test file56File srcFile = Utils.locateJar("golden.jar");57File goldenFile = new File("golden.jar");58Utils.copyFile(srcFile, goldenFile);5960JarFile goldenJarFile = new JarFile(goldenFile);61File packFile = new File("golden.pack");6263// set the test timezone and pack the file64TimeZone.setDefault(TimeZone.getTimeZone("IST"));65Utils.pack(goldenJarFile, packFile);66TimeZone.setDefault(tz); // reset the timezone6768// unpack in the test timezone69File istFile = new File("golden.jar.java.IST");70unpackJava(packFile, istFile);71verifyJar(goldenFile, istFile);72istFile.delete();7374// unpack in some other timezone75File pstFile = new File("golden.jar.java.PST");76unpackJava(packFile, pstFile);77verifyJar(goldenFile, pstFile);78pstFile.delete();7980// repeat the test for unpack200 tool.81istFile = new File("golden.jar.native.IST");82unpackNative(packFile, istFile);83verifyJar(goldenFile, istFile);84istFile.delete();8586pstFile = new File("golden.jar.native.PST");87unpackNative(packFile, pstFile);88verifyJar(goldenFile, pstFile);89pstFile.delete();90Utils.cleanup();91}9293static void unpackNative(File packFile, File outFile) {94String name = outFile.getName();95String tzname = name.substring(name.lastIndexOf(".") + 1);96HashMap<String, String> env = new HashMap<>();97switch(tzname) {98case "PST":99env.put("TZ", "US/Pacific");100break;101case "IST":102env.put("TZ", "Asia/Calcutta");103break;104default:105throw new RuntimeException("not implemented: " + tzname);106}107List<String> cmdsList = new ArrayList<>();108cmdsList.add(Utils.getUnpack200Cmd());109cmdsList.add(packFile.getName());110cmdsList.add(outFile.getName());111Utils.runExec(cmdsList, env);112}113114static void unpackJava(File packFile, File outFile) throws IOException {115String name = outFile.getName();116String tzname = name.substring(name.lastIndexOf(".") + 1);117JarOutputStream jos = null;118try {119TimeZone.setDefault(TimeZone.getTimeZone(tzname));120jos = new JarOutputStream(new FileOutputStream(outFile));121System.out.println("Using timezone: " + TimeZone.getDefault());122Utils.unpackj(packFile, jos);123} finally {124Utils.close(jos);125TimeZone.setDefault(tz); // always reset126}127}128129static void verifyJar(File f1, File f2) throws IOException {130int errors = 0;131JarFile jf1 = null;132JarFile jf2 = null;133try {134jf1 = new JarFile(f1);135jf2 = new JarFile(f2);136System.out.println("Verifying: " + f1 + " and " + f2);137for (JarEntry je1 : Collections.list(jf1.entries())) {138JarEntry je2 = jf2.getJarEntry(je1.getName());139if (je1.getTime() != je2.getTime()) {140System.out.println("Error:");141System.out.println(" expected:" + jf1.getName() + ":"142+ je1.getName() + ":" + je1.getTime());143System.out.println(" obtained:" + jf2.getName() + ":"144+ je2.getName() + ":" + je2.getTime());145errors++;146}147}148} finally {149Utils.close(jf1);150Utils.close(jf2);151}152if (errors > 0) {153throw new RuntimeException("FAIL:" + errors + " error(s) encounted");154}155}156}157158159