Skip to content
Permalink
Browse files
Minor Improvements:
* Add final to variable
* Unnecessary semicolon
* Use Standard Charset object
* Unnecessary conversion to String
* Simplifiable conditional expression
* Replace 'Arrays.asList()' with Collections.singletonList
* Redundant local variable. Simplify
  • Loading branch information
arturobernalg authored and ok2c committed Dec 31, 2020
1 parent 25c1249 commit 36e1bde6ff77a08a0a3e01714ae3583f2283a2d8
Showing 26 changed files with 54 additions and 50 deletions.
@@ -27,6 +27,7 @@
package org.apache.hc.client5.http.impl.cache;

import java.util.Arrays;
import java.util.Collections;
import java.util.Date;
import java.util.HashSet;
import java.util.Iterator;
@@ -86,7 +87,7 @@ public ResponseCachingPolicy(final long maxObjectSizeBytes,
this.sharedCache = sharedCache;
this.neverCache1_0ResponsesWithQueryString = neverCache1_0ResponsesWithQueryString;
if (allow303Caching) {
uncacheableStatusCodes = new HashSet<>(Arrays.asList(HttpStatus.SC_PARTIAL_CONTENT));
uncacheableStatusCodes = new HashSet<>(Collections.singletonList(HttpStatus.SC_PARTIAL_CONTENT));
} else {
uncacheableStatusCodes = new HashSet<>(Arrays.asList(HttpStatus.SC_PARTIAL_CONTENT, HttpStatus.SC_SEE_OTHER));
}
@@ -43,7 +43,7 @@
class WarningValue {

private int offs;
private int init_offs;
private final int init_offs;
private final String src;
private int warnCode;
private String warnAgent;
@@ -51,7 +51,7 @@ public abstract class AbstractProtocolTest {

protected static final int MAX_BYTES = 1024;
protected static final int MAX_ENTRIES = 100;
protected int entityLength = 128;
protected final int entityLength = 128;
protected HttpHost host;
protected HttpRoute route;
protected HttpEntity body;
@@ -29,7 +29,7 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collections;

import org.apache.hc.client5.http.impl.RequestCopier;
import org.apache.hc.core5.http.HttpRequest;
@@ -68,7 +68,7 @@ public void testRequestWithWeekETagForPUTOrDELETEIfMatchAllowed() throws Excepti
final HttpRequest req = new BasicHttpRequest("PUT", "http://example.com/");
req.setHeader("If-Match", "W/\"weak\"");
impl = new RequestProtocolCompliance(true);
assertEquals(Arrays.asList(), impl.requestIsFatallyNonCompliant(req));
assertEquals(Collections.emptyList(), impl.requestIsFatallyNonCompliant(req));
}

@Test
@@ -35,7 +35,7 @@

private static final String IDENTIFIER = "some-identifier";

private ConcurrentCountMap<String> map = new ConcurrentCountMap<>();
private final ConcurrentCountMap<String> map = new ConcurrentCountMap<>();

@Test
public void testBasics() {
@@ -26,7 +26,7 @@
*/
package org.apache.hc.client5.testing.async;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.Future;
@@ -505,7 +505,7 @@ protected void customizeUnauthorizedResponse(final HttpResponse unauthorized) {
});

final RequestConfig config = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Arrays.asList("MyBasic"))
.setTargetPreferredAuthSchemes(Collections.singletonList("MyBasic"))
.build();
final HttpClientContext context = HttpClientContext.create();
context.setCredentialsProvider(credsProvider);
@@ -29,7 +29,7 @@
import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.apache.hc.client5.http.ClientProtocolException;
import org.apache.hc.client5.http.fluent.Content;
@@ -140,11 +140,11 @@ public void testPostRequest() throws Exception {
public void testContentAsStringWithCharset() throws Exception {
final HttpHost target = start();
final String baseURL = "http://localhost:" + target.getPort();
final Content content = Request.post(baseURL + "/echo").bodyByteArray("Ü".getBytes("utf-8")).execute()
final Content content = Request.post(baseURL + "/echo").bodyByteArray("Ü".getBytes(StandardCharsets.UTF_8)).execute()
.returnContent();
Assert.assertEquals((byte)-61, content.asBytes()[0]);
Assert.assertEquals((byte)-100, content.asBytes()[1]);
Assert.assertEquals("Ü", content.asString(Charset.forName("utf-8")));
Assert.assertEquals("Ü", content.asString(StandardCharsets.UTF_8));
}

@Test
@@ -30,7 +30,7 @@
import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicLong;
@@ -650,7 +650,7 @@ public String getName() {
new UsernamePasswordCredentials("test", "test".toCharArray()));

final RequestConfig config = RequestConfig.custom()
.setTargetPreferredAuthSchemes(Arrays.asList("MyBasic"))
.setTargetPreferredAuthSchemes(Collections.singletonList("MyBasic"))
.build();
final Registry<AuthSchemeFactory> authSchemeRegistry = RegistryBuilder.<AuthSchemeFactory>create()
.register("MyBasic", myBasicAuthSchemeFactory)
@@ -29,7 +29,7 @@
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.Queue;
import java.util.concurrent.ConcurrentLinkedQueue;

@@ -630,7 +630,7 @@ public HttpServerRequestHandler decorate(final HttpServerRequestHandler requestH

@Test
public void testDefaultHeadersRedirect() throws Exception {
this.clientBuilder.setDefaultHeaders(Arrays.asList(new BasicHeader(HttpHeaders.USER_AGENT, "my-test-client")));
this.clientBuilder.setDefaultHeaders(Collections.singletonList(new BasicHeader(HttpHeaders.USER_AGENT, "my-test-client")));

final HttpHost target = start(null, new Decorator<HttpServerRequestHandler>() {

@@ -91,9 +91,9 @@ public void handle(
*/
private static class NegotiateSchemeWithMockGssManager extends SPNegoScheme {

GSSManager manager = Mockito.mock(GSSManager.class);
GSSName name = Mockito.mock(GSSName.class);
GSSContext context = Mockito.mock(GSSContext.class);
final GSSManager manager = Mockito.mock(GSSManager.class);
final GSSName name = Mockito.mock(GSSName.class);
final GSSContext context = Mockito.mock(GSSContext.class);

NegotiateSchemeWithMockGssManager() throws Exception {
super(KerberosConfig.DEFAULT, SystemDefaultDnsResolver.INSTANCE);
@@ -75,7 +75,7 @@ public AuthScope(
this.host = host != null ? host.toLowerCase(Locale.ROOT) : null;
this.port = port >= 0 ? port: -1;
this.realm = realm;
this.schemeName = schemeName != null ? schemeName : null;
this.schemeName = schemeName;
}

/**
@@ -98,7 +98,7 @@ public AuthScope(
this.host = origin.getHostName().toLowerCase(Locale.ROOT);
this.port = origin.getPort() >= 0 ? origin.getPort() : -1;
this.realm = realm;
this.schemeName = schemeName != null ? schemeName : null;
this.schemeName = schemeName;
}

/**
@@ -87,7 +87,7 @@ public String toString() {
}
buffer.append(this.host);
buffer.append(':');
buffer.append(Integer.toString(this.port));
buffer.append(this.port);
buffer.append(this.path);
buffer.append(']');
return buffer.toString();
@@ -33,7 +33,7 @@

class LoggingExceptionCallback implements Callback<Exception> {

static LoggingExceptionCallback INSTANCE = new LoggingExceptionCallback();
static final LoggingExceptionCallback INSTANCE = new LoggingExceptionCallback();

private static final Logger LOG = LoggerFactory.getLogger("org.apache.hc.client5.http.impl.async");

@@ -798,7 +798,7 @@ private static byte[] lmv2Response(final byte[] hash, final byte[] challenge, fi

enum Mode
{
CLIENT, SERVER;
CLIENT, SERVER
}

static class Handle
@@ -1113,8 +1113,8 @@ static class NTLMMessage {
// Check to be sure there's a type 2 message indicator next
final int type = readULong(SIGNATURE.length);
if (type != expectedType) {
throw new NTLMEngineException("NTLM type " + Integer.toString(expectedType)
+ " message expected - instead got type " + Integer.toString(type));
throw new NTLMEngineException("NTLM type " + expectedType
+ " message expected - instead got type " + type);
}

currentOutputPosition = messageContents.length;
@@ -145,7 +145,7 @@ public ManagedHttpClientConnection createConnection(final Socket socket) throws
charEncoder.onMalformedInput(malformedInputAction);
charEncoder.onUnmappableCharacter(unmappableInputAction);
}
final String id = "http-outgoing-" + Long.toString(COUNTER.getAndIncrement());
final String id = "http-outgoing-" + COUNTER.getAndIncrement();
final DefaultManagedHttpClientConnection conn = new DefaultManagedHttpClientConnection(
id,
charDecoder,
@@ -33,7 +33,7 @@
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.hc.core5.annotation.Contract;
@@ -88,7 +88,7 @@ public static PublicSuffixMatcher getDefault() {
LOG.warn("Failure loading public suffix list from default resource", ex);
}
} else {
DEFAULT_INSTANCE = new PublicSuffixMatcher(DomainType.ICANN, Arrays.asList("com"), null);
DEFAULT_INSTANCE = new PublicSuffixMatcher(DomainType.ICANN, Collections.singletonList("com"), null);
}
}
}
@@ -334,7 +334,7 @@ public void testMultipartFormStringPartsMultiCharsets() throws Exception {

final FormBodyPart p1 = FormBodyPartBuilder.create(
"field1",
new StringBody(s1, ContentType.create("text/plain", Charset.forName("ISO-8859-1")))).build();
new StringBody(s1, ContentType.create("text/plain", StandardCharsets.ISO_8859_1))).build();
final FormBodyPart p2 = FormBodyPartBuilder.create(
"field2",
new StringBody(s2, ContentType.create("text/plain", Charset.forName("KOI8-R")))).build();
@@ -293,7 +293,7 @@ public void testMultipartPartStringPartsMultiCharsets() throws Exception {
final String s2 = constructString(RUSSIAN_HELLO);

final MultipartPart p1 = MultipartPartBuilder.create(
new StringBody(s1, ContentType.create("text/plain", Charset.forName("ISO-8859-1")))).build();
new StringBody(s1, ContentType.create("text/plain", StandardCharsets.ISO_8859_1))).build();
final MultipartPart p2 = MultipartPartBuilder.create(
new StringBody(s2, ContentType.create("text/plain", Charset.forName("KOI8-R")))).build();
final HttpStrictMultipart multipart = new HttpStrictMultipart(null, "foo",
@@ -29,6 +29,7 @@

import java.io.File;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.hc.core5.http.ContentType;
@@ -47,8 +48,8 @@ public void testBuildBodyPartBasics() throws Exception {
Assert.assertEquals(stringBody, part.getBody());
final Header header = part.getHeader();
Assert.assertNotNull(header);
assertFields(Arrays.asList(
new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
assertFields(Collections.singletonList(
new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
header.getFields());
}

@@ -63,8 +64,8 @@ public void testBuildBodyPartMultipleBuilds() throws Exception {
Assert.assertEquals(stringBody, part1.getBody());
final Header header1 = part1.getHeader();
Assert.assertNotNull(header1);
assertFields(Arrays.asList(
new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
assertFields(Collections.singletonList(
new MimeField("Content-Type", "text/plain; charset=ISO-8859-1")),
header1.getFields());
final FileBody fileBody = new FileBody(new File("/path/stuff.bin"), ContentType.DEFAULT_BINARY);
final MultipartPart part2 = builder
@@ -75,8 +76,8 @@ public void testBuildBodyPartMultipleBuilds() throws Exception {
Assert.assertEquals(fileBody, part2.getBody());
final Header header2 = part2.getHeader();
Assert.assertNotNull(header2);
assertFields(Arrays.asList(
new MimeField("Content-Type", "application/octet-stream")),
assertFields(Collections.singletonList(
new MimeField("Content-Type", "application/octet-stream")),
header2.getFields());
}

@@ -32,6 +32,7 @@
import java.nio.charset.CodingErrorAction;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;

import javax.net.ssl.SSLContext;

@@ -196,7 +197,7 @@ public InetAddress[] resolve(final String host) throws UnknownHostException {
.setCookieSpec(StandardCookieSpec.STRICT)
.setExpectContinueEnabled(true)
.setTargetPreferredAuthSchemes(Arrays.asList(StandardAuthScheme.NTLM, StandardAuthScheme.DIGEST))
.setProxyPreferredAuthSchemes(Arrays.asList(StandardAuthScheme.BASIC))
.setProxyPreferredAuthSchemes(Collections.singletonList(StandardAuthScheme.BASIC))
.build();

// Create an HttpClient with the given custom dependencies and configuration.
@@ -88,7 +88,7 @@ static void checkMD4(final String input, final String hexOutput) throws Exceptio
while (i < answer.length) {
if (answer[i] != correctAnswer[i]) {
throw new Exception("Answer value for MD4('" + input + "') disagrees at position "
+ Integer.toString(i));
+ i);
}
i++;
}
@@ -71,7 +71,7 @@ public class TestFutureRequestExecutionService {
private final AtomicBoolean blocked = new AtomicBoolean(false);

@Rule
public ExpectedException thrown = ExpectedException.none();
public final ExpectedException thrown = ExpectedException.none();

@Before
public void before() throws Exception {
@@ -28,6 +28,7 @@
package org.apache.hc.client5.http.impl.cookie;

import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import org.apache.hc.client5.http.cookie.CommonCookieAttributeHandler;
@@ -264,7 +265,7 @@ public void testFormatCookiesBasics() throws Exception {
final Cookie cookie1 = new BasicClientCookie("name1", "value");

final RFC6265CookieSpec cookiespec = new RFC6265CookieSpec();
final List<Header> headers = cookiespec.formatCookies(Arrays.asList(cookie1));
final List<Header> headers = cookiespec.formatCookies(Collections.singletonList(cookie1));
Assert.assertNotNull(headers);
Assert.assertEquals(1, headers.size());
final Header header = headers.get(0);
@@ -53,7 +53,7 @@ public void testParse() throws Exception {
}
Assert.assertNotNull(suffixList);
Assert.assertEquals(Arrays.asList("xx", "jp", "ac.jp", "*.tokyo.jp", "no", "h\u00E5.no"), suffixList.getRules());
Assert.assertEquals(Arrays.asList("metro.tokyo.jp"), suffixList.getExceptions());
Assert.assertEquals(Collections.singletonList("metro.tokyo.jp"), suffixList.getExceptions());
}

@Test
@@ -74,7 +74,7 @@ public void testParseByType() throws Exception {
Assert.assertNotNull(publicSuffixList1);
Assert.assertEquals(DomainType.ICANN, publicSuffixList1.getType());
Assert.assertEquals(Arrays.asList("jp", "ac.jp", "*.tokyo.jp"), publicSuffixList1.getRules());
Assert.assertEquals(Arrays.asList("metro.tokyo.jp"), publicSuffixList1.getExceptions());
Assert.assertEquals(Collections.singletonList("metro.tokyo.jp"), publicSuffixList1.getExceptions());

final PublicSuffixList publicSuffixList2 = suffixLists.get(1);
Assert.assertNotNull(publicSuffixList2);
@@ -43,7 +43,6 @@
import java.nio.charset.StandardCharsets;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

@@ -376,18 +375,18 @@ public void testHTTPCLIENT_1997_UNKNOWN() { // Only True on all domains (same as
@Test // Check compressed IPv6 hostname matching
public void testHTTPCLIENT_1316() throws Exception{
final String host1 = "2001:0db8:aaaa:bbbb:cccc:0:0:0001";
DefaultHostnameVerifier.matchIPv6Address(host1, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc:0:0:0001")));
DefaultHostnameVerifier.matchIPv6Address(host1, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::1")));
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, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10")));
DefaultHostnameVerifier.matchIPv6Address(host1, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10")));
Assert.fail("SSLException expected");
} catch (final SSLException expected) {
}
final String host2 = "2001:0db8:aaaa:bbbb:cccc::1";
DefaultHostnameVerifier.matchIPv6Address(host2, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc:0:0:0001")));
DefaultHostnameVerifier.matchIPv6Address(host2, Arrays.asList(SubjectName.IP("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, Arrays.asList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10")));
DefaultHostnameVerifier.matchIPv6Address(host2, Collections.singletonList(SubjectName.IP("2001:0db8:aaaa:bbbb:cccc::10")));
Assert.fail("SSLException expected");
} catch (final SSLException expected) {
}

0 comments on commit 36e1bde

Please sign in to comment.