|
| 1 | +package com.baeldung.javanetworking.url.test; |
| 2 | + |
| 3 | +import static org.junit.Assert.assertEquals; |
| 4 | +import static org.junit.Assert.assertNotNull; |
| 5 | +import static org.junit.Assert.assertTrue; |
| 6 | + |
| 7 | +import java.io.BufferedReader; |
| 8 | +import java.io.IOException; |
| 9 | +import java.io.InputStreamReader; |
| 10 | +import java.net.HttpURLConnection; |
| 11 | +import java.net.MalformedURLException; |
| 12 | +import java.net.URL; |
| 13 | +import java.net.URLConnection; |
| 14 | + |
| 15 | +import org.junit.BeforeClass; |
| 16 | +import org.junit.FixMethodOrder; |
| 17 | +import org.junit.Test; |
| 18 | +import org.slf4j.Logger; |
| 19 | +import org.slf4j.LoggerFactory; |
| 20 | + |
| 21 | +import com.baeldung.javanetworking.url.URLDemo; |
| 22 | + |
| 23 | +@FixMethodOrder |
| 24 | +public class URLDemoTest { |
| 25 | + private final Logger log = LoggerFactory.getLogger(URLDemo.class); |
| 26 | + static String URLSTRING = "https://wordpress.org:443/support/topic/page-jumps-within-wordpress/?replies=3#post-2278484"; |
| 27 | + // parsed locator |
| 28 | + static String URLPROTOCOL = "https"; |
| 29 | + String URLAUTHORITY = "wordpress.org:443"; |
| 30 | + static String URLHOST = "wordpress.org"; |
| 31 | + static String URLPATH = "/support/topic/page-jumps-within-wordpress/"; |
| 32 | + String URLFILENAME = "/support/topic/page-jumps-within-wordpress/?replies=3"; |
| 33 | + int URLPORT = 443; |
| 34 | + static int URLDEFAULTPORT = 443; |
| 35 | + static String URLQUERY = "replies=3"; |
| 36 | + static String URLREFERENCE = "post-2278484"; |
| 37 | + static String URLCOMPOUND = URLPROTOCOL + "://" + URLHOST + ":" + URLDEFAULTPORT + URLPATH + "?" + URLQUERY + "#" + URLREFERENCE; |
| 38 | + |
| 39 | + static URL url; |
| 40 | + URLConnection urlConnection = null; |
| 41 | + HttpURLConnection connection = null; |
| 42 | + BufferedReader in = null; |
| 43 | + String urlContent = ""; |
| 44 | + |
| 45 | + @BeforeClass |
| 46 | + public static void givenEmplyURL_whenInitializeURL_thenSuccess() throws MalformedURLException { |
| 47 | + url = new URL(URLCOMPOUND); |
| 48 | + } |
| 49 | + |
| 50 | + // check parsed URL |
| 51 | + @Test |
| 52 | + public void givenURL_whenURLIsParsed_thenSuccess() { |
| 53 | + assertNotNull("URL is null", url); |
| 54 | + assertEquals("URL string is not equal", url.toString(), URLSTRING); |
| 55 | + assertEquals("Protocol is not equal", url.getProtocol(), URLPROTOCOL); |
| 56 | + assertEquals("Authority is not equal", url.getAuthority(), URLAUTHORITY); |
| 57 | + assertEquals("Host string is not equal", url.getHost(), URLHOST); |
| 58 | + assertEquals("Path string is not equal", url.getPath(), URLPATH); |
| 59 | + assertEquals("File string is not equal", url.getFile(), URLFILENAME); |
| 60 | + assertEquals("Port number is not equal", url.getPort(), URLPORT); |
| 61 | + assertEquals("Default port number is not equal", url.getDefaultPort(), URLDEFAULTPORT); |
| 62 | + assertEquals("Query string is not equal", url.getQuery(), URLQUERY); |
| 63 | + assertEquals("Reference string is not equal", url.getRef(), URLREFERENCE); |
| 64 | + } |
| 65 | + |
| 66 | + // Obtain the content from location |
| 67 | + @Test |
| 68 | + public void givenURL_whenOpenConnectionAndContentIsNotEmpty_thenSuccess() throws IOException { |
| 69 | + try { |
| 70 | + urlConnection = url.openConnection(); |
| 71 | + } catch (IOException ex) { |
| 72 | + urlConnection = null; |
| 73 | + ex.printStackTrace(); |
| 74 | + } |
| 75 | + assertNotNull("URL Connection is null", urlConnection); |
| 76 | + |
| 77 | + connection = null; |
| 78 | + assertTrue("URLConnection is not HttpURLConnection", urlConnection instanceof HttpURLConnection); |
| 79 | + if (urlConnection instanceof HttpURLConnection) { |
| 80 | + connection = (HttpURLConnection) urlConnection; |
| 81 | + } |
| 82 | + assertNotNull("Connection is null", connection); |
| 83 | + |
| 84 | + log.info(connection.getResponseCode() + " " + connection.getResponseMessage()); |
| 85 | + |
| 86 | + try { |
| 87 | + in = new BufferedReader(new InputStreamReader(connection.getInputStream())); |
| 88 | + } catch (IOException ex) { |
| 89 | + in = null; |
| 90 | + ex.printStackTrace(); |
| 91 | + } |
| 92 | + assertNotNull("Input stream failed", in); |
| 93 | + |
| 94 | + String current; |
| 95 | + try { |
| 96 | + while ((current = in.readLine()) != null) { |
| 97 | + urlContent += current; |
| 98 | + } |
| 99 | + } catch (IOException ex) { |
| 100 | + urlContent = null; |
| 101 | + ex.printStackTrace(); |
| 102 | + } |
| 103 | + assertNotNull("Content is null", urlContent); |
| 104 | + assertTrue("Content is empty", urlContent.length() > 0); |
| 105 | + } |
| 106 | +} |
0 commit comments