• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: | 推荐给好友 上一篇 | 下一篇

对DAO编写单元测试

发布: 2008-7-14 10:04 | 作者: 廖雪峰 | 来源: 网络转载 | 查看: 60次 | 进入软件测试论坛讨论

领测软件测试网

 public class HibernateUtil {

private static final SessionFactory sessionFactory;

static {

try {

sessionFactory = new AnnotationConfiguration()

.configure()

.buildSessionFactory();

}

catch(Exception e) {

throw new ExceptionInInitializerError(e);

}

}
  public static Session getCurrentSession() {

return sessionFactory.getCurrentSession();

}

}

  HibernateUtil还包含了一些辅助方法,如:

 public static Object query(Class clazz, Serializable id);

public static void createEntity(Object entity);

public static Object queryForObject(String hql, Object[] params);

public static List queryForList(String hql, Object[] params);

  在此不再多述。
实体类User使用JPA注解,代表一个用户:

  @Entity

@Table(name="T_USER")

public class User {

public static final String REGEX_USERNAME = "[a-z0-9][a-z0-9\\-]{1,18}[a-z0-9]";

public static final String REGEX_PASSWORD = "[a-f0-9]{32}";

public static final String REGEX_EMAIL = "([0-9a-zA-Z]([-.\\w]*[0-9a-zA-Z])*@([0-9a-zA-Z][-\\w]*[0-9a-zA-Z]\\.)+[a-zA-Z]{2,9})";
private String username;     // 用户名

private String password;     // MD5口令

private boolean admin;       // 是否是管理员

private String email;        // 电子邮件

private int emailValidation; // 电子邮件验证码

private long createdDate;    // 创建时间

private long lockDate;       // 锁定时间
public User() {}
public User(String username, String password, boolean admin, long lastSignOnDate) {

this.username = username;

this.password = password;

this.admin = admin;

}
@Id

@Column(updatable=false, length=20)

@Pattern(regex=REGEX_USERNAME)

public String getUsername() { return username; }

public void setUsername(String username) { this.username = username; }
@Column(nullable=false, length=32)

@Pattern(regex=REGEX_PASSWORD)

public String getPassword() { return password; }

public void setPassword(String password) { this.password = password; }
@Column(nullable=false, length=50)

@Pattern(regex=REGEX_EMAIL)

public String getEmail() { return email; }

public void setEmail(String email) { this.email = email; }
@Column(nullable=false)

public boolean getAdmin() { return admin; }

public void setAdmin(boolean admin) { this.admin = admin; }
@Column(nullable=false, updatable=false)

public long getCreatedDate() { return createdDate; }

public void setCreatedDate(long createdDate) { this.createdDate = createdDate; }
@Column(nullable=false)

public int getEmailValidation() { return emailValidation; }

public void setEmailValidation(int emailValidation) { this.emailValidation = emailValidation; }
@Column(nullable=false)

public long getLockDate() { return lockDate; }

public void setLockDate(long lockDate) { this.lockDate = lockDate; }
@Transient

public boolean getEmailValidated() { return emailValidation==0; }
@Transient

public boolean getLocked() {

return !admin && lockDate>0 && lockDate>System.currentTimeMillis();

}

}

  实体类PasswordTicket代表一个重置口令的请求:

  @Entity

@Table(name="T_PWDT")

public class PasswordTicket {

private String id;

private User user;

private String ticket;

private long createdDate;
@Id

@Column(nullable=false, updatable=false, length=32)

@GeneratedValue(generator="system-uuid")

@GenericGenerator(name="system-uuid", strategy="uuid")

public String getId() { return id; }

protected void setId(String id) { this.id = id; }
@ManyToOne

@JoinColumn(nullable=false, updatable=false)

public User getUser() { return user; }

public void setUser(User user) { this.user = user; }
@Column(nullable=false, updatable=false, length=32)

public String getTicket() { return ticket; }

public void setTicket(String ticket) { this.ticket = ticket; }
@Column(nullable=false, updatable=false)

public long getCreatedDate() { return createdDate; }

public void setCreatedDate(long createdDate) { this.createdDate = createdDate; }

}

  UserDao接口定义了对用户的相关操作:

 public interface UserDao {

User queryForSignOn(String username);

User queryUser(String username);

void createUser(User user);

void updateUser(User user);

boolean updateEmailValidation(String username, int ticket);

String createPasswordTicket(User user);

boolean updatePassword(String username, String oldPassword, String newPassword);

boolean queryResetPassword(User user, String ticket);

boolean updateResetPassword(User user, String ticket, String password);

void updateLock(User user, long lockTime);

void updateUnlock(User user);

}

  UserDaoImpl是其实现类:

 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);

}

// 其余方法略

...

}

延伸阅读

文章来源于领测软件测试网 https://www.ltesting.net/

42/4<1234>

关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备10010545号-5
技术支持和业务联系:info@testage.com.cn 电话:010-51297073

软件测试 | 领测国际ISTQBISTQB官网TMMiTMMi认证国际软件测试工程师认证领测软件测试网