1 /* 2 * Copyright (c) 2015, 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 8075526 8135108 27 * @key intermittent 28 * @summary Test timestamp via ZipEntry.get/setTimeLocal() 29 */ 30 31 import java.io.*; 32 import java.nio.file.*; 33 import java.time.*; 34 import java.util.*; 35 import java.util.zip.*; 36 37 public class TestLocalTime { 38 private static TimeZone tz0 = TimeZone.getDefault(); 39 40 public static void main(String[] args) throws Throwable{ 41 try { 42 LocalDateTime ldt = LocalDateTime.now(); 43 test(getBytes(ldt), ldt); // now 44 45 ldt = ldt.withYear(1968); test(getBytes(ldt), ldt); 46 ldt = ldt.withYear(1970); test(getBytes(ldt), ldt); 47 ldt = ldt.withYear(1982); test(getBytes(ldt), ldt); 48 ldt = ldt.withYear(2037); test(getBytes(ldt), ldt); 49 ldt = ldt.withYear(2100); test(getBytes(ldt), ldt); 50 ldt = ldt.withYear(2106); test(getBytes(ldt), ldt); 51 52 TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai"); 53 // dos time does not support < 1980, have to use 54 // utc in mtime. 55 testWithTZ(tz, ldt.withYear(1982)); 56 testWithTZ(tz, ldt.withYear(2037)); 57 testWithTZ(tz, ldt.withYear(2100)); 58 testWithTZ(tz, ldt.withYear(2106)); 59 60 // for #8135108 61 ldt = LocalDateTime.of(2100, 12, 06, 12, 34, 34, 973); 62 test(getBytes(ldt), ldt); 63 ldt = LocalDateTime.of(2106, 12, 06, 12, 34, 34, 973); 64 test(getBytes(ldt), ldt); 65 66 } finally { 67 TimeZone.setDefault(tz0); 68 } 69 } 70 71 static byte[] getBytes(LocalDateTime mtime) throws Throwable { 72 ByteArrayOutputStream baos = new ByteArrayOutputStream(); 73 ZipOutputStream zos = new ZipOutputStream(baos); 74 ZipEntry ze = new ZipEntry("TestLocalTime.java"); 75 ze.setTimeLocal(mtime); 76 check(ze, mtime); 77 78 zos.putNextEntry(ze); 79 zos.write(new byte[] { 1, 2, 3, 4}); 80 zos.close(); 81 return baos.toByteArray(); 82 } 83 84 static void testWithTZ(TimeZone tz, LocalDateTime ldt) throws Throwable { 85 TimeZone.setDefault(tz); 86 byte[] zbytes = getBytes(ldt); 87 TimeZone.setDefault(tz0); 88 test(zbytes, ldt); 89 } 90 91 static void test(byte[] zbytes, LocalDateTime expected) throws Throwable { 92 System.out.printf("--------------------%nTesting: [%s]%n", expected); 93 // ZipInputStream 94 ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zbytes)); 95 ZipEntry ze = zis.getNextEntry(); 96 zis.close(); 97 check(ze, expected); 98 99 // ZipFile 100 Path zpath = Paths.get(System.getProperty("test.dir", "."), 101 "TestLocalTime.zip"); 102 try { 103 Files.copy(new ByteArrayInputStream(zbytes), zpath); 104 ZipFile zf = new ZipFile(zpath.toFile()); 105 ze = zf.getEntry("TestLocalTime.java"); 106 check(ze, expected); 107 zf.close(); 108 } finally { 109 Files.deleteIfExists(zpath); 110 } 111 } 112 113 static void check(ZipEntry ze, LocalDateTime expected) { 114 LocalDateTime ldt = ze.getTimeLocal(); 115 if (ldt.atOffset(ZoneOffset.UTC).toEpochSecond() >> 1 116 != expected.atOffset(ZoneOffset.UTC).toEpochSecond() >> 1) { 117 throw new RuntimeException("Timestamp: storing mtime failed!"); 118 } 119 } 120 }