JAVA-20429 : Used URI constructions instead of the deprecated URL constructors.
This commit is contained in:
parent
77d51bcb95
commit
27e89503fa
@ -4,12 +4,14 @@ import static java.util.stream.Collectors.toList;
|
|||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
import java.net.MalformedURLException;
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URI;
|
||||||
import java.net.URISyntaxException;
|
import java.net.URISyntaxException;
|
||||||
import java.net.URL;
|
import java.net.URL;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
import org.apache.http.client.utils.URIBuilder;
|
import org.apache.http.client.utils.URIBuilder;
|
||||||
import org.apache.http.message.BasicNameValuePair;
|
import org.apache.http.message.BasicNameValuePair;
|
||||||
|
import org.junit.Assert;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.web.util.UriComponentsBuilder;
|
import org.springframework.web.util.UriComponentsBuilder;
|
||||||
|
|
||||||
@ -18,95 +20,101 @@ import com.google.common.collect.ImmutableMap;
|
|||||||
public class UrlUnitTest {
|
public class UrlUnitTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenCanIdentifyProtocol_thenCorrect() throws MalformedURLException {
|
public void givenUrl_whenCanIdentifyProtocol_thenCorrect() throws MalformedURLException, URISyntaxException {
|
||||||
final URL url = new URL("http://baeldung.com");
|
final URL url = new URI("http://baeldung.com").toURL();
|
||||||
assertEquals("http", url.getProtocol());
|
assertEquals("http", url.getProtocol());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenCanGetHost_thenCorrect() throws MalformedURLException {
|
public void givenUrl_whenCanGetHost_thenCorrect() throws MalformedURLException, URISyntaxException {
|
||||||
final URL url = new URL("http://baeldung.com");
|
final URL url = new URI("http://baeldung.com").toURL();
|
||||||
assertEquals("baeldung.com", url.getHost());
|
assertEquals("baeldung.com", url.getHost());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenCanGetFileName_thenCorrect2() throws MalformedURLException {
|
public void givenUrl_whenCanGetFileName_thenCorrect2() throws MalformedURLException, URISyntaxException {
|
||||||
final URL url = new URL("http://baeldung.com/articles?topic=java&version=8");
|
final URL url = new URI("http://baeldung.com/articles?topic=java&version=8").toURL();
|
||||||
assertEquals("/articles?topic=java&version=8", url.getFile());
|
assertEquals("/articles?topic=java&version=8", url.getFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenCanGetFileName_thenCorrect1() throws MalformedURLException {
|
public void givenUrl_whenCanGetFileName_thenCorrect1() throws MalformedURLException, URISyntaxException {
|
||||||
final URL url = new URL("http://baeldung.com/guidelines.txt");
|
final URL url = new URI("http://baeldung.com/guidelines.txt").toURL();
|
||||||
assertEquals("/guidelines.txt", url.getFile());
|
assertEquals("/guidelines.txt", url.getFile());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenCanGetPathParams_thenCorrect() throws MalformedURLException {
|
public void givenUrl_whenCanGetPathParams_thenCorrect() throws MalformedURLException, URISyntaxException {
|
||||||
final URL url = new URL("http://baeldung.com/articles?topic=java&version=8");
|
final URL url = new URI("http://baeldung.com/articles?topic=java&version=8").toURL();
|
||||||
assertEquals("/articles", url.getPath());
|
assertEquals("/articles", url.getPath());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenCanGetQueryParams_thenCorrect() throws MalformedURLException {
|
public void givenUrl_whenCanGetQueryParams_thenCorrect() throws MalformedURLException, URISyntaxException {
|
||||||
final URL url = new URL("http://baeldung.com/articles?topic=java");
|
final URL url = new URI("http://baeldung.com/articles?topic=java&version=8").toURL();
|
||||||
assertEquals("topic=java", url.getQuery());
|
assertEquals("topic=java&version=8", url.getQuery());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenGetsDefaultPort_thenCorrect() throws MalformedURLException {
|
public void givenUrl_whenGetsDefaultPort_thenCorrect() throws MalformedURLException, URISyntaxException {
|
||||||
final URL url = new URL("http://baeldung.com");
|
final URL url = new URI("http://baeldung.com").toURL();
|
||||||
assertEquals(-1, url.getPort());
|
assertEquals(-1, url.getPort());
|
||||||
assertEquals(80, url.getDefaultPort());
|
assertEquals(80, url.getDefaultPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrl_whenGetsPort_thenCorrect() throws MalformedURLException {
|
public void givenUrl_whenGetsPort_thenCorrect() throws MalformedURLException, URISyntaxException {
|
||||||
final URL url = new URL("http://baeldung.com:8090");
|
final URL url = new URI("http://baeldung.com:8090").toURL();
|
||||||
assertEquals(8090, url.getPort());
|
assertEquals(8090, url.getPort());
|
||||||
assertEquals(80, url.getDefaultPort());
|
assertEquals(80, url.getDefaultPort());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenBaseUrl_whenCreatesRelativeUrl_thenCorrect() throws MalformedURLException {
|
public void givenHomeUrlAndFullUrl_whenRelativize_thenCorrect() throws MalformedURLException, URISyntaxException {
|
||||||
final URL baseUrl = new URL("http://baeldung.com");
|
final URI homeUri = new URI("http://baeldung.com");
|
||||||
final URL relativeUrl = new URL(baseUrl, "a-guide-to-java-sockets");
|
final URI fullUri = new URI("http://baeldung.com" + "/a-guide-to-java-sockets");
|
||||||
assertEquals("http://baeldung.com/a-guide-to-java-sockets", relativeUrl.toString());
|
final URI relativeUri = homeUri.relativize(fullUri);
|
||||||
|
assertEquals("a-guide-to-java-sockets", relativeUri.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenAbsoluteUrl_whenIgnoresBaseUrl_thenCorrect() throws MalformedURLException {
|
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect() throws MalformedURLException, URISyntaxException {
|
||||||
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 {
|
|
||||||
final String protocol = "http";
|
final String protocol = "http";
|
||||||
final String host = "baeldung.com";
|
final String host = "baeldung.com";
|
||||||
final String file = "/guidelines.txt";
|
final String file = "/guidelines.txt";
|
||||||
final URL url = new URL(protocol, host, file);
|
final String fragment = "myImage";
|
||||||
assertEquals("http://baeldung.com/guidelines.txt", url.toString());
|
final URL url = new URI(protocol, host, file, fragment).toURL();
|
||||||
|
assertEquals("http://baeldung.com/guidelines.txt#myImage", url.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect2() throws MalformedURLException {
|
public void givenUrlComponents_whenConstructsCompleteUrl_thenCorrect2() throws MalformedURLException, URISyntaxException {
|
||||||
final String protocol = "http";
|
final String protocol = "http";
|
||||||
|
final String username = "admin";
|
||||||
final String host = "baeldung.com";
|
final String host = "baeldung.com";
|
||||||
final String file = "/articles?topic=java&version=8";
|
final String file = "/articles";
|
||||||
final URL url = new URL(protocol, host, file);
|
final String query = "topic=java&version=8";
|
||||||
assertEquals("http://baeldung.com/articles?topic=java&version=8", url.toString());
|
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
|
@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 protocol = "http";
|
||||||
|
final String username = "admin";
|
||||||
final String host = "baeldung.com";
|
final String host = "baeldung.com";
|
||||||
final int port = 9000;
|
final int port = 9000;
|
||||||
final String file = "/guidelines.txt";
|
final String file = "/guidelines.txt";
|
||||||
final URL url = new URL(protocol, host, port, file);
|
final String fragment = "myImage";
|
||||||
assertEquals("http://baeldung.com:9000/guidelines.txt", url.toString());
|
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
|
@Test
|
||||||
@ -115,7 +123,8 @@ public class UrlUnitTest {
|
|||||||
uriBuilder.setPort(9090);
|
uriBuilder.setPort(9090);
|
||||||
uriBuilder.addParameter("topic", "java");
|
uriBuilder.addParameter("topic", "java");
|
||||||
uriBuilder.addParameter("version", "8");
|
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());
|
assertEquals("http://baeldung.com:9090/articles?topic=java&version=8", url.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -129,7 +138,8 @@ public class UrlUnitTest {
|
|||||||
.map(entry -> new BasicNameValuePair(entry.getKey(), entry.getValue()))
|
.map(entry -> new BasicNameValuePair(entry.getKey(), entry.getValue()))
|
||||||
.collect(toList()));
|
.collect(toList()));
|
||||||
|
|
||||||
URL url = uriBuilder.build().toURL();
|
URL url = uriBuilder.build()
|
||||||
|
.toURL();
|
||||||
assertEquals("http://baeldung.com:9090/articles?topic=java&version=8", url.toString());
|
assertEquals("http://baeldung.com:9090/articles?topic=java&version=8", url.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user