author | kvn |
Thu, 05 Dec 2013 15:13:12 -0800 | |
changeset 22858 | f4a6f0eba875 |
parent 21611 | 34085733bead |
child 32649 | 2ee9017c7597 |
permissions | -rw-r--r-- |
2 | 1 |
/* |
19578
2d9b4346f4a4
8023430: Replace File.mkdirs with Files.createDirectories to get MaxPathLength.java failure details
dxu
parents:
14342
diff
changeset
|
2 |
* Copyright (c) 2002, 2013, 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 |
/* @test |
|
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
25 |
@bug 4759207 4403166 4165006 4403166 6182812 6274272 7160013 |
2 | 26 |
@summary Test to see if win32 path length can be greater than 260 |
27 |
*/ |
|
28 |
||
29 |
import java.io.*; |
|
19578
2d9b4346f4a4
8023430: Replace File.mkdirs with Files.createDirectories to get MaxPathLength.java failure details
dxu
parents:
14342
diff
changeset
|
30 |
import java.nio.file.Files; |
21611
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
31 |
import java.nio.file.Path; |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
32 |
import java.nio.file.DirectoryNotEmptyException; |
2 | 33 |
|
34 |
public class MaxPathLength { |
|
35 |
private static String sep = File.separator; |
|
36 |
private static String pathComponent = sep + |
|
37 |
"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"; |
|
38 |
private static String fileName = |
|
39 |
"areallylongfilenamethatsforsur"; |
|
40 |
private static boolean isWindows = false; |
|
41 |
||
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
42 |
private final static int MAX_LENGTH = 256; |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
43 |
|
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
44 |
private static int counter = 0; |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
45 |
|
2 | 46 |
public static void main(String[] args) throws Exception { |
47 |
String osName = System.getProperty("os.name"); |
|
48 |
if (osName.startsWith("Windows")) { |
|
49 |
isWindows = true; |
|
50 |
} |
|
51 |
||
52 |
for (int i = 4; i < 7; i++) { |
|
53 |
String name = fileName; |
|
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
54 |
while (name.length() < MAX_LENGTH) { |
2 | 55 |
testLongPath (i, name, false); |
56 |
testLongPath (i, name, true); |
|
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
57 |
name = getNextName(name); |
2 | 58 |
} |
59 |
} |
|
60 |
||
13595
aa1d59f91187
6962637: TEST_BUG: java/io/File/MaxPathLength.java may fail in busy system
alanb
parents:
5506
diff
changeset
|
61 |
// test long paths on windows |
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
62 |
// And these long pathes cannot be handled on Solaris and Mac platforms |
13595
aa1d59f91187
6962637: TEST_BUG: java/io/File/MaxPathLength.java may fail in busy system
alanb
parents:
5506
diff
changeset
|
63 |
if (isWindows) { |
2 | 64 |
String name = fileName; |
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
65 |
while (name.length() < MAX_LENGTH) { |
13595
aa1d59f91187
6962637: TEST_BUG: java/io/File/MaxPathLength.java may fail in busy system
alanb
parents:
5506
diff
changeset
|
66 |
testLongPath (20, name, false); |
aa1d59f91187
6962637: TEST_BUG: java/io/File/MaxPathLength.java may fail in busy system
alanb
parents:
5506
diff
changeset
|
67 |
testLongPath (20, name, true); |
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
68 |
name = getNextName(name); |
2 | 69 |
} |
70 |
} |
|
71 |
} |
|
72 |
||
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
73 |
private static String getNextName(String fName) { |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
74 |
return (fName.length() < MAX_LENGTH/2) ? fName + fName |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
75 |
: fName + "A"; |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
76 |
} |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
77 |
|
2 | 78 |
static void testLongPath(int max, String fn, |
79 |
boolean tryAbsolute) throws Exception { |
|
80 |
String[] created = new String[max]; |
|
81 |
String pathString = "."; |
|
82 |
for (int i = 0; i < max -1; i++) { |
|
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
83 |
pathString = pathString + pathComponent + (counter++); |
2 | 84 |
created[max - 1 -i] = pathString; |
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
85 |
} |
2 | 86 |
|
87 |
File dirFile = new File(pathString); |
|
88 |
File f = new File(pathString + sep + fn); |
|
89 |
||
90 |
String tPath = f.getPath(); |
|
91 |
if (tryAbsolute) { |
|
92 |
tPath = f.getCanonicalPath(); |
|
93 |
} |
|
94 |
created[0] = tPath; |
|
95 |
||
96 |
//for getCanonicalPath testing on win32 |
|
97 |
File fu = new File(pathString + sep + fn.toUpperCase()); |
|
98 |
||
99 |
if (dirFile.exists()) { |
|
100 |
System.err.println("Warning: Test directory structure exists already!"); |
|
101 |
return; |
|
102 |
} |
|
19578
2d9b4346f4a4
8023430: Replace File.mkdirs with Files.createDirectories to get MaxPathLength.java failure details
dxu
parents:
14342
diff
changeset
|
103 |
|
2 | 104 |
try { |
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
105 |
Files.createDirectories(dirFile.toPath()); |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
106 |
|
2 | 107 |
if (tryAbsolute) |
108 |
dirFile = new File(dirFile.getCanonicalPath()); |
|
109 |
if (!dirFile.isDirectory()) |
|
110 |
throw new RuntimeException ("File.isDirectory() failed"); |
|
111 |
f = new File(tPath); |
|
112 |
if (!f.createNewFile()) { |
|
113 |
throw new RuntimeException ("File.createNewFile() failed"); |
|
114 |
} |
|
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
115 |
|
2 | 116 |
if (!f.exists()) |
117 |
throw new RuntimeException ("File.exists() failed"); |
|
118 |
if (!f.isFile()) |
|
119 |
throw new RuntimeException ("File.isFile() failed"); |
|
120 |
if (!f.canRead()) |
|
121 |
throw new RuntimeException ("File.canRead() failed"); |
|
122 |
if (!f.canWrite()) |
|
123 |
throw new RuntimeException ("File.canWrite() failed"); |
|
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
124 |
|
2 | 125 |
if (!f.delete()) |
126 |
throw new RuntimeException ("File.delete() failed"); |
|
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
127 |
|
2 | 128 |
FileOutputStream fos = new FileOutputStream(f); |
129 |
fos.write(1); |
|
130 |
fos.close(); |
|
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
131 |
|
2 | 132 |
if (f.length() != 1) |
133 |
throw new RuntimeException ("File.length() failed"); |
|
134 |
long time = System.currentTimeMillis(); |
|
135 |
if (!f.setLastModified(time)) |
|
136 |
throw new RuntimeException ("File.setLastModified() failed"); |
|
137 |
if (f.lastModified() == 0) { |
|
138 |
throw new RuntimeException ("File.lastModified() failed"); |
|
139 |
} |
|
140 |
String[] list = dirFile.list(); |
|
141 |
if (list == null || !fn.equals(list[0])) { |
|
142 |
throw new RuntimeException ("File.list() failed"); |
|
143 |
} |
|
144 |
||
145 |
File[] flist = dirFile.listFiles(); |
|
146 |
if (flist == null || !fn.equals(flist[0].getName())) |
|
147 |
throw new RuntimeException ("File.listFiles() failed"); |
|
148 |
||
149 |
if (isWindows && |
|
150 |
!fu.getCanonicalPath().equals(f.getCanonicalPath())) |
|
151 |
throw new RuntimeException ("getCanonicalPath() failed"); |
|
152 |
||
153 |
char[] cc = tPath.toCharArray(); |
|
154 |
cc[cc.length-1] = 'B'; |
|
155 |
File nf = new File(new String(cc)); |
|
156 |
if (!f.renameTo(nf)) { |
|
157 |
/*there is a known issue that renameTo fails if |
|
158 |
(1)the path is a UNC path and |
|
159 |
(2)the path length is bigger than 1092 |
|
160 |
so don't stop if above are true |
|
161 |
*/ |
|
162 |
String abPath = f.getAbsolutePath(); |
|
163 |
if (!abPath.startsWith("\\\\") || |
|
164 |
abPath.length() < 1093) { |
|
165 |
throw new RuntimeException ("File.renameTo() failed for lenth=" |
|
166 |
+ abPath.length()); |
|
167 |
} |
|
19792
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
168 |
} else { |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
169 |
if (!nf.canRead()) |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
170 |
throw new RuntimeException ("Renamed file is not readable"); |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
171 |
if (!nf.canWrite()) |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
172 |
throw new RuntimeException ("Renamed file is not writable"); |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
173 |
if (nf.length() != 1) |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
174 |
throw new RuntimeException ("Renamed file's size is not correct"); |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
175 |
if (!nf.renameTo(f)) { |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
176 |
created[0] = nf.getPath(); |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
177 |
} |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
178 |
/* add a script to test these two if we got a regression later |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
179 |
if (!f.setReadOnly()) |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
180 |
throw new RuntimeException ("File.setReadOnly() failed"); |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
181 |
f.deleteOnExit(); |
74f9d8ff22d0
8023765: Improve MaxPathLength.java testcase and reduce its test load
dxu
parents:
19578
diff
changeset
|
182 |
*/ |
2 | 183 |
} |
184 |
} finally { |
|
185 |
// Clean up |
|
186 |
for (int i = 0; i < max; i++) { |
|
21611
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
187 |
Path p = (new File(created[i])).toPath(); |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
188 |
try { |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
189 |
Files.deleteIfExists(p); |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
190 |
// Test if the file is really deleted and wait for 1 second at most |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
191 |
for (int j = 0; j < 10 && Files.exists(p); j++) { |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
192 |
Thread.sleep(100); |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
193 |
} |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
194 |
} catch (DirectoryNotEmptyException ex) { |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
195 |
// Give up the clean-up, let jtreg handle it. |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
196 |
System.err.println("Dir, " + p + ", is not empty"); |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
197 |
break; |
34085733bead
8027612: java/io/File/MaxPathLength.java fails intermittently in the clean-up stage
dxu
parents:
19792
diff
changeset
|
198 |
} |
2 | 199 |
} |
200 |
} |
|
201 |
} |
|
202 |
} |