< prev index next >

test/java/util/zip/TestExtraTime.java

Print this page
rev 11478 : 8073497: Lazy conversion of ZipEntry time
Reviewed-by: sherman, plevart
   1 /*
   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  */


  54             TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
  55 
  56             for (byte[] extra : new byte[][] { null, new byte[] {1, 2, 3}}) {
  57                 test(mtime, null, null, null, extra);
  58                 // ms-dos 1980 epoch problem
  59                 test(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null, extra);
  60                 // non-default tz
  61                 test(mtime, null, null, tz, extra);
  62 
  63                 test(mtime, atime, null, null, extra);
  64                 test(mtime, null, ctime, null, extra);
  65                 test(mtime, atime, ctime, null, extra);
  66 
  67                 test(mtime, atime, null, tz, extra);
  68                 test(mtime, null, ctime, tz, extra);
  69                 test(mtime, atime, ctime, tz, extra);
  70             }
  71         }
  72 
  73         testNullHandling();

  74     }
  75 
  76     static void test(FileTime mtime, FileTime atime, FileTime ctime,
  77                      TimeZone tz, byte[] extra) throws Throwable {
  78         System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
  79                           mtime, atime, ctime);
  80         TimeZone tz0 = TimeZone.getDefault();
  81         if (tz != null) {
  82             TimeZone.setDefault(tz);
  83         }
  84         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  85         ZipOutputStream zos = new ZipOutputStream(baos);
  86         ZipEntry ze = new ZipEntry("TestExtraTime.java");
  87         ze.setExtra(extra);
  88         ze.setLastModifiedTime(mtime);
  89         if (atime != null)
  90             ze.setLastAccessTime(atime);
  91         if (ctime != null)
  92             ze.setCreationTime(ctime);
  93         zos.putNextEntry(ze);


 159 
 160     static void testNullHandling() {
 161         ZipEntry ze = new ZipEntry("TestExtraTime.java");
 162         try {
 163             ze.setLastAccessTime(null);
 164             throw new RuntimeException("setLastAccessTime(null) should throw NPE");
 165         } catch (NullPointerException ignored) {
 166             // pass
 167         }
 168         try {
 169             ze.setCreationTime(null);
 170             throw new RuntimeException("setCreationTime(null) should throw NPE");
 171         } catch (NullPointerException ignored) {
 172             // pass
 173         }
 174         try {
 175             ze.setLastModifiedTime(null);
 176             throw new RuntimeException("setLastModifiedTime(null) should throw NPE");
 177         } catch (NullPointerException ignored) {
 178             // pass





























 179         }
 180     }
 181 }
   1 /*
   2  * Copyright (c) 2013, 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  */


  54             TimeZone tz = TimeZone.getTimeZone("Asia/Shanghai");
  55 
  56             for (byte[] extra : new byte[][] { null, new byte[] {1, 2, 3}}) {
  57                 test(mtime, null, null, null, extra);
  58                 // ms-dos 1980 epoch problem
  59                 test(FileTime.from(10, TimeUnit.MILLISECONDS), null, null, null, extra);
  60                 // non-default tz
  61                 test(mtime, null, null, tz, extra);
  62 
  63                 test(mtime, atime, null, null, extra);
  64                 test(mtime, null, ctime, null, extra);
  65                 test(mtime, atime, ctime, null, extra);
  66 
  67                 test(mtime, atime, null, tz, extra);
  68                 test(mtime, null, ctime, tz, extra);
  69                 test(mtime, atime, ctime, tz, extra);
  70             }
  71         }
  72 
  73         testNullHandling();
  74         testTimeConversions();
  75     }
  76 
  77     static void test(FileTime mtime, FileTime atime, FileTime ctime,
  78                      TimeZone tz, byte[] extra) throws Throwable {
  79         System.out.printf("--------------------%nTesting: [%s]/[%s]/[%s]%n",
  80                           mtime, atime, ctime);
  81         TimeZone tz0 = TimeZone.getDefault();
  82         if (tz != null) {
  83             TimeZone.setDefault(tz);
  84         }
  85         ByteArrayOutputStream baos = new ByteArrayOutputStream();
  86         ZipOutputStream zos = new ZipOutputStream(baos);
  87         ZipEntry ze = new ZipEntry("TestExtraTime.java");
  88         ze.setExtra(extra);
  89         ze.setLastModifiedTime(mtime);
  90         if (atime != null)
  91             ze.setLastAccessTime(atime);
  92         if (ctime != null)
  93             ze.setCreationTime(ctime);
  94         zos.putNextEntry(ze);


 160 
 161     static void testNullHandling() {
 162         ZipEntry ze = new ZipEntry("TestExtraTime.java");
 163         try {
 164             ze.setLastAccessTime(null);
 165             throw new RuntimeException("setLastAccessTime(null) should throw NPE");
 166         } catch (NullPointerException ignored) {
 167             // pass
 168         }
 169         try {
 170             ze.setCreationTime(null);
 171             throw new RuntimeException("setCreationTime(null) should throw NPE");
 172         } catch (NullPointerException ignored) {
 173             // pass
 174         }
 175         try {
 176             ze.setLastModifiedTime(null);
 177             throw new RuntimeException("setLastModifiedTime(null) should throw NPE");
 178         } catch (NullPointerException ignored) {
 179             // pass
 180         }
 181     }
 182 
 183     // verify that setting and getting any time is possible as per the intent
 184     // of 4759491
 185     static void testTimeConversions() {
 186         // Sample across the entire range
 187         long step = Long.MAX_VALUE / 100L;
 188         testTimeConversions(Long.MIN_VALUE, Long.MAX_VALUE - step, step);
 189 
 190         // Samples through the near future
 191         long currentTime = System.currentTimeMillis();
 192         testTimeConversions(currentTime, currentTime + 1_000_000, 10_000);
 193     }
 194 
 195     static void testTimeConversions(long from, long to, long step) {
 196         ZipEntry ze = new ZipEntry("TestExtraTime.java");
 197         for (long time = from; time <= to; time += step) {
 198             ze.setTime(time);
 199             FileTime lastModifiedTime = ze.getLastModifiedTime();
 200             if (lastModifiedTime.toMillis() != time) {
 201                 throw new RuntimeException("setTime should make getLastModifiedTime " +
 202                         "return the specified instant: " + time +
 203                         " got: " + lastModifiedTime.toMillis());
 204             }
 205             if (ze.getTime() != time) {
 206                 throw new RuntimeException("getTime after setTime, expected: " +
 207                         time + " got: " + ze.getTime());
 208             }
 209         }
 210     }
 211 }
< prev index next >