JSP

하이버네이트  validator 다운로드

 

http://hibernate.org/validator/

 

필수 라이브러리

hibernate-validator-5.4.1.Final.jar

classmate-1.3.1.jar

javax.el-3.0.1-b08.jar

jboss-logging-3.3.0.Final.jar

validation-api-1.1.0.Final.jar

 

 

class Use

package com.slipp.user;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;

import org.hibernate.validator.constraints.Email;

import com.google.gson.annotations.Expose;
import com.slipp.PasswordMismatchException;

public class User {
	
	@Expose
	@NotNull
	@Size(min=4, max=12)
	private String userid;
	
	@Expose(serialize=false)
	@NotNull
	@Size(min=4, max=12)
	private  String password;
	
	@Expose
	@NotNull
	@Size(min=2, max=12)
	private String name;
	
	@Expose
	@Email
	private String email;
	
	
	public User() {
	
	}
	
	public User(String userid, String password, String name, String email) {
		super();
		this.userid = userid;
		this.password = password;
		this.name = name;
		this.email = email;
	}

	public String getUserid() {
		return userid;
	}

	public void setUserid(String userid) {
		this.userid = userid;
	}

	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 String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public boolean matchPassword(String newPassword) {
		return this.password.equals(newPassword);
	}
	
	public static boolean login(String userid, String password) throws NotFoundUserException, PasswordMismatchException {
		UserDAO userDao=new UserDAO();
		User user=null;
		//User user =Database.findByUserId(userid);
		
		user=userDao.findByUserId(userid);
		
		if(user==null){
			throw new NotFoundUserException();
		}
		
		if(!user.matchPassword(password)){
			throw new PasswordMismatchException();
		}
		
		return true;
	}
	
	
	
	@Override
	public String toString() {
		return "User [userid=" + userid + ", password=" + password + ", name=" + name + ", email=" + email + "]";
	}


	
	
	
}


 

 

 

assertEquals 의 첫번째 인자값은  오류 수 이다.

 

class UserValidatorTest

package com.slipp.user;

import static org.junit.Assert.assertEquals;

import java.util.Iterator;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;
import javax.validation.ValidatorFactory;

import org.junit.BeforeClass;
import org.junit.Test;

public class UserValidatorTest {


    private static Validator validator;

    @BeforeClass
    public static void setUpValidator() {
        ValidatorFactory factory = Validation.buildDefaultValidatorFactory();
        validator = factory.getValidator();
    }

    //@Test
    public void userIdNull() {
       User User = new User(null, "sssss", "5555", "email@navaerr.com" ); 
       Set<ConstraintViolation<User>> constraintViolations =validator.validate( User );
       assertEquals(1, constraintViolations.size() );
       System.out.println("userIdNull : " + constraintViolations.iterator().next().getMessage());
      
    }

  //  @Test
    public void userIdLength() {
    	User User = new User( "22", "22222", "name", "email@nsrbr.com" );
        Set<ConstraintViolation<User>> constraintViolations =validator.validate( User );
        assertEquals(1, constraintViolations.size() );
        System.out.println("userIdLength : " + constraintViolations.iterator().next().getMessage());  
    }
    

  //  @Test
    public void emailTest() {
    	User User = new User( "userid", "DD-AB-123", "name", "emailm" );

        Set<ConstraintViolation<User>> constraintViolations = validator.validate( User );
               
        assertEquals(1, constraintViolations.size() );
        System.out.println("emailTest : " + constraintViolations.iterator().next().getMessage());
  
  
    }

    //assertEquals 의 첫번째 인자값은  오류 수 이다.
    
    @Test
    public void UserIsValid() {
    	System.out.println("UserIsValid ");
    	User User = new User( null, "23", "name", "email" );
        Set<ConstraintViolation<User>> constraintViolations =validator.validate( User );
        
        assertEquals(3, constraintViolations.size() );
        
        Iterator<ConstraintViolation<User>> violations=constraintViolations.iterator();
        while(violations.hasNext()){
        	ConstraintViolation<User> each=violations.next();
        	System.out.println(each.getPropertyPath()+" : " + each.getMessage());
        }
        
    }
    
    
}

 

 

 

 

 

 

 

 

 

 

 

 

jsp

 

about author

PHRASE

Level 60  머나먼나라

내닫기는 주막집 강아지라 , 무슨 일이 있기만 하면, 금세 나서서 무게 없이 왔다갔다하는 사람을 이르는 말.

댓글 ( 4)

댓글 남기기

작성