Skip to content

Commit 4e4eaf5

Browse files
committed
Migrate from JUnit 4 to 5, part 2
1 parent f7537a4 commit 4e4eaf5

17 files changed

+565
-695
lines changed

src/test/java/com/trilead/ssh2/ConnectionTest.java

Lines changed: 49 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,23 @@
11
package com.trilead.ssh2;
22

3-
import static org.junit.Assert.assertArrayEquals;
4-
import static org.junit.Assert.assertEquals;
5-
import static org.junit.Assert.assertFalse;
6-
import static org.junit.Assert.assertNotNull;
7-
import static org.junit.Assert.assertNull;
8-
import static org.junit.Assert.assertTrue;
9-
import static org.junit.Assert.fail;
3+
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
4+
import static org.junit.jupiter.api.Assertions.assertEquals;
5+
import static org.junit.jupiter.api.Assertions.assertFalse;
6+
import static org.junit.jupiter.api.Assertions.assertNotNull;
7+
import static org.junit.jupiter.api.Assertions.assertNull;
8+
import static org.junit.jupiter.api.Assertions.assertTrue;
9+
import static org.junit.jupiter.api.Assertions.fail;
1010

1111
import java.io.IOException;
1212
import java.security.SecureRandom;
13-
import org.junit.Before;
14-
import org.junit.Test;
13+
import org.junit.jupiter.api.BeforeEach;
14+
import org.junit.jupiter.api.Test;
1515

1616
public class ConnectionTest {
1717

1818
private Connection connection;
1919

20-
@Before
20+
@BeforeEach
2121
public void setUp() {
2222
connection = new Connection("testhost.example.com", 2222);
2323
}
@@ -29,10 +29,10 @@ public void testConnectionConstructorWithHostnameAndPort() {
2929

3030
Connection conn = new Connection(hostname, port);
3131

32-
assertEquals("Hostname should match constructor parameter", hostname,
33-
conn.getHostname());
34-
assertEquals("Port should match constructor parameter", port,
35-
conn.getPort());
32+
assertEquals(hostname,
33+
conn.getHostname(), "Hostname should match constructor parameter");
34+
assertEquals(port,
35+
conn.getPort(), "Port should match constructor parameter");
3636
}
3737

3838
@Test
@@ -41,75 +41,71 @@ public void testConnectionConstructorWithHostnameOnly() {
4141

4242
Connection conn = new Connection(hostname);
4343

44-
assertEquals("Hostname should match constructor parameter", hostname,
45-
conn.getHostname());
46-
assertEquals("Port should default to 22", 22, conn.getPort());
44+
assertEquals(hostname,
45+
conn.getHostname(), "Hostname should match constructor parameter");
46+
assertEquals(22, conn.getPort(), "Port should default to 22");
4747
}
4848

4949
@Test
5050
public void testGetHostname() {
51-
assertEquals("Should return hostname passed to constructor",
52-
"testhost.example.com", connection.getHostname());
51+
assertEquals("testhost.example.com", connection.getHostname(), "Should return hostname passed to constructor");
5352
}
5453

5554
@Test
5655
public void testGetPort() {
57-
assertEquals("Should return port passed to constructor", 2222,
58-
connection.getPort());
56+
assertEquals(2222,
57+
connection.getPort(), "Should return port passed to constructor");
5958
}
6059

6160
@Test
6261
public void testIsAuthenticationCompleteInitiallyFalse() {
63-
assertFalse("Authentication should initially be incomplete",
64-
connection.isAuthenticationComplete());
62+
assertFalse(connection.isAuthenticationComplete(), "Authentication should initially be incomplete");
6563
}
6664

6765
@Test
6866
public void testIsAuthenticationPartialSuccessInitiallyFalse() {
69-
assertFalse("Partial success should initially be false",
70-
connection.isAuthenticationPartialSuccess());
67+
assertFalse(connection.isAuthenticationPartialSuccess(), "Partial success should initially be false");
7168
}
7269

7370
@Test
7471
public void testGetAvailableCiphers() {
7572
String[] ciphers = Connection.getAvailableCiphers();
7673

77-
assertNotNull("Cipher list should not be null", ciphers);
78-
assertTrue("Should have at least one cipher available", ciphers.length > 0);
74+
assertNotNull(ciphers, "Cipher list should not be null");
75+
assertTrue(ciphers.length > 0, "Should have at least one cipher available");
7976

8077
// Check that all returned values are non-null strings
8178
for (String cipher : ciphers) {
82-
assertNotNull("Cipher name should not be null", cipher);
83-
assertTrue("Cipher name should not be empty", cipher.length() > 0);
79+
assertNotNull(cipher, "Cipher name should not be null");
80+
assertTrue(cipher.length() > 0, "Cipher name should not be empty");
8481
}
8582
}
8683

8784
@Test
8885
public void testGetAvailableMACs() {
8986
String[] macs = Connection.getAvailableMACs();
9087

91-
assertNotNull("MAC list should not be null", macs);
92-
assertTrue("Should have at least one MAC available", macs.length > 0);
88+
assertNotNull(macs, "MAC list should not be null");
89+
assertTrue(macs.length > 0, "Should have at least one MAC available");
9390

9491
// Check that all returned values are non-null strings
9592
for (String mac : macs) {
96-
assertNotNull("MAC name should not be null", mac);
97-
assertTrue("MAC name should not be empty", mac.length() > 0);
93+
assertNotNull(mac, "MAC name should not be null");
94+
assertTrue(mac.length() > 0, "MAC name should not be empty");
9895
}
9996
}
10097

10198
@Test
10299
public void testGetAvailableServerHostKeyAlgorithms() {
103100
String[] algorithms = Connection.getAvailableServerHostKeyAlgorithms();
104101

105-
assertNotNull("Algorithm list should not be null", algorithms);
106-
assertTrue("Should have at least one algorithm available",
107-
algorithms.length > 0);
102+
assertNotNull(algorithms, "Algorithm list should not be null");
103+
assertTrue(algorithms.length > 0, "Should have at least one algorithm available");
108104

109105
// Check that all returned values are non-null strings
110106
for (String algorithm : algorithms) {
111-
assertNotNull("Algorithm name should not be null", algorithm);
112-
assertTrue("Algorithm name should not be empty", algorithm.length() > 0);
107+
assertNotNull(algorithm, "Algorithm name should not be null");
108+
assertTrue(algorithm.length() > 0, "Algorithm name should not be empty");
113109
}
114110
}
115111

@@ -119,17 +115,17 @@ public void testStaticMethodsReturnSameResults() {
119115
String[] ciphers1 = Connection.getAvailableCiphers();
120116
String[] ciphers2 = Connection.getAvailableCiphers();
121117

122-
assertArrayEquals("Cipher lists should be identical", ciphers1, ciphers2);
118+
assertArrayEquals(ciphers1, ciphers2, "Cipher lists should be identical");
123119

124120
String[] macs1 = Connection.getAvailableMACs();
125121
String[] macs2 = Connection.getAvailableMACs();
126122

127-
assertArrayEquals("MAC lists should be identical", macs1, macs2);
123+
assertArrayEquals(macs1, macs2, "MAC lists should be identical");
128124

129125
String[] algos1 = Connection.getAvailableServerHostKeyAlgorithms();
130126
String[] algos2 = Connection.getAvailableServerHostKeyAlgorithms();
131127

132-
assertArrayEquals("Algorithm lists should be identical", algos1, algos2);
128+
assertArrayEquals(algos1, algos2, "Algorithm lists should be identical");
133129
}
134130

135131
@Test
@@ -156,8 +152,7 @@ public void testGetConnectionInfoThrowsWhenNotConnected() {
156152
connection.getConnectionInfo();
157153
fail("Should throw IllegalStateException when not connected");
158154
} catch (IllegalStateException e) {
159-
assertTrue("Exception message should indicate connection required",
160-
e.getMessage().contains("establish a connection first"));
155+
assertTrue(e.getMessage().contains("establish a connection first"), "Exception message should indicate connection required");
161156
} catch (Exception e) {
162157
fail("Should throw IllegalStateException, got: " +
163158
e.getClass().getSimpleName());
@@ -170,8 +165,7 @@ public void testIsAuthMethodAvailableWithNullMethod() {
170165
connection.isAuthMethodAvailable("testuser", null);
171166
fail("Should throw IllegalArgumentException with null method");
172167
} catch (IllegalArgumentException e) {
173-
assertTrue("Exception message should mention method argument",
174-
e.getMessage().contains("method argument"));
168+
assertTrue(e.getMessage().contains("method argument"), "Exception message should mention method argument");
175169
} catch (Exception e) {
176170
fail("Should throw IllegalArgumentException, got: " +
177171
e.getClass().getSimpleName());
@@ -184,14 +178,11 @@ public void testSendIgnorePacketWithNullData() {
184178
connection.sendIgnorePacket(null);
185179
fail("Should throw IllegalArgumentException with null data");
186180
} catch (IllegalArgumentException e) {
187-
assertTrue("Exception message should mention data argument",
188-
e.getMessage().contains("data argument"));
181+
assertTrue(e.getMessage().contains("data argument"), "Exception message should mention data argument");
189182
} catch (Exception e) {
190183
// May also throw IllegalStateException if not connected
191-
assertTrue(
192-
"Should throw IllegalArgumentException or IllegalStateException",
193-
e instanceof IllegalArgumentException || e instanceof
194-
IllegalStateException);
184+
assertTrue(e instanceof IllegalArgumentException || e instanceof
185+
IllegalStateException, "Should throw IllegalArgumentException or IllegalStateException");
195186
}
196187
}
197188

@@ -228,19 +219,17 @@ public void testSetCompressionWithoutConnection() {
228219
public void testConnectionIdentification() {
229220
String identification = Connection.identification;
230221

231-
assertNotNull("Identification should not be null", identification);
232-
assertTrue("Identification should not be empty",
233-
identification.length() > 0);
234-
assertTrue("Identification should contain version info",
235-
identification.contains("TrileadSSH2Java"));
222+
assertNotNull(identification, "Identification should not be null");
223+
assertTrue(identification.length() > 0, "Identification should not be empty");
224+
assertTrue(identification.contains("TrileadSSH2Java"), "Identification should contain version info");
236225
}
237226

238227
@Test
239228
public void testHostnameNullHandling() {
240229
// Test with null hostname (may be allowed by implementation)
241230
try {
242231
Connection conn = new Connection(null, 22);
243-
assertNull("Null hostname should be preserved", conn.getHostname());
232+
assertNull(conn.getHostname(), "Null hostname should be preserved");
244233
} catch (Exception e) {
245234
// If constructor throws exception with null hostname, that's also
246235
// acceptable
@@ -251,15 +240,15 @@ public void testHostnameNullHandling() {
251240
public void testPortEdgeCases() {
252241
// Test with port 0
253242
Connection conn1 = new Connection("example.com", 0);
254-
assertEquals("Port 0 should be preserved", 0, conn1.getPort());
243+
assertEquals(0, conn1.getPort(), "Port 0 should be preserved");
255244

256245
// Test with high port number
257246
Connection conn2 = new Connection("example.com", 65535);
258-
assertEquals("High port should be preserved", 65535, conn2.getPort());
247+
assertEquals(65535, conn2.getPort(), "High port should be preserved");
259248

260249
// Test with negative port (behavior depends on implementation)
261250
Connection conn3 = new Connection("example.com", -1);
262-
assertEquals("Negative port should be preserved", -1, conn3.getPort());
251+
assertEquals(-1, conn3.getPort(), "Negative port should be preserved");
263252
}
264253

265254
@Test

0 commit comments

Comments
 (0)