Moved code for BAEL-5752 to new module

This commit is contained in:
Saajan N
2022-11-21 18:06:30 +05:30
parent 1627bfee46
commit 9b780b138a
6 changed files with 27 additions and 1 deletions

View File

@@ -14,5 +14,4 @@ This module contains articles about networking in Java
- [Get Domain Name From Given URL in Java](https://www.baeldung.com/java-domain-name-from-url)
- [Java HttpClient Timeout](https://www.baeldung.com/java-httpclient-timeout)
- [Port Scanning With Java](https://www.baeldung.com/java-port-scanning)
- [Validating URL in Java](https://www.baeldung.com/java-validate-url)
- [[<-- Prev]](/core-java-modules/core-java-networking-2)

View File

@@ -1,36 +0,0 @@
package com.baeldung.urlvalidation;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import java.net.URL;
import org.apache.commons.validator.routines.UrlValidator;
public class UrlValidation {
public boolean isValidURLJavaNet(String url) throws MalformedURLException, URISyntaxException {
try {
new URL(url).toURI();
return true;
} catch (MalformedURLException e) {
return false;
} catch (URISyntaxException e) {
return false;
}
}
public boolean invalidUrlAsValidJavaNet(String url) throws MalformedURLException {
try {
new URL(url);
return true;
} catch (MalformedURLException e) {
return false;
}
}
public boolean isValidURLApache(String url) throws MalformedURLException {
UrlValidator validator = new UrlValidator();
return validator.isValid(url);
}
}

View File

@@ -1,44 +0,0 @@
package com.baeldung.urlvalidation;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.assertFalse;
import java.net.MalformedURLException;
import java.net.URISyntaxException;
import org.junit.Test;
import com.baeldung.urlvalidation.UrlValidation;
public class UrlValidateUnitTest {
@Test
public void givenValidStringAsURL_whenUsingJDK_shouldReturnTrue() throws MalformedURLException, URISyntaxException {
UrlValidation urlValidator = new UrlValidation();
assertTrue(urlValidator.isValidURLJavaNet("http://baeldung.com/"));
}
@Test
public void givenInvalidStringAsURL_whenUsingJDK_shouldReturnFalse() throws MalformedURLException, URISyntaxException {
UrlValidation urlValidator = new UrlValidation();
assertFalse(urlValidator.isValidURLJavaNet("https://www.baeldung.com/ java-%%$^&& iuyi"));
}
@Test
public void givenInvalidStringAsURL_whenUsingJDK_shouldReturnTrue() throws MalformedURLException {
UrlValidation urlValidator = new UrlValidation();
assertTrue(urlValidator.invalidUrlAsValidJavaNet("https://www.baeldung.com/ java-%%$^&& iuyi"));
}
@Test
public void givenValidStringAsURL_whenUsingApache_shouldReturnTrue() throws MalformedURLException {
UrlValidation urlValidator = new UrlValidation();
assertTrue(urlValidator.isValidURLApache("http://baeldung.com/"));
}
@Test
public void givenInvalidStringAsURL_whenUsingApache_shouldReturnFalse() throws MalformedURLException {
UrlValidation urlValidator = new UrlValidation();
assertFalse(urlValidator.isValidURLApache("https://www.baeldung.com/ java-%%$^&& iuyi"));
}
}