package org.jfox.dao;

import java.util.Collection;
import java.util.Random;

import junit.framework.TestCase;

/**
 * @author <a href="mailto:yangyong@ufsoft.com.cn">Young Yang</a>
 */

public class EmployeeTest extends TestCase {
    private static int count = new Random().nextInt();
    EmployeeDAO dao = new EmployeeDAO();
    Employee employee;

    protected void setUp() throws Exception {
        employee = new Employee();
        employee.setName("employee " + count++);
    }

    protected void tearDown() throws Exception {
        super.tearDown();
    }

    public void testInsert() throws Exception {
        int res = dao.insert(employee);
        assertEquals(res, 1);
    }

    public void testInsertBatch() throws Exception {
        int size = 10;
        Employee[] employees = new Employee[size];
        for(int i=0; i<10; i++){
            Employee employee = new Employee();
            employee.setName("employee " + count++);
            employees[i] = employee;
        }
        int[] res = dao.insertBatch(employees);
        assertEquals(res.length,size);
    }

    public void testUpdate() throws Exception {
        employee = new Employee();
        employee.setId(40);
        employee.setName("young");
        int res = dao.update(employee);
        assertEquals(res, 1);
    }

    public void testUpdateByPK() throws Exception {
        Integer pk = new Integer(40);
        employee = new Employee();
        employee.setId(20);
        employee.setName("young yang");
        int res = dao.updateByPrimaryKey(employee,pk);
        assertEquals(res, 1);
    }


    public void testUpdateBatch() throws Exception {
        int size = 10;
        int startId = 41;
        Employee[] employees = new Employee[size];
        for(int i=0; i<10; i++){
            Employee employee = new Employee();
            employee.setId(startId++);
            employee.setName("young");
            employees[i] = employee;
        }
        int[] res = dao.updateBatch(employees);
        assertEquals(res.length,size);
    }

    public void testDelete() throws Exception {
        employee = new Employee();
        employee.setId(20);
        employee.setName("young");
        int res = dao.delete(employee);
        assertEquals(res, 1);
    }

    public void testDeleteByPK() throws Exception {
        Integer pk = new Integer(41);
        int res = dao.deleteByPrimaryKey(Employee.class,pk);
        assertEquals(res, 1);
    }

    public void testDeleteBatch() throws Exception {
        int size = 10;
        int startId = 41;
        Employee[] employees = new Employee[size];
        for(int i=0; i<10; i++){
            Employee employee = new Employee();
            employee.setId(startId++);
            employee.setName("young");
            employees[i] = employee;
        }
        int[] res = dao.deleteBatch(employees);
        assertEquals(res.length,size);
    }

    public void testDeleteBatchByPrimaryKeys() throws Exception {
        int size = 10;
        int startId = 41;
        Integer[] pks = new Integer[size];
        for(int i=0; i<10; i++){
            pks[i] = new Integer(startId++);
        }
        int[] res = dao.deleteBatchByPrimaryKeys(Employee.class, pks);
        assertEquals(res.length,size);
    }

    public void testFetch() throws Exception {
        Employee employee = (Employee)dao.fetch(Employee.class,"select * from employee where id=1");
        System.out.println("name: " + employee.getName());
        assertEquals(employee.getId(),1);
    }

    public void testFetchByPK() throws Exception {
        Employee employee = (Employee)dao.fetchByPrimaryKey(Employee.class,new Integer(1));
        System.out.println("name: " + employee.getName());
        assertEquals(employee.getId(),1);
    }

    public void testFetchBatch() throws Exception {
        Collection employees = dao.fetchBatch(Employee.class,"select * from employee");
        assertTrue(employees.size()>1);
    }

    public void testFetchBatchByPrimaryKeys() throws Exception {
        Collection employees = dao.fetchBatchByPrimaryKeys(Employee.class,new Integer[]{new Integer(1),new Integer(2)});
        assertTrue(employees.size()>1);
    }

    public static void main(String[] args) {

    }
}

