Skip to content

Commit bd7dd28

Browse files
committed
Refactor @test(expected) with assertThrows
1 parent 43c8896 commit bd7dd28

File tree

1 file changed

+35
-23
lines changed
  • hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/ssl

1 file changed

+35
-23
lines changed

hadoop-common-project/hadoop-common/src/test/java/org/apache/hadoop/security/ssl/TestSSLFactory.java

Lines changed: 35 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
import static org.junit.Assert.assertEquals;
2828
import static org.junit.Assert.assertNotEquals;
2929
import static org.junit.Assert.assertNull;
30+
import static org.junit.Assert.assertThrows;
3031
import static org.junit.Assert.assertTrue;
3132

3233
import org.apache.hadoop.conf.Configuration;
@@ -152,18 +153,20 @@ public void testSslConfClassPathFirst() throws Exception {
152153
assertNotEquals(conf, sslConfLoaded);
153154
}
154155

155-
@Test(expected = IllegalStateException.class)
156+
@Test
156157
public void clientMode() throws Exception {
157158
Configuration conf = createConfiguration(false, true);
158159
SSLFactory sslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, conf);
159-
try {
160-
sslFactory.init();
161-
Assert.assertNotNull(sslFactory.createSSLSocketFactory());
162-
Assert.assertNotNull(sslFactory.getHostnameVerifier());
163-
sslFactory.createSSLServerSocketFactory();
164-
} finally {
165-
sslFactory.destroy();
166-
}
160+
assertThrows(IllegalStateException.class, () -> {
161+
try {
162+
sslFactory.init();
163+
Assert.assertNotNull(sslFactory.createSSLSocketFactory());
164+
Assert.assertNotNull(sslFactory.getHostnameVerifier());
165+
sslFactory.createSSLServerSocketFactory();
166+
} finally {
167+
sslFactory.destroy();
168+
}
169+
});
167170
}
168171

169172
private void serverMode(boolean clientCert, boolean socket) throws Exception {
@@ -184,24 +187,31 @@ private void serverMode(boolean clientCert, boolean socket) throws Exception {
184187
}
185188

186189

187-
@Test(expected = IllegalStateException.class)
190+
@Test
188191
public void serverModeWithoutClientCertsSocket() throws Exception {
189-
serverMode(false, true);
192+
assertThrows(IllegalStateException.class, () -> {
193+
serverMode(false, true);
194+
});
190195
}
191196

192-
@Test(expected = IllegalStateException.class)
197+
@Test
193198
public void serverModeWithClientCertsSocket() throws Exception {
194-
serverMode(true, true);
199+
assertThrows(IllegalStateException.class, () -> {
200+
serverMode(true, true);
195201
}
196202

197-
@Test(expected = IllegalStateException.class)
203+
@Test
198204
public void serverModeWithoutClientCertsVerifier() throws Exception {
199-
serverMode(false, false);
205+
assertThrows(IllegalStateException.class, () -> {
206+
serverMode(false, false);
207+
});
200208
}
201209

202-
@Test(expected = IllegalStateException.class)
210+
@Test
203211
public void serverModeWithClientCertsVerifier() throws Exception {
204-
serverMode(true, false);
212+
assertThrows(IllegalStateException.class, () -> {
213+
serverMode(true, false);
214+
});
205215
}
206216

207217
private void runDelegatedTasks(SSLEngineResult result, SSLEngine engine)
@@ -357,16 +367,18 @@ public void validHostnameVerifier() throws Exception {
357367
sslFactory.destroy();
358368
}
359369

360-
@Test(expected = GeneralSecurityException.class)
370+
@Test
361371
public void invalidHostnameVerifier() throws Exception {
362372
Configuration conf = createConfiguration(false, true);
363373
conf.set(SSLFactory.SSL_HOSTNAME_VERIFIER_KEY, "foo");
364374
SSLFactory sslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, conf);
365-
try {
366-
sslFactory.init();
367-
} finally {
368-
sslFactory.destroy();
369-
}
375+
assertThrows(GeneralSecurityException.class, () -> {
376+
try {
377+
sslFactory.init();
378+
} finally {
379+
sslFactory.destroy();
380+
}
381+
});
370382
}
371383

372384
@Test

0 commit comments

Comments
 (0)