Moved code for BAEL-5752 to new module
This commit is contained in:
@@ -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,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -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"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user