/*
 * 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.preperd3;

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/preperd3/test-db-data.xml");
    }
    
    public void testSelect(){
        String sql = "SELECT " +
            "    CUSTOMER.NAME AS CUSTOMER_DETAIL_AS_NAME, " +
            "    USER.NAME AS CUSTOMER_DETAIL_AS_USER_NAME " +
            " FROM CUSTOMER, USER " +
            " WHERE CUSTOMER.USER_ID = USER.ID";
        PreparedStatementWrapper mapper = TransactionUtil.getPreparedStatementWrapper(sql);
        List result = mapper.select(CustomerDetail.class, PreparedStatementWrapper.MAP_KIND_ASNAME_IS_CLASS);
        if( result.size() == 0 ){
            fail("result count is 0");
        }
        for( int i=0; i<result.size(); i++){
            CustomerDetail customer = (CustomerDetail)result.get(i);
            System.out.println(" "+customer.getName() +" " +customer.getUserName());
            
        }
    }
    
    public void testSelectNG(){
        String sql = "SELECT * " +
            " FROM CUSTOMER, USER " +
            " WHERE CUSTOMER.USER_ID = USER.ID";
        PreparedStatementWrapper mapper = TransactionUtil.getPreparedStatementWrapper(sql);
        List result = mapper.select(CustomerDetail.class, PreparedStatementWrapper.MAP_KIND_ASNAME_IS_CLASS);
        if( result.size() > 0 ){
            fail("result count is not 0 => " + result.size());
        }
    }
    
    public void testSelect2(){
        String sql = "SELECT * " +
            " FROM CUSTOMER, USER " +
            " WHERE CUSTOMER.USER_ID = USER.ID";
        PreparedStatementWrapper mapper = TransactionUtil.getPreparedStatementWrapper(sql);
        List result = mapper.select(Customer.class, PreparedStatementWrapper.MAP_KIND_ASNAME_IS_CLASS);
        if( result.size() == 0 ){
            fail("result count is 0");
        }
        for( int i=0; i<result.size(); i++){
            Customer customer = (Customer)result.get(i);
            System.out.println(" "+customer.getName() + " " + customer.getUser().getName());
        }
    }
}
