8362429: AssertionError in File.listFiles(FileFilter | FilenameFilter)

Reviewed-by: alanb
This commit is contained in:
Brian Burkhalter 2025-07-17 06:31:34 +00:00
parent bc72f476d1
commit be0161a8e6
2 changed files with 59 additions and 15 deletions

View File

@ -1134,15 +1134,9 @@ public class File
if (ss == null) return null;
int n = ss.length;
File[] fs = new File[n];
if (path.isEmpty()) {
for (int i = 0; i < n; i++) {
fs[i] = new File(ss[i]);
}
} else {
for (int i = 0; i < n; i++) {
fs[i] = new File(ss[i], this);
}
}
boolean isEmpty = path.isEmpty();
for (int i = 0; i < n; i++)
fs[i] = isEmpty ? new File(ss[i]) : new File(ss[i], this);
return fs;
}
@ -1175,9 +1169,10 @@ public class File
String[] ss = normalizedList();
if (ss == null) return null;
ArrayList<File> files = new ArrayList<>();
boolean isEmpty = path.isEmpty();
for (String s : ss)
if ((filter == null) || filter.accept(this, s))
files.add(new File(s, this));
files.add(isEmpty ? new File(s) : new File(s, this));
return files.toArray(new File[files.size()]);
}
@ -1208,8 +1203,9 @@ public class File
String[] ss = normalizedList();
if (ss == null) return null;
ArrayList<File> files = new ArrayList<>();
boolean isEmpty = path.isEmpty();
for (String s : ss) {
File f = new File(s, this);
File f = isEmpty ? new File(s) : new File(s, this);
if ((filter == null) || filter.accept(f))
files.add(f);
}

View File

@ -22,15 +22,18 @@
*/
/* @test
* @bug 4842706 8024695 8361587
* @bug 4842706 8024695 8361587 8362429
* @summary Test some file operations with empty path
* @run junit EmptyPath
*/
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FilenameFilter;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.MalformedURLException;
import java.nio.file.Files;
import java.nio.file.FileStore;
import java.nio.file.Path;
@ -38,6 +41,7 @@ import java.util.Arrays;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.function.Function;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeAll;
@ -211,7 +215,15 @@ public class EmptyPath {
@Test
public void list() throws IOException {
String[] files = f.list();
list(f.list());
}
@Test
public void listFilenameFilter() throws IOException {
list(f.list((FilenameFilter)null));
}
private void list(String[] files) throws IOException {
assertNotNull(files);
Set<String> ioSet = new HashSet(Arrays.asList(files));
Set<String> nioSet = new HashSet();
@ -221,11 +233,42 @@ public class EmptyPath {
@Test
public void listFiles() throws IOException {
File child = new File(f.getAbsoluteFile(), "child");
listFiles(x -> x.listFiles());
}
@Test
public void listFilesFileFilter() throws IOException {
FileFilter ff = new FileFilter() {
public boolean accept(File pathname) { return true; }
};
listFiles(x -> x.listFiles(ff));
}
@Test
public void listFilesNullFileFilter() throws IOException {
listFiles(x -> x.listFiles((FileFilter)null));
}
@Test
public void listFilesFilenameFilter() throws IOException {
FilenameFilter fnf = new FilenameFilter() {
public boolean accept(File dir, String name) { return true; }
};
listFiles(x -> x.listFiles(fnf));
}
@Test
public void listFilesNullFilenameFilter() throws IOException {
listFiles(x -> x.listFiles((FilenameFilter)null));
}
private void listFiles(Function<File,File[]> func) throws IOException {
String childName = "child" + System.nanoTime();
File child = new File(f.getAbsoluteFile(), childName);
assertTrue(child.createNewFile());
child.deleteOnExit();
File[] files = f.listFiles();
File[] files = func.apply(f);
for (File file : files)
assertEquals(-1, f.toString().indexOf(File.separatorChar));
@ -348,4 +391,9 @@ public class EmptyPath {
public void toURI() {
assertEquals(f.toPath().toUri(), f.toURI());
}
@Test
public void toURL() throws MalformedURLException {
assertEquals(f.toPath().toUri().toURL(), f.toURL());
}
}