1 /*
   2  * Copyright (c) 2017, 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 6212869
  27  * @summary Determine if lastModified() works after TimeZone.setDefault()
  28  * @run main/othervm TimeZoneLastModified
  29  */
  30 
  31 import java.io.File;
  32 import java.util.Date;
  33 import java.util.TimeZone;
  34 import java.text.SimpleDateFormat;
  35 
  36 public class TimeZoneLastModified {
  37     // Tue, 04 Jun 2002 13:56:50.002 GMT
  38     private static final long TIME = 1023199010002L;
  39 
  40     public static void main(String[] args) throws Throwable {
  41         int failures = test(null);
  42         for (String timeZoneID : TimeZone.getAvailableIDs()) {
  43             failures += test(timeZoneID);
  44         }
  45         if (failures != 0) {
  46             throw new RuntimeException("TimeZoneLastModified failed");
  47         }
  48         System.out.println("TimeZoneLastModified passed");
  49     }
  50 
  51     private static int test(String timeZoneID) throws Throwable {
  52         File f = new File("test-timezone.txt");
  53         int failures = 0;
  54         try {
  55             f.createNewFile();
  56 
  57             if (timeZoneID != null) {
  58                 TimeZone.setDefault(TimeZone.getTimeZone(timeZoneID));
  59             }
  60 
  61             boolean succeeded = f.setLastModified(TIME);
  62             if (!succeeded) {
  63                 System.err.format
  64                     ("Setting time to %d failed for time zone %s%n",
  65                     TIME, timeZoneID);
  66                 failures++;
  67             }
  68 
  69             long time = f.lastModified();
  70             if (Math.abs(time - TIME) > 999) { // account for second precision
  71                 System.err.format
  72                     ("Wrong modification time (ms): expected %d, obtained %d%n",
  73                     TIME, time);
  74                 failures++;
  75             }
  76         } finally {
  77             f.delete();
  78         }
  79 
  80         return failures;
  81     }
  82 }