Skip to content

Commit e44bb89

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

File tree

1 file changed

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

1 file changed

+36
-23
lines changed

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

Lines changed: 36 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,32 @@ 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);
201+
});
195202
}
196203

197-
@Test(expected = IllegalStateException.class)
204+
@Test
198205
public void serverModeWithoutClientCertsVerifier() throws Exception {
199-
serverMode(false, false);
206+
assertThrows(IllegalStateException.class, () -> {
207+
serverMode(false, false);
208+
});
200209
}
201210

202-
@Test(expected = IllegalStateException.class)
211+
@Test
203212
public void serverModeWithClientCertsVerifier() throws Exception {
204-
serverMode(true, false);
213+
assertThrows(IllegalStateException.class, () -> {
214+
serverMode(true, false);
215+
});
205216
}
206217

207218
private void runDelegatedTasks(SSLEngineResult result, SSLEngine engine)
@@ -357,16 +368,18 @@ public void validHostnameVerifier() throws Exception {
357368
sslFactory.destroy();
358369
}
359370

360-
@Test(expected = GeneralSecurityException.class)
371+
@Test
361372
public void invalidHostnameVerifier() throws Exception {
362373
Configuration conf = createConfiguration(false, true);
363374
conf.set(SSLFactory.SSL_HOSTNAME_VERIFIER_KEY, "foo");
364375
SSLFactory sslFactory = new SSLFactory(SSLFactory.Mode.CLIENT, conf);
365-
try {
366-
sslFactory.init();
367-
} finally {
368-
sslFactory.destroy();
369-
}
376+
assertThrows(GeneralSecurityException.class, () -> {
377+
try {
378+
sslFactory.init();
379+
} finally {
380+
sslFactory.destroy();
381+
}
382+
});
370383
}
371384

372385
@Test

0 commit comments

Comments
 (0)