Skip to content

Commit 02730de

Browse files
authored
[JAVA-37265] Update URL API (#17029)
1 parent b26054e commit 02730de

File tree

17 files changed

+56
-35
lines changed

17 files changed

+56
-35
lines changed

core-java-modules/core-java-security-4/src/main/java/com/baeldung/enablessldebug/SSLDebugLogger.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.BufferedReader;
44
import java.io.InputStreamReader;
5+
import java.net.URI;
56
import java.net.URL;
67

78
import javax.net.ssl.HttpsURLConnection;
@@ -18,7 +19,7 @@ public static void enableSSLDebugUsingSystemProperties() {
1819

1920
public static void makeHttpsRequest() throws Exception {
2021
String url = "https://github.com/eugenp/tutorials";
21-
URL httpsUrl = new URL(url);
22+
URL httpsUrl = new URI(url).toURL();
2223
HttpsURLConnection connection = (HttpsURLConnection) httpsUrl.openConnection();
2324

2425
try (BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream()))) {

core-java-modules/core-java-streams-simple/src/main/java/com/baeldung/streams/filter/Customer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import java.io.IOException;
44
import java.net.HttpURLConnection;
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
57
import java.net.URL;
68

79
import javax.net.ssl.HttpsURLConnection;
@@ -37,18 +39,18 @@ public boolean hasOverHundredPoints() {
3739
return this.points > 100;
3840
}
3941

40-
public boolean hasValidProfilePhoto() throws IOException {
41-
URL url = new URL(this.profilePhotoUrl);
42+
public boolean hasValidProfilePhoto() throws IOException, URISyntaxException {
43+
URL url = new URI(this.profilePhotoUrl).toURL();
4244
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
4345
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
4446
}
4547

4648
public boolean hasValidProfilePhotoWithoutCheckedException() {
4749
try {
48-
URL url = new URL(this.profilePhotoUrl);
50+
URL url = new URI(this.profilePhotoUrl).toURL();
4951
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
5052
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
51-
} catch (IOException e) {
53+
} catch (Exception e) {
5254
throw new RuntimeException(e);
5355
}
5456
}

core-java-modules/core-java-streams-simple/src/test/java/com/baeldung/streams/filter/StreamFilterUnitTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ public void givenListOfCustomers_whenFilterWithTryCatch_thenGetTwo() {
129129
.filter(c -> {
130130
try {
131131
return c.hasValidProfilePhoto();
132-
} catch (IOException e) {
132+
} catch (Exception e) {
133133
//handle exception
134134
}
135135
return false;
@@ -150,7 +150,7 @@ public void givenListOfCustomers_whenFilterWithTryCatchAndRuntime_thenThrowExcep
150150
.filter(c -> {
151151
try {
152152
return c.hasValidProfilePhoto();
153-
} catch (IOException e) {
153+
} catch (Exception e) {
154154
throw new RuntimeException(e);
155155
}
156156
})

core-java-modules/core-java-streams/src/main/java/com/baeldung/stream/filter/Customer.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import javax.net.ssl.HttpsURLConnection;
44
import java.io.IOException;
55
import java.net.HttpURLConnection;
6+
import java.net.URI;
7+
import java.net.URISyntaxException;
68
import java.net.URL;
79

810
public class Customer {
@@ -36,18 +38,18 @@ public boolean hasOverHundredPoints() {
3638
return this.points > 100;
3739
}
3840

39-
public boolean hasValidProfilePhoto() throws IOException {
40-
URL url = new URL(this.profilePhotoUrl);
41+
public boolean hasValidProfilePhoto() throws Exception {
42+
URL url = new URI(this.profilePhotoUrl).toURL();
4143
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
4244
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
4345
}
4446

4547
public boolean hasValidProfilePhotoWithoutCheckedException() {
4648
try {
47-
URL url = new URL(this.profilePhotoUrl);
49+
URL url = new URI(this.profilePhotoUrl).toURL();
4850
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
4951
return connection.getResponseCode() == HttpURLConnection.HTTP_OK;
50-
} catch (IOException e) {
52+
} catch (Exception e) {
5153
throw new RuntimeException(e);
5254
}
5355
}

jackson-simple/src/test/java/com/baeldung/jackson/objectmapper/JavaReadWriteJsonExampleUnitTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import static org.junit.Assert.assertThat;
88

99
import java.io.File;
10+
import java.net.URI;
1011
import java.net.URL;
1112
import java.util.List;
1213
import java.util.Map;
@@ -103,7 +104,7 @@ public void wheReadFromFile_thanCorrect() throws Exception {
103104

104105
@Test
105106
public void wheReadFromUrl_thanCorrect() throws Exception {
106-
URL resource = new URL("file:src/test/resources/json_car.json");
107+
URL resource = new URI("file:src/test/resources/json_car.json").toURL();
107108

108109
ObjectMapper objectMapper = new ObjectMapper();
109110
Car fromFile = objectMapper.readValue(resource, Car.class);

libraries-apache-commons-2/src/test/java/com/baeldung/commons/ftp/JdkFtpClientIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import java.io.File;
66
import java.io.IOException;
77
import java.io.InputStream;
8+
import java.net.URI;
89
import java.net.URL;
910
import java.net.URLConnection;
1011
import java.nio.file.Files;
@@ -44,10 +45,10 @@ public void teardown() throws IOException {
4445
}
4546

4647
@Test
47-
public void givenRemoteFile_whenDownloading_thenItIsOnTheLocalFilesystem() throws IOException {
48+
public void givenRemoteFile_whenDownloading_thenItIsOnTheLocalFilesystem() throws Exception {
4849
String ftpUrl = String.format("ftp://user:password@localhost:%d/foobar.txt", fakeFtpServer.getServerControlPort());
4950

50-
URLConnection urlConnection = new URL(ftpUrl).openConnection();
51+
URLConnection urlConnection = new URI(ftpUrl).toURL().openConnection();
5152
InputStream inputStream = urlConnection.getInputStream();
5253
Files.copy(inputStream, new File("downloaded_buz.txt").toPath());
5354
inputStream.close();

libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherJMockitUnitTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

66
import java.net.HttpURLConnection;
7+
import java.net.URI;
78
import java.net.URL;
89

910
import org.junit.jupiter.api.Test;
@@ -24,7 +25,7 @@ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableTrue(@Mocked URL anyURL, @
2425
result = HttpURLConnection.HTTP_OK;
2526
}};
2627

27-
UrlFetcher fetcher = new UrlFetcher(new URL("https://www.baeldung.com/"));
28+
UrlFetcher fetcher = new UrlFetcher(new URI("https://www.baeldung.com/").toURL());
2829
assertTrue(fetcher.isUrlAvailable(), "Url should be available: ");
2930
}
3031

@@ -35,7 +36,7 @@ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableFalse(@Mocked URL anyURL,
3536
result = HttpURLConnection.HTTP_INTERNAL_ERROR;
3637
}};
3738

38-
UrlFetcher fetcher = new UrlFetcher(new URL("https://www.baeldung.com/"));
39+
UrlFetcher fetcher = new UrlFetcher(new URI("https://www.baeldung.com/").toURL());
3940
assertFalse(fetcher.isUrlAvailable(), "Url should NOT be available: ");
4041
}
4142

libraries-http-2/src/test/java/com/baeldung/mock/url/UrlFetcherUnitTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import static org.junit.jupiter.api.Assertions.assertTrue;
55

66
import java.net.HttpURLConnection;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.net.URI;
910

@@ -32,7 +33,7 @@ void givenMockedUrl_whenRequestSent_thenIsUrlAvailableTrue() throws Exception {
3233
@Test
3334
void givenMockedUrl_whenRequestSent_thenIsUrlAvailableFalse() throws Exception {
3435
mockHttpURLConnection.setResponseCode(HttpURLConnection.HTTP_FORBIDDEN);
35-
URL url = new URL("https://www.baeldung.com/");
36+
URL url = new URI("https://www.baeldung.com/").toURL();
3637

3738
UrlFetcher fetcher = new UrlFetcher(url);
3839
assertFalse(fetcher.isUrlAvailable(), "Url should NOT be available: ");

maven-modules/maven-integration-test/src/test/java/com/baeldung/maven/it/RestIT.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.IOException;
66
import java.io.InputStream;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.net.URLConnection;
910
import java.util.Scanner;
@@ -12,9 +13,9 @@
1213

1314
public class RestIT {
1415
@Test
15-
public void whenSendingGet_thenMessageIsReturned() throws IOException {
16+
public void whenSendingGet_thenMessageIsReturned() throws Exception {
1617
String url = "http://localhost:8999";
17-
URLConnection connection = new URL(url).openConnection();
18+
URLConnection connection = new URI(url).toURL().openConnection();
1819
try (InputStream response = connection.getInputStream();
1920
Scanner scanner = new Scanner(response)) {
2021
String responseBody = scanner.nextLine();

maven-modules/maven-integration-test/src/test/java/com/baeldung/maven/it/RestIntegrationTest.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import java.io.IOException;
66
import java.io.InputStream;
7+
import java.net.URI;
78
import java.net.URL;
89
import java.net.URLConnection;
910
import java.util.Scanner;
@@ -12,9 +13,9 @@
1213

1314
public class RestIntegrationTest {
1415
@Test
15-
public void whenSendingGet_thenMessageIsReturned() throws IOException {
16+
public void whenSendingGet_thenMessageIsReturned() throws Exception {
1617
String url = "http://localhost:8999";
17-
URLConnection connection = new URL(url).openConnection();
18+
URLConnection connection = new URI(url).toURL().openConnection();
1819
try (InputStream response = connection.getInputStream();
1920
Scanner scanner = new Scanner(response)) {
2021
String responseBody = scanner.nextLine();

0 commit comments

Comments
 (0)