-
Notifications
You must be signed in to change notification settings - Fork 6.1k
8362429: AssertionError in File.listFiles(FileFilter | FilenameFilter) #26353
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,22 +22,26 @@ | |
*/ | ||
|
||
/* @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; | ||
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 void length() throws IOException { | |
|
||
@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,26 @@ public void list() throws IOException { | |
|
||
@Test | ||
public void listFiles() throws IOException { | ||
File child = new File(f.getAbsoluteFile(), "child"); | ||
listFiles(x -> x.listFiles()); | ||
} | ||
|
||
@Test | ||
public void listFilesFileFilter() throws IOException { | ||
listFiles(x -> x.listFiles((FileFilter)null)); | ||
} | ||
|
||
@Test | ||
public void listFilesFilenameFilter() throws IOException { | ||
listFiles(x -> x.listFiles((FilenameFilter)null)); | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What would you think about test a non-null filter too? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Okay. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add |
||
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 +375,9 @@ public String toString() { | |
public void toURI() { | ||
assertEquals(f.toPath().toUri(), f.toURI()); | ||
} | ||
|
||
@Test | ||
public void toURL() throws MalformedURLException { | ||
assertEquals(f.toPath().toUri().toURL(), f.toURL()); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than duplicating the loop, it could be changed to introduce
boolean isEmpty = path.isEmpty
and then thef
can be created withFile f = isEmpty ? new File(s) : new File(this, s);
It should be a bit cleaner.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had rejected that in the case of the parameter-less
listFiles
to avoid a ternary operator in each loop iteration but it is cleaner. Will update.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So changed in cfd494f.