Skip to content

Commit 58469d7

Browse files
juarezjaramillopivovarit
authored andcommitted
BAEL-864 Difference between URL and URI (eugenp#2438)
1 parent 99eee4d commit 58469d7

1 file changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
package com.baeldung.javanetworking.uriurl;
2+
3+
import java.io.IOException;
4+
import java.net.MalformedURLException;
5+
import java.net.URI;
6+
import java.net.URISyntaxException;
7+
import java.net.URL;
8+
import org.apache.commons.io.IOUtils;
9+
import static org.junit.Assert.*;
10+
import org.junit.Test;
11+
12+
public class URIvsURLUnitTest {
13+
14+
@Test
15+
public void whenCreatingURIs_thenSameInfo() throws URISyntaxException {
16+
URI firstURI = new URI("somescheme://theuser:thepassword@someauthority:80/some/path?thequery#somefragment");
17+
URI secondURI = new URI("somescheme", "theuser:thepassword", "someuthority", 80, "/some/path", "thequery", "somefragment");
18+
19+
assertEquals(firstURI.getScheme(), secondURI.getScheme());
20+
assertEquals(firstURI.getPath(), secondURI.getPath());
21+
}
22+
23+
@Test
24+
public void whenCreatingURLs_thenSameInfo() throws MalformedURLException {
25+
URL firstURL = new URL("http://theuser:thepassword@somehost:80/path/to/file?thequery#somefragment");
26+
URL secondURL = new URL("http", "somehost", 80, "/path/to/file");
27+
28+
assertEquals(firstURL.getHost(), secondURL.getHost());
29+
assertEquals(firstURL.getPath(), secondURL.getPath());
30+
}
31+
32+
@Test
33+
public void whenCreatingURI_thenCorrect() {
34+
URI uri = URI.create("urn:isbn:1234567890");
35+
36+
assertNotNull(uri);
37+
}
38+
39+
@Test(expected = MalformedURLException.class)
40+
public void whenCreatingURLs_thenException() throws MalformedURLException {
41+
URL theURL = new URL("otherprotocol://somehost/path/to/file");
42+
43+
assertNotNull(theURL);
44+
}
45+
46+
@Test
47+
public void givenObjects_whenConverting_thenCorrect() throws MalformedURLException, URISyntaxException {
48+
String aURIString = "http://somehost:80/path?thequery";
49+
URI uri = new URI(aURIString);
50+
URL url = new URL(aURIString);
51+
52+
URL toURL = uri.toURL();
53+
URI toURI = url.toURI();
54+
55+
assertNotNull(url);
56+
assertNotNull(uri);
57+
assertEquals(toURL.toString(), toURI.toString());
58+
}
59+
60+
@Test(expected = MalformedURLException.class)
61+
public void givenURI_whenConvertingToURL_thenException() throws MalformedURLException, URISyntaxException {
62+
URI uri = new URI("somescheme://someauthority/path?thequery");
63+
64+
URL url = uri.toURL();
65+
66+
assertNotNull(url);
67+
}
68+
69+
@Test
70+
public void givenURL_whenGettingContents_thenCorrect() throws MalformedURLException, IOException {
71+
URL url = new URL("http://courses.baeldung.com");
72+
73+
String contents = IOUtils.toString(url.openStream());
74+
75+
assertTrue(contents.contains("<!DOCTYPE html>"));
76+
}
77+
78+
}

0 commit comments

Comments
 (0)