Merge pull request #14919 from alvin577austria/JAVA-20429

JAVA-20429 : Used URI() instead of the deprecated URL()
This commit is contained in:
Kasra Madadipouya 2023-10-17 01:43:16 +02:00 committed by GitHub
commit 0eac8610bd
1 changed files with 61 additions and 52 deletions

View File

@ -15,6 +15,7 @@ import java.util.Map;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicNameValuePair;
import org.junit.Assert;
import org.junit.Test;
import org.springframework.web.util.UriComponentsBuilder;
@ -23,95 +24,101 @@ import com.google.common.collect.ImmutableMap;
public class UrlUnitTest {
@Test
public void givenUrl_whenCanIdentifyProtocol_thenCorrect() throws MalformedURLException {
final URL url = new URL("http://baeldung.com");
public void givenUrl_whenCanIdentifyProtocol_thenCorrect() throws MalformedURLException, URISyntaxException {
final URL url = new URI("http://baeldung.com").toURL();
assertEquals("http", url.getProtocol());
}
@Test
public void givenUrl_whenCanGetHost_thenCorrect() throws MalformedURLException {
final URL url = new URL("http://baeldung.com");
public void givenUrl_whenCanGetHost_thenCorrect() throws MalformedURLException, URISyntaxException {
final URL url = new URI("http://baeldung.com").toURL();
assertEquals("baeldung.com", url.getHost());
}
@Test
public void givenUrl_whenCanGetFileName_thenCorrect2() throws MalformedURLException {
final URL url = new URL("http://baeldung.com/articles?topic=java&version=8");
public void givenUrl_whenCanGetFileName_thenCorrect2() throws MalformedURLException, URISyntaxException {
final URL url = new URI("http://baeldung.com/articles?topic=java&version=8").toURL();
assertEquals("/articles?topic=java&version=8", url.getFile());
}
@Test
public void givenUrl_whenCanGetFileName_thenCorrect1() throws MalformedURLException {
final URL url = new URL("http://baeldung.com/guidelines.txt");
public void givenUrl_whenCanGetFileName_thenCorrect1() throws MalformedURLException, URISyntaxException {
final URL url = new URI("http://baeldung.com/guidelines.txt").toURL();
assertEquals("/guidelines.txt", url.getFile());
}
@Test
public void givenUrl_whenCanGetPathParams_thenCorrect() throws MalformedURLException {
final URL url = new URL("http://baeldung.com/articles?topic=java&version=8");
public void givenUrl_whenCanGetPathParams_thenCorrect() throws MalformedURLException, URISyntaxException {
final URL url = new URI("http://baeldung.com/articles?topic=java&version=8").toURL();
assertEquals("/articles", url.getPath());
}
@Test
public void givenUrl_whenCanGetQueryParams_thenCorrect() throws MalformedURLException {
final URL url = new URL("http://baeldung.com/articles?topic=java");
assertEquals("topic=java", url.getQuery());
public void givenUrl_whenCanGetQueryParams_thenCorrect() throws MalformedURLException, URISyntaxException {
final URL url = new URI("http://baeldung.com/articles?topic=java&version=8").toURL();
assertEquals("topic=java&version=8", url.getQuery());
}
@Test
public void givenUrl_whenGetsDefaultPort_thenCorrect() throws MalformedURLException {
final URL url = new URL("http://baeldung.com");
public void givenUrl_whenGetsDefaultPort_thenCorrect() throws MalformedURLException, URISyntaxException {
final URL url = new URI("http://baeldung.com").toURL();
assertEquals(-1, url.getPort());
assertEquals(80, url.getDefaultPort());
}
@Test
public void givenUrl_whenGetsPort_thenCorrect() throws MalformedURLException {
final URL url = new URL("http://baeldung.com:8090");
public void givenUrl_whenGetsPort_thenCorrect() throws MalformedURLException, URISyntaxException {
final URL url = new URI("http://baeldung.com:8090").toURL();
assertEquals(8090, url.getPort());
assertEquals(80, url.getDefaultPort());
}
@Test
public void givenBaseUrl_whenCreatesRelativeUrl_thenCorrect() throws MalformedURLException {
final URL baseUrl = new URL("http://baeldung.com");
final URL relativeUrl = new URL(baseUrl, "a-guide-to-java-sockets");
assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString());
public void givenHomeUrlAndFullUrl_whenRelativize_thenCorrect() throws MalformedURLException, URISyntaxException {
final URI homeUri = new URI("http://baeldung.com");
final URI fullUri = new URI("http://baeldung.com" + "/a-guide-to-java-sockets");
final URI relativeUri = homeUri.relativize(fullUri);
assertEquals("a-guide-to-java-sockets", relativeUri.toString());
}
@Test
public void givenAbsoluteUrl_whenIgnoresBaseUrl_thenCorrect() throws MalformedURLException {
final URL baseUrl = new URL("http://baeldung.com");
final URL relativeUrl = new URL(baseUrl, "http://baeldung.com/a-guide-to-java-sockets");
assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString());
}
@Test
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException {
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException, URISyntaxException {
final String protocol = "http";
final String host = "baeldung.com";
final String file = "/guidelines.txt";
final URL url = new URL(protocol, host, file);
assertEquals("http://baeldung.com/guidelines.txt", url.toString());
final String fragment = "myImage";
final URL url = new URI(protocol, host, file, fragment).toURL();
assertEquals("http://baeldung.com/guidelines.txt#myImage", url.toString());
}
@Test
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect2() throws MalformedURLException {
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect2() throws MalformedURLException, URISyntaxException {
final String protocol = "http";
final String username = "admin";
final String host = "baeldung.com";
final String file = "/articles?topic=java&version=8";
final URL url = new URL(protocol, host, file);
assertEquals("http://baeldung.com/articles?topic=java&version=8", url.toString());
final String file = "/articles";
final String query = "topic=java&version=8";
final String fragment = "myImage";
final URL url = new URI(protocol, username, host, -1, file, query, fragment).toURL();
assertEquals("http://admin@baeldung.com/articles?topic=java&version=8#myImage", url.toString());
}
@Test
public void givenUrlComponentsWithPort_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException {
public void givenRelativeUrl_whenCreatesRelativeUrl_thenThrows() throws URISyntaxException, MalformedURLException {
final URI uri = new URI("/a-guide-to-java-sockets");
Assert.assertThrows(IllegalArgumentException.class, () -> uri.toURL());
}
@Test
public void givenUrlComponentsWithPort_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException, URISyntaxException {
final String protocol = "http";
final String username = "admin";
final String host = "baeldung.com";
final int port = 9000;
final String file = "/guidelines.txt";
final URL url = new URL(protocol, host, port, file);
assertEquals("http://baeldung.com:9000/guidelines.txt", url.toString());
final String fragment = "myImage";
final URL url = new URI(protocol, username, host, port, file, null, fragment).toURL();
assertEquals("http://admin@baeldung.com:9000/guidelines.txt#myImage", url.toString());
}
@Test
@ -120,7 +127,8 @@ public class UrlUnitTest {
uriBuilder.setPort(9090);
uriBuilder.addParameter("topic", "java");
uriBuilder.addParameter("version", "8");
URL url = uriBuilder.build().toURL();
URL url = uriBuilder.build()
.toURL();
assertEquals("http://baeldung.com:9090/articles?topic=java&version=8", url.toString());
}
@ -130,26 +138,27 @@ public class UrlUnitTest {
URIBuilder uriBuilder = new URIBuilder("http://baeldung.com/articles");
uriBuilder.setPort(9090);
uriBuilder.addParameters(paramMap.entrySet()
.stream()
.map(entry -> new BasicNameValuePair(entry.getKey(), entry.getValue()))
.collect(toList()));
.stream()
.map(entry -> new BasicNameValuePair(entry.getKey(), entry.getValue()))
.collect(toList()));
URL url = uriBuilder.build().toURL();
URL url = uriBuilder.build()
.toURL();
assertEquals("http://baeldung.com:9090/articles?topic=java&version=8", url.toString());
}
@Test
public void givenUrlParameters_whenBuildUrlWithSpringUriComponentsBuilder_thenSuccess() throws MalformedURLException {
URL url = UriComponentsBuilder.newInstance()
.scheme("http")
.host("baeldung.com")
.port(9090)
.path("articles")
.queryParam("topic", "java")
.queryParam("version", "8")
.build()
.toUri()
.toURL();
.scheme("http")
.host("baeldung.com")
.port(9090)
.path("articles")
.queryParam("topic", "java")
.queryParam("version", "8")
.build()
.toUri()
.toURL();
assertEquals("http://baeldung.com:9090/articles?topic=java&version=8", url.toString());
}