This PR is related to BAEL-7174 (#15712)

* Update pom.xml

Add:        <module>core-java-regex-3</module>

* This commit is related to BAEL-7174

This commit aims to add a new module named "core-java-regex-3" and a new project named "passwordvalidation" to validate passwords using regex.

* Update PasswordValidationUsingRegexUnitTest.java

Change else-if to         assertTrue(matcher.matches());
This commit is contained in:
Mo Helmy 2024-01-23 21:21:56 +02:00 committed by GitHub
parent 15de11de9a
commit 72cd365b26
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 81 additions and 0 deletions

View File

@ -0,0 +1,3 @@
### Relevant Articles:
- More articles: [[<-- prev]](/core-java-modules/core-java-regex-2)

View File

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>core-java-regex-3</artifactId>
<name>core-java-regex-3</name>
<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<packaging>jar</packaging>
<parent>
<groupId>com.baeldung.core-java-modules</groupId>
<artifactId>core-java-modules</artifactId>
<version>0.0.1-SNAPSHOT</version>
</parent>
</project>

View File

@ -0,0 +1,53 @@
package com.baeldung.passwordvalidation;
import org.junit.jupiter.api.Test;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import static org.junit.jupiter.api.Assertions.*;
public class PasswordValidationUsingRegexUnitTest {
String password = "Baeldung20@";
@Test
public void givenStringPassword_whenUsingDynamicPasswordValidationRules_thenCheckIfPasswordValid() {
boolean result = false;
try {
if (password != null) {
String MIN_LENGHT = "8";
String MAX_LENGHT = "20";
boolean SPECIAL_CHAR_NEEDED = false;
String ONE_DIGIT = "(?=.*[0-9])";
String LOWER_CASE = "(?=.*[a-z])";
String UPPER_CASE = "(?=.*[A-Z])";
String SPECIAL_CHAR = SPECIAL_CHAR_NEEDED ? "(?=.*[@#$%^&+=])" : "";
String NO_SPACE = "(?=\\S+$)";
String MIN_MAX_CHAR = ".{" + MIN_LENGHT + "," + MAX_LENGHT + "}";
String PATTERN = ONE_DIGIT + LOWER_CASE + UPPER_CASE + SPECIAL_CHAR + NO_SPACE + MIN_MAX_CHAR;
assertTrue(password.matches(PATTERN));
}
} catch (Exception ex) {
ex.printStackTrace();
fail("Exception occurred: " + ex.getMessage());
}
}
@Test
public void givenStringPassword_whenUsingRegulaExpressions_thenCheckIfPasswordValid() {
String regExpn =
"^(?=.*[0-9])(?=.*[a-z])(?=.*[A-Z])(?=.*[@#$%^&+=])(?=\\S+$).{8,20}$";
Pattern pattern = Pattern.compile(regExpn, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(password);
assertTrue(matcher.matches());
}
}

View File

@ -200,6 +200,7 @@
<module>core-java-string-operations-7</module> <module>core-java-string-operations-7</module>
<module>core-java-regex</module> <module>core-java-regex</module>
<module>core-java-regex-2</module> <module>core-java-regex-2</module>
<module>core-java-regex-3</module>
<module>core-java-uuid</module> <module>core-java-uuid</module>
<module>core-java-collections-maps-6</module> <module>core-java-collections-maps-6</module>
<module>core-java-records</module> <module>core-java-records</module>