📄 companydaohibernatetest.java
字号:
/**
* Hibernate Demo
* Copyright by cinc
*/
package dao.hibernate;
import junit.framework.TestCase;
import dao.CompanyDAO;
import dao.DAOFactory;
import dao.ProvinceDAO;
import bean.Company;
import bean.Person;
import bean.Province;
import net.sf.hibernate.HibernateException;
import java.util.List;
import java.text.SimpleDateFormat;
import java.text.DateFormat;
import java.text.ParseException;
public class CompanyDAOHibernateTest extends TestCase{
CompanyDAO dao = DAOFactory.getInstance().getCompanyDAO();
ProvinceDAO pdao = DAOFactory.getInstance().getProvinceDAO();
public CompanyDAOHibernateTest(String s) {
super(s);
}
/*
public void testFindByIdNormal(){
try {
Company c = dao.findById( 1 );
assertNotNull( c );
assertEquals( "name", "cinc.org", c.getName() );
Set persons = c.getPersons();
Iterator iterator = persons.iterator();
Person person1 = (Person) iterator.next();
assertEquals( "person1 name", "cinc", person1.getName());
Person person2 = (Person) iterator.next();
assertEquals( "person2 name", "dind", person2.getName());
} catch (HibernateException e) {
fail();
e.printStackTrace();
}
}
public void testFindByIdNotFound(){
try {
Company c = dao.findById( 111 );
fail();
} catch (HibernateException e) {
// success
}
}
*/
/**
* new a company, save to db
*/
public void testAddCompany(){
Company c = new Company();
c.setName( "公司" );
try {
c = dao.addCompany( c );
//System.out.println("c.getId() = " + c.getId());
Company newc = dao.findById( c.getId() );
assertEquals( "name", c.getName(), newc.getName());
dao.removeCompany( c );
} catch (HibernateException e) {
fail();
e.printStackTrace();
}
}
/**
* get a exsisting company, modify name, save to db
*/
public void testModifyCompany(){
Company c1 = new Company();
c1.setName( "company1");
try {
c1 = dao.addCompany( c1 );
Company newc = dao.findById( c1.getId() );
// modify newc's name
newc.setName( "newcompany1" );
newc = dao.updateCompany( newc );
Company dbnewc = dao.findById( newc.getId() );
assertEquals( "name", newc.getName(), dbnewc.getName() );
dao.removeCompany( dbnewc );
} catch (HibernateException e) {
fail();
e.printStackTrace();
}
}
/**
* new a company, add a new person to company, save to db
*/
public void testAddNewCompanyWithNewPerson(){
try {
DateFormat formatter = new SimpleDateFormat("MM-dd-yy");
Province province1 = pdao.findById( 1 );
Person person1 = new Person();
person1.setName( "pname1" );
person1.setProvince( province1 );
person1.setBirthday( formatter.parse("01-21-1966") );
Person person2 = new Person();
person2.setName( "pname2" );
person2.setProvince( province1 );
person2.setBirthday( formatter.parse("02-22-1988") );
Company c1 = new Company();
c1.setName( "companyname" );
c1.addPerson( person1 );
c1.addPerson( person2 );
c1 = dao.addCompany( c1 );
Company newc = dao.findById( c1.getId() );
assertEquals( "company name", c1.getName(), newc.getName());
List newPersons = newc.getPersons();
// person1
Person newPerson1 = (Person) newPersons.get( 0 );
assertEquals( "person count", c1.getPersons().size(), newPersons.size() );
assertEquals( "person1 name", person1.getName(), newPerson1.getName());
assertEquals( "person1 birthday", person1.getBirthday(), newPerson1.getBirthday());
// person1
Person newPerson2 = (Person) newPersons.get( 1 );
assertEquals( "person count", c1.getPersons().size(), newPersons.size() );
assertEquals( "person1 name", person2.getName(), newPerson2.getName());
assertEquals( "person1 birthday", person2.getBirthday(), newPerson2.getBirthday());
dao.removeCompany( c1 );
} catch (HibernateException e) {
fail();
e.printStackTrace();
} catch (ParseException e) {
e.printStackTrace();
}
}
/**
* get a exsisting company, add a new person to company, save to db
*/
public void testAddPersonToExistingCompany(){
DateFormat formatter = new SimpleDateFormat("MM-dd-yy");
try {
Province province1 = pdao.findById( 1 );
Person person1 = new Person();
person1.setName( "pname1" );
person1.setProvince( province1 );
person1.setBirthday( formatter.parse("01-21-1966") );
Company c1 = new Company();
c1.setName( "companyname" );
c1.addPerson( person1 );
c1 = dao.addCompany( c1 );
Company newc = dao.findById( c1.getId() );
Person person2 = new Person();
person2.setName( "pname2" );
person2.setBirthday( formatter.parse("04-20-1977") );
newc.addPerson( person2 );
newc = dao.updateCompany( newc );
Company endc = dao.findById( newc.getId() );
List persons = endc.getPersons();
Person endPerson2 = (Person)persons.get( 1 );
assertEquals( "Person count", newc.getPersons().size(), persons.size() );
assertEquals( "person2 name", person2.getName(), endPerson2.getName() );
dao.removeCompany( endc );
} catch (ParseException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
/**
* get a exsisting company, modify a person's name, save to db
*/
public void testModifyPersonInCompany(){
DateFormat formatter = new SimpleDateFormat("MM-dd-yy");
try {
Province province1 = pdao.findById( 1 );
Province province2 = pdao.findById( 2 );
Person person1 = new Person();
person1.setName( "pname1" );
person1.setProvince( province1 );
person1.setBirthday( formatter.parse("01-21-1966") );
Company c1 = new Company();
c1.setName( "companyname" );
c1.addPerson( person1 );
c1 = dao.addCompany( c1 );
Company newc = dao.findById( c1.getId() );
Person newp = (Person) newc.getPersons().get( 0 );
newp.setName( "pnewname1" );
newp.setProvince( province2 );
newp.setBirthday( formatter.parse("02-22-2002") );
dao.updateCompany( newc );
Company endc = dao.findById( newc.getId() );
Person endPerson1 = (Person) endc.getPersons().get( 0 );
assertEquals( "person name", newp.getName(), endPerson1.getName() );
assertEquals( "person province", newp.getProvince().getName(), endPerson1.getProvince().getName() );
assertEquals( "person birthday", newp.getBirthday(), endPerson1.getBirthday() );
dao.removeCompany( endc );
} catch (ParseException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
/**
* get a exsisting company, delete a person, save to db
*/
public void testRemovePersonFromCompany(){
DateFormat formatter = new SimpleDateFormat("MM-dd-yy");
try {
Province province1 = pdao.findById( 1 );
Person person1 = new Person();
person1.setName( "pname1" );
person1.setProvince( province1 );
person1.setBirthday( formatter.parse("01-21-1966") );
Company c1 = new Company();
c1.setName( "companyname" );
c1.addPerson( person1 );
c1 = dao.addCompany( c1 );
Company newc = dao.findById( c1.getId() );
Person newp = (Person) newc.getPersons().get( 0 );
newc.removePerson( newp );
// You need to manually delete the person from db
DAOFactory.getInstance().getPersonDAO().removePerson( newp );
//System.out.println("newc.getPersons().size() = " + newc.getPersons().size());
dao.updateCompany( newc );
Company endc = dao.findById( newc.getId() );
assertEquals( "person count", newc.getPersons().size(), endc.getPersons().size() );
dao.removeCompany( endc );
} catch (ParseException e) {
e.printStackTrace();
} catch (HibernateException e) {
e.printStackTrace();
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -