Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/test/jdk/java/nio/file/Files/SymlinkTime.java
41153 views
1
/*
2
* Copyright (c) 2019, 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
/* @test
25
* @bug 8220793
26
* @summary Unit test for updating access and modification times of symlinks
27
* @requires (os.family == "linux" | os.family == "mac" | os.family == "windows")
28
* @library ..
29
* @build SymlinkTime
30
* @run main/othervm SymlinkTime
31
*/
32
33
import java.io.IOException;
34
import java.nio.file.Files;
35
import java.nio.file.LinkOption;
36
import java.nio.file.Path;
37
import java.nio.file.attribute.BasicFileAttributes;
38
import java.nio.file.attribute.BasicFileAttributeView;
39
import java.nio.file.attribute.FileTime;
40
41
public class SymlinkTime {
42
public static void main(String[] args) throws IOException {
43
Path dir = TestUtil.createTemporaryDirectory();
44
if (!TestUtil.supportsLinks(dir)) {
45
System.out.println("Links not supported: skipping test");
46
return;
47
}
48
49
try {
50
// Create file and symbolic link to it
51
final Path file = dir.resolve("file");
52
final Path link = dir.resolve("link");
53
Files.createFile(file);
54
try {
55
// Delay creating the link to get different time attributes
56
Thread.currentThread().sleep(5000);
57
} catch (InterruptedException ignored) {
58
}
59
Files.createSymbolicLink(link, file);
60
61
// Save file modification and access times
62
BasicFileAttributeView view = Files.getFileAttributeView(link,
63
BasicFileAttributeView.class);
64
BasicFileAttributes attr = view.readAttributes();
65
printTimes("Original file times", attr);
66
FileTime fileModTime = attr.lastModifiedTime();
67
FileTime fileAccTime = attr.lastAccessTime();
68
69
// Read link modification and access times
70
view = Files.getFileAttributeView(link,
71
BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
72
attr = view.readAttributes();
73
printTimes("Original link times", attr);
74
75
// Set new base time and offset increment
76
long base = 1000000000000L; // 2001-09-09T01:46:40Z
77
long delta = 1000*60L;
78
79
// Set new link modification and access times
80
FileTime linkModTime = FileTime.fromMillis(base + delta);
81
FileTime linkAccTime = FileTime.fromMillis(base + 2L*delta);
82
view.setTimes(linkModTime, linkAccTime, null);
83
84
// Verify link modification and access times updated correctly
85
view = Files.getFileAttributeView(link,
86
BasicFileAttributeView.class, LinkOption.NOFOLLOW_LINKS);
87
attr = view.readAttributes();
88
printTimes("Updated link times", attr);
89
check("Link", attr, linkModTime, linkAccTime);
90
91
// Verify file modification and access times unchanged
92
view = Files.getFileAttributeView(file,
93
BasicFileAttributeView.class);
94
attr = view.readAttributes();
95
printTimes("File times", attr);
96
check("File", attr, fileModTime, fileAccTime);
97
} finally {
98
TestUtil.removeAll(dir);
99
}
100
}
101
102
private static void check(String pathType, BasicFileAttributes attr,
103
FileTime modTimeExpected, FileTime accTimeExpected) {
104
if (!attr.lastModifiedTime().equals(modTimeExpected) ||
105
!attr.lastAccessTime().equals(accTimeExpected)) {
106
String message = String.format(
107
"%s - modification time: expected %s, actual %s;%n" +
108
"access time: expected %s, actual %s.%n", pathType,
109
modTimeExpected, attr.lastModifiedTime(),
110
accTimeExpected, attr.lastAccessTime());
111
throw new RuntimeException(message);
112
}
113
}
114
115
private static void printTimes(String label, BasicFileAttributes attr) {
116
System.out.format
117
("%s%ncreation: %s%nmodification: %s%naccess: %s%n%n",
118
label, attr.creationTime(), attr.lastModifiedTime(),
119
attr.lastAccessTime());
120
}
121
}
122
123