BAEL-6049-validate-ipv4-address (#13287)

* validate ipv4

* add unit test

* add more unit tests

* add more unit tests

* add more unit test

---------

Co-authored-by: tienvn4 <tienvn4@ghtk.co>
This commit is contained in:
vunamtien 2023-02-07 01:54:29 +07:00 committed by GitHub
parent 107232d079
commit 35e516d31e
2 changed files with 148 additions and 0 deletions

View File

@ -0,0 +1,27 @@
package com.baeldung.urlvalidation;
import com.google.common.net.InetAddresses;
import org.apache.commons.validator.routines.InetAddressValidator;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class IPv4Validation {
public static boolean validateWithApacheCommons(String ip) {
InetAddressValidator validator = InetAddressValidator.getInstance();
return validator.isValid(ip);
}
public static boolean validateWithGuava(String ip) {
return InetAddresses.isInetAddress(ip);
}
public static boolean validateWithRegex(String ip) {
String regex = "^((25[0-5]|(2[0-4]|1\\d|[1-9]|)\\d)\\.?\\b){4}$";
Pattern pattern = Pattern.compile(regex);
Matcher matcher = pattern.matcher(ip);
return matcher.matches();
}
}

View File

@ -0,0 +1,121 @@
package com.baeldung.ipv4validation;
import org.junit.Test;
import static com.baeldung.urlvalidation.IPv4Validation.*;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
public class IPv4ValidationUnitTest {
@Test
public void givenValidIPv4_whenValidate_thenReturnsTrue() {
String ip = "192.168.0.1";
assertTrue(validateWithApacheCommons(ip));
assertTrue(validateWithGuava(ip));
assertTrue(validateWithRegex(ip));
}
@Test
public void givenIPv4WithThreeOctets_whenValidate_thenReturnsFalse() {
String ip = "192.168.0";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
@Test
public void givenIPv4WithLeadingZero_whenValidate_thenReturnsFalse() {
String ip = "192.168.0.01";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
@Test
public void givenIPv4WithInvalidCharacter_whenValidate_thenReturnsFalse() {
String ip = "a.168.0.01";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
@Test
public void givenIPv4HaveValueAbove255_whenValidate_thenReturnsFalse() {
String ip = "192.168.256.1";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
@Test
public void givenValidIPv4WithTwoDigitOctet_whenValidate_thenReturnsTrue() {
String ip = "192.168.42.1";
assertTrue(validateWithApacheCommons(ip));
assertTrue(validateWithGuava(ip));
assertTrue(validateWithRegex(ip));
}
@Test
public void givenValidIPv4WithNumberInRange200And249_whenValidate_thenReturnsTrue() {
String ip = "192.168.42.222";
assertTrue(validateWithApacheCommons(ip));
assertTrue(validateWithGuava(ip));
assertTrue(validateWithRegex(ip));
}
@Test
public void givenIPv4WithFourDigitOctet_whenValidate_thenReturnsFalse() {
String ip = "1921.168.42.222";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
@Test
public void givenIPv4WithFiveOctets_whenValidate_thenReturnsFalse() {
String ip = "192.168.42.222.10";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
@Test
public void givenIPv4WithTwoConsecutiveDots_whenValidate_thenReturnsFalse() {
String ip = "192.168..1";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
@Test
public void givenOnlyDots_whenValidate_thenReturnsFalse() {
String ip = "...";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
@Test
public void givenBlankString_whenValidate_thenReturnsFalse() {
String ip = " ";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
@Test
public void givenIPv4StartWithDot_whenValidate_thenReturnsFalse() {
String ip = ".192.168.0.1";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
@Test
public void givenIPv4EndWithDot_whenValidate_thenReturnsFalse() {
String ip = "192.168.0.1.";
assertFalse(validateWithApacheCommons(ip));
assertFalse(validateWithGuava(ip));
assertFalse(validateWithRegex(ip));
}
}