forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* apache bval project * update tests * remove extra lines
- Loading branch information
1 parent
bf057fa
commit 46f854c
Showing
5 changed files
with
328 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
<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> | ||
<groupId>apache-bval</groupId> | ||
<artifactId>apache-bval</artifactId> | ||
<version>0.0.1-SNAPSHOT</version> | ||
|
||
<dependencies> | ||
<dependency> | ||
<groupId>org.apache.bval</groupId> | ||
<artifactId>bval-jsr</artifactId> | ||
<version>${bval.version}</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>javax.validation</groupId> | ||
<artifactId>validation-api</artifactId> | ||
<version>1.1.0.Final</version> | ||
</dependency> | ||
<dependency> | ||
<groupId>org.apache.bval</groupId> | ||
<artifactId>bval-extras</artifactId> | ||
<version>${bval.version}</version> | ||
</dependency> | ||
|
||
<dependency> | ||
<groupId>junit</groupId> | ||
<artifactId>junit</artifactId> | ||
<version>${junit.version}</version> | ||
<scope>test</scope> | ||
</dependency> | ||
</dependencies> | ||
|
||
<build> | ||
<plugins> | ||
<plugin> | ||
<groupId>org.apache.maven.plugins</groupId> | ||
<artifactId>maven-compiler-plugin</artifactId> | ||
<version>${maven-compiler-plugin.version}</version> | ||
<configuration> | ||
<source>1.8</source> | ||
<target>1.8</target> | ||
</configuration> | ||
</plugin> | ||
</plugins> | ||
</build> | ||
<properties> | ||
<maven-compiler-plugin.version>3.6.0</maven-compiler-plugin.version> | ||
<junit.version>4.12</junit.version> | ||
<bval.version>1.1.2</bval.version> | ||
</properties> | ||
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package com.baeldung.model; | ||
|
||
import java.io.File; | ||
|
||
import javax.validation.constraints.Min; | ||
import javax.validation.constraints.NotNull; | ||
import javax.validation.constraints.Size; | ||
|
||
import org.apache.bval.constraints.Email; | ||
import org.apache.bval.constraints.NotEmpty; | ||
import org.apache.bval.extras.constraints.checkdigit.IBAN; | ||
import org.apache.bval.extras.constraints.creditcard.Visa; | ||
import org.apache.bval.extras.constraints.file.Directory; | ||
import org.apache.bval.extras.constraints.net.InetAddress; | ||
|
||
import com.baeldung.validation.Password; | ||
|
||
public class User { | ||
@NotNull | ||
private String email; | ||
|
||
@NotEmpty | ||
@Password | ||
private String password; | ||
|
||
@Size(min = 1, max = 20) | ||
private String name; | ||
|
||
@Min(18) | ||
private int age; | ||
|
||
@Visa | ||
private String cardNumber = ""; | ||
|
||
@IBAN | ||
private String iban = ""; | ||
|
||
@InetAddress | ||
private String website = ""; | ||
|
||
@Directory | ||
private File mainDirectory=new File("."); | ||
|
||
public User() { | ||
} | ||
|
||
public User(String email, String password, String name, int age) { | ||
super(); | ||
this.email = email; | ||
this.password = password; | ||
this.name = name; | ||
this.age = age; | ||
} | ||
|
||
public String getEmail() { | ||
return email; | ||
} | ||
|
||
public void setEmail(String email) { | ||
this.email = email; | ||
} | ||
|
||
public String getPassword() { | ||
return password; | ||
} | ||
|
||
public void setPassword(String password) { | ||
this.password = password; | ||
} | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public void setName(String name) { | ||
this.name = name; | ||
} | ||
|
||
public int getAge() { | ||
return age; | ||
} | ||
|
||
public void setAge(int age) { | ||
this.age = age; | ||
} | ||
|
||
public String getCardNumber() { | ||
return cardNumber; | ||
} | ||
|
||
public void setCardNumber(String cardNumber) { | ||
this.cardNumber = cardNumber; | ||
} | ||
|
||
public String getIban() { | ||
return iban; | ||
} | ||
|
||
public void setIban(String iban) { | ||
this.iban = iban; | ||
} | ||
|
||
public String getWebsite() { | ||
return website; | ||
} | ||
|
||
public void setWebsite(String website) { | ||
this.website = website; | ||
} | ||
|
||
public File getMainDirectory() { | ||
return mainDirectory; | ||
} | ||
|
||
public void setMainDirectory(File mainDirectory) { | ||
this.mainDirectory = mainDirectory; | ||
} | ||
|
||
} |
25 changes: 25 additions & 0 deletions
25
apache-bval/src/main/java/com/baeldung/validation/Password.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package com.baeldung.validation; | ||
|
||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
import javax.validation.Constraint; | ||
import javax.validation.Payload; | ||
|
||
import static java.lang.annotation.ElementType.*; | ||
|
||
@Constraint(validatedBy = { PasswordValidator.class }) | ||
@Target({ METHOD, FIELD, ANNOTATION_TYPE, CONSTRUCTOR, PARAMETER }) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface Password { | ||
String message() default "Invalid password"; | ||
|
||
Class<?>[] groups() default {}; | ||
|
||
Class<? extends Payload>[] payload() default {}; | ||
|
||
int length() default 6; | ||
|
||
int nonAlpha() default 1; | ||
} |
35 changes: 35 additions & 0 deletions
35
apache-bval/src/main/java/com/baeldung/validation/PasswordValidator.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package com.baeldung.validation; | ||
|
||
import javax.validation.ConstraintValidator; | ||
import javax.validation.ConstraintValidatorContext; | ||
|
||
public class PasswordValidator implements ConstraintValidator<Password, String> { | ||
|
||
private int length; | ||
private int nonAlpha; | ||
|
||
@Override | ||
public void initialize(Password password) { | ||
this.length = password.length(); | ||
this.nonAlpha = password.nonAlpha(); | ||
|
||
} | ||
|
||
@Override | ||
public boolean isValid(String value, ConstraintValidatorContext context) { | ||
if (value.length() < length) { | ||
return false; | ||
} | ||
int nonAlphaNr = 0; | ||
for (int i = 0; i < value.length(); i++) { | ||
if (!Character.isLetterOrDigit(value.charAt(i))) { | ||
nonAlphaNr++; | ||
} | ||
} | ||
if (nonAlphaNr < nonAlpha) { | ||
return false; | ||
} | ||
return true; | ||
} | ||
|
||
} |
97 changes: 97 additions & 0 deletions
97
apache-bval/src/test/java/com/baeldung/validation/ValidationTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
package com.baeldung.validation; | ||
|
||
import java.io.File; | ||
import java.util.Set; | ||
|
||
import javax.validation.ConstraintViolation; | ||
import javax.validation.Validation; | ||
import javax.validation.Validator; | ||
import javax.validation.ValidatorFactory; | ||
|
||
import org.apache.bval.jsr.ApacheValidationProvider; | ||
import org.junit.AfterClass; | ||
import org.junit.BeforeClass; | ||
import org.junit.Test; | ||
|
||
import static org.junit.Assert.*; | ||
|
||
import com.baeldung.model.User; | ||
|
||
public class ValidationTest { | ||
private static ValidatorFactory validatorFactory; | ||
private static Validator validator; | ||
|
||
@BeforeClass | ||
public static void setup() { | ||
validatorFactory = Validation.byProvider(ApacheValidationProvider.class) | ||
.configure() | ||
.buildValidatorFactory(); | ||
validator = validatorFactory.getValidator(); | ||
} | ||
|
||
@Test | ||
public void givenUser_whenValidate_thenValidationViolations() { | ||
User user = new User("[email protected]", "pass", "nameTooLong_______________", 15); | ||
|
||
Set<ConstraintViolation<User>> violations = validator.validate(user); | ||
assertTrue("no violations", violations.size() > 0); | ||
} | ||
|
||
@Test | ||
public void givenInvalidAge_whenValidateProperty_thenConstraintViolation() { | ||
User user = new User("[email protected]", "pass", "Ana", 12); | ||
|
||
Set<ConstraintViolation<User>> propertyViolations = validator.validateProperty(user, "age"); | ||
assertEquals("size is not 1", 1, propertyViolations.size()); | ||
} | ||
|
||
@Test | ||
public void givenValidAge_whenValidateValue_thenNoConstraintViolation() { | ||
User user = new User("[email protected]", "pass", "Ana", 18); | ||
|
||
Set<ConstraintViolation<User>> valueViolations = validator.validateValue(User.class, "age", 20); | ||
assertEquals("size is not 0", 0, valueViolations.size()); | ||
} | ||
|
||
@Test | ||
public void whenValidateNonJSR_thenCorrect() { | ||
User user = new User("[email protected]", "pass", "Ana", 20); | ||
user.setCardNumber("1234"); | ||
user.setIban("1234"); | ||
user.setWebsite("10.0.2.50"); | ||
user.setMainDirectory(new File(".")); | ||
|
||
Set<ConstraintViolation<User>> violations = validator.validateProperty(user, "iban"); | ||
assertEquals("size is not 1", 1, violations.size()); | ||
|
||
violations = validator.validateProperty(user, "website"); | ||
assertEquals("size is not 0", 0, violations.size()); | ||
|
||
violations = validator.validateProperty(user, "mainDirectory"); | ||
assertEquals("size is not 0", 0, violations.size()); | ||
} | ||
|
||
@Test | ||
public void givenInvalidPassword_whenValidatePassword_thenConstraintViolation() { | ||
User user = new User("[email protected]", "password", "Ana", 20); | ||
Set<ConstraintViolation<User>> violations = validator.validateProperty(user, "password"); | ||
assertEquals("message incorrect", "Invalid password", violations.iterator() | ||
.next() | ||
.getMessage()); | ||
} | ||
|
||
@Test | ||
public void givenValidPassword_whenValidatePassword_thenNoConstraintViolation() { | ||
User user = new User("[email protected]", "password#", "Ana", 20); | ||
|
||
Set<ConstraintViolation<User>> violations = validator.validateProperty(user, "password"); | ||
assertEquals("size is not 0", 0, violations.size()); | ||
} | ||
|
||
@AfterClass | ||
public static void close() { | ||
if (validatorFactory != null) { | ||
validatorFactory.close(); | ||
} | ||
} | ||
} |