diff --git a/spring-cloud-commons/pom.xml b/spring-cloud-commons/pom.xml index 462b60f4..0a8fc317 100644 --- a/spring-cloud-commons/pom.xml +++ b/spring-cloud-commons/pom.xml @@ -66,8 +66,6 @@ - 3.6.0 - 4.5.4 1.0.0 @@ -139,19 +137,16 @@ com.squareup.okhttp3 okhttp - ${okhttp3.version} true com.squareup.okhttp3 logging-interceptor - ${okhttp3.version} true org.apache.httpcomponents httpclient - ${apachehttpclient.version} true diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactoryTests.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactoryTests.java index 40da4b2d..e97334ac 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactoryTests.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultApacheHttpClientConnectionManagerFactoryTests.java @@ -41,37 +41,35 @@ import static org.assertj.core.api.BDDAssertions.then; public class DefaultApacheHttpClientConnectionManagerFactoryTests { @Test - public void newConnectionManager() throws Exception { + public void newConnectionManager() { HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() .newConnectionManager(false, 2, 6); then(((PoolingHttpClientConnectionManager) connectionManager) .getDefaultMaxPerRoute()).isEqualTo(6); then(((PoolingHttpClientConnectionManager) connectionManager).getMaxTotal()) .isEqualTo(2); - Object pool = getField(((PoolingHttpClientConnectionManager) connectionManager), - "pool"); + Object pool = getField((connectionManager), "pool"); then((Long) getField(pool, "timeToLive")).isEqualTo(new Long(-1)); - TimeUnit timeUnit = getField(pool, "tunit"); + TimeUnit timeUnit = getField(pool, "timeUnit"); then(timeUnit).isEqualTo(TimeUnit.MILLISECONDS); } @Test - public void newConnectionManagerWithTTL() throws Exception { + public void newConnectionManagerWithTTL() { HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() .newConnectionManager(false, 2, 6, 56L, TimeUnit.DAYS, null); then(((PoolingHttpClientConnectionManager) connectionManager) .getDefaultMaxPerRoute()).isEqualTo(6); then(((PoolingHttpClientConnectionManager) connectionManager).getMaxTotal()) .isEqualTo(2); - Object pool = getField(((PoolingHttpClientConnectionManager) connectionManager), - "pool"); + Object pool = getField((connectionManager), "pool"); then((Long) getField(pool, "timeToLive")).isEqualTo(new Long(56)); - TimeUnit timeUnit = getField(pool, "tunit"); + TimeUnit timeUnit = getField(pool, "timeUnit"); then(timeUnit).isEqualTo(TimeUnit.DAYS); } @Test - public void newConnectionManagerWithSSL() throws Exception { + public void newConnectionManagerWithSSL() { HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() .newConnectionManager(false, 2, 6); @@ -82,7 +80,7 @@ public class DefaultApacheHttpClientConnectionManagerFactoryTests { } @Test - public void newConnectionManagerWithDisabledSSLValidation() throws Exception { + public void newConnectionManagerWithDisabledSSLValidation() { HttpClientConnectionManager connectionManager = new DefaultApacheHttpClientConnectionManagerFactory() .newConnectionManager(true, 2, 6); @@ -112,6 +110,10 @@ public class DefaultApacheHttpClientConnectionManagerFactoryTests { @SuppressWarnings("unchecked") protected T getField(Object target, String name) { Field field = ReflectionUtils.findField(target.getClass(), name); + if (field == null) { + throw new IllegalArgumentException( + "Can not find field " + name + " in " + target.getClass()); + } ReflectionUtils.makeAccessible(field); Object value = ReflectionUtils.getField(field, target); return (T) value; diff --git a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactoryTest.java b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactoryTest.java index 8fe2e2c4..a68c4ff8 100644 --- a/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactoryTest.java +++ b/spring-cloud-commons/src/test/java/org/springframework/cloud/commons/httpclient/DefaultOkHttpClientConnectionPoolFactoryTest.java @@ -20,6 +20,7 @@ import java.lang.reflect.Field; import java.util.concurrent.TimeUnit; import okhttp3.ConnectionPool; +import okhttp3.internal.connection.RealConnectionPool; import org.junit.Test; import org.springframework.util.ReflectionUtils; @@ -32,18 +33,23 @@ import static org.assertj.core.api.BDDAssertions.then; public class DefaultOkHttpClientConnectionPoolFactoryTest { @Test - public void create() throws Exception { + public void create() { DefaultOkHttpClientConnectionPoolFactory connectionPoolFactory = new DefaultOkHttpClientConnectionPoolFactory(); ConnectionPool connectionPool = connectionPoolFactory.create(2, 3, TimeUnit.MILLISECONDS); - int idleConnections = getField(connectionPool, "maxIdleConnections"); - long keepAliveDuration = getField(connectionPool, "keepAliveDurationNs"); + RealConnectionPool delegate = getField(connectionPool, "delegate"); + int idleConnections = getField(delegate, "maxIdleConnections"); + long keepAliveDuration = getField(delegate, "keepAliveDurationNs"); then(idleConnections).isEqualTo(2); then(keepAliveDuration).isEqualTo(TimeUnit.MILLISECONDS.toNanos(3)); } protected T getField(Object target, String name) { Field field = ReflectionUtils.findField(target.getClass(), name); + if (field == null) { + throw new IllegalArgumentException( + "Can not find field " + name + " in " + target.getClass()); + } ReflectionUtils.makeAccessible(field); Object value = ReflectionUtils.getField(field, target); return (T) value;