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