Skip to content

Commit 2a6197e

Browse files
ssy352959096JacksonTian
authored andcommitted
update ApacheHttpClientTest
1 parent e2e763e commit 2a6197e

File tree

9 files changed

+300
-132
lines changed

9 files changed

+300
-132
lines changed

aliyun-java-sdk-core/src/main/java/com/aliyuncs/http/clients/ApacheHttpClient.java

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,22 @@
11
package com.aliyuncs.http.clients;
22

3-
import com.aliyuncs.exceptions.ClientException;
4-
import com.aliyuncs.http.*;
5-
import com.aliyuncs.utils.IOUtils;
6-
import com.aliyuncs.utils.StringUtils;
3+
import java.io.IOException;
4+
import java.security.KeyManagementException;
5+
import java.security.NoSuchAlgorithmException;
6+
import java.security.cert.CertificateException;
7+
import java.security.cert.X509Certificate;
8+
import java.util.Map;
9+
import java.util.concurrent.Callable;
10+
import java.util.concurrent.ExecutorService;
11+
import java.util.concurrent.Future;
12+
import java.util.concurrent.SynchronousQueue;
13+
import java.util.concurrent.ThreadFactory;
14+
import java.util.concurrent.ThreadPoolExecutor;
15+
import java.util.concurrent.TimeUnit;
16+
import java.util.concurrent.atomic.AtomicInteger;
17+
18+
import javax.net.ssl.SSLContext;
19+
720
import org.apache.http.Header;
821
import org.apache.http.HttpResponse;
922
import org.apache.http.client.config.RequestConfig;
@@ -28,16 +41,14 @@
2841
import org.apache.http.ssl.TrustStrategy;
2942
import org.apache.http.util.EntityUtils;
3043

31-
import javax.net.ssl.SSLContext;
32-
import java.io.IOException;
33-
import java.security.KeyManagementException;
34-
import java.security.NoSuchAlgorithmException;
35-
import java.security.cert.CertificateException;
36-
import java.security.cert.X509Certificate;
37-
import java.util.Map;
38-
import java.util.concurrent.*;
39-
import java.util.concurrent.atomic.AtomicInteger;
40-
44+
import com.aliyuncs.exceptions.ClientException;
45+
import com.aliyuncs.http.CallBack;
46+
import com.aliyuncs.http.FormatType;
47+
import com.aliyuncs.http.HttpClientConfig;
48+
import com.aliyuncs.http.HttpRequest;
49+
import com.aliyuncs.http.IHttpClient;
50+
import com.aliyuncs.utils.IOUtils;
51+
import com.aliyuncs.utils.StringUtils;
4152

4253
public class ApacheHttpClient extends IHttpClient {
4354

@@ -65,11 +76,9 @@ protected void init(final HttpClientConfig config) throws ClientException {
6576
}
6677

6778
// default request config
68-
RequestConfig defaultConfig = RequestConfig.custom()
69-
.setConnectTimeout((int) config.getConnectionTimeoutMillis())
70-
.setSocketTimeout((int) config.getReadTimeoutMillis())
71-
.setConnectionRequestTimeout((int) config.getWriteTimeoutMillis())
72-
.build();
79+
RequestConfig defaultConfig = RequestConfig.custom().setConnectTimeout((int) config
80+
.getConnectionTimeoutMillis()).setSocketTimeout((int) config.getReadTimeoutMillis())
81+
.setConnectionRequestTimeout((int) config.getWriteTimeoutMillis()).build();
7382
builder.setDefaultRequestConfig(defaultConfig);
7483

7584
// https
@@ -85,7 +94,8 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
8594
}
8695
}).build();
8796

88-
SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
97+
SSLConnectionSocketFactory connectionFactory = new SSLConnectionSocketFactory(sslContext,
98+
NoopHostnameVerifier.INSTANCE);
8999

90100
socketFactoryRegistryBuilder.register("https", connectionFactory);
91101

@@ -94,10 +104,11 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
94104
}
95105
} else {
96106
if (config.getSslSocketFactory() != null) {
97-
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(config.getSslSocketFactory(),
98-
config.getHostnameVerifier());
107+
SSLConnectionSocketFactory sslConnectionSocketFactory = new SSLConnectionSocketFactory(config
108+
.getSslSocketFactory(), config.getHostnameVerifier());
99109
socketFactoryRegistryBuilder.register("https", sslConnectionSocketFactory);
100-
} else if (config.getKeyManagers() != null || config.getX509TrustManagers() != null || config.getSecureRandom() != null) {
110+
} else if (config.getKeyManagers() != null || config.getX509TrustManagers() != null || config
111+
.getSecureRandom() != null) {
101112
try {
102113
SSLContext sslContext = SSLContext.getInstance("TLS");
103114
sslContext.init(config.getKeyManagers(), config.getX509TrustManagers(), config.getSecureRandom());
@@ -120,9 +131,8 @@ public boolean isTrusted(X509Certificate[] chain, String authType) throws Certif
120131

121132
// async
122133
if (config.getExecutorService() == null) {
123-
executorService = new ThreadPoolExecutor(0, config.getMaxRequests(), DEFAULT_THREAD_KEEP_ALIVE_TIME, TimeUnit.SECONDS,
124-
new SynchronousQueue<Runnable>(),
125-
new DefaultAsyncThreadFactory());
134+
executorService = new ThreadPoolExecutor(0, config.getMaxRequests(), DEFAULT_THREAD_KEEP_ALIVE_TIME,
135+
TimeUnit.SECONDS, new SynchronousQueue<Runnable>(), new DefaultAsyncThreadFactory());
126136
} else {
127137
executorService = config.getExecutorService();
128138
}
@@ -226,7 +236,8 @@ public final com.aliyuncs.http.HttpResponse syncInvoke(HttpRequest apiRequest) t
226236
}
227237

228238
@Override
229-
public final Future<com.aliyuncs.http.HttpResponse> asyncInvoke(final HttpRequest apiRequest, final CallBack callback) {
239+
public final Future<com.aliyuncs.http.HttpResponse> asyncInvoke(final HttpRequest apiRequest,
240+
final CallBack callback) {
230241
return executorService.submit(new Callable<com.aliyuncs.http.HttpResponse>() {
231242
@Override
232243
public com.aliyuncs.http.HttpResponse call() throws Exception {

aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/ECSMetadataServiceCredentialsFetcherTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ public void getMetadataThrowClientException1() throws IOException, ClientExcepti
9494
new ClientException("client exception"));
9595
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher();
9696
fetcher.setRoleName("role");
97-
String res = fetcher.getMetadata();
97+
fetcher.getMetadata();
9898
}
9999

100100
@Test
@@ -108,7 +108,7 @@ public void getMetadataThrowClientException2() throws IOException, ClientExcepti
108108
BDDMockito.given(CompatibleUrlConnClient.compatibleGetResponse(any(HttpRequest.class))).willReturn(response);
109109
ECSMetadataServiceCredentialsFetcher fetcher = new ECSMetadataServiceCredentialsFetcher();
110110
fetcher.setRoleName("role");
111-
String res = fetcher.getMetadata();
111+
fetcher.getMetadata();
112112
}
113113

114114
@Test

aliyun-java-sdk-core/src/test/java/com/aliyuncs/auth/InstanceProfileCredentialsTest.java

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
11
package com.aliyuncs.auth;
22

3-
import org.apache.commons.logging.Log;
4-
import org.apache.commons.logging.LogFactory;
3+
import java.text.SimpleDateFormat;
4+
import java.util.Date;
5+
import java.util.TimeZone;
6+
57
import org.junit.Assert;
68
import org.junit.Rule;
79
import org.junit.Test;
810
import org.junit.rules.ExpectedException;
911

10-
import java.text.SimpleDateFormat;
11-
import java.util.Date;
12-
import java.util.TimeZone;
13-
1412
public class InstanceProfileCredentialsTest {
15-
private static final Log log = LogFactory.getLog(InstanceProfileCredentialsTest.class);
1613

1714
@Rule
1815
public ExpectedException thrown = ExpectedException.none();
Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,26 @@
11
package com.aliyuncs.auth.sts;
22

3-
import com.aliyuncs.transform.UnmarshallerContext;
3+
import java.util.HashMap;
4+
import java.util.Map;
5+
46
import org.junit.Assert;
57
import org.junit.Test;
68

7-
import java.util.HashMap;
8-
import java.util.Map;
9+
import com.aliyuncs.transform.UnmarshallerContext;
910

1011
public class AssumeRoleResponseUnmarshallerTest {
1112

1213
@Test
1314
public void testAssumeRoleResponseUnmarshaller() {
1415
AssumeRoleResponseUnmarshaller assumeRoleResponseUnmarshaller = new AssumeRoleResponseUnmarshaller();
16+
Assert.assertTrue(assumeRoleResponseUnmarshaller instanceof AssumeRoleResponseUnmarshaller);
1517
AssumeRoleResponse assumeRoleResponse = new AssumeRoleResponse();
1618
UnmarshallerContext unmarshallerContext = new UnmarshallerContext();
1719
Map<String, String> responseMap = new HashMap<String, String>();
1820
responseMap.put("key", "3");
1921
unmarshallerContext.setResponseMap(responseMap);
20-
Assert.assertTrue(AssumeRoleResponseUnmarshaller.unmarshall(assumeRoleResponse, unmarshallerContext) instanceof AssumeRoleResponse);
22+
Assert.assertTrue(AssumeRoleResponseUnmarshaller.unmarshall(assumeRoleResponse,
23+
unmarshallerContext) instanceof AssumeRoleResponse);
2124

2225
}
2326
}

aliyun-java-sdk-core/src/test/java/com/aliyuncs/endpoint/LocationServiceEndpointResolverTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@
1313
import java.util.ArrayList;
1414
import java.util.List;
1515

16-
import org.junit.Assert;
1716
import org.junit.Rule;
1817
import org.junit.Test;
1918
import org.junit.rules.ExpectedException;
@@ -218,6 +217,6 @@ public void setLocationServiceEndpointTest() {
218217
public void testGetValidRegionIdsByProduct() {
219218
DefaultAcsClient client = mock(DefaultAcsClient.class);
220219
LocationServiceEndpointResolver resolver = new LocationServiceEndpointResolver(client);
221-
Assert.assertNull(resolver.getValidRegionIdsByProduct("ecs"));
220+
assertNull(resolver.getValidRegionIdsByProduct("ecs"));
222221
}
223222
}

0 commit comments

Comments
 (0)