/*
 * SamplePrepare.java
 *
 * Created on 2007/04/12, 13:51
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package test.org.itscool.commons.dao.preperd2;

import java.util.List;
import org.itscool.commons.dao.PreparedStatementWrapper;
import org.itscool.commons.connection.TransactionUtil;
import org.itscool.weber.testcase.WeberDbTestCase;

/**
 * PreparedStatementのテスト（非ORマッピング）
 */
public class PrepareTest extends WeberDbTestCase{
    public PrepareTest(){
        this.initTables("test/org/itscool/commons/dao/preperd2/test-db-data.xml");
    }
    
    public void testSelect(){
        String sql = "SELECT * FROM COMPANY, CUSTOMER WHERE COMPANY.ID = CUSTOMER.COMPANY_ID";
        PreparedStatementWrapper mapper = TransactionUtil.getPreparedStatementWrapper(sql);
        List result = mapper.select(Company.class);
        if( result.size() == 0 ){
            fail("result count is 0");
        }
        for( int i=0; i<result.size(); i++){
            Company company = (Company)result.get(i);
            
            System.out.println(company.getName());
            List customers = company.getCustomers();
            if( customers.size() == 0 ){
                fail("customers count is 0");
            }else{
                System.out.println("customers count is " + customers.size());
            }
            for( int j=0; j<customers.size(); j++){
                Customer customer = (Customer)customers.get(j);
                System.out.println(" "+customer.getName());
            }
        }
    }
    
    public void testSelect2(){
        String sql = "SELECT * FROM COMPANY, CUSTOMER WHERE COMPANY.ID = CUSTOMER.COMPANY_ID";
        PreparedStatementWrapper mapper = TransactionUtil.getPreparedStatementWrapper(sql);
        List result = mapper.select(Company.class, PreparedStatementWrapper.MAP_KIND_TABLEMANE_IS_CLASS);
        if( result.size() == 0 ){
            fail("result count is 0");
        }
        for( int i=0; i<result.size(); i++){
            Company company = (Company)result.get(i);
            
            System.out.println(company.getName());
            List customers = company.getCustomers();
            if( customers.size() == 0 ){
                fail("customers count is 0");
            }else{
                System.out.println("customers count is " + customers.size());
            }
            for( int j=0; j<customers.size(); j++){
                Customer customer = (Customer)customers.get(j);
                System.out.println(" "+customer.getName());
            }
        }
    }
}
