日期:2014-05-17  浏览次数:20721 次

登录验证求助
做登录,我做到同样的账号密码无法重复注册的Test 但是如何写空账号密码无法注册的验证。
请求帮助
Java code

package jc.ac.sojou.cis.kunma.login.service;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.junit.Assert.assertThat;

import java.util.List;

import jc.ac.sojou.cis.kunma.login.model.Account;

import org.junit.Test;
import org.slim3.tester.AppEngineTestCase;

public class AccountServiceTest extends AppEngineTestCase {

    private AccountService service = new AccountService();
      
      @Test
    public void test() throws Exception {
        assertThat(service, is(notNullValue()));
        
        Account ac = new Account();
        ac.setId("test");
        ac.setPassword("password");
        service.save(ac);
        
        Account ac2 = service.load();
        
        assertThat(ac2,is(notNullValue()));
        assertThat(ac.getId(),is(ac2.getId()));
        assertThat(ac.getPassword(),is(ac2.getPassword()));
    }
      @Test  //注册与登录的验证
      
      public void testValid() throws Exception{
          assertThat(service,is(notNullValue()));
          
          Account ac = new Account();
          ac.setId("test");
          ac.setPassword("password");
          service.save(ac);
          
          boolean b1 = service.isValid("test","password");
          assertThat(b1,is(true));
          
          boolean b2 = service.isValid("test", "fail");
          assertThat(b2,is(false));  
      }
      @Test //重复帐号密码注册验证

      public void testGetList() throws Exception { 
          assertThat(service, is(notNullValue()));
          
          Account ac = new Account(); 
          ac.setId("test"); 
          ac.setPassword("password");
          boolean b1 = service.save(ac); 
          assertThat(b1, is(true));
          
          Account ac2 = new Account(); 
          ac2.setId("test"); 
          ac2.setPassword("password"); 
          boolean b2 = service.save(ac2); 
          assertThat(b2, is(false));
          
          List<Account> list = service.list("test", "password");
          
          assertThat(list, is(notNullValue())); 
          assertThat(list.size(), is(1)); 
          Account rac = list.get(0);
          assertThat(rac.getId(), is(ac.getId())); 
          assertThat(rac.getPassword(), is(ac.getPassword()));
          
      }
}



------解决方案--------------------
HTML code

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
 <head>
 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>user Add</title>
  <meta name="Description" content="">
 </head>
 <script type="text/javascript">
  <!--
    function validate()
    {
    var id = document.getElementById("id").value;
    var password = document.getElementById("password").value;
    if(id.replace(/(^\s+)|(\s+$)/g,"").length ==0)
    {
    alert("ID不能为空");
    return false;
    }
    if(password.replace(/(^\s+)|(\s+$)/g,"").length ==0)
    //如果密码允许空格的话,那么就直接用password.length ==0来判断就可以了
    {
    alert("密码不能为空");
    return false;
    }
    return true;
    }
  //-->
  </script>
 <body>
 
 <form action="addPost" onsubmit ="return validate()">
<div>
 ID:<input type="text" name="id"/>
</div>
<div>
Password :
<input type="password" name="password" />
</div>
<input type="sub