diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestAbstractSerializingCacheStorage.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestAbstractSerializingCacheStorage.java index 67473014a..ca939619f 100644 --- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestAbstractSerializingCacheStorage.java +++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestAbstractSerializingCacheStorage.java @@ -217,13 +217,10 @@ public void testCacheUpdateFail() throws Exception { when(impl.digestToStorageKey(key)).thenReturn("bar"); when(impl.getForUpdateCAS("bar")).thenReturn("stuff"); when(impl.getStorageObject("stuff")).thenReturn(serialize(key, existingValue)); - when(impl.updateCAS(ArgumentMatchers.eq("bar"), ArgumentMatchers.eq("stuff"), ArgumentMatchers.any())).thenReturn(false, false, false, true); + when(impl.updateCAS(ArgumentMatchers.eq("bar"), ArgumentMatchers.eq("stuff"), ArgumentMatchers.any())) + .thenReturn(false, false, false, true); - try { - impl.updateEntry(key, existing -> updatedValue); - Assert.fail("HttpCacheUpdateException expected"); - } catch (final HttpCacheUpdateException ignore) { - } + Assert.assertThrows(HttpCacheUpdateException.class, () -> impl.updateEntry(key, existing -> updatedValue)); verify(impl, Mockito.times(3)).getForUpdateCAS("bar"); verify(impl, Mockito.times(3)).getStorageObject("stuff"); diff --git a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRequirements.java b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRequirements.java index 1cadf0389..18f53147a 100644 --- a/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRequirements.java +++ b/httpclient5-cache/src/test/java/org/apache/hc/client5/http/impl/cache/TestProtocolRequirements.java @@ -658,13 +658,9 @@ public void test100ContinueResponsesAreNotForwardedTo1_0ClientsWhoDidNotAskForTh originResponse = new BasicClassicHttpResponse(100, "Continue"); Mockito.when(mockExecChain.proceed(Mockito.any(), Mockito.any())).thenReturn(originResponse); - try { - // if a 100 response gets up to us from the HttpClient - // backend, we can't really handle it at that point - execute(post); - Assert.fail("should have thrown an exception"); - } catch (final ClientProtocolException expected) { - } + // if a 100 response gets up to us from the HttpClient + // backend, we can't really handle it at that point + Assert.assertThrows(ClientProtocolException.class, () -> execute(post)); } /* diff --git a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestConnectionManagement.java b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestConnectionManagement.java index 3dd759826..9b1f8fd11 100644 --- a/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestConnectionManagement.java +++ b/httpclient5-testing/src/test/java/org/apache/hc/client5/testing/sync/TestConnectionManagement.java @@ -94,19 +94,14 @@ public void testReleaseConnection() throws Exception { } // check that there is no auto-release by default - try { - // this should fail quickly, connection has not been released - final LeaseRequest leaseRequest2 = this.connManager.lease("id2", route,null); - leaseRequest2.get(Timeout.ofMilliseconds(10)); - Assert.fail("TimeoutException expected"); - } catch (final TimeoutException ex) { - // expected - } + // this should fail quickly, connection has not been released + final LeaseRequest leaseRequest2 = this.connManager.lease("id2", route,null); + Assert.assertThrows(TimeoutException.class, () -> leaseRequest2.get(Timeout.ofMilliseconds(10))); endpoint1.close(); this.connManager.release(endpoint1, null, TimeValue.NEG_ONE_MILLISECOND); - final LeaseRequest leaseRequest2 = this.connManager.lease("id2", route,null); - final ConnectionEndpoint endpoint2 = leaseRequest2.get(Timeout.ZERO_MILLISECONDS); + final LeaseRequest leaseRequest3 = this.connManager.lease("id2", route,null); + final ConnectionEndpoint endpoint2 = leaseRequest3.get(Timeout.ZERO_MILLISECONDS); Assert.assertFalse(endpoint2.isConnected()); this.connManager.connect(endpoint2, TimeValue.NEG_ONE_MILLISECOND, context); @@ -119,8 +114,8 @@ public void testReleaseConnection() throws Exception { // expect the next connection obtained to be open this.connManager.release(endpoint2, null, TimeValue.NEG_ONE_MILLISECOND); - final LeaseRequest leaseRequest3 = this.connManager.lease("id3", route,null); - final ConnectionEndpoint endpoint3 = leaseRequest3.get(Timeout.ZERO_MILLISECONDS); + final LeaseRequest leaseRequest4 = this.connManager.lease("id3", route,null); + final ConnectionEndpoint endpoint3 = leaseRequest4.get(Timeout.ZERO_MILLISECONDS); Assert.assertTrue(endpoint3.isConnected()); // repeat the communication, no need to prepare the request again @@ -162,20 +157,15 @@ public void testReleaseConnectionWithTimeLimits() throws Exception { } // check that there is no auto-release by default - try { - // this should fail quickly, connection has not been released - final LeaseRequest leaseRequest2 = this.connManager.lease("id2", route,null); - leaseRequest2.get(Timeout.ofMilliseconds(10)); - Assert.fail("TimeoutException expected"); - } catch (final TimeoutException ex) { - // expected - } + final LeaseRequest leaseRequest2 = this.connManager.lease("id2", route,null); + // this should fail quickly, connection has not been released + Assert.assertThrows(TimeoutException.class, () -> leaseRequest2.get(Timeout.ofMilliseconds(10))); endpoint1.close(); this.connManager.release(endpoint1, null, TimeValue.ofMilliseconds(100)); - final LeaseRequest leaseRequest2 = this.connManager.lease("id2", route,null); - final ConnectionEndpoint endpoint2 = leaseRequest2.get(Timeout.ZERO_MILLISECONDS); + final LeaseRequest leaseRequest3 = this.connManager.lease("id2", route,null); + final ConnectionEndpoint endpoint2 = leaseRequest3.get(Timeout.ZERO_MILLISECONDS); Assert.assertFalse(endpoint2.isConnected()); this.connManager.connect(endpoint2, TimeValue.NEG_ONE_MILLISECOND, context); @@ -186,8 +176,8 @@ public void testReleaseConnectionWithTimeLimits() throws Exception { this.connManager.release(endpoint2, null, TimeValue.ofMilliseconds(100)); - final LeaseRequest leaseRequest3 = this.connManager.lease("id3", route,null); - final ConnectionEndpoint endpoint3 = leaseRequest3.get(Timeout.ZERO_MILLISECONDS); + final LeaseRequest leaseRequest4 = this.connManager.lease("id3", route,null); + final ConnectionEndpoint endpoint3 = leaseRequest4.get(Timeout.ZERO_MILLISECONDS); Assert.assertTrue(endpoint3.isConnected()); // repeat the communication, no need to prepare the request again @@ -198,8 +188,8 @@ public void testReleaseConnectionWithTimeLimits() throws Exception { this.connManager.release(endpoint3, null, TimeValue.ofMilliseconds(100)); Thread.sleep(150); - final LeaseRequest leaseRequest4 = this.connManager.lease("id4", route,null); - final ConnectionEndpoint endpoint4 = leaseRequest4.get(Timeout.ZERO_MILLISECONDS); + final LeaseRequest leaseRequest5 = this.connManager.lease("id4", route,null); + final ConnectionEndpoint endpoint4 = leaseRequest5.get(Timeout.ZERO_MILLISECONDS); Assert.assertFalse(endpoint4.isConnected()); // repeat the communication, no need to prepare the request again diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestAuthenticationStrategy.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestAuthenticationStrategy.java index 8df2029d9..3fff24469 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestAuthenticationStrategy.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestAuthenticationStrategy.java @@ -62,21 +62,12 @@ public class TestAuthenticationStrategy { public void testSelectInvalidInput() throws Exception { final DefaultAuthenticationStrategy authStrategy = new DefaultAuthenticationStrategy(); final HttpClientContext context = HttpClientContext.create(); - try { - authStrategy.select(null, Collections.emptyMap(), context); - Assert.fail("NullPointerException expected"); - } catch (final NullPointerException ex) { - } - try { - authStrategy.select(ChallengeType.TARGET, null, context); - Assert.fail("NullPointerException expected"); - } catch (final NullPointerException ex) { - } - try { - authStrategy.select(ChallengeType.TARGET, Collections.emptyMap(), null); - Assert.fail("NullPointerException expected"); - } catch (final NullPointerException ex) { - } + Assert.assertThrows(NullPointerException.class, () -> + authStrategy.select(null, Collections.emptyMap(), context)); + Assert.assertThrows(NullPointerException.class, () -> + authStrategy.select(ChallengeType.TARGET, null, context)); + Assert.assertThrows(NullPointerException.class, () -> + authStrategy.select(ChallengeType.TARGET, Collections.emptyMap(), null)); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestDefaultRedirectStrategy.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestDefaultRedirectStrategy.java index d07b295d1..fce0dd7ba 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestDefaultRedirectStrategy.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/TestDefaultRedirectStrategy.java @@ -112,16 +112,10 @@ public void testIsRedirectedInvalidInput() throws Exception { final HttpClientContext context = HttpClientContext.create(); final HttpGet httpget = new HttpGet("http://localhost/"); final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_SEE_OTHER, "Redirect"); - try { - redirectStrategy.isRedirected(null, response, context); - Assert.fail("NullPointerException expected"); - } catch (final NullPointerException expected) { - } - try { - redirectStrategy.isRedirected(httpget, null, context); - Assert.fail("NullPointerException expected"); - } catch (final NullPointerException expected) { - } + Assert.assertThrows(NullPointerException.class, () -> + redirectStrategy.isRedirected(null, response, context)); + Assert.assertThrows(NullPointerException.class, () -> + redirectStrategy.isRedirected(httpget, null, context)); } @Test @@ -207,21 +201,12 @@ public void testGetLocationUriInvalidInput() throws Exception { final HttpGet httpget = new HttpGet("http://localhost/"); final HttpResponse response = new BasicHttpResponse(HttpStatus.SC_MOVED_TEMPORARILY, "Redirect"); response.addHeader("Location", "http://localhost/stuff"); - try { - redirectStrategy.getLocationURI(null, response, context); - Assert.fail("NullPointerException expected"); - } catch (final NullPointerException expected) { - } - try { - redirectStrategy.getLocationURI(httpget, null, context); - Assert.fail("NullPointerException expected"); - } catch (final NullPointerException expected) { - } - try { - redirectStrategy.getLocationURI(httpget, response, null); - Assert.fail("NullPointerException expected"); - } catch (final NullPointerException expected) { - } + Assert.assertThrows(NullPointerException.class, () -> + redirectStrategy.getLocationURI(null, response, context)); + Assert.assertThrows(NullPointerException.class, () -> + redirectStrategy.getLocationURI(httpget, null, context)); + Assert.assertThrows(NullPointerException.class, () -> + redirectStrategy.getLocationURI(httpget, response, null)); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestDigestScheme.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestDigestScheme.java index e57a24197..7a5e43034 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestDigestScheme.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/auth/TestDigestScheme.java @@ -157,21 +157,12 @@ public void testDigestAuthenticationInvalidInput() throws Exception { final DigestScheme authscheme = new DigestScheme(); authscheme.processChallenge(authChallenge, null); - try { - authscheme.isResponseReady(null, credentialsProvider, null); - Assert.fail("NullPointerException should have been thrown"); - } catch (final NullPointerException ex) { - } - try { - authscheme.isResponseReady(host, null, null); - Assert.fail("NullPointerException should have been thrown"); - } catch (final NullPointerException ex) { - } - try { - authscheme.generateAuthResponse(host, null, null); - Assert.fail("NullPointerException should have been thrown"); - } catch (final NullPointerException ex) { - } + Assert.assertThrows(NullPointerException.class, () -> + authscheme.isResponseReady(null, credentialsProvider, null)); + Assert.assertThrows(NullPointerException.class, () -> + authscheme.isResponseReady(host, null, null)); + Assert.assertThrows(NullPointerException.class, () -> + authscheme.generateAuthResponse(host, null, null)); } @Test @@ -579,16 +570,8 @@ public void testHttpEntityDigest() throws Exception { Assert.assertNull(digester.getDigest()); digester.close(); Assert.assertEquals("acd2b59cd01c7737d8069015584c6cac", DigestScheme.formatHex(digester.getDigest())); - try { - digester.write('a'); - Assert.fail("IOException should have been thrown"); - } catch (final IOException ex) { - } - try { - digester.write(new byte[] { 'a', 'b', 'c'}); - Assert.fail("IOException should have been thrown"); - } catch (final IOException ex) { - } + Assert.assertThrows(IOException.class, () -> digester.write('a')); + Assert.assertThrows(IOException.class, () -> digester.write(new byte[] { 'a', 'b', 'c'})); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java index 769d21832..23f758038 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestAbstractHttpClientResponseHandler.java @@ -75,14 +75,11 @@ public void testUnsuccessfulResponse() throws Exception { Mockito.when(response.getEntity()).thenReturn(entity); final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler(); - try { - handler.handleResponse(response); - Assert.fail("HttpResponseException expected"); - } catch (final HttpResponseException ex) { - Assert.assertEquals(404, ex.getStatusCode()); - Assert.assertEquals("NOT FOUND", ex.getReasonPhrase()); - Assert.assertEquals("status code: 404, reason phrase: NOT FOUND", ex.getMessage()); - } + final HttpResponseException exception = Assert.assertThrows(HttpResponseException.class, () -> + handler.handleResponse(response)); + Assert.assertEquals(404, exception.getStatusCode()); + Assert.assertEquals("NOT FOUND", exception.getReasonPhrase()); + Assert.assertEquals("status code: 404, reason phrase: NOT FOUND", exception.getMessage()); Mockito.verify(entity).getContent(); Mockito.verify(inStream).close(); } @@ -99,14 +96,11 @@ public void testUnsuccessfulResponseEmptyReason() throws Exception { Mockito.when(response.getEntity()).thenReturn(entity); final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler(); - try { - handler.handleResponse(response); - Assert.fail("HttpResponseException expected"); - } catch (final HttpResponseException ex) { - Assert.assertEquals(404, ex.getStatusCode()); - Assert.assertNull(ex.getReasonPhrase()); - Assert.assertEquals("status code: 404", ex.getMessage()); - } + final HttpResponseException exception = Assert.assertThrows(HttpResponseException.class, () -> + handler.handleResponse(response)); + Assert.assertEquals(404, exception.getStatusCode()); + Assert.assertNull(exception.getReasonPhrase()); + Assert.assertEquals("status code: 404", exception.getMessage()); Mockito.verify(entity).getContent(); Mockito.verify(inStream).close(); } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestBasicResponseHandler.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestBasicResponseHandler.java index 958b903de..7edf4f985 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestBasicResponseHandler.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestBasicResponseHandler.java @@ -66,12 +66,9 @@ public void testUnsuccessfulResponse() throws Exception { Mockito.when(response.getEntity()).thenReturn(entity); final BasicHttpClientResponseHandler handler = new BasicHttpClientResponseHandler(); - try { - handler.handleResponse(response); - Assert.fail("HttpResponseException expected"); - } catch (final HttpResponseException ex) { - Assert.assertEquals(404, ex.getStatusCode()); - } + final HttpResponseException exception = Assert.assertThrows(HttpResponseException.class, () -> + handler.handleResponse(response)); + Assert.assertEquals(404, exception.getStatusCode()); Mockito.verify(entity).getContent(); Mockito.verify(inStream).close(); } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestResponseEntityWrapper.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestResponseEntityWrapper.java index 3f74f0a80..52f3c7e5a 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestResponseEntityWrapper.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/classic/TestResponseEntityWrapper.java @@ -71,11 +71,7 @@ public void testReusableEntityStreamClosedIOError() throws Exception { Mockito.when(entity.isStreaming()).thenReturn(true); Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true); Mockito.doThrow(new IOException()).when(inStream).close(); - try { - EntityUtils.consume(wrapper); - Assert.fail("IOException expected"); - } catch (final IOException ex) { - } + Assert.assertThrows(IOException.class, () -> EntityUtils.consume(wrapper)); Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint(); } @@ -104,11 +100,7 @@ public void testReusableEntityWriteToIOError() throws Exception { Mockito.when(entity.isStreaming()).thenReturn(true); Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true); Mockito.doThrow(new IOException()).when(entity).writeTo(outStream); - try { - wrapper.writeTo(outStream); - Assert.fail("IOException expected"); - } catch (final IOException ex) { - } + Assert.assertThrows(IOException.class, () -> wrapper.writeTo(outStream)); Mockito.verify(execRuntime, Mockito.never()).releaseEndpoint(); Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint(); } @@ -131,11 +123,7 @@ public void testReusableEntityEndOfStreamIOError() throws Exception { Mockito.when(execRuntime.isConnectionReusable()).thenReturn(true); Mockito.doThrow(new IOException()).when(inStream).close(); final InputStream content = wrapper.getContent(); - try { - content.read(); - Assert.fail("IOException expected"); - } catch (final IOException ex) { - } + Assert.assertThrows(IOException.class, () -> content.read()); Mockito.verify(execRuntime, Mockito.atLeast(1)).discardEndpoint(); } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestBasicClientCookie.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestBasicClientCookie.java index 4ad3e9cf2..6f01febff 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestBasicClientCookie.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestBasicClientCookie.java @@ -46,12 +46,7 @@ public void testConstructor() { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); Assert.assertEquals("name", cookie.getName()); Assert.assertEquals("value", cookie.getValue()); - try { - new BasicClientCookie(null, null); - Assert.fail("NullPointerException should have been thrown"); - } catch (final NullPointerException ex) { - //expected - } + Assert.assertThrows(NullPointerException.class, () -> new BasicClientCookie(null, null)); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestBasicCookieAttribHandlers.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestBasicCookieAttribHandlers.java index 80b3bc48e..1d9d6738a 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestBasicCookieAttribHandlers.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestBasicCookieAttribHandlers.java @@ -79,19 +79,9 @@ public void testBasicDomainValidate1() throws Exception { h.validate(cookie, origin); cookie.setDomain(".otherdomain.com"); - try { - h.validate(cookie, origin); - Assert.fail("MalformedCookieException should have been thrown"); - } catch (final MalformedCookieException ex) { - // expected - } + Assert.assertThrows(MalformedCookieException.class, () -> h.validate(cookie, origin)); cookie.setDomain("www.otherdomain.com"); - try { - h.validate(cookie, origin); - Assert.fail("MalformedCookieException should have been thrown"); - } catch (final MalformedCookieException ex) { - // expected - } + Assert.assertThrows(MalformedCookieException.class, () -> h.validate(cookie, origin)); } @Test @@ -104,12 +94,7 @@ public void testBasicDomainValidate2() throws Exception { h.validate(cookie, origin); cookie.setDomain("otherhost"); - try { - h.validate(cookie, origin); - Assert.fail("MalformedCookieException should have been thrown"); - } catch (final MalformedCookieException ex) { - // expected - } + Assert.assertThrows(MalformedCookieException.class, () -> h.validate(cookie, origin)); } @Test @@ -129,12 +114,7 @@ public void testBasicDomainValidate4() throws Exception { final CookieAttributeHandler h = new BasicDomainHandler(); cookie.setDomain(null); - try { - h.validate(cookie, origin); - Assert.fail("MalformedCookieException should have been thrown"); - } catch (final MalformedCookieException ex) { - // expected - } + Assert.assertThrows(MalformedCookieException.class, () -> h.validate(cookie, origin)); } @Test @@ -193,36 +173,13 @@ public void testBasicDomainMatchMixedCase() throws Exception { @Test public void testBasicDomainInvalidInput() throws Exception { final CookieAttributeHandler h = new BasicDomainHandler(); - try { - h.parse(null, null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } - try { - h.validate(null, null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } - try { - h.validate(new BasicClientCookie("name", "value"), null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } - try { - h.match(null, null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } - try { - h.match(new BasicClientCookie("name", "value"), null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> h.parse(null, null)); + Assert.assertThrows(NullPointerException.class, () -> h.validate(null, null)); + Assert.assertThrows(NullPointerException.class, () -> + h.validate(new BasicClientCookie("name", "value"), null)); + Assert.assertThrows(NullPointerException.class, () -> h.match(null, null)); + Assert.assertThrows(NullPointerException.class, () -> + h.match(new BasicClientCookie("name", "value"), null)); } @Test @@ -302,24 +259,10 @@ public void testBasicPathMatch7() throws Exception { @Test public void testBasicPathInvalidInput() throws Exception { final CookieAttributeHandler h = new BasicPathHandler(); - try { - h.parse(null, null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } - try { - h.match(null, null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } - try { - h.match(new BasicClientCookie("name", "value"), null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> h.parse(null, null)); + Assert.assertThrows(NullPointerException.class, () -> h.match(null, null)); + Assert.assertThrows(NullPointerException.class, () -> + h.match(new BasicClientCookie("name", "value"), null)); } @Test @@ -334,29 +277,14 @@ public void testBasicMaxAgeParse() throws Exception { public void testBasicMaxAgeParseInvalid() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieAttributeHandler h = new BasicMaxAgeHandler(); - try { - h.parse(cookie, "garbage"); - Assert.fail("MalformedCookieException must have been thrown"); - } catch (final MalformedCookieException ex) { - // expected - } - try { - h.parse(cookie, null); - Assert.fail("MalformedCookieException must have been thrown"); - } catch (final MalformedCookieException ex) { - // expected - } + Assert.assertThrows(MalformedCookieException.class, () -> h.parse(cookie, "garbage")); + Assert.assertThrows(MalformedCookieException.class, () -> h.parse(cookie, null)); } @Test public void testBasicMaxAgeInvalidInput() throws Exception { final CookieAttributeHandler h = new BasicMaxAgeHandler(); - try { - h.parse(null, null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> h.parse(null, null)); } @Test @@ -390,24 +318,10 @@ public void testBasicSecureMatch() throws Exception { @Test public void testBasicSecureInvalidInput() throws Exception { final CookieAttributeHandler h = new BasicSecureHandler(); - try { - h.parse(null, null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } - try { - h.match(null, null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } - try { - h.match(new BasicClientCookie("name", "value"), null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> h.parse(null, null)); + Assert.assertThrows(NullPointerException.class, () -> h.match(null, null)); + Assert.assertThrows(NullPointerException.class, () -> + h.match(new BasicClientCookie("name", "value"), null)); } @Test @@ -428,36 +342,18 @@ public void testBasicExpiresParse() throws Exception { public void testBasicExpiresParseInvalid() throws Exception { final BasicClientCookie cookie = new BasicClientCookie("name", "value"); final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123}); - try { - h.parse(cookie, "garbage"); - Assert.fail("MalformedCookieException must have been thrown"); - } catch (final MalformedCookieException ex) { - // expected - } - try { - h.parse(cookie, null); - Assert.fail("MalformedCookieException must have been thrown"); - } catch (final MalformedCookieException ex) { - // expected - } + Assert.assertThrows(MalformedCookieException.class, () -> + h.parse(cookie, "garbage")); + Assert.assertThrows(MalformedCookieException.class, () -> + h.parse(cookie, null)); } @SuppressWarnings("unused") @Test public void testBasicExpiresInvalidInput() throws Exception { - try { - new BasicExpiresHandler(null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> new BasicExpiresHandler(null)); final CookieAttributeHandler h = new BasicExpiresHandler(new String[] {DateUtils.PATTERN_RFC1123}); - try { - h.parse(null, null); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> h.parse(null, null)); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestLaxCookieAttribHandlers.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestLaxCookieAttribHandlers.java index 0a9d8e03d..71378fce8 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestLaxCookieAttribHandlers.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/cookie/TestLaxCookieAttribHandlers.java @@ -80,12 +80,7 @@ public void testBasicMaxAgeParseInvalid() throws Exception { @Test public void testBasicMaxAgeInvalidInput() throws Exception { final CookieAttributeHandler h = new LaxMaxAgeHandler(); - try { - h.parse(null, "stuff"); - Assert.fail("NullPointerException must have been thrown"); - } catch (final NullPointerException ex) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> h.parse(null, "stuff")); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestBasicHttpClientConnectionManager.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestBasicHttpClientConnectionManager.java index 5c07769cf..76f199d77 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestBasicHttpClientConnectionManager.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/io/TestBasicHttpClientConnectionManager.java @@ -261,12 +261,8 @@ public void testShutdown() throws Exception { Mockito.verify(conn, Mockito.times(1)).close(CloseMode.GRACEFUL); - try { - final LeaseRequest connRequest2 = mgr.lease("some-id", route, null); - connRequest2.get(Timeout.ZERO_MILLISECONDS); - Assert.fail("IllegalStateException expected"); - } catch (final IllegalStateException ex) { - } + final LeaseRequest connRequest2 = mgr.lease("some-id", route, null); + Assert.assertThrows(IllegalStateException.class, () -> connRequest2.get(Timeout.ZERO_MILLISECONDS)); // Should have no effect mgr.closeExpired(); diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/routing/TestRouteTracker.java b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/routing/TestRouteTracker.java index d33ff5dca..6ba60a539 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/impl/routing/TestRouteTracker.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/impl/routing/TestRouteTracker.java @@ -111,14 +111,7 @@ public void testCstrTargetLocal() { Assert.assertNull("wrong route (target,local)", rt.toRoute()); checkCTLS(rt, false, false, false, false); - - rt = null; - try { - new RouteTracker(null, LOCAL41); - Assert.fail("null target not detected"); - } catch (final NullPointerException iax) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> new RouteTracker(null, LOCAL41)); } @SuppressWarnings("unused") @@ -161,14 +154,7 @@ public void testCstrRoute() { Assert.assertNull("wrong route (r3)", rt.toRoute()); checkCTLS(rt, false, false, false, false); - - rt = null; - try { - new RouteTracker(null); - Assert.fail("null route not detected"); - } catch (final NullPointerException npx) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> new RouteTracker(null)); } @Test @@ -176,49 +162,15 @@ public void testIllegalArgs() { final RouteTracker rt = new RouteTracker(TARGET2, null); - try { - rt.connectProxy(null, true); - Assert.fail("missing proxy argument not detected (connect/false)"); - } catch (final NullPointerException iax) { - // expected - } - - try { - rt.connectProxy(null, false); - Assert.fail("missing proxy argument not detected (connect/true)"); - } catch (final NullPointerException iax) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> rt.connectProxy(null, true)); + Assert.assertThrows(NullPointerException.class, () -> rt.connectProxy(null, false)); rt.connectProxy(PROXY1, false); - try { - rt.tunnelProxy(null, false); - Assert.fail("missing proxy argument not detected (tunnel/false)"); - } catch (final NullPointerException iax) { - // expected - } - - try { - rt.tunnelProxy(null, true); - Assert.fail("missing proxy argument not detected (tunnel/true)"); - } catch (final NullPointerException iax) { - // expected - } - - try { - rt.getHopTarget(-1); - Assert.fail("negative hop index not detected"); - } catch (final IllegalArgumentException iax) { - // expected - } - - try { - rt.getHopTarget(2); - Assert.fail("excessive hop index not detected"); - } catch (final IllegalArgumentException iax) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> rt.tunnelProxy(null, false)); + Assert.assertThrows(NullPointerException.class, () -> rt.tunnelProxy(null, true)); + Assert.assertThrows(IllegalArgumentException.class, () -> rt.getHopTarget(-1)); + Assert.assertThrows(IllegalArgumentException.class, () -> rt.getHopTarget(2)); } @Test @@ -226,58 +178,17 @@ public void testIllegalStates() { final RouteTracker rt = new RouteTracker(TARGET1, null); - try { - rt.tunnelTarget(false); - Assert.fail("unconnectedness not detected (tunnelTarget)"); - } catch (final IllegalStateException isx) { - // expected - } - - try { - rt.tunnelProxy(PROXY1, false); - Assert.fail("unconnectedness not detected (tunnelProxy)"); - } catch (final IllegalStateException isx) { - // expected - } - - try { - rt.layerProtocol(true); - Assert.fail("unconnectedness not detected (layerProtocol)"); - } catch (final IllegalStateException isx) { - // expected - } - + Assert.assertThrows(IllegalStateException.class, () -> rt.tunnelTarget(false)); + Assert.assertThrows(IllegalStateException.class, () -> rt.tunnelProxy(PROXY1, false)); + Assert.assertThrows(IllegalStateException.class, () -> rt.layerProtocol(true)); // connect directly rt.connectTarget(false); - try { - rt.connectTarget(false); - Assert.fail("connectedness not detected (connectTarget)"); - } catch (final IllegalStateException isx) { - // expected - } - - try { - rt.connectProxy(PROXY2, false); - Assert.fail("connectedness not detected (connectProxy)"); - } catch (final IllegalStateException isx) { - // expected - } - - try { - rt.tunnelTarget(false); - Assert.fail("unproxiedness not detected (tunnelTarget)"); - } catch (final IllegalStateException isx) { - // expected - } - - try { - rt.tunnelProxy(PROXY1, false); - Assert.fail("unproxiedness not detected (tunnelProxy)"); - } catch (final IllegalStateException isx) { - // expected - } + Assert.assertThrows(IllegalStateException.class, () -> rt.connectTarget(false)); + Assert.assertThrows(IllegalStateException.class, () -> rt.connectProxy(PROXY2, false)); + Assert.assertThrows(IllegalStateException.class, () -> rt.tunnelTarget(false)); + Assert.assertThrows(IllegalStateException.class, () -> rt.tunnelProxy(PROXY1, false)); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestExpectContinue.java b/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestExpectContinue.java index 500d1eeda..107547c90 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestExpectContinue.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/protocol/TestRequestExpectContinue.java @@ -109,12 +109,7 @@ public void testRequestExpectContinueZeroContent() throws Exception { @Test public void testRequestExpectContinueInvalidInput() throws Exception { final RequestExpectContinue interceptor = new RequestExpectContinue(); - try { - interceptor.process(null, null, null); - Assert.fail("NullPointerException should have been thrown"); - } catch (final NullPointerException ex) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> interceptor.process(null, null, null)); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/routing/TestHttpRoute.java b/httpclient5/src/test/java/org/apache/hc/client5/http/routing/TestHttpRoute.java index cf6667f81..a0d4d45d7 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/routing/TestHttpRoute.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/routing/TestHttpRoute.java @@ -234,21 +234,10 @@ public void testInvalidArguments() { TunnelType.TUNNELLED, LayerType.PLAIN); Assert.assertNotNull(route); - try { - new HttpRoute(null, null, chain1, false, - TunnelType.TUNNELLED, LayerType.PLAIN); - Assert.fail("missing target not detected"); - } catch (final NullPointerException iax) { - // expected - } - - try { - new HttpRoute(TARGET1, null, (HttpHost[]) null, false, - TunnelType.TUNNELLED, LayerType.PLAIN); - Assert.fail("missing proxy for tunnel not detected"); - } catch (final IllegalArgumentException iax) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> + new HttpRoute(null, null, chain1, false,TunnelType.TUNNELLED, LayerType.PLAIN)); + Assert.assertThrows(IllegalArgumentException.class, () -> + new HttpRoute(TARGET1, null, (HttpHost[]) null, false, TunnelType.TUNNELLED, LayerType.PLAIN)); } @Test @@ -413,65 +402,33 @@ public void testEqualsHashcodeClone() throws CloneNotSupportedException { @Test public void testHopping() { // test getHopCount() and getHopTarget() with different proxy chains - HttpHost[] proxies = null; - HttpRoute route = new HttpRoute(TARGET1, null, proxies, true, + final HttpHost[] proxies = null; + final HttpRoute route = new HttpRoute(TARGET1, null, proxies, true, TunnelType.PLAIN, LayerType.PLAIN); Assert.assertEquals("A: hop count", 1, route.getHopCount()); Assert.assertEquals("A: hop 0", TARGET1, route.getHopTarget(0)); - try { - final HttpHost beyond = route.getHopTarget(1); - Assert.fail("A: hop 1 is " + beyond); - } catch (final IllegalArgumentException iax) { - // expected - } - try { - final HttpHost before = route.getHopTarget(-1); - Assert.fail("A: hop -1 is " + before); - } catch (final IllegalArgumentException iax) { - // expected - } + Assert.assertThrows(IllegalArgumentException.class, () -> route.getHopTarget(1)); + Assert.assertThrows(IllegalArgumentException.class, () -> route.getHopTarget(-1)); - - proxies = new HttpHost[]{ PROXY3 }; - route = new HttpRoute(TARGET1, LOCAL62, proxies, false, + final HttpHost[] proxies2 = new HttpHost[]{ PROXY3 }; + final HttpRoute route2 = new HttpRoute(TARGET1, LOCAL62, proxies2, false, TunnelType.TUNNELLED, LayerType.PLAIN); - Assert.assertEquals("B: hop count", 2, route.getHopCount()); - Assert.assertEquals("B: hop 0", PROXY3, route.getHopTarget(0)); - Assert.assertEquals("B: hop 1", TARGET1, route.getHopTarget(1)); - try { - final HttpHost beyond = route.getHopTarget(2); - Assert.fail("B: hop 2 is " + beyond); - } catch (final IllegalArgumentException iax) { - // expected - } - try { - final HttpHost before = route.getHopTarget(-2); - Assert.fail("B: hop -2 is " + before); - } catch (final IllegalArgumentException iax) { - // expected - } + Assert.assertEquals("B: hop count", 2, route2.getHopCount()); + Assert.assertEquals("B: hop 0", PROXY3, route2.getHopTarget(0)); + Assert.assertEquals("B: hop 1", TARGET1, route2.getHopTarget(1)); + Assert.assertThrows(IllegalArgumentException.class, () -> route2.getHopTarget(2)); + Assert.assertThrows(IllegalArgumentException.class, () -> route2.getHopTarget(-2)); - - proxies = new HttpHost[]{ PROXY3, PROXY1, PROXY2 }; - route = new HttpRoute(TARGET1, LOCAL42, proxies, false, + final HttpHost[] proxies3 = new HttpHost[]{ PROXY3, PROXY1, PROXY2 }; + final HttpRoute route3 = new HttpRoute(TARGET1, LOCAL42, proxies3, false, TunnelType.PLAIN, LayerType.LAYERED); - Assert.assertEquals("C: hop count", 4, route.getHopCount()); - Assert.assertEquals("C: hop 0", PROXY3 , route.getHopTarget(0)); - Assert.assertEquals("C: hop 1", PROXY1 , route.getHopTarget(1)); - Assert.assertEquals("C: hop 2", PROXY2 , route.getHopTarget(2)); - Assert.assertEquals("C: hop 3", TARGET1, route.getHopTarget(3)); - try { - final HttpHost beyond = route.getHopTarget(4); - Assert.fail("C: hop 4 is " + beyond); - } catch (final IllegalArgumentException iax) { - // expected - } - try { - final HttpHost before = route.getHopTarget(Integer.MIN_VALUE); - Assert.fail("C: hop - is " + before); - } catch (final IllegalArgumentException iax) { - // expected - } + Assert.assertEquals("C: hop count", 4, route3.getHopCount()); + Assert.assertEquals("C: hop 0", PROXY3 , route3.getHopTarget(0)); + Assert.assertEquals("C: hop 1", PROXY1 , route3.getHopTarget(1)); + Assert.assertEquals("C: hop 2", PROXY2 , route3.getHopTarget(2)); + Assert.assertEquals("C: hop 3", TARGET1, route3.getHopTarget(3)); + Assert.assertThrows(IllegalArgumentException.class, () -> route3.getHopTarget(4)); + Assert.assertThrows(IllegalArgumentException.class, () -> route3.getHopTarget(Integer.MIN_VALUE)); } @Test @@ -515,12 +472,8 @@ public void testCstr4() { Assert.assertEquals("bad convenience route 4/secure", route, should); // this constructor REQUIRES a proxy to be specified - try { - new HttpRoute(TARGET1, LOCAL61, null, false); - Assert.fail("missing proxy not detected"); - } catch (final NullPointerException iax) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> + new HttpRoute(TARGET1, LOCAL61, null, false)); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestDefaultHostnameVerifier.java b/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestDefaultHostnameVerifier.java index 88487ffae..9609f5f85 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestDefaultHostnameVerifier.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/ssl/TestDefaultHostnameVerifier.java @@ -27,15 +27,6 @@ package org.apache.hc.client5.http.ssl; -import org.apache.hc.client5.http.psl.DomainType; -import org.apache.hc.client5.http.psl.PublicSuffixList; -import org.apache.hc.client5.http.psl.PublicSuffixListParser; -import org.apache.hc.client5.http.psl.PublicSuffixMatcher; -import org.junit.Assert; -import org.junit.Before; -import org.junit.Test; - -import javax.net.ssl.SSLException; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; @@ -46,6 +37,16 @@ import java.util.Collections; import java.util.List; +import javax.net.ssl.SSLException; + +import org.apache.hc.client5.http.psl.DomainType; +import org.apache.hc.client5.http.psl.PublicSuffixList; +import org.apache.hc.client5.http.psl.PublicSuffixListParser; +import org.apache.hc.client5.http.psl.PublicSuffixMatcher; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Test; + /** * Unit tests for {@link org.apache.hc.client5.http.ssl.DefaultHostnameVerifier}. */ @@ -198,35 +199,14 @@ public void testSubjectAlt() throws Exception { impl.verify("localhost.localdomain", x509); impl.verify("127.0.0.1", x509); - try { - impl.verify("localhost", x509); - Assert.fail("SSLException should have been thrown"); - } catch (final SSLException ex) { - // expected - } - try { - impl.verify("local.host", x509); - Assert.fail("SSLException should have been thrown"); - } catch (final SSLException ex) { - // expected - } - try { - impl.verify("127.0.0.2", x509); - Assert.fail("SSLException should have been thrown"); - } catch (final SSLException ex) { - // expected - } + Assert.assertThrows(SSLException.class, () -> impl.verify("localhost", x509)); + Assert.assertThrows(SSLException.class, () -> impl.verify("local.host", x509)); + Assert.assertThrows(SSLException.class, () -> impl.verify("127.0.0.2", x509)); } public void exceptionPlease(final DefaultHostnameVerifier hv, final String host, final X509Certificate x509) { - try { - hv.verify(host, x509); - Assert.fail("HostnameVerifier shouldn't allow [" + host + "]"); - } - catch(final SSLException e) { - // whew! we're okay! - } + Assert.assertThrows(SSLException.class, () -> hv.verify(host, x509)); } @Test @@ -378,19 +358,13 @@ public void testHTTPCLIENT_1316() throws Exception{ final String host1 = "2001:0db8:aaaa:bbbb:cccc:0:0:0001"; DefaultHostnameVerifier.matchIPv6Address(host1, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc:0:0:0001"))); DefaultHostnameVerifier.matchIPv6Address(host1, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::1"))); - try { - DefaultHostnameVerifier.matchIPv6Address(host1, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10"))); - Assert.fail("SSLException expected"); - } catch (final SSLException expected) { - } + Assert.assertThrows(SSLException.class, () -> + DefaultHostnameVerifier.matchIPv6Address(host1, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10")))); final String host2 = "2001:0db8:aaaa:bbbb:cccc::1"; DefaultHostnameVerifier.matchIPv6Address(host2, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc:0:0:0001"))); DefaultHostnameVerifier.matchIPv6Address(host2, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::1"))); - try { - DefaultHostnameVerifier.matchIPv6Address(host2, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10"))); - Assert.fail("SSLException expected"); - } catch (final SSLException expected) { - } + Assert.assertThrows(SSLException.class, () -> + DefaultHostnameVerifier.matchIPv6Address(host2, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10")))); } @Test @@ -418,16 +392,10 @@ public void testExtractCN() throws Exception { Assert.assertEquals("blah, blah", DefaultHostnameVerifier.extractCN("cn=\"blah, blah\", ou=blah, o=blah")); Assert.assertEquals("blah, blah", DefaultHostnameVerifier.extractCN("cn=blah\\, blah, ou=blah, o=blah")); Assert.assertEquals("blah", DefaultHostnameVerifier.extractCN("c = cn=uuh, cn=blah, ou=blah, o=blah")); - try { - DefaultHostnameVerifier.extractCN("blah,blah"); - Assert.fail("SSLException expected"); - } catch (final SSLException expected) { - } - try { - DefaultHostnameVerifier.extractCN("cn,o=blah"); - Assert.fail("SSLException expected"); - } catch (final SSLException expected) { - } + Assert.assertThrows(SSLException.class, () -> + DefaultHostnameVerifier.extractCN("blah,blah")); + Assert.assertThrows(SSLException.class, () -> + DefaultHostnameVerifier.extractCN("cn,o=blah")); } @Test @@ -453,15 +421,11 @@ public void testMatchDNSName() throws Exception { Collections.singletonList(SubjectName.DNS("hostname-workspace-1.local")), publicSuffixMatcher); - try { - DefaultHostnameVerifier.matchDNSName( - "host.domain.com", - Collections.singletonList(SubjectName.DNS("some.other.com")), - publicSuffixMatcher); - Assert.fail("SSLException should have been thrown"); - } catch (final SSLException ex) { - // expected - } + Assert.assertThrows(SSLException.class, () -> + DefaultHostnameVerifier.matchDNSName( + "host.domain.com", + Collections.singletonList(SubjectName.DNS("some.other.com")), + publicSuffixMatcher)); } } diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestByteArrayBuilder.java b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestByteArrayBuilder.java index 775af3c3e..b082dd211 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestByteArrayBuilder.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestByteArrayBuilder.java @@ -67,36 +67,11 @@ public void testInvalidAppendBytes() throws Exception { buffer.append((byte[])null, 0, 0); final byte[] tmp = new byte[] { 1, 2, 3, 4}; - try { - buffer.append(tmp, -1, 0); - Assert.fail("IndexOutOfBoundsException should have been thrown"); - } catch (final IndexOutOfBoundsException ex) { - // expected - } - try { - buffer.append(tmp, 0, -1); - Assert.fail("IndexOutOfBoundsException should have been thrown"); - } catch (final IndexOutOfBoundsException ex) { - // expected - } - try { - buffer.append(tmp, 0, 8); - Assert.fail("IndexOutOfBoundsException should have been thrown"); - } catch (final IndexOutOfBoundsException ex) { - // expected - } - try { - buffer.append(tmp, 10, Integer.MAX_VALUE); - Assert.fail("IndexOutOfBoundsException should have been thrown"); - } catch (final IndexOutOfBoundsException ex) { - // expected - } - try { - buffer.append(tmp, 2, 4); - Assert.fail("IndexOutOfBoundsException should have been thrown"); - } catch (final IndexOutOfBoundsException ex) { - // expected - } + Assert.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, -1, 0)); + Assert.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, -1)); + Assert.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, 8)); + Assert.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 10, Integer.MAX_VALUE)); + Assert.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 2, 4)); } @Test @@ -135,36 +110,11 @@ public void testInvalidAppendChars() throws Exception { buffer.append((char[])null, 0, 0); final char[] tmp = new char[] { 1, 2, 3, 4}; - try { - buffer.append(tmp, -1, 0); - Assert.fail("IndexOutOfBoundsException should have been thrown"); - } catch (final IndexOutOfBoundsException ex) { - // expected - } - try { - buffer.append(tmp, 0, -1); - Assert.fail("IndexOutOfBoundsException should have been thrown"); - } catch (final IndexOutOfBoundsException ex) { - // expected - } - try { - buffer.append(tmp, 0, 8); - Assert.fail("IndexOutOfBoundsException should have been thrown"); - } catch (final IndexOutOfBoundsException ex) { - // expected - } - try { - buffer.append(tmp, 10, Integer.MAX_VALUE); - Assert.fail("IndexOutOfBoundsException should have been thrown"); - } catch (final IndexOutOfBoundsException ex) { - // expected - } - try { - buffer.append(tmp, 2, 4); - Assert.fail("IndexOutOfBoundsException should have been thrown"); - } catch (final IndexOutOfBoundsException ex) { - // expected - } + Assert.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, -1, 0)); + Assert.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, -1)); + Assert.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 0, 8)); + Assert.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 10, Integer.MAX_VALUE)); + Assert.assertThrows(IndexOutOfBoundsException.class, () -> buffer.append(tmp, 2, 4)); } @Test diff --git a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java index da3e130da..57b62ce60 100644 --- a/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java +++ b/httpclient5/src/test/java/org/apache/hc/client5/http/utils/TestDateUtils.java @@ -79,24 +79,9 @@ public void testMalformedDate() { @Test public void testInvalidInput() throws Exception { - try { - DateUtils.parseDate(null, null, null); - Assert.fail("NullPointerException should habe been thrown"); - } catch (final NullPointerException ex) { - // expected - } - try { - DateUtils.formatDate(null); - Assert.fail("NullPointerException should habe been thrown"); - } catch (final NullPointerException ex) { - // expected - } - try { - DateUtils.formatDate(new Date(), null); - Assert.fail("NullPointerException should habe been thrown"); - } catch (final NullPointerException ex) { - // expected - } + Assert.assertThrows(NullPointerException.class, () -> DateUtils.parseDate(null, null, null)); + Assert.assertThrows(NullPointerException.class, () -> DateUtils.formatDate(null)); + Assert.assertThrows(NullPointerException.class, () -> DateUtils.formatDate(new Date(), null)); } @Test