blob: 6af11b12055b1a243574f7bbd2781d270ec32374 [file] [log] [blame]
sherman21984b02013-05-29 19:50:47 -07001/*
2 * Copyright (c) 2013, 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
24/**
25 * @test
26 * @bug 4759491 6303183 7012868
27 * @summary Test ZOS and ZIS timestamp in extra field correctly
28 */
29
30import java.io.*;
31import java.util.TimeZone;
32import java.util.concurrent.TimeUnit;
33import java.util.zip.ZipEntry;
34import java.util.zip.ZipInputStream;
35import java.util.zip.ZipOutputStream;
36
37
38public class TestExtraTime {
39
40 public static void main(String[] args) throws Throwable{
41
42 File src = new File(System.getProperty("test.src", "."), "TestExtraTime.java");
43 if (src.exists()) {
44 long mtime = src.lastModified();
45 test(mtime, null);
46 test(10, null); // ms-dos 1980 epoch problem
47 test(mtime, TimeZone.getTimeZone("Asia/Shanghai"));
48 }
49 }
50
51 private static void test(long mtime, TimeZone tz) throws Throwable {
52 TimeZone tz0 = TimeZone.getDefault();
53 if (tz != null) {
54 TimeZone.setDefault(tz);
55 }
56 ByteArrayOutputStream baos = new ByteArrayOutputStream();
57 ZipOutputStream zos = new ZipOutputStream(baos);
58 ZipEntry ze = new ZipEntry("TestExtreTime.java");
59
60 ze.setTime(mtime);
61 zos.putNextEntry(ze);
62 zos.write(new byte[] { 1,2 ,3, 4});
63 zos.close();
64 if (tz != null) {
65 TimeZone.setDefault(tz0);
66 }
67 ZipInputStream zis = new ZipInputStream(
68 new ByteArrayInputStream(baos.toByteArray()));
69 ze = zis.getNextEntry();
70 zis.close();
71
72 System.out.printf("%tc => %tc%n", mtime, ze.getTime());
73
74 if (TimeUnit.MILLISECONDS.toSeconds(mtime) !=
75 TimeUnit.MILLISECONDS.toSeconds(ze.getTime()))
76 throw new RuntimeException("Timestamp storing failed!");
77
78 }
79}