blob: 22cb5326554fc8064f1be2893aaaa8ad4758206b [file] [log] [blame]
duke6e45e102007-12-01 00:00:00 +00001/*
2 * Copyright 1998-1999 Sun Microsystems, Inc. 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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/* @test
25 @bug 4091757
26 @summary Basic test for setLastModified method
27 */
28
29import java.io.*;
30
31
32public class SetLastModified {
33
34 private static void ck(File f, long nt, long rt) throws Exception {
35 if (rt == nt) return;
36 if ((rt / 10 == nt / 10)
37 || (rt / 100 == nt / 100)
38 || (rt / 1000 == nt / 1000)
39 || (rt / 10000 == (nt / 10000))) {
40 System.err.println(f + ": Time set to " + nt
41 + ", rounded down by filesystem to " + rt);
42 return;
43 }
44 if ((rt / 10 == (nt + 5) / 10)
45 || (rt / 100 == (nt + 50) / 100)
46 || (rt / 1000 == (nt + 500) / 1000)
47 || (rt / 10000 == ((nt + 5000) / 10000))) {
48 System.err.println(f + ": Time set to " + nt
49 + ", rounded up by filesystem to " + rt);
50 return;
51 }
52 throw new Exception(f + ": Time set to " + nt
53 + ", then read as " + rt);
54 }
55
56 public static void main(String[] args) throws Exception {
57 File d = new File(System.getProperty("test.dir", "."));
58 File d2 = new File(d, "x.SetLastModified.dir");
59 File f = new File(d2, "x.SetLastModified");
60 long ot, t;
61
62 /* New time: One week ago */
63 long nt = System.currentTimeMillis() - 1000 * 60 * 60 * 24 * 7;
64
65 if (f.exists()) f.delete();
66 if (d2.exists()) d2.delete();
67 if (!d2.mkdir()) {
68 throw new Exception("Can't create test directory " + d2);
69 }
70
71 boolean threw = false;
72 try {
73 d2.setLastModified(-nt);
74 } catch (IllegalArgumentException x) {
75 threw = true;
76 }
77 if (!threw)
78 throw new Exception("setLastModified succeeded with a negative time");
79
80 ot = d2.lastModified();
81 if (ot != 0) {
82 if (d2.setLastModified(nt)) {
83 ck(d2, nt, d2.lastModified());
84 d2.setLastModified(ot);
85 } else {
86 System.err.println("Warning: setLastModified on directories "
87 + "not supported");
88 }
89 }
90
91 if (f.exists()) {
92 if (!f.delete())
93 throw new Exception("Can't delete test file " + f);
94 }
95 if (f.setLastModified(nt))
96 throw new Exception("Succeeded on non-existent file: " + f);
97
98 OutputStream o = new FileOutputStream(f);
99 o.write('x');
100 o.close();
101 ot = f.lastModified();
102 if (!f.setLastModified(nt))
103 throw new Exception("setLastModified failed on file: " + f);
104 ck(f, nt, f.lastModified());
105
106 if (!f.delete()) throw new Exception("Can't delete test file " + f);
107 if (!d2.delete()) throw new Exception("Can't delete test directory " + d2);
108 }
109
110}