Path: blob/master/test/jdk/java/io/File/TimeZoneLastModified.java
41149 views
/*1* Copyright (c) 2017, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 621286926* @summary Determine if lastModified() works after TimeZone.setDefault()27* @run main/othervm TimeZoneLastModified28*/2930import java.io.File;31import java.util.Date;32import java.util.TimeZone;33import java.text.SimpleDateFormat;3435public class TimeZoneLastModified {36// Tue, 04 Jun 2002 13:56:50.002 GMT37private static final long TIME = 1023199010002L;3839public static void main(String[] args) throws Throwable {40int failures = test(null);41for (String timeZoneID : TimeZone.getAvailableIDs()) {42failures += test(timeZoneID);43}44if (failures != 0) {45throw new RuntimeException("TimeZoneLastModified failed");46}47System.out.println("TimeZoneLastModified passed");48}4950private static int test(String timeZoneID) throws Throwable {51File f = new File("test-timezone.txt");52int failures = 0;53try {54f.createNewFile();5556if (timeZoneID != null) {57TimeZone.setDefault(TimeZone.getTimeZone(timeZoneID));58}5960boolean succeeded = f.setLastModified(TIME);61if (!succeeded) {62System.err.format63("Setting time to %d failed for time zone %s%n",64TIME, timeZoneID);65failures++;66}6768long time = f.lastModified();69if (Math.abs(time - TIME) > 999) { // account for second precision70System.err.format71("Wrong modification time (ms): expected %d, obtained %d%n",72TIME, time);73failures++;74}75} finally {76f.delete();77}7879return failures;80}81}828384