Search in sources :

Example 1 with SalesOrder

use of com.alibaba.cobar.manager.jdbcmock.domain.SalesOrder in project cobar by alibaba.

the class JdbcTest method test.

public void test() {
    IMocksControl control = EasyMock.createControl();
    DBUtility mockDBUtility = control.createMock(DBUtility.class);
    Connection mockConnection = control.createMock(Connection.class);
    Statement mockStatement = control.createMock(Statement.class);
    ResultSet mockResultSet = control.createMock(ResultSet.class);
    try {
        mockDBUtility.getConnection();
        EasyMock.expectLastCall().andStubReturn(mockConnection);
        mockConnection.createStatement();
        EasyMock.expectLastCall().andStubReturn(mockStatement);
        mockStatement.executeQuery(SQLEquals.sqlEquals("SELECT * FROM sales_order_table"));
        EasyMock.expectLastCall().andStubReturn(mockResultSet);
        mockResultSet.next();
        EasyMock.expectLastCall().andReturn(true).times(3);
        EasyMock.expectLastCall().andReturn(false).times(1);
        mockResultSet.getString(1);
        EasyMock.expectLastCall().andReturn("DEMO_ORDER_001").times(1);
        EasyMock.expectLastCall().andReturn("DEMO_ORDER_002").times(1);
        EasyMock.expectLastCall().andReturn("DEMO_ORDER_003").times(1);
        mockResultSet.getString(2);
        EasyMock.expectLastCall().andReturn("Asia Pacific").times(1);
        EasyMock.expectLastCall().andReturn("Europe").times(1);
        EasyMock.expectLastCall().andReturn("America").times(1);
        mockResultSet.getDouble(3);
        EasyMock.expectLastCall().andReturn(350.0).times(1);
        EasyMock.expectLastCall().andReturn(1350.0).times(1);
        EasyMock.expectLastCall().andReturn(5350.0).times(1);
        control.replay();
        Connection conn = mockDBUtility.getConnection();
        Statement stat = conn.createStatement();
        ResultSet rs = stat.executeQuery("select * from sales_order_table");
        int i = 0;
        String[] priceLevels = { "Level_A", "Level_C", "Level_E" };
        while (rs.next()) {
            SalesOrder order = new SalesOrderImpl();
            order.loadDataFromDB(rs);
            assertEquals(order.getPriceLevel(), priceLevels[i]);
            i++;
        }
        control.verify();
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : IMocksControl(org.easymock.IMocksControl) Statement(java.sql.Statement) DBUtility(com.alibaba.cobar.manager.jdbcmock.domain.DBUtility) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) SalesOrderImpl(com.alibaba.cobar.manager.jdbcmock.domain.SalesOrderImpl) SalesOrder(com.alibaba.cobar.manager.jdbcmock.domain.SalesOrder)

Aggregations

DBUtility (com.alibaba.cobar.manager.jdbcmock.domain.DBUtility)1 SalesOrder (com.alibaba.cobar.manager.jdbcmock.domain.SalesOrder)1 SalesOrderImpl (com.alibaba.cobar.manager.jdbcmock.domain.SalesOrderImpl)1 Connection (java.sql.Connection)1 ResultSet (java.sql.ResultSet)1 Statement (java.sql.Statement)1 IMocksControl (org.easymock.IMocksControl)1