对DAO编写单元测试源代码

时间:2011-04-04 04:32:03
【文件属性】:

文件名称:对DAO编写单元测试源代码

文件大小:4.79MB

文件格式:RAR

更新时间:2011-04-04 04:32:03

对DAO编写单元测试,其中的如下
package com.javaeedev.dao.impl;

import java.util.List;
import java.util.UUID;

import com.javaeedev.dao.UserDao;
import com.javaeedev.domain.PasswordTicket;
import com.javaeedev.domain.User;
import com.javaeedev.exception.LockException;
import com.javaeedev.util.HibernateUtil;

public class UserDaoImpl implements UserDao {

public User queryForSignOn(String username) {
User user = queryUser(username);
if(user.getLocked())
throw new LockException(user.getLockDate());
return user;
}

public User queryUser(String username) {
return (User) HibernateUtil.query(User.class, username);
}

public void createUser(User user) {
user.setEmailValidation((int)(Math.random() * 1000000) + 0xf);
HibernateUtil.createEntity(user);
}

public void updateUser(User user) {
HibernateUtil.updateEntity(user);
}

public boolean updateEmailValidation(String username, int ticket) {
if(ticket==0)
return false;
return 1==HibernateUtil.executeUpdate(
"update User as u set u.emailValidation=0 where u.username=? and u.emailValidation=?",
new Object[] { username, ticket }
);
}

public String createPasswordTicket(User user) {
HibernateUtil.executeUpdate(
"delete from PasswordTicket as pt where pt.user=?",
new Object[] { user }
);
String ticket = UUID.randomUUID().toString().replaceAll("\\-", "");
PasswordTicket pt = new PasswordTicket();
pt.setUser(user);
pt.setTicket(ticket);
pt.setCreatedDate(System.currentTimeMillis());
HibernateUtil.createEntity(pt);
return ticket;
}

public boolean updatePassword(String username, String oldPassword, String newPassword) {
if(!newPassword.matches(User.REGEX_PASSWORD))
return false;
return 1==HibernateUtil.executeUpdate(
"update User as u set u.password=? where u.username=? and u.password=?",
new Object[] { newPassword, username, oldPassword }
);
}

public boolean queryResetPassword(User user, String ticket) {
long time = System.currentTimeMillis() - 48L * 3600000L;
return !HibernateUtil.queryForList(
"select pt from PasswordTicket as pt where pt.user=? and pt.ticket=? and pt.createdDate>?",
new Object[] { user, ticket, time}
).isEmpty();
}

@SuppressWarnings("unchecked")
public boolean updateResetPassword(User user, String ticket, String password) {
if(!password.matches(User.REGEX_PASSWORD))
return false;
long time = System.currentTimeMillis() - 48L * 3600000L;
List list = HibernateUtil.queryForList(
"select pt from PasswordTicket as pt where pt.user=? and pt.ticket=? and pt.createdDate>?",
new Object[] { user, ticket, time}
);
if(list.isEmpty())
return false;
HibernateUtil.executeUpdate(
"delete from PasswordTicket as pt where pt.user=?",
new Object[] { user }
);
HibernateUtil.executeUpdate(
"update User as u set u.password=? where u.username=?",
new Object[] { password, user.getUsername() }
);
return true;
}

public void updateLock(User user, long lockTime) {
HibernateUtil.executeUpdate(
"update User as u set u.lockDate=? where u.username=?",
new Object[] { System.currentTimeMillis() + lockTime, user.getUsername() }
);
}

public void updateUnlock(User user) {
HibernateUtil.executeUpdate(
"update User as u set u.lockDate=0 where u.username=?",
new Object[] { user.getUsername() }
);
}

}


【文件预览】:
DAOTest
----commons-logging.jar(52KB)
----.project(952B)
----bin()
--------com()
--------hibernate.cfg.xml(2KB)
--------schema.sql(617B)
----hibernate-commons-annotations.jar(65KB)
----hibernate-annotations.jar(259KB)
----test()
--------com()
----jta.jar(9KB)
----hibernate3.jar(2.11MB)
----ejb3-persistence.jar(49KB)
----hsqldb.jar(624KB)
----src()
--------com()
--------hibernate.cfg.xml(2KB)
--------schema.sql(617B)
----dom4j-1.6.1.jar(307KB)
----ext()
--------ant-junit.jar(72KB)
--------junit.jar(110KB)
----asm.jar(26KB)
----hibernate-tools.jar(278KB)
----hibernate-validator.jar(60KB)
----.classpath(1KB)
----.externalToolBuilders()
--------org.eclipse.jdt.core.javabuilder (2).launch(519B)
--------DaoTest_Builder.launch(1KB)
----cglib-2.1.3.jar(276KB)
----antlr-2.7.6.jar(433KB)
----commons-collections.jar(558KB)
----asm-attrs.jar(16KB)
----build.xml(3KB)
----gen-schema.bat(24B)

网友评论

  • 挺好的,结合我自己其他的资料,搞出dao相对简单操作的单元测试啦!呼啦啦……
  • 还好,是想找的东西