Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/openjdk-multiarch-jdk8u
Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/tools/jar/JarEntryTime.java
47252 views
1
/*
2
* Copyright (c) 2006, 2013, 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 4225317 6969651
27
* @summary Check extracted files have date as per those in the .jar file
28
*/
29
30
import java.io.File;
31
import java.io.PrintWriter;
32
import java.nio.file.attribute.FileTime;
33
import sun.tools.jar.Main;
34
35
public class JarEntryTime {
36
37
// ZipEntry's mod date has 2 seconds precision: give extra time to
38
// allow for e.g. rounding/truncation and networked/samba drives.
39
static final long PRECISION = 10000L;
40
41
static boolean cleanup(File dir) throws Throwable {
42
boolean rc = true;
43
File[] x = dir.listFiles();
44
if (x != null) {
45
for (int i = 0; i < x.length; i++) {
46
rc &= x[i].delete();
47
}
48
}
49
return rc & dir.delete();
50
}
51
52
static void extractJar(File jarFile, boolean useExtractionTime) throws Throwable {
53
String javahome = System.getProperty("java.home");
54
if (javahome.endsWith("jre")) {
55
javahome = javahome.substring(0, javahome.length() - 4);
56
}
57
String jarcmd = javahome + File.separator + "bin" + File.separator + "jar";
58
String[] args;
59
if (useExtractionTime) {
60
args = new String[] {
61
jarcmd,
62
"-J-Dsun.tools.jar.useExtractionTime=true",
63
"xf",
64
jarFile.getName() };
65
} else {
66
args = new String[] {
67
jarcmd,
68
"xf",
69
jarFile.getName() };
70
}
71
Process p = Runtime.getRuntime().exec(args);
72
check(p != null && (p.waitFor() == 0));
73
}
74
75
public static void realMain(String[] args) throws Throwable {
76
77
File dirOuter = new File("outer");
78
File dirInner = new File(dirOuter, "inner");
79
File jarFile = new File("JarEntryTime.jar");
80
81
// Remove any leftovers from prior run
82
cleanup(dirInner);
83
cleanup(dirOuter);
84
jarFile.delete();
85
86
/* Create a directory structure
87
* outer/
88
* inner/
89
* foo.txt
90
* Set the lastModified dates so that outer is created now, inner
91
* yesterday, and foo.txt created "earlier".
92
*/
93
check(dirOuter.mkdir());
94
check(dirInner.mkdir());
95
File fileInner = new File(dirInner, "foo.txt");
96
try (PrintWriter pw = new PrintWriter(fileInner)) {
97
pw.println("hello, world");
98
}
99
100
// Get the "now" from the "last-modified-time" of the last file we
101
// just created, instead of the "System.currentTimeMillis()", to
102
// workaround the possible "time difference" due to nfs.
103
final long now = fileInner.lastModified();
104
final long earlier = now - (60L * 60L * 6L * 1000L);
105
final long yesterday = now - (60L * 60L * 24L * 1000L);
106
107
check(dirOuter.setLastModified(now));
108
check(dirInner.setLastModified(yesterday));
109
check(fileInner.setLastModified(earlier));
110
111
// Make a jar file from that directory structure
112
Main jartool = new Main(System.out, System.err, "jar");
113
check(jartool.run(new String[] {
114
"cf",
115
jarFile.getName(), dirOuter.getName() } ));
116
check(jarFile.exists());
117
118
check(cleanup(dirInner));
119
check(cleanup(dirOuter));
120
121
// Extract and check that the last modified values are those specified
122
// in the archive
123
extractJar(jarFile, false);
124
check(dirOuter.exists());
125
check(dirInner.exists());
126
check(fileInner.exists());
127
checkFileTime(dirOuter.lastModified(), now);
128
checkFileTime(dirInner.lastModified(), yesterday);
129
checkFileTime(fileInner.lastModified(), earlier);
130
131
check(cleanup(dirInner));
132
check(cleanup(dirOuter));
133
134
// Extract and check the last modified values are the current times.
135
// See sun.tools.jar.Main
136
extractJar(jarFile, true);
137
check(dirOuter.exists());
138
check(dirInner.exists());
139
check(fileInner.exists());
140
checkFileTime(dirOuter.lastModified(), now);
141
checkFileTime(dirInner.lastModified(), now);
142
checkFileTime(fileInner.lastModified(), now);
143
144
check(cleanup(dirInner));
145
check(cleanup(dirOuter));
146
147
check(jarFile.delete());
148
}
149
150
static void checkFileTime(long now, long original) {
151
if (Math.abs(now - original) > PRECISION) {
152
System.out.format("Extracted to %s, expected to be close to %s%n",
153
FileTime.fromMillis(now), FileTime.fromMillis(original));
154
fail();
155
}
156
}
157
158
//--------------------- Infrastructure ---------------------------
159
static volatile int passed = 0, failed = 0;
160
static void pass() {passed++;}
161
static void fail() {failed++; Thread.dumpStack();}
162
static void fail(String msg) {System.out.println(msg); fail();}
163
static void unexpected(Throwable t) {failed++; t.printStackTrace();}
164
static void check(boolean cond) {if (cond) pass(); else fail();}
165
static void equal(Object x, Object y) {
166
if (x == null ? y == null : x.equals(y)) pass();
167
else fail(x + " not equal to " + y);}
168
public static void main(String[] args) throws Throwable {
169
try {realMain(args);} catch (Throwable t) {unexpected(t);}
170
System.out.println("\nPassed = " + passed + " failed = " + failed);
171
if (failed > 0) throw new AssertionError("Some tests failed");}
172
}
173
174