Skip to content

Commit dbe34a9

Browse files
committed
Fix FtpFileObject.exists() returning true for root-level folders after
connection drop #757 - Use static imports for JUnit only - Sort members - Reduce vertical whitespace
1 parent a29cb51 commit dbe34a9

1 file changed

Lines changed: 24 additions & 30 deletions

File tree

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftp/FtpRootExistsOnDisconnectTest.java

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@
1414
* See the License for the specific language governing permissions and
1515
* limitations under the License.
1616
*/
17+
1718
package org.apache.commons.vfs2.provider.ftp;
1819

20+
import static org.junit.jupiter.api.Assertions.assertNull;
21+
import static org.junit.jupiter.api.Assertions.assertTrue;
22+
1923
import java.time.Duration;
2024

2125
import org.apache.commons.vfs2.FileObject;
@@ -24,21 +28,28 @@
2428
import org.apache.commons.vfs2.VfsTestUtils;
2529
import org.apache.commons.vfs2.impl.DefaultFileSystemManager;
2630
import org.junit.jupiter.api.AfterEach;
27-
import org.junit.jupiter.api.Assertions;
2831
import org.junit.jupiter.api.BeforeEach;
2932
import org.junit.jupiter.api.Test;
3033

3134
/**
3235
* Tests {@link FtpFileObject#exists()} for root-level FTP folders where {@code getParent()} returns {@code null}.
3336
* <p>
34-
* Regression test for the bug in {@link FtpFileObject} where {@code setFTPFile()} blindly assumed that root-level
35-
* directories exist ({@code setType(DIRECTORY_TYPE)}) without verifying on the server. This caused {@code exists()} to
36-
* return {@code true} even after the FTP connection was lost, while non-root folders correctly reported the connection
37-
* failure via {@link FileSystemException}.
37+
* Regression test for the bug in {@link FtpFileObject} where {@code setFTPFile()} blindly assumed that root-level directories exist
38+
* ({@code setType(DIRECTORY_TYPE)}) without verifying on the server. This caused {@code exists()} to return {@code true} even after the FTP connection was
39+
* lost, while non-root folders correctly reported the connection failure via {@link FileSystemException}.
3840
* </p>
3941
*/
4042
public class FtpRootExistsOnDisconnectTest {
4143

44+
private static FileSystemOptions createOptions() {
45+
final FileSystemOptions options = new FileSystemOptions();
46+
final FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance();
47+
builder.setUserDirIsRoot(options, true);
48+
builder.setPassiveMode(options, true);
49+
builder.setConnectTimeout(options, Duration.ofSeconds(10));
50+
return options;
51+
}
52+
4253
@BeforeEach
4354
public void setUp() throws Exception {
4455
FtpProviderTest.setUpClass(VfsTestUtils.getTestDirectory(), null, null);
@@ -49,26 +60,15 @@ public void tearDown() {
4960
FtpProviderTest.tearDownClass();
5061
}
5162

52-
private static FileSystemOptions createOptions() {
53-
final FileSystemOptions options = new FileSystemOptions();
54-
final FtpFileSystemConfigBuilder builder = FtpFileSystemConfigBuilder.getInstance();
55-
builder.setUserDirIsRoot(options, true);
56-
builder.setPassiveMode(options, true);
57-
builder.setConnectTimeout(options, Duration.ofSeconds(10));
58-
return options;
59-
}
60-
6163
/**
62-
* Tests that {@code exists()} returns {@code true} when the server is running, and does not silently return
63-
* {@code true} after the FTP connection is lost.
64+
* Tests that {@code exists()} returns {@code true} when the server is running, and does not silently return {@code true} after the FTP connection is lost.
6465
* <p>
65-
* With {@code userDirIsRoot=true}, the root's {@code getParent()} returns {@code null}, which triggers the
66-
* {@code verifyRootDirectory()} code path in {@code setFTPFile()}.
66+
* With {@code userDirIsRoot=true}, the root's {@code getParent()} returns {@code null}, which triggers the {@code verifyRootDirectory()} code path in
67+
* {@code setFTPFile()}.
6768
* </p>
6869
* <p>
69-
* Before the fix, {@code setFTPFile()} set {@code type=DIRECTORY} when {@code getParent()} returned {@code null},
70-
* without contacting the server. After the fix, {@code setFTPFile()} uses CWD to verify, which fails on a dead
71-
* connection.
70+
* Before the fix, {@code setFTPFile()} set {@code type=DIRECTORY} when {@code getParent()} returned {@code null}, without contacting the server. After the
71+
* fix, {@code setFTPFile()} uses CWD to verify, which fails on a dead connection.
7272
* </p>
7373
*/
7474
@Test
@@ -77,18 +77,13 @@ public void testRootExistsFailsWhenConnectionDropped() throws Exception {
7777
manager.addProvider("ftp", new FtpFileProvider());
7878
manager.init();
7979
final FileObject root = manager.resolveFile(FtpProviderTest.getConnectionUri(), createOptions());
80-
8180
// Verify precondition: with userDirIsRoot=true, getParent() returns null,
8281
// which is the code path this test exercises.
83-
Assertions.assertNull(root.getParent(),
84-
"Root folder's getParent() should return null with userDirIsRoot=true");
85-
82+
assertNull(root.getParent(), "Root folder's getParent() should return null with userDirIsRoot=true");
8683
// Verify the root exists initially.
87-
Assertions.assertTrue(root.exists(), "Root should exist while server is running");
88-
84+
assertTrue(root.exists(), "Root should exist while server is running");
8985
// Stop the server to simulate a connection drop.
9086
FtpProviderTest.tearDownClass();
91-
9287
// exists() must throw FileSystemException on the dead connection.
9388
// The first call may still succeed if the MINA server processes one last
9489
// CWD during graceful shutdown, but the second call must fail.
@@ -101,8 +96,7 @@ public void testRootExistsFailsWhenConnectionDropped() throws Exception {
10196
threwException = true;
10297
}
10398
}
104-
Assertions.assertTrue(threwException,
105-
"exists() must throw FileSystemException after FTP connection is lost");
99+
assertTrue(threwException, "exists() must throw FileSystemException after FTP connection is lost");
106100
}
107101
}
108102
}

0 commit comments

Comments
 (0)