Skip to content

Commit a23ce97

Browse files
authored
Merge branch 'eugenp:master' into JAVA-20167_1
2 parents bcfbc7b + d4a0ed5 commit a23ce97

File tree

304 files changed

+3873
-755
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

304 files changed

+3873
-755
lines changed

algorithms-modules/algorithms-miscellaneous-3/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
1010
- [Converting Between Roman and Arabic Numerals in Java](https://www.baeldung.com/java-convert-roman-arabic)
1111
- [Practical Java Examples of the Big O Notation](https://www.baeldung.com/java-algorithm-complexity)
1212
- [Checking If a List Is Sorted in Java](https://www.baeldung.com/java-check-if-list-sorted)
13-
- [Checking if a Java Graph has a Cycle](https://www.baeldung.com/java-graph-has-a-cycle)
13+
- [Checking if a Java Graph Has a Cycle](https://www.baeldung.com/java-graph-has-a-cycle)
1414
- [A Guide to the Folding Technique in Java](https://www.baeldung.com/folding-hashing-technique)
1515
- [Creating a Triangle with for Loops in Java](https://www.baeldung.com/java-print-triangle)
1616
- [The K-Means Clustering Algorithm in Java](https://www.baeldung.com/java-k-means-clustering-algorithm)

algorithms-modules/algorithms-miscellaneous-4/README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
55
### Relevant articles:
66

77
- [Multi-Swarm Optimization Algorithm in Java](https://www.baeldung.com/java-multi-swarm-algorithm)
8-
- [Check If a String Contains All The Letters of The Alphabet with Java](https://www.baeldung.com/java-string-contains-all-letters)
8+
- [Check if a String Contains All the Letters of the Alphabet With Java](https://www.baeldung.com/java-string-contains-all-letters)
99
- [Find the Middle Element of a Linked List in Java](https://www.baeldung.com/java-linked-list-middle-element)
1010
- [Find Substrings That Are Palindromes in Java](https://www.baeldung.com/java-palindrome-substrings)
11-
- [Find the Longest Substring without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
11+
- [Find the Longest Substring Without Repeating Characters](https://www.baeldung.com/java-longest-substring-without-repeated-characters)
1212
- [Permutations of an Array in Java](https://www.baeldung.com/java-array-permutations)
1313
- [Find the Smallest Missing Integer in an Array](https://www.baeldung.com/java-smallest-missing-integer-in-array)
1414
- [Permutations of a String in Java](https://www.baeldung.com/java-string-permutations)

algorithms-modules/algorithms-miscellaneous-5/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ This module contains articles about algorithms. Some classes of algorithms, e.g.
99
- [Reversing a Binary Tree in Java](https://www.baeldung.com/java-reversing-a-binary-tree)
1010
- [Find If Two Numbers Are Relatively Prime in Java](https://www.baeldung.com/java-two-relatively-prime-numbers)
1111
- [Knapsack Problem Implementation in Java](https://www.baeldung.com/java-knapsack)
12-
- [How to Determine if a Binary Tree is Balanced in Java](https://www.baeldung.com/java-balanced-binary-tree)
12+
- [How to Determine if a Binary Tree Is Balanced in Java](https://www.baeldung.com/java-balanced-binary-tree)
1313
- [Overview of Combinatorial Problems in Java](https://www.baeldung.com/java-combinatorial-algorithms)
1414
- [Prim’s Algorithm with a Java Implementation](https://www.baeldung.com/java-prim-algorithm)
1515
- [Maximum Subarray Problem in Java](https://www.baeldung.com/java-maximum-subarray)
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
### Relevant Articles:
22

3-
- [Server-Sent Events (SSE) In JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse)
3+
- [Server-Sent Events (SSE) in JAX-RS](https://www.baeldung.com/java-ee-jax-rs-sse)

apache-httpclient-2/README.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,4 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
1313
- [How To Get Cookies From the Apache HttpClient Response](https://www.baeldung.com/java-apache-httpclient-cookies)
1414
- [Enabling Logging for Apache HttpClient](https://www.baeldung.com/apache-httpclient-enable-logging)
1515
- [Apache HttpClient vs. CloseableHttpClient](https://www.baeldung.com/apache-httpclient-vs-closeablehttpclient)
16-
- [Expand Shortened URLs with Apache HttpClient](https://www.baeldung.com/apache-httpclient-expand-url)
1716
- More articles: [[<-- prev]](../apache-httpclient)

apache-httpclient/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ The "REST With Spring" Classes: http://bit.ly/restwithspring
88

99
### Relevant Articles:
1010
- [Apache HttpClient – Cancel Request](https://www.baeldung.com/httpclient-cancel-request)
11-
- [Apache HttpClient 4 Cookbook](https://www.baeldung.com/httpclient4)
11+
- [Apache HttpClient Cookbook](https://www.baeldung.com/apache-httpclient-cookbook)
1212
- [Apache HttpClient – Follow Redirects for POST](https://www.baeldung.com/httpclient-redirect-on-http-post)
1313
- [Multipart Upload with Apache HttpClient](https://www.baeldung.com/httpclient-multipart-upload)
1414
- [Apache HttpAsyncClient Tutorial](https://www.baeldung.com/httpasyncclient-tutorial)

apache-httpclient/src/test/java/com/baeldung/httpclient/GetRequestMockServer.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@ public class GetRequestMockServer {
2121
public static String serviceOneUrl;
2222
public static String serviceTwoUrl;
2323

24-
private static int serverPort;
24+
public static int serverPort;
2525

2626
public static final String SERVER_ADDRESS = "127.0.0.1";
2727
public static final String PATH_ONE = "/test1";
2828
public static final String PATH_TWO = "/test2";
2929
public static final String METHOD = "GET";
3030

3131
@BeforeAll
32-
static void startServer() throws IOException, URISyntaxException {
33-
//serverPort = getFreePort();
34-
serverPort = 8080;
32+
static void startServer() throws IOException {
33+
serverPort = getFreePort();
3534
System.out.println("Free port "+serverPort);
3635
serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE;
3736
serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO;

apache-httpclient/src/test/java/com/baeldung/httpclient/HttpAsyncClientLiveTest.java

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
import javax.net.ssl.SSLContext;
1111

12+
import org.apache.hc.client5.http.impl.routing.DefaultProxyRoutePlanner;
1213
import org.junit.jupiter.api.Test;
1314

1415
import org.apache.hc.client5.http.async.methods.SimpleHttpRequest;
@@ -55,12 +56,12 @@ class HttpAsyncClientLiveTest extends GetRequestMockServer {
5556

5657
@Test
5758
void whenUseHttpAsyncClient_thenCorrect() throws InterruptedException, ExecutionException, IOException {
58-
final HttpHost target = new HttpHost(HOST);
59+
final HttpHost target = new HttpHost(HOST_WITH_COOKIE);
5960
final SimpleHttpRequest request = SimpleRequestBuilder.get()
6061
.setHttpHost(target)
6162
.build();
6263

63-
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
64+
final CloseableHttpAsyncClient client = HttpAsyncClients.custom().build();
6465
client.start();
6566

6667

@@ -102,30 +103,15 @@ void whenUseMultipleHttpAsyncClient_thenCorrect() throws Exception {
102103

103104
@Test
104105
void whenUseProxyWithHttpClient_thenCorrect() throws Exception {
105-
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
106+
final HttpHost proxy = new HttpHost("127.0.0.1", GetRequestMockServer.serverPort);
107+
DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy);
108+
final CloseableHttpAsyncClient client = HttpAsyncClients.custom()
109+
.setRoutePlanner(routePlanner)
110+
.build();
106111
client.start();
107-
final HttpHost proxy = new HttpHost("127.0.0.1", 8080);
108-
final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
109-
final SimpleHttpRequest request = new SimpleHttpRequest("GET" ,HOST_WITH_PROXY);
110-
request.setConfig(config);
111-
final Future<SimpleHttpResponse> future = client.execute(request, new FutureCallback<>(){
112-
@Override
113-
public void completed(SimpleHttpResponse response) {
114-
115-
System.out.println("responseData");
116-
}
117-
118-
@Override
119-
public void failed(Exception ex) {
120-
System.out.println("Error executing HTTP request: " + ex.getMessage());
121-
}
122-
123-
@Override
124-
public void cancelled() {
125-
System.out.println("HTTP request execution cancelled");
126-
}
127-
});
128112

113+
final SimpleHttpRequest request = new SimpleHttpRequest("GET" ,HOST_WITH_PROXY);
114+
final Future<SimpleHttpResponse> future = client.execute(request, null);
129115
final HttpResponse response = future.get();
130116
assertThat(response.getCode(), equalTo(200));
131117
client.close();
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package com.baeldung.httpclient;
2+
3+
import static org.mockserver.integration.ClientAndServer.startClientAndServer;
4+
import static org.mockserver.matchers.Times.exactly;
5+
import static org.mockserver.model.HttpRequest.request;
6+
import static org.mockserver.model.HttpResponse.response;
7+
8+
import java.io.IOException;
9+
import java.net.ServerSocket;
10+
import java.net.URISyntaxException;
11+
12+
import org.apache.http.HttpStatus;
13+
import org.junit.jupiter.api.AfterAll;
14+
import org.junit.jupiter.api.BeforeAll;
15+
import org.mockserver.client.MockServerClient;
16+
import org.mockserver.integration.ClientAndServer;
17+
18+
public class GetRequestMockServer {
19+
20+
public static ClientAndServer mockServer;
21+
public static String serviceOneUrl;
22+
public static String serviceTwoUrl;
23+
24+
public static int serverPort;
25+
26+
public static final String SERVER_ADDRESS = "127.0.0.1";
27+
public static final String PATH_ONE = "/test1";
28+
public static final String PATH_TWO = "/test2";
29+
public static final String METHOD = "GET";
30+
31+
@BeforeAll
32+
static void startServer() throws IOException, URISyntaxException {
33+
serverPort = getFreePort();
34+
serviceOneUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_ONE;
35+
serviceTwoUrl = "http://" + SERVER_ADDRESS + ":" + serverPort + PATH_TWO;
36+
mockServer = startClientAndServer(serverPort);
37+
mockGetRequest();
38+
}
39+
40+
@AfterAll
41+
static void stopServer() {
42+
mockServer.stop();
43+
}
44+
45+
private static void mockGetRequest() {
46+
new MockServerClient(SERVER_ADDRESS, serverPort)
47+
.when(
48+
request()
49+
.withPath(PATH_ONE)
50+
.withMethod(METHOD),
51+
exactly(5)
52+
)
53+
.respond(
54+
response()
55+
.withStatusCode(HttpStatus.SC_OK)
56+
.withBody("{\"status\":\"ok\"}")
57+
);
58+
new MockServerClient(SERVER_ADDRESS, serverPort)
59+
.when(
60+
request()
61+
.withPath(PATH_TWO)
62+
.withMethod(METHOD),
63+
exactly(1)
64+
)
65+
.respond(
66+
response()
67+
.withStatusCode(HttpStatus.SC_OK)
68+
.withBody("{\"status\":\"ok\"}")
69+
);
70+
}
71+
72+
private static int getFreePort () throws IOException {
73+
try (ServerSocket serverSocket = new ServerSocket(0)) {
74+
return serverSocket.getLocalPort();
75+
}
76+
}
77+
}

apache-httpclient4/src/test/java/com/baeldung/httpclient/HttpAsyncClientV4LiveTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
import org.apache.http.ssl.SSLContexts;
3232
import org.junit.jupiter.api.Test;
3333

34-
class HttpAsyncClientV4LiveTest {
34+
35+
class HttpAsyncClientV4LiveTest extends GetRequestMockServer {
3536

3637
private static final String HOST = "http://www.google.com";
3738
private static final String HOST_WITH_SSL = "https://mms.nw.ru/";
@@ -87,7 +88,7 @@ void whenUseMultipleHttpAsyncClient_thenCorrect() throws Exception {
8788
void whenUseProxyWithHttpClient_thenCorrect() throws Exception {
8889
final CloseableHttpAsyncClient client = HttpAsyncClients.createDefault();
8990
client.start();
90-
final HttpHost proxy = new HttpHost("127.0.0.1", 8080);
91+
final HttpHost proxy = new HttpHost("127.0.0.1", GetRequestMockServer.serverPort);
9192
final RequestConfig config = RequestConfig.custom().setProxy(proxy).build();
9293
final HttpGet request = new HttpGet(HOST_WITH_PROXY);
9394
request.setConfig(config);

0 commit comments

Comments
 (0)