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