FileDocCategorySizeDatePackage
TestAccountFactory.javaAPI DocExample2375Mon Nov 18 20:09:38 GMT 2002com.oreilly.mock

TestAccountFactory

public class TestAccountFactory extends TestCase

Fields Summary
Constructors Summary
public TestAccountFactory(String name)

        super(name);
    
Methods Summary
public voidtestGetAccount()

        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 voidtestGetAccountType()

        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) {
        }