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