lucastheisen / jsch-nio

Java nio FileSystem implementation over SSH
MIT License
99 stars 22 forks source link

DirectoryStream's iterator returns parent directory if the directory is empty #4

Closed wgtthompson closed 9 years ago

wgtthompson commented 9 years ago
Summary

DirectoryStream's iterator returns parent directory if the directory is empty. For example if you pass path /my/path to get the DirectoryStream, then the iterator returns the path /my/path if /my/path is empty. DirectoryStream returns correct results if the directory is not empty.

Remote Machine

Linux homer 3.2.0-4-amd64 #1 SMP Debian 3.2.60-1+deb7u1 x86_64 GNU/Linux

JUnit Test Snippet

The following test fails at assertFalse(iter.hasNext());. The next method also returns a Path (but that section of code is not reached in the test below).

    @Test
    public void test_directoryStream_emptyDir() throws IOException {
        FileSystem fs = FS;

        Path p0 = fs.getPath("test/test_directoryStream_emptyDir_" + TIMESTAMP);

        //create test dir
        assertFalse(Files.exists(p0));
        Files.createDirectories(p0);
        assertTrue(Files.exists(p0));
        assertTrue(Files.isDirectory(p0));

        //test dirstream
        DirectoryStream<Path> ds = Files.newDirectoryStream(p0);
        try {
            Iterator<Path> iter = ds.iterator();
            assertFalse(iter.hasNext());
            try {
                iter.next();
                fail("expected an exception");
            } catch( NoSuchElementException e) {
                //pass
            }
        }finally {
            ds.close();
        }
    }
lucastheisen commented 9 years ago

Fixed in release version 0.1.4. Should be available on maven central in a couple hours.

wgtthompson commented 9 years ago

@lucastheisen Thank you.