test/java/util/zip/TestLocalTime.java

Print this page


   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  * @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 
  44             ldt = ldt.withYear(1968); test(getBytes(ldt), ldt);
  45             ldt = ldt.withYear(1970); test(getBytes(ldt), ldt);
  46             ldt = ldt.withYear(1982); test(getBytes(ldt), ldt);
  47             ldt = ldt.withYear(2037); test(getBytes(ldt), ldt);
  48             ldt = ldt.withYear(2100); test(getBytes(ldt), ldt);
  49             ldt = ldt.withYear(2106); test(getBytes(ldt), ldt);
  50 
  51             TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
  52             // dos time does not support < 1980, have to use
  53             // utc in mtime.
  54             testWithTZ(tz, ldt.withYear(1982));
  55             testWithTZ(tz, ldt.withYear(2037));
  56             testWithTZ(tz, ldt.withYear(2100));
  57             testWithTZ(tz, ldt.withYear(2106));
  58 
  59             // for #8135108
  60             ldt = LocalDateTime.of(2100, 12, 06, 12, 34, 34, 973);
  61             test(getBytes(ldt), ldt);
  62             ldt = LocalDateTime.of(2106, 12, 06, 12, 34, 34, 973);
  63             test(getBytes(ldt), ldt);




  64 
  65         } finally {
  66             TimeZone.setDefault(tz0);
  67         }
  68     }
  69 
  70     static byte[] getBytes(LocalDateTime mtime) throws Throwable {
  71         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  72         ZipOutputStream zos = new ZipOutputStream(baos);
  73         ZipEntry ze = new ZipEntry("TestLocalTime.java");
  74         ze.setTimeLocal(mtime);
  75         check(ze, mtime);
  76 
  77         zos.putNextEntry(ze);
  78         zos.write(new byte[] { 1, 2, 3, 4});
  79         zos.close();
  80         return baos.toByteArray();
  81     }
  82 
  83     static void testWithTZ(TimeZone tz, LocalDateTime ldt) throws Throwable {
  84        TimeZone.setDefault(tz);
  85        byte[] zbytes = getBytes(ldt);
  86        TimeZone.setDefault(tz0);
  87        test(zbytes, ldt);
  88     }
  89 




  90     static void test(byte[] zbytes, LocalDateTime expected) throws Throwable {
  91         System.out.printf("--------------------%nTesting: [%s]%n", expected);
  92         // ZipInputStream
  93         ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zbytes));
  94         ZipEntry ze = zis.getNextEntry();
  95         zis.close();
  96         check(ze, expected);
  97 
  98         // ZipFile
  99         Path zpath = Paths.get(System.getProperty("test.dir", "."),
 100                                "TestLocalTime.zip");
 101         try {
 102             Files.copy(new ByteArrayInputStream(zbytes), zpath);
 103             ZipFile zf = new ZipFile(zpath.toFile());
 104             ze = zf.getEntry("TestLocalTime.java");
 105             check(ze, expected);
 106             zf.close();
 107         } finally {
 108             Files.deleteIfExists(zpath);
 109         }
 110     }
 111 
 112     static void check(ZipEntry ze, LocalDateTime expected) {
 113         LocalDateTime ldt = ze.getTimeLocal();
 114         if (ldt.atOffset(ZoneOffset.UTC).toEpochSecond() >> 1
 115             != expected.atOffset(ZoneOffset.UTC).toEpochSecond() >> 1) {

















 116             throw new RuntimeException("Timestamp: storing mtime failed!");
 117         }
 118     }
 119 }
   1 /*
   2  * Copyright (c) 2015, 2016, 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 8155616
  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(ldt);    // now
  43             test(ldt.withYear(1968));
  44             test(ldt.withYear(1970));
  45             test(ldt.withYear(1982));
  46             test(ldt.withYear(2037));
  47             test(ldt.withYear(2100));
  48             test(ldt.withYear(2106));

  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 
  58             // for #8135108
  59             test(LocalDateTime.of(2100, 12, 06, 12, 34, 34, 973));
  60             test(LocalDateTime.of(2106, 12, 06, 12, 34, 34, 973));
  61 
  62             // for #8155616
  63             test(LocalDateTime.of(2016, 03, 13, 2, 50, 00));  // gap
  64             test(LocalDateTime.of(2015, 11, 1,  1, 30, 00));  // overlap
  65             test(LocalDateTime.of(1968, 04, 28, 2, 51, 25));
  66             test(LocalDateTime.of(1970, 04, 26, 2, 31, 52));
  67 
  68         } finally {
  69             TimeZone.setDefault(tz0);
  70         }
  71     }
  72 
  73     static byte[] getBytes(LocalDateTime mtime) throws Throwable {
  74         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  75         ZipOutputStream zos = new ZipOutputStream(baos);
  76         ZipEntry ze = new ZipEntry("TestLocalTime.java");
  77         ze.setTimeLocal(mtime);
  78         check(ze, mtime);

  79         zos.putNextEntry(ze);
  80         zos.write(new byte[] { 1, 2, 3, 4});
  81         zos.close();
  82         return baos.toByteArray();
  83     }
  84 
  85     static void testWithTZ(TimeZone tz, LocalDateTime ldt) throws Throwable {
  86        TimeZone.setDefault(tz);
  87        byte[] zbytes = getBytes(ldt);
  88        TimeZone.setDefault(tz0);
  89        test(zbytes, ldt);
  90     }
  91 
  92     static void test(LocalDateTime ldt) throws Throwable {
  93         test(getBytes(ldt), ldt);
  94     }
  95 
  96     static void test(byte[] zbytes, LocalDateTime expected) throws Throwable {
  97         System.out.printf("--------------------%nTesting: [%s]%n", expected);
  98         // ZipInputStream
  99         ZipInputStream zis = new ZipInputStream(new ByteArrayInputStream(zbytes));
 100         ZipEntry ze = zis.getNextEntry();
 101         zis.close();
 102         check(ze, expected);
 103 
 104         // ZipFile
 105         Path zpath = Paths.get(System.getProperty("test.dir", "."),
 106                                "TestLocalTime.zip");
 107         try {
 108             Files.copy(new ByteArrayInputStream(zbytes), zpath);
 109             ZipFile zf = new ZipFile(zpath.toFile());
 110             ze = zf.getEntry("TestLocalTime.java");
 111             check(ze, expected);
 112             zf.close();
 113         } finally {
 114             Files.deleteIfExists(zpath);
 115         }
 116     }
 117 
 118     static void check(ZipEntry ze, LocalDateTime expected) {
 119         LocalDateTime ldt = ze.getTimeLocal();
 120         if (ldt.atOffset(ZoneOffset.UTC).toEpochSecond() >> 1
 121             != expected.atOffset(ZoneOffset.UTC).toEpochSecond() >> 1) {
 122             // if the LDT is out of the range of the standard ms-dos date-time
 123             // format ( < 1980 ) AND the date-time is within the daylight saving
 124             // time gap (which means the LDT is actually "invalid"), the LDT will
 125             // be adjusted accordingly when ZipEntry.setTimeLocal() converts the
 126             // date-time via ldt -> zdt -> Instant -> FileTime.
 127             // See ZonedDateTime.of(LocalDateTime, ZoneId) for more details.
 128             if (ldt.getYear() < 1980 || ldt.getYear() > (1980 + 0x7f)) {
 129                 System.out.println(" Non-MSDOS    ldt : " + ldt);
 130                 System.out.println("         expected : " + expected);
 131                 // try to adjust the "expected", assume daylight saving gap
 132                 expected = ZonedDateTime.of(expected, ZoneId.systemDefault())
 133                                         .toLocalDateTime();
 134                 if (ldt.atOffset(ZoneOffset.UTC).toEpochSecond() >> 1
 135                     == expected.atOffset(ZoneOffset.UTC).toEpochSecond() >> 1) {
 136                     return;
 137                 }
 138             }
 139             throw new RuntimeException("Timestamp: storing mtime failed!");
 140         }
 141     }
 142 }