Skip to content

Commit 69ea85e

Browse files
author
Brian Burkhalter
committed
8361587: AssertionError in File.listFiles() when path is empty and -esa is enabled
Reviewed-by: alanb Backport-of: eefbfdc
1 parent 93260d6 commit 69ea85e

File tree

2 files changed

+83
-4
lines changed

2 files changed

+83
-4
lines changed

src/java.base/share/classes/java/io/File.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1134,8 +1134,14 @@ public File[] listFiles() {
11341134
if (ss == null) return null;
11351135
int n = ss.length;
11361136
File[] fs = new File[n];
1137-
for (int i = 0; i < n; i++) {
1138-
fs[i] = new File(ss[i], this);
1137+
if (path.isEmpty()) {
1138+
for (int i = 0; i < n; i++) {
1139+
fs[i] = new File(ss[i]);
1140+
}
1141+
} else {
1142+
for (int i = 0; i < n; i++) {
1143+
fs[i] = new File(ss[i], this);
1144+
}
11391145
}
11401146
return fs;
11411147
}

test/jdk/java/io/File/EmptyPath.java

Lines changed: 75 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
*/
2323

2424
/* @test
25-
* @bug 4842706 8024695
25+
* @bug 4842706 8024695 8361587
2626
* @summary Test some file operations with empty path
2727
* @run junit EmptyPath
2828
*/
@@ -36,13 +36,14 @@
3636
import java.nio.file.Path;
3737
import java.util.Arrays;
3838
import java.util.HashSet;
39+
import java.util.List;
3940
import java.util.Set;
41+
import java.util.stream.Collectors;
4042

4143
import org.junit.jupiter.api.BeforeAll;
4244
import org.junit.jupiter.api.Test;
4345

4446
import org.junit.jupiter.api.condition.DisabledOnOs;
45-
import org.junit.jupiter.api.condition.EnabledOnOs;
4647
import org.junit.jupiter.api.condition.OS;
4748

4849
import static org.junit.jupiter.api.Assertions.*;
@@ -105,13 +106,28 @@ public void exists() {
105106
assertTrue(f.exists());
106107
}
107108

109+
@Test
110+
public void getAbsoluteFile() {
111+
assertEquals(p.toAbsolutePath().toFile(), f.getAbsoluteFile());
112+
}
113+
108114
@Test
109115
public void getAbsolutePath() {
110116
System.out.println(p.toAbsolutePath().toString() + "\n" +
111117
f.getAbsolutePath());
112118
assertEquals(p.toAbsolutePath().toString(), f.getAbsolutePath());
113119
}
114120

121+
@Test
122+
public void getCanonicalFile() throws IOException {
123+
assertEquals(p.toRealPath().toFile(), f.getCanonicalFile());
124+
}
125+
126+
@Test
127+
public void getCanonicalPath() throws IOException {
128+
assertEquals(p.toRealPath().toString(), f.getCanonicalPath());
129+
}
130+
115131
private void checkSpace(long expected, long actual) {
116132
if (expected == 0) {
117133
assertEquals(0L, actual);
@@ -136,6 +152,11 @@ public void getParent() {
136152
assertNull(f.getParent());
137153
}
138154

155+
@Test
156+
public void getParentFile() {
157+
assertNull(f.getParentFile());
158+
}
159+
139160
@Test
140161
public void getPath() {
141162
assertEquals(p.toString(), f.getPath());
@@ -198,11 +219,57 @@ public void list() throws IOException {
198219
assertEquals(nioSet, ioSet);
199220
}
200221

222+
@Test
223+
public void listFiles() throws IOException {
224+
File child = new File(f.getAbsoluteFile(), "child");
225+
assertTrue(child.createNewFile());
226+
child.deleteOnExit();
227+
228+
File[] files = f.listFiles();
229+
for (File file : files)
230+
assertEquals(-1, f.toString().indexOf(File.separatorChar));
231+
232+
Set<String> ioSet = Arrays.stream(files)
233+
.map(File::getName)
234+
.collect(Collectors.toSet());
235+
236+
assertTrue(ioSet.contains(child.getName()));
237+
238+
Set<String> nioSet = Files.list(p)
239+
.map(Path::getFileName)
240+
.map(Path::toString)
241+
.collect(Collectors.toSet());
242+
assertEquals(nioSet, ioSet);
243+
}
244+
245+
@Test
246+
public void listRoots() {
247+
Set<String> expected = Arrays.stream(f.getAbsoluteFile().listRoots())
248+
.map(File::toString)
249+
.collect(Collectors.toSet());
250+
Set<String> actual = Arrays.stream(f.listRoots())
251+
.map(File::toString)
252+
.collect(Collectors.toSet());
253+
assertEquals(expected, actual);
254+
}
255+
201256
@Test
202257
public void mkdir() {
203258
assertFalse(f.mkdir());
204259
}
205260

261+
@Test
262+
public void mkdirs() {
263+
assertFalse(f.mkdirs());
264+
}
265+
266+
@Test
267+
public void renameTo() throws IOException {
268+
File tmp = File.createTempFile("foo", "bar", f.getAbsoluteFile());
269+
assertTrue(tmp.exists());
270+
assertFalse(f.renameTo(tmp));
271+
}
272+
206273
@Test
207274
public void setLastModified() {
208275
long t0 = f.lastModified();
@@ -271,6 +338,12 @@ public void toPath() {
271338
assertEquals(p, f.toPath());
272339
}
273340

341+
@Test
342+
public String toString() {
343+
assertEquals(EMPTY_STRING, f.toString());
344+
return EMPTY_STRING;
345+
}
346+
274347
@Test
275348
public void toURI() {
276349
assertEquals(f.toPath().toUri(), f.toURI());

0 commit comments

Comments
 (0)