Table of Contents
ToggleURL in Java with Examples
是 URI 的子集,標識資源可用的位置之外,還描述存取資源的主要機制形式,但反之則不然, URL in Java 介紹常見的建構子 、 equals 、 toString 等方法,本篇增加了範例,並透過單元測試來驗證產出結果。
檔案目錄
./
+- src
+- test
| +- org
| +- ruoxue
| +- java_147
| +- net
| +- url
| +- URLWithExamplesTest.java
單元測試
URL Java 提供建立、取得等操作 URL 。
constructor
建立一個 URL ,提供相關資訊。
@Test
public void constructor() {
try {
String value = "http://www.ruoxue.org/java-learn";
URL url = new URL(value);
System.out.println(url);
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Authority: " + url.getAuthority());
System.out.println("UserInfo: " + url.getUserInfo());
System.out.println("Host: " + url.getHost());
System.out.println("Port: " + url.getPort());
System.out.println("DefaultPort: " + url.getDefaultPort());
System.out.println("File: " + url.getFile());
System.out.println("Path: " + url.getPath());
System.out.println("Query: " + url.getQuery());
System.out.println("Ref: " + url.getRef());
URL url2 = new URL("http", "www.ruoxue.org", 80, "/java-learn");
System.out.println(url2);
assertThat(url.getProtocol()).isEqualTo(url2.getProtocol());
assertThat(url.getHost()).isEqualTo(url2.getHost());
assertThat(url.getDefaultPort()).isEqualTo(url2.getDefaultPort());
assertThat(url.getFile()).isEqualTo(url2.getFile());
assertThat(url.getPath()).isEqualTo(url2.getPath());
assertThat(url.getQuery()).isEqualTo(url2.getQuery());
assertThat(url.getRef()).isEqualTo(url2.getRef());
} catch (MalformedURLException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
http://www.ruoxue.org/java-learn
Protocol: http
Authority: www.ruoxue.org
UserInfo: null
Host: www.ruoxue.org
Port: -1
DefaultPort: 80
File: /java-learn
Path: /java-learn
Query: null
Ref: null
http://www.ruoxue.org:80/java-learn
createThrowException
URL Java 建立一個 URL ,傳入無法辨識的字串,會拋出例外。
@Test
public void constructorThrowException() {
assertThatCode(() -> {
String value = "/java-learn";
URL url = new URL(value);
System.out.println(url);
}).isInstanceOf(MalformedURLException.class);
}
equalz
URL Java 建立兩個 URL ,比較是否相等。
@Test
public void equalz() {
try {
URL url = new URL("https://www.ruoxue.org/#top");
System.out.println(url);
URL url2 = new URL("https://WWW.RUOXUE.ORG/#top");
System.out.println(url2);
boolean result = url.equals(url2);
System.out.println(result);
assertThat(result).isTrue();
url = new URL("https://www.ruoxue.org?s=a#bottom");
System.out.println(url);
url2 = new URL("https://www.ruoxue.org?s=%61#bottom");
System.out.println(url2);
result = url.equals(url2);
System.out.println(result);
assertThat(result).isFalse();
} catch (MalformedURLException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
https://www.ruoxue.org/#top
https://WWW.RUOXUE.ORG/#top
true
https://www.ruoxue.org?s=a#bottom
https://www.ruoxue.org?s=%61#bottom
false
toStringz
Java URL 建立一個 URL ,轉成字串。
@Test
public void toStringz() {
try {
String value = "https://www.ruoxue.org";
URL url = new URL(value);
System.out.println(url.toString());
assertThat(url.toString()).isEqualTo("https://www.ruoxue.org");
value = "mailto:ruoxueorg@gmail.com";
url = new URL(value);
System.out.println(url.toString());
assertThat(url.toString()).isEqualTo("mailto:ruoxueorg@gmail.com");
} catch (MalformedURLException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
https://www.ruoxue.org
mailto:ruoxueorg@gmail.com
URLWithExamplesTest.java
URL in Java 新增單元測試,驗證 Java URL Example 是否符合預期。
package org.ruoxue.java_147.net.url;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatCode;
import java.net.MalformedURLException;
import java.net.URL;
import org.junit.Test;
public class URLWithExamplesTest {
@Test
public void constructor() {
try {
String value = "http://www.ruoxue.org/java-learn";
URL url = new URL(value);
System.out.println(url);
System.out.println("Protocol: " + url.getProtocol());
System.out.println("Authority: " + url.getAuthority());
System.out.println("UserInfo: " + url.getUserInfo());
System.out.println("Host: " + url.getHost());
System.out.println("Port: " + url.getPort());
System.out.println("DefaultPort: " + url.getDefaultPort());
System.out.println("File: " + url.getFile());
System.out.println("Path: " + url.getPath());
System.out.println("Query: " + url.getQuery());
System.out.println("Ref: " + url.getRef());
URL url2 = new URL("http", "www.ruoxue.org", 80, "/java-learn");
System.out.println(url2);
assertThat(url.getProtocol()).isEqualTo(url2.getProtocol());
assertThat(url.getHost()).isEqualTo(url2.getHost());
assertThat(url.getDefaultPort()).isEqualTo(url2.getDefaultPort());
assertThat(url.getFile()).isEqualTo(url2.getFile());
assertThat(url.getPath()).isEqualTo(url2.getPath());
assertThat(url.getQuery()).isEqualTo(url2.getQuery());
assertThat(url.getRef()).isEqualTo(url2.getRef());
} catch (MalformedURLException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
@Test
public void constructorThrowException() {
assertThatCode(() -> {
String value = "/java-learn";
URL url = new URL(value);
System.out.println(url);
}).isInstanceOf(MalformedURLException.class);
}
@Test
public void equalz() {
try {
URL url = new URL("https://www.ruoxue.org/#top");
System.out.println(url);
URL url2 = new URL("https://WWW.RUOXUE.ORG/#top");
System.out.println(url2);
boolean result = url.equals(url2);
System.out.println(result);
assertThat(result).isTrue();
url = new URL("https://www.ruoxue.org?s=a#bottom");
System.out.println(url);
url2 = new URL("https://www.ruoxue.org?s=%61#bottom");
System.out.println(url2);
result = url.equals(url2);
System.out.println(result);
assertThat(result).isFalse();
} catch (MalformedURLException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
@Test
public void toStringz() {
try {
String value = "https://www.ruoxue.org";
URL url = new URL(value);
System.out.println(url.toString());
assertThat(url.toString()).isEqualTo("https://www.ruoxue.org");
value = "mailto:ruoxueorg@gmail.com";
url = new URL(value);
System.out.println(url.toString());
assertThat(url.toString()).isEqualTo("mailto:ruoxueorg@gmail.com");
} catch (MalformedURLException ex) {
throw new RuntimeException(ex.getMessage(), ex);
}
}
}
心得分享
Java URL Example 統一資源定位碼 ( Uniform Resource Locator ) 是網路上資源的引用或位址,透過網路進行通訊,URL 類別來表示資源的位址,熟悉 Java URL 這些方法的操作,例如: 建構子 、 equals 、 toString 等方法,提供範例參考。