BAEL-5752: Check for valid URL in java (#12836)
This commit is contained in:
parent
63058bc24c
commit
d39aa6b1c6
@ -58,6 +58,12 @@
|
||||
<artifactId>guava</artifactId>
|
||||
<version>${guava.version}</version>
|
||||
</dependency>
|
||||
<!-- https://mvnrepository.com/artifact/commons-validator/commons-validator -->
|
||||
<dependency>
|
||||
<groupId>commons-validator</groupId>
|
||||
<artifactId>commons-validator</artifactId>
|
||||
<version>${apache.commons-validator.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
@ -90,6 +96,7 @@
|
||||
<jgonian.commons-ip-math.version>1.32</jgonian.commons-ip-math.version>
|
||||
<googlecode.ipv6.version>0.17</googlecode.ipv6.version>
|
||||
<javax.mail.version>1.6.2</javax.mail.version>
|
||||
<apache.commons-validator.version>1.7</apache.commons-validator.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -0,0 +1,36 @@
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,44 @@
|
||||
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"));
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user