Skip to content

Commit b14ee39

Browse files
committed
Convert JUnit 4 Parameterized test to @nested tests in JUnit 5
1 parent 7157606 commit b14ee39

File tree

2 files changed

+164
-160
lines changed

2 files changed

+164
-160
lines changed

src/test/java/org/apache/commons/net/ftp/AbstractFtpsTest.java

Lines changed: 154 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@
2626
import java.net.SocketException;
2727
import java.net.URL;
2828
import java.time.Duration;
29+
import java.time.Instant;
30+
import java.util.Calendar;
2931

3032
import org.apache.commons.io.FileUtils;
3133
import org.apache.commons.io.output.NullOutputStream;
@@ -39,6 +41,9 @@
3941
import org.apache.ftpserver.ssl.SslConfigurationFactory;
4042
import org.apache.ftpserver.usermanager.PropertiesUserManagerFactory;
4143
import org.apache.ftpserver.usermanager.impl.BaseUser;
44+
import org.junit.jupiter.api.BeforeAll;
45+
import org.junit.jupiter.api.Test;
46+
import org.junit.jupiter.api.Timeout;
4247

4348
/**
4449
* Tests {@link FTPSClient}.
@@ -62,6 +67,10 @@ public abstract class AbstractFtpsTest {
6267
private static final boolean TRACE_CALLS = Boolean.parseBoolean(System.getenv("TRACE_CALLS"));
6368
private static final boolean ADD_LISTENER = Boolean.parseBoolean(System.getenv("ADD_LISTENER"));
6469
private static final long startTime = System.nanoTime();
70+
private static final String USER_PROPS_RES = "org/apache/commons/net/ftpsserver/users.properties";
71+
private static final String SERVER_JKS_RES = "org/apache/commons/net/ftpsserver/ftpserver.jks";
72+
73+
private final boolean endpointCheckingEnabled;
6574

6675
/**
6776
* Returns the test directory as a String.
@@ -136,9 +145,12 @@ protected static void trace(final String msg) {
136145
}
137146
}
138147

139-
private final boolean endpointCheckingEnabled;
148+
@BeforeAll
149+
public static void setupServer() throws Exception {
150+
AbstractFtpsTest.setupServer(AbstractFtpsTest.IMPLICIT, USER_PROPS_RES, SERVER_JKS_RES, "target/test-classes/org/apache/commons/net/test-data");
151+
}
140152

141-
public AbstractFtpsTest(final boolean endpointCheckingEnabled, final String userPropertiesResource, final String serverJksResource) {
153+
public AbstractFtpsTest(final boolean endpointCheckingEnabled) {
142154
this.endpointCheckingEnabled = endpointCheckingEnabled;
143155
}
144156

@@ -208,4 +220,144 @@ protected void retrieveFile(final String pathname) throws SocketException, IOExc
208220
client.disconnect();
209221
}
210222
}
223+
224+
225+
@Test
226+
@Timeout(TEST_TIMEOUT)
227+
public void testHasFeature() throws SocketException, IOException {
228+
trace(">>testHasFeature");
229+
loginClient().disconnect();
230+
trace("<<testHasFeature");
231+
}
232+
233+
private void testListFiles(final String pathname) throws SocketException, IOException {
234+
final FTPSClient client = loginClient();
235+
try {
236+
// do it twice
237+
assertNotNull(client.listFiles(pathname));
238+
assertNotNull(client.listFiles(pathname));
239+
} finally {
240+
client.disconnect();
241+
}
242+
}
243+
244+
@Test
245+
@Timeout(TEST_TIMEOUT)
246+
public void testListFilesPathNameEmpty() throws SocketException, IOException {
247+
trace(">>testListFilesPathNameEmpty");
248+
testListFiles("");
249+
trace("<<testListFilesPathNameEmpty");
250+
}
251+
252+
@Test
253+
@Timeout(TEST_TIMEOUT)
254+
public void testListFilesPathNameJunk() throws SocketException, IOException {
255+
trace(">>testListFilesPathNameJunk");
256+
testListFiles(" Junk ");
257+
trace("<<testListFilesPathNameJunk");
258+
}
259+
260+
@Test
261+
@Timeout(TEST_TIMEOUT)
262+
public void testListFilesPathNameNull() throws SocketException, IOException {
263+
trace(">>testListFilesPathNameNull");
264+
testListFiles(null);
265+
trace("<<testListFilesPathNameNull");
266+
}
267+
268+
@Test
269+
@Timeout(TEST_TIMEOUT)
270+
public void testListFilesPathNameRoot() throws SocketException, IOException {
271+
trace(">>testListFilesPathNameRoot");
272+
testListFiles("/");
273+
trace("<<testListFilesPathNameRoot");
274+
}
275+
276+
@Test
277+
@Timeout(TEST_TIMEOUT)
278+
public void testMdtmCalendar() throws SocketException, IOException {
279+
trace(">>testMdtmCalendar");
280+
testMdtmCalendar("/file.txt");
281+
trace("<<testMdtmCalendar");
282+
}
283+
284+
private void testMdtmCalendar(final String pathname) throws SocketException, IOException {
285+
final FTPSClient client = loginClient();
286+
try {
287+
// do it twice
288+
final Calendar mdtmCalendar1 = client.mdtmCalendar(pathname);
289+
final Calendar mdtmCalendar2 = client.mdtmCalendar(pathname);
290+
assertNotNull(mdtmCalendar1);
291+
assertNotNull(mdtmCalendar2);
292+
assertEquals(mdtmCalendar1, mdtmCalendar2);
293+
} finally {
294+
client.disconnect();
295+
}
296+
}
297+
298+
@Test
299+
@Timeout(TEST_TIMEOUT)
300+
public void testMdtmFile() throws SocketException, IOException {
301+
trace(">>testMdtmFile");
302+
testMdtmFile("/file.txt");
303+
trace("<<testMdtmFile");
304+
}
305+
306+
private void testMdtmFile(final String pathname) throws SocketException, IOException {
307+
final FTPSClient client = loginClient();
308+
try {
309+
// do it twice
310+
final FTPFile mdtmFile1 = client.mdtmFile(pathname);
311+
final FTPFile mdtmFile2 = client.mdtmFile(pathname);
312+
assertNotNull(mdtmFile1);
313+
assertNotNull(mdtmFile2);
314+
assertEquals(mdtmFile1.toString(), mdtmFile2.toString());
315+
} finally {
316+
client.disconnect();
317+
}
318+
}
319+
320+
@Test
321+
@Timeout(TEST_TIMEOUT)
322+
public void testMdtmInstant() throws SocketException, IOException {
323+
trace(">>testMdtmInstant");
324+
testMdtmInstant("/file.txt");
325+
trace("<<testMdtmInstant");
326+
}
327+
328+
private void testMdtmInstant(final String pathname) throws SocketException, IOException {
329+
final FTPSClient client = loginClient();
330+
try {
331+
// do it twice
332+
final Instant mdtmInstant1 = client.mdtmInstant(pathname);
333+
final Instant mdtmInstant2 = client.mdtmInstant(pathname);
334+
assertNotNull(mdtmInstant1);
335+
assertNotNull(mdtmInstant2);
336+
assertEquals(mdtmInstant1, mdtmInstant2);
337+
} finally {
338+
client.disconnect();
339+
}
340+
}
341+
342+
@Test
343+
@Timeout(TEST_TIMEOUT)
344+
public void testOpenClose() throws SocketException, IOException {
345+
trace(">>testOpenClose");
346+
final FTPSClient ftpsClient = loginClient();
347+
try {
348+
assertTrue(ftpsClient.hasFeature("MODE"));
349+
assertTrue(ftpsClient.hasFeature(FTPCmd.MODE));
350+
} finally {
351+
ftpsClient.disconnect();
352+
}
353+
trace("<<testOpenClose");
354+
}
355+
356+
@Test
357+
@Timeout(TEST_TIMEOUT)
358+
public void testRetrieveFilePathNameRoot() throws SocketException, IOException {
359+
trace(">>testRetrieveFilePathNameRoot");
360+
retrieveFile("/file.txt");
361+
trace("<<testRetrieveFilePathNameRoot");
362+
}
211363
}

src/test/java/org/apache/commons/net/ftp/FTPSClientTest.java

Lines changed: 10 additions & 158 deletions
Original file line numberDiff line numberDiff line change
@@ -17,20 +17,7 @@
1717

1818
package org.apache.commons.net.ftp;
1919

20-
import static org.junit.jupiter.api.Assertions.assertEquals;
21-
import static org.junit.jupiter.api.Assertions.assertNotNull;
22-
import static org.junit.jupiter.api.Assertions.assertTrue;
23-
24-
import java.io.IOException;
25-
import java.net.SocketException;
26-
import java.time.Instant;
27-
import java.util.Calendar;
28-
29-
import org.junit.BeforeClass;
30-
import org.junit.Test;
31-
import org.junit.runner.RunWith;
32-
import org.junit.runners.Parameterized;
33-
import org.junit.runners.Parameterized.Parameters;
20+
import org.junit.jupiter.api.Nested;
3421

3522
/**
3623
* Tests {@link FTPSClient}.
@@ -45,154 +32,19 @@
4532
* This test does the above programmatically.
4633
* </p>
4734
*/
48-
@RunWith(Parameterized.class)
49-
public class FTPSClientTest extends AbstractFtpsTest {
50-
51-
private static final String USER_PROPS_RES = "org/apache/commons/net/ftpsserver/users.properties";
52-
53-
private static final String SERVER_JKS_RES = "org/apache/commons/net/ftpsserver/ftpserver.jks";
54-
55-
@BeforeClass
56-
public static void setupServer() throws Exception {
57-
setupServer(IMPLICIT, USER_PROPS_RES, SERVER_JKS_RES, "target/test-classes/org/apache/commons/net/test-data");
58-
}
59-
60-
@Parameters(name = "endpointCheckingEnabled={0}")
61-
public static Boolean[] testConstructurData() {
62-
return new Boolean[] { Boolean.FALSE, Boolean.TRUE };
63-
}
64-
65-
public FTPSClientTest(final boolean endpointCheckingEnabled) {
66-
super(endpointCheckingEnabled, null, null);
67-
}
68-
69-
@Test(timeout = TEST_TIMEOUT)
70-
public void testHasFeature() throws SocketException, IOException {
71-
trace(">>testHasFeature");
72-
loginClient().disconnect();
73-
trace("<<testHasFeature");
74-
}
75-
76-
private void testListFiles(final String pathname) throws SocketException, IOException {
77-
final FTPSClient client = loginClient();
78-
try {
79-
// do it twice
80-
assertNotNull(client.listFiles(pathname));
81-
assertNotNull(client.listFiles(pathname));
82-
} finally {
83-
client.disconnect();
84-
}
85-
}
86-
87-
@Test(timeout = TEST_TIMEOUT)
88-
public void testListFilesPathNameEmpty() throws SocketException, IOException {
89-
trace(">>testListFilesPathNameEmpty");
90-
testListFiles("");
91-
trace("<<testListFilesPathNameEmpty");
92-
}
93-
94-
@Test(timeout = TEST_TIMEOUT)
95-
public void testListFilesPathNameJunk() throws SocketException, IOException {
96-
trace(">>testListFilesPathNameJunk");
97-
testListFiles(" Junk ");
98-
trace("<<testListFilesPathNameJunk");
99-
}
35+
public class FTPSClientTest {
10036

101-
@Test(timeout = TEST_TIMEOUT)
102-
public void testListFilesPathNameNull() throws SocketException, IOException {
103-
trace(">>testListFilesPathNameNull");
104-
testListFiles(null);
105-
trace("<<testListFilesPathNameNull");
106-
}
107-
108-
@Test(timeout = TEST_TIMEOUT)
109-
public void testListFilesPathNameRoot() throws SocketException, IOException {
110-
trace(">>testListFilesPathNameRoot");
111-
testListFiles("/");
112-
trace("<<testListFilesPathNameRoot");
113-
}
114-
115-
@Test(timeout = TEST_TIMEOUT)
116-
public void testMdtmCalendar() throws SocketException, IOException {
117-
trace(">>testMdtmCalendar");
118-
testMdtmCalendar("/file.txt");
119-
trace("<<testMdtmCalendar");
120-
}
121-
122-
private void testMdtmCalendar(final String pathname) throws SocketException, IOException {
123-
final FTPSClient client = loginClient();
124-
try {
125-
// do it twice
126-
final Calendar mdtmCalendar1 = client.mdtmCalendar(pathname);
127-
final Calendar mdtmCalendar2 = client.mdtmCalendar(pathname);
128-
assertNotNull(mdtmCalendar1);
129-
assertNotNull(mdtmCalendar2);
130-
assertEquals(mdtmCalendar1, mdtmCalendar2);
131-
} finally {
132-
client.disconnect();
37+
@Nested
38+
class EndpointCheckingEnabledTest extends AbstractFtpsTest {
39+
public EndpointCheckingEnabledTest() {
40+
super(true);
13341
}
13442
}
13543

136-
@Test(timeout = TEST_TIMEOUT)
137-
public void testMdtmFile() throws SocketException, IOException {
138-
trace(">>testMdtmFile");
139-
testMdtmFile("/file.txt");
140-
trace("<<testMdtmFile");
141-
}
142-
143-
private void testMdtmFile(final String pathname) throws SocketException, IOException {
144-
final FTPSClient client = loginClient();
145-
try {
146-
// do it twice
147-
final FTPFile mdtmFile1 = client.mdtmFile(pathname);
148-
final FTPFile mdtmFile2 = client.mdtmFile(pathname);
149-
assertNotNull(mdtmFile1);
150-
assertNotNull(mdtmFile2);
151-
assertEquals(mdtmFile1.toString(), mdtmFile2.toString());
152-
} finally {
153-
client.disconnect();
44+
@Nested
45+
class EndpointCheckingDisabledTest extends AbstractFtpsTest {
46+
public EndpointCheckingDisabledTest() {
47+
super(false);
15448
}
15549
}
156-
157-
@Test(timeout = TEST_TIMEOUT)
158-
public void testMdtmInstant() throws SocketException, IOException {
159-
trace(">>testMdtmInstant");
160-
testMdtmInstant("/file.txt");
161-
trace("<<testMdtmInstant");
162-
}
163-
164-
private void testMdtmInstant(final String pathname) throws SocketException, IOException {
165-
final FTPSClient client = loginClient();
166-
try {
167-
// do it twice
168-
final Instant mdtmInstant1 = client.mdtmInstant(pathname);
169-
final Instant mdtmInstant2 = client.mdtmInstant(pathname);
170-
assertNotNull(mdtmInstant1);
171-
assertNotNull(mdtmInstant2);
172-
assertEquals(mdtmInstant1, mdtmInstant2);
173-
} finally {
174-
client.disconnect();
175-
}
176-
}
177-
178-
@Test(timeout = TEST_TIMEOUT)
179-
public void testOpenClose() throws SocketException, IOException {
180-
trace(">>testOpenClose");
181-
final FTPSClient ftpsClient = loginClient();
182-
try {
183-
assertTrue(ftpsClient.hasFeature("MODE"));
184-
assertTrue(ftpsClient.hasFeature(FTPCmd.MODE));
185-
} finally {
186-
ftpsClient.disconnect();
187-
}
188-
trace("<<testOpenClose");
189-
}
190-
191-
@Test(timeout = TEST_TIMEOUT)
192-
public void testRetrieveFilePathNameRoot() throws SocketException, IOException {
193-
trace(">>testRetrieveFilePathNameRoot");
194-
retrieveFile("/file.txt");
195-
trace("<<testRetrieveFilePathNameRoot");
196-
}
197-
19850
}

0 commit comments

Comments
 (0)