/* * created on 2005-4-21 */package org.appfuse.model;
import org.apache.commons.lang.builder.equalsbuilder;import org.apache.commons.lang.builder.hashcodebuilder;import org.apache.commons.lang.builder.tostringbuilder;
/** * @hibernate.class table="person" */public class person extends baseobject {
private long id;
private string firstname;
private string lastname;
/** * @return returns the firstname. * @hibernate.property column="first_name" length="50" */ public string getfirstname() { return firstname; }
/** * @param firstname * the firstname to set. */ public void setfirstname(string firstname) { this.firstname = firstname; }
/** * @return returns the id. * @hibernate.id column="id" generator-class="native" unsaved-value="null" */ public long getid() { return id; }
/** * @param id * the id to set. */ public void setid(long id) { this.id = id; }
/** * @return returns the lastname. * @hibernate.property column="last_name" length="50" */ public string getlastname() { return lastname; }
/** * @param lastname * the lastname to set. */ public void setlastname(string lastname) { this.lastname = lastname; }
/** * @see java.lang.object#equals(object) */ public boolean equals(object object) { if (!(object instanceof person)) { return false; } person rhs = (person) object; return new equalsbuilder() .append(this.firstname, rhs.firstname) .append(this.id, rhs.id) .append(this.lastname, rhs.lastname).isequals(); } /** * public boolean equals(object object) { if (!(object instanceof address)) { return false; }
address rhs = (address) object;
return new equalsbuilder().append(this.postalcode, rhs.postalcode) .append(this.country, rhs.country) .append(this.address, rhs.address) .append(this.province, rhs.province) .append(this.city, rhs.city).isequals(); } */ /** * @see java.lang.object#hashcode() */ public int hashcode() { return new hashcodebuilder(-1196181247, -1232855255) .append(this.firstname) .append(this.id) .append(this.lastname).tohashcode(); }
/** * @see java.lang.object#tostring() */ public string tostring() { return new tostringbuilder(this) .append("lastname", this.lastname) .append("id", this.id) .append("firstname", this.firstname).tostring(); }}
##############################################################package org.appfuse.dao;
import org.appfuse.model.person;import org.springframework.dao.dataaccessexception;
public class persondaotest extends basedaotestcase { //一个基本的初始化、销毁persondao对象的junit测试 private person person = null;
private persondao dao = null;
protected void setup() throws exception { super.setup(); dao = (persondao) ctx.getbean("persondao"); }
// "ctx" 对象是对spring的applicationcontext的一个引用,它在basedaotestcases // 类的静态代码块中被初始化。 protected void teardown() throws exception { super.teardown(); dao = null; }
//—————————————————————————————————————————– public void testgetperson() throws exception { person = new person(); person.setfirstname("matt"); person.setlastname("raible");
dao.saveperson(person); assertnotnull(person.getid());
person = dao.getperson(person.getid()); assertequals(person.getfirstname(), "matt"); }
public void testsaveperson() throws exception { person = dao.getperson(new long(1)); person.setfirstname("matt");
person.setlastname("last name updated");
dao.saveperson(person);
if (log.isdebugenabled()) { log.debug("updated person: " + person); }
assertequals(person.getlastname(), "last name updated"); }
public void testaddandremoveperson() throws exception { person = new person(); person.setfirstname("bill"); person.setlastname("joy");
dao.saveperson(person);
assertequals(person.getfirstname(), "bill"); assertnotnull(person.getid());
if (log.isdebugenabled()) { log.debug("removing person…"); }
dao.removeperson(person.getid());
try { person = dao.getperson(person.getid()); fail("person found in database"); } catch (dataaccessexception dae) { log.debug("expected exception: " + dae.getmessage()); assertnotnull(dae); } }
}
