apache bval project (#1178)
* apache bval project * update tests * remove extra lines
This commit is contained in:
parent
bf057fa8db
commit
46f854c03b
|
@ -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>
|
|
@ -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
|
||||
@Email
|
||||
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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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;
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
|
||||
}
|
|
@ -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("ana@yahoo.com", "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("ana@yahoo.com", "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("ana@yahoo.com", "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("ana@yahoo.com", "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("ana@yahoo.com", "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("ana@yahoo.com", "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();
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue