Better exception asserts in unit tests

This commit is contained in:
Oleg Kalnichevski 2021-06-19 21:09:19 +02:00
parent 760795b6df
commit aff1d2024c
19 changed files with 176 additions and 615 deletions

View File

@ -217,13 +217,10 @@ public class TestAbstractSerializingCacheStorage {
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");

View File

@ -658,13 +658,9 @@ public class TestProtocolRequirements {
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));
}
/*

View File

@ -94,19 +94,14 @@ public class TestConnectionManagement extends LocalServerTestBase {
}
// 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 class TestConnectionManagement extends LocalServerTestBase {
// 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 class TestConnectionManagement extends LocalServerTestBase {
}
// 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 class TestConnectionManagement extends LocalServerTestBase {
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 class TestConnectionManagement extends LocalServerTestBase {
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

View File

@ -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

View File

@ -112,16 +112,10 @@ public class TestDefaultRedirectStrategy {
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 class TestDefaultRedirectStrategy {
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

View File

@ -157,21 +157,12 @@ public class TestDigestScheme {
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 class TestDigestScheme {
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

View File

@ -75,14 +75,11 @@ public class TestAbstractHttpClientResponseHandler {
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 class TestAbstractHttpClientResponseHandler {
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();
}

View File

@ -66,12 +66,9 @@ public class TestBasicResponseHandler {
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();
}

View File

@ -71,11 +71,7 @@ public class TestResponseEntityWrapper {
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 class TestResponseEntityWrapper {
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 class TestResponseEntityWrapper {
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();
}

View File

@ -46,12 +46,7 @@ public class TestBasicClientCookie {
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

View File

@ -79,19 +79,9 @@ public class TestBasicCookieAttribHandlers {
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 class TestBasicCookieAttribHandlers {
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 class TestBasicCookieAttribHandlers {
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 class TestBasicCookieAttribHandlers {
@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 class TestBasicCookieAttribHandlers {
@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 class TestBasicCookieAttribHandlers {
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 class TestBasicCookieAttribHandlers {
@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 class TestBasicCookieAttribHandlers {
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

View File

@ -80,12 +80,7 @@ public class TestLaxCookieAttribHandlers {
@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

View File

@ -261,12 +261,8 @@ public class TestBasicHttpClientConnectionManager {
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();

View File

@ -111,14 +111,7 @@ public class TestRouteTracker {
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 class TestRouteTracker {
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 class TestRouteTracker {
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 class TestRouteTracker {
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

View File

@ -109,12 +109,7 @@ public class TestRequestExpectContinue {
@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

View File

@ -234,21 +234,10 @@ public class TestHttpRoute {
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 class TestHttpRoute {
@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 -<min> 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 class TestHttpRoute {
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

View File

@ -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.security.cert.X509Certificate;
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 class TestDefaultHostnameVerifier {
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 class TestDefaultHostnameVerifier {
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 class TestDefaultHostnameVerifier {
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 class TestDefaultHostnameVerifier {
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));
}
}

View File

@ -67,36 +67,11 @@ public class TestByteArrayBuilder {
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 class TestByteArrayBuilder {
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

View File

@ -79,24 +79,9 @@ public class TestDateUtils {
@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