/* * 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的一個引用,它在BaseDAOTestCase's // 類的靜態代碼塊中被初始化。 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); } }
}