test/jdk/java/io/File/SetLastModified.java
author bpb
Tue, 01 Oct 2019 07:37:39 -0700
changeset 58430 814156987833
parent 47216 71c04702a3d5
permissions -rw-r--r--
8229333: java/io/File/SetLastModified.java timed out Reviewed-by: lancea, rriggs
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
58430
814156987833 8229333: java/io/File/SetLastModified.java timed out
bpb
parents: 47216
diff changeset
     2
 * Copyright (c) 1998, 2019, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
90ce3da70b43 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 715
diff changeset
    21
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    22
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    23
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
/* @test
45178
9ff621041842 8177809: File.lastModified() is losing milliseconds (always ends in 000)
bpb
parents: 9035
diff changeset
    25
   @bug 4091757 6652379 8177809
58430
814156987833 8229333: java/io/File/SetLastModified.java timed out
bpb
parents: 47216
diff changeset
    26
   @requires os.maxMemory >= 16G
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
   @summary Basic test for setLastModified method
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.io.*;
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
    31
import java.nio.ByteBuffer;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
    32
import java.nio.channels.FileChannel;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
public class SetLastModified {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
    private static void ck(File f, long nt, long rt) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
        if (rt == nt) return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
        if ((rt / 10 == nt / 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
            || (rt / 100 == nt / 100)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
            || (rt / 1000 == nt / 1000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
            || (rt / 10000 == (nt / 10000))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
            System.err.println(f + ": Time set to " + nt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
                               + ", rounded down by filesystem to " + rt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
        if ((rt / 10 == (nt + 5) / 10)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
            || (rt / 100 == (nt + 50) / 100)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
            || (rt / 1000 == (nt + 500) / 1000)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
            || (rt / 10000 == ((nt + 5000) / 10000))) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
            System.err.println(f + ": Time set to " + nt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
                               + ", rounded up by filesystem to " + rt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
        throw new Exception(f + ": Time set to " + nt
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
                            + ", then read as " + rt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    57
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
    public static void main(String[] args) throws Exception {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
        File d = new File(System.getProperty("test.dir", "."));
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
        File d2 = new File(d, "x.SetLastModified.dir");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
        File f = new File(d2, "x.SetLastModified");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
        long ot, t;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
        /* New time: One week ago */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    66
        long nt = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
        if (f.exists()) f.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
        if (d2.exists()) d2.delete();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
        if (!d2.mkdir()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
            throw new Exception("Can't create test directory " + d2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
        boolean threw = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
            d2.setLastModified(-nt);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
        } catch (IllegalArgumentException x) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
            threw = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
        if (!threw)
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
            throw new Exception("setLastModified succeeded with a negative time");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
        ot = d2.lastModified();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
        if (ot != 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
            if (d2.setLastModified(nt)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
                ck(d2, nt, d2.lastModified());
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
                d2.setLastModified(ot);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    89
                System.err.println("Warning: setLastModified on directories "
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
                                   + "not supported");
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
        if (f.exists()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
            if (!f.delete())
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
                throw new Exception("Can't delete test file " + f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
        if (f.setLastModified(nt))
90ce3da70b43 Initial load
duke
parents:
diff changeset
    99
            throw new Exception("Succeeded on non-existent file: " + f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   101
        // set/check last modified on files of size 1, 1GB+1, 2GB+1, ..
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   102
        // On Windows we only test with a tiny file as that platform doesn't
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   103
        // support sparse files by default and so the test takes too long.
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   104
        final long G = 1024L * 1024L * 1024L;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   105
        final long MAX_POSITION =
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   106
            System.getProperty("os.name").startsWith("Windows") ? 0L : 3L*G;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   107
        long pos = 0L;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   108
        while (pos <= MAX_POSITION) {
8559
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
   109
            try (FileChannel fc = new FileOutputStream(f).getChannel()) {
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
   110
                fc.position(pos).write(ByteBuffer.wrap("x".getBytes()));
826c03991926 7022624: use try-with-resources in java.io tests
smarks
parents: 5506
diff changeset
   111
            }
692
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   112
            ot = f.lastModified();
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   113
            System.out.format("check with file size: %d\n", f.length());
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   114
            if (!f.setLastModified(nt))
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   115
                throw new Exception("setLastModified failed on file: " + f);
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   116
            ck(f, nt, f.lastModified());
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   117
            pos += G;
50a393caed65 6652379: File.setLastModified fails on large files (lnx only)
alanb
parents: 2
diff changeset
   118
        }
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
        if (!f.delete()) throw new Exception("Can't delete test file " + f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
        if (!d2.delete()) throw new Exception("Can't delete test directory " + d2);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
}