Skip to content

Commit 51b941b

Browse files
committed
Refactor FTPS tests to use @nested classes for better lifecycle management
Replaced separate FtpsProviderExplicitTest and FtpsProviderImplicitTest classes with a single FtpsProviderTest class containing @nested classes for each mode. Benefits: - Eliminates server lifecycle conflicts between test classes - Both explicit and implicit mode tests run successfully in same JVM - Cleaner architecture with shared outer class - Each nested class manages its own server lifecycle independently - Tests run sequentially within the same test class, avoiding port conflicts Changes: - Created FtpsProviderTest with ExplicitMode and ImplicitMode nested classes - Removed FtpsProviderExplicitTest.java - Removed FtpsProviderImplicitTest.java - Simplified tearDownClass() by removing unnecessary waits Test Results: - ImplicitMode: 97 tests, 0 failures, 2 skipped - ExplicitMode: 97 tests, 0 failures, 2 skipped - Total: 194 tests, 0 failures, 4 skipped - Full suite: 2700 tests, 0 failures, 645 skipped
1 parent 3e3e180 commit 51b941b

File tree

4 files changed

+129
-180
lines changed

4 files changed

+129
-180
lines changed

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/AbstractFtpsProviderTestCase.java

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -154,22 +154,10 @@ synchronized static void setUpClass(final boolean implicit) throws FtpException
154154
*/
155155
synchronized static void tearDownClass() {
156156
if (embeddedFtpServer != null) {
157-
// First suspend to stop accepting new connections
158157
embeddedFtpServer.suspend();
159-
160-
// Wait a bit for active connections to finish
161-
try {
162-
Thread.sleep(300);
163-
} catch (final InterruptedException e) {
164-
e.printStackTrace();
165-
}
166-
167-
// Now stop the server
168158
embeddedFtpServer.stop();
169159
Thread.yield();
170-
171-
// Wait for server to fully stop
172-
int count = 20; // Increased from 10 to 20
160+
int count = 10;
173161
while (count-- > 0 && !embeddedFtpServer.isStopped()) {
174162
final int millis = 200;
175163
System.out.println(String.format("Waiting %,d milliseconds for %s to stop", millis, embeddedFtpServer));
@@ -179,18 +167,9 @@ synchronized static void tearDownClass() {
179167
e.printStackTrace();
180168
}
181169
}
182-
183-
// Clear the reference
184170
embeddedFtpServer = null;
185171
socketPort = 0;
186172
connectionUri = null;
187-
188-
// Additional wait to ensure port is fully released and all resources cleaned up
189-
try {
190-
Thread.sleep(1000); // Increased from 500 to 1000
191-
} catch (final InterruptedException e) {
192-
e.printStackTrace();
193-
}
194173
}
195174
}
196175

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/FtpsProviderExplicitTest.java

Lines changed: 0 additions & 79 deletions
This file was deleted.

commons-vfs2/src/test/java/org/apache/commons/vfs2/provider/ftps/FtpsProviderImplicitTest.java

Lines changed: 0 additions & 79 deletions
This file was deleted.
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one or more
3+
* contributor license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright ownership.
5+
* The ASF licenses this file to You under the Apache License, Version 2.0
6+
* (the "License"); you may not use this file except in compliance with
7+
* the License. You may obtain a copy of the License at
8+
*
9+
* https://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
package org.apache.commons.vfs2.provider.ftps;
18+
19+
import org.apache.commons.vfs2.FileSystemException;
20+
import org.apache.commons.vfs2.ProviderTestSuiteJunit5;
21+
import org.apache.ftpserver.ftplet.FtpException;
22+
import org.junit.jupiter.api.AfterAll;
23+
import org.junit.jupiter.api.BeforeAll;
24+
import org.junit.jupiter.api.Nested;
25+
import org.junit.jupiter.api.TestInstance;
26+
import org.opentest4j.TestAbortedException;
27+
28+
/**
29+
* Tests for FTPS file systems with both explicit and implicit FTPS connections (JUnit 5).
30+
* Uses @Nested classes to organize tests by connection mode while sharing server lifecycle.
31+
*/
32+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
33+
public class FtpsProviderTest {
34+
35+
/**
36+
* Tests for FTPS with explicit SSL/TLS mode.
37+
* In explicit mode, the client connects to the standard FTP port and then
38+
* explicitly requests security via the AUTH command.
39+
*/
40+
@Nested
41+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
42+
class ExplicitMode extends ProviderTestSuiteJunit5 {
43+
44+
public ExplicitMode() throws Exception {
45+
super(new FtpsProviderExplicitTestCase(), "", false);
46+
}
47+
48+
@BeforeAll
49+
void setUpServer() throws Exception {
50+
if (FtpsProviderExplicitTestCase.getSystemTestUriOverride() == null) {
51+
try {
52+
FtpsProviderExplicitTestCase.setUpClass(false); // explicit mode
53+
} catch (final FtpException e) {
54+
// Server failed to start - abort all tests in this class
55+
throw new TestAbortedException("FTP server failed to start: " + e.getMessage(), e);
56+
}
57+
}
58+
}
59+
60+
@Override
61+
protected void setUp() throws Exception {
62+
try {
63+
super.setUp();
64+
} catch (final FileSystemException e) {
65+
// Could not connect to FTP server - abort test
66+
throw new TestAbortedException("Could not connect to FTP server: " + e.getMessage(), e);
67+
}
68+
}
69+
70+
@AfterAll
71+
void tearDownServer() throws Exception {
72+
try {
73+
tearDown();
74+
} finally {
75+
FtpsProviderExplicitTestCase.tearDownClass();
76+
}
77+
}
78+
}
79+
80+
/**
81+
* Tests for FTPS with implicit SSL/TLS mode.
82+
* In implicit mode, the SSL/TLS connection is established immediately
83+
* upon connection, without an explicit AUTH command.
84+
*
85+
* Note: Implicit mode is not standardized and the protocol may differ
86+
* between FTPS servers. This mode is deprecated in favor of explicit mode.
87+
*/
88+
@Nested
89+
@TestInstance(TestInstance.Lifecycle.PER_CLASS)
90+
class ImplicitMode extends ProviderTestSuiteJunit5 {
91+
92+
public ImplicitMode() throws Exception {
93+
super(new FtpsProviderImplicitTestCase(), "", false);
94+
}
95+
96+
@BeforeAll
97+
void setUpServer() throws Exception {
98+
if (FtpsProviderImplicitTestCase.getSystemTestUriOverride() == null) {
99+
try {
100+
FtpsProviderImplicitTestCase.setUpClass(true); // implicit mode
101+
} catch (final FtpException e) {
102+
// Server failed to start - abort all tests in this class
103+
throw new TestAbortedException("FTP server failed to start: " + e.getMessage(), e);
104+
}
105+
}
106+
}
107+
108+
@Override
109+
protected void setUp() throws Exception {
110+
try {
111+
super.setUp();
112+
} catch (final FileSystemException e) {
113+
// Could not connect to FTP server - abort test
114+
throw new TestAbortedException("Could not connect to FTP server: " + e.getMessage(), e);
115+
}
116+
}
117+
118+
@AfterAll
119+
void tearDownServer() throws Exception {
120+
try {
121+
tearDown();
122+
} finally {
123+
FtpsProviderImplicitTestCase.tearDownClass();
124+
}
125+
}
126+
}
127+
}
128+

0 commit comments

Comments
 (0)