oa_01+

package com.bjsxt.oa.manager.impl;

import java.util.List;

import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.bjsxt.oa.manager.OrgManager;
import com.bjsxt.oa.model.Organization;

public class OrgManagerImpl extends HibernateDaoSupport implements OrgManager {
	
	public void addOrg(Organization org, int parentId) {
		if(parentId != 0){
			org.setParent(findOrg(parentId));
		}
		getHibernateTemplate().save(org);
		
		//自动生成机构编号
		org.setSn(
			(org.getParent() == null ? "" : org.getParent().getSn() + "_")
			+ org.getId()
		);
		
		getHibernateTemplate().update(org);
	}

	public void delOrg(int orgId) {
		Organization org = findOrg(orgId);
		
		//判断子机构列表是否为空
		if(org.getChildren().size() > 0){
			throw new RuntimeException("存在子机构信息,不允许删除");
		}
		
		//判断人员是否非空
		String hql = "select count(*) from Person p where p.org.id = ?";
		Long personSize = (Long)getSession().createQuery(hql).setParameter(0, orgId).uniqueResult();
		if(personSize > 0){
			throw new RuntimeException("机构下面有人员信息,不允许删除");
		}
		
		getHibernateTemplate().delete(org);
	}

	public Organization findOrg(int orgId) {
		return (Organization)getHibernateTemplate().load(Organization.class, orgId);
	}

	public List searchOrgs(int parentId) {
		
		if(parentId == 0){
			return getHibernateTemplate().find("select o from Organization o where o.parent is null");
		}
		
		String hql = "select o from Organization o where o.parent.id = ?";
		
		return getHibernateTemplate().find(hql, parentId);
	}

	public void updateOrg(Organization org, int parentId) {
		if(parentId != 0){
			org.setParent(findOrg(parentId));
		}
		getHibernateTemplate().update(org);
		
	}

}



package com.bjsxt.oa.manager;

import java.util.Iterator;
import java.util.List;

import junit.framework.TestCase;

import org.springframework.beans.factory.BeanFactory;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.bjsxt.oa.model.Organization;

public class OrgManagerTest extends TestCase {

	private static BeanFactory factory = new ClassPathXmlApplicationContext("applicationContext-*.xml");
	
	public void testAddOrg() {
		OrgManager om = (OrgManager)factory.getBean("orgManager");
		
//		Organization org = new Organization();
//		org.setName("测试机构");
//		org.setDescription("描述");
//		om.addOrg(org, 0);
		
		for(int i=0; i<5; i++){
			Organization org = new Organization();
			org.setName("父机构"+i);
			om.addOrg(org, 0);
			
			for(int j=0; j<10; j++){
				Organization c = new Organization();
				c.setName("["+org.getName()+"]下面的子机构"+j);
				om.addOrg(c, org.getId());
			}
		}
		
		
	}

	public void testDelOrg() {
		fail("Not yet implemented");
	}

	public void testUpdateOrg() {
		fail("Not yet implemented");
	}

	public void testFindOrg() {
		OrgManager om = (OrgManager)factory.getBean("orgManager");
		
		Organization org = om.findOrg(5);
		
		System.out.println(org.getName());
		
	}

	public void testSearchOrgs() {
		OrgManager om = (OrgManager)factory.getBean("orgManager");
		
		List orgs = om.searchOrgs(0);
		for (Iterator iterator = orgs.iterator(); iterator.hasNext();) {
			Organization org = (Organization) iterator.next();
			System.out.println(org.getName());
		}
		
	}

}



package com.bjsxt.oa.manager;

import junit.framework.TestCase;

public class JUnitTest extends TestCase {

	@Override
	protected void setUp() throws Exception {
		System.out.println("做一些前提条件的设置");
	}

	@Override
	protected void tearDown() throws Exception {
		System.out.println("释放一些资源");
	}
	
	public void testSomething(){
		System.out.println("执行测试单元testSomething");
	}
	
	public void testSomething2(){
		System.out.println("执行测试单元testSomething2");
	}

}

你可能感兴趣的:(C++,c,C#,JUnit,单元测试)