diff --git a/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/urlvalidation/IPv4Validation.java b/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/urlvalidation/IPv4Validation.java new file mode 100644 index 0000000000..5d72873745 --- /dev/null +++ b/core-java-modules/core-java-networking-4/src/main/java/com/baeldung/urlvalidation/IPv4Validation.java @@ -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(); + } + +} diff --git a/core-java-modules/core-java-networking-4/src/test/java/com/baeldung/ipv4validation/IPv4ValidationUnitTest.java b/core-java-modules/core-java-networking-4/src/test/java/com/baeldung/ipv4validation/IPv4ValidationUnitTest.java new file mode 100644 index 0000000000..367d70117a --- /dev/null +++ b/core-java-modules/core-java-networking-4/src/test/java/com/baeldung/ipv4validation/IPv4ValidationUnitTest.java @@ -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)); + } + +}