package com.oreilly.mock;
import com.mockobjects.sql.*;
import junit.framework.TestCase;
import java.sql.SQLException;
public class TestAccountFactory extends TestCase {
public TestAccountFactory(String name) {
super(name);
}
public void testGetAccount() throws Exception {
class MyMockResultSet extends MockResultSetJdk14 {
public Object getObject(int columnIndex) throws SQLException {
return null;
}
public Object getObject(String columnName) throws SQLException {
if ("acctType".equals(columnName)) {
return "CH";
}
if ("balance".equals(columnName)) {
return new Double(100.0);
}
return null;
}
public boolean next() throws SQLException {
super.myNextCalls.inc();
return true;
}
public int getRow() throws SQLException {
return 1;
}
}
MockResultSet mockRs = new MyMockResultSet();
mockRs.setExpectedCloseCalls(1);
mockRs.setExpectedNextCalls(1);
MockPreparedStatement mockPs = new MockPreparedStatement();
mockPs.addExpectedSetParameter(1, "0001");
mockPs.setExpectedCloseCalls(1);
mockPs.addResultSet(mockRs);
MockConnection mockConnection = new MockConnection();
mockConnection.setupAddPreparedStatement(mockPs);
mockConnection.setExpectedCloseCalls(0);
AccountFactory acctFact = new AccountFactory();
// call the method that we are actually testing
Account acct = acctFact.getAccount("0001", mockConnection);
mockRs.verify();
mockPs.verify();
mockConnection.verify();
}
public void testGetAccountType() throws Exception {
AccountFactory acctFact = new AccountFactory();
assertEquals("account type", Account.CHECKING, acctFact.getAccountType("CH"));
assertEquals("account type", Account.SAVINGS, acctFact.getAccountType("SA"));
try {
acctFact.getAccountType("bogus");
fail("Expected DataSourceException");
} catch (DataSourceException expected) {
}
}
}
|