1 package com.mockobjects.examples.mailinglist; 2 3 import java.sql.SQLException ; 4 import com.mockobjects.sql.*; 5 import com.mockobjects.util.TestCaseMo; 6 7 public class TestMailingList extends TestCaseMo { 8 public TestMailingList(String name) { 9 super(name); 10 } 11 12 public final static String [] COLUMN_NAMES = new String [] {"email_address", "name"}; 13 14 public final static String EMAIL = "fred.bloggs@an.address"; 15 public final static String NAME = "Fred Bloggs"; 16 public final static String [] ONE_ROW = new String [] {EMAIL, NAME}; 17 public final static String [][] TWO_ROWS = 18 new String [][] { ONE_ROW, new String [] {EMAIL + 2, NAME + 2}}; 19 20 21 private MailingList list = new MailingList(); 22 private MockListAction mockListAction = new MockListAction(); 23 private MockConnection mockConnection = new MockConnection(); 24 private MockPreparedStatement mockPreparedStatement = new MockPreparedStatement(); 25 private MockStatement mockStatement = new MockStatement(); 26 27 public void setUp() { 28 mockConnection.setupAddPreparedStatement(mockPreparedStatement); 29 mockConnection.setupStatement(mockStatement); 30 } 31 32 public void testAddNewMember() throws MailingListException, SQLException { 33 setExpectationsForAddMember(); 34 35 list.addMember(mockConnection, EMAIL, NAME); 36 37 verifyJDBC(); 38 } 39 40 public void testAddExistingMember() throws SQLException { 41 mockPreparedStatement.setupThrowExceptionOnExecute( 42 new SQLException ("MockStatment", "Duplicate", 43 DatabaseConstants.UNIQUE_CONSTRAINT_VIOLATED)); 44 45 setExpectationsForAddMember(); 46 47 try { 48 list.addMember(mockConnection, EMAIL, NAME); 49 fail("Should have thrown exception"); 50 } catch (MailingListException ignored) { 51 } 52 verifyJDBC(); 53 } 54 55 public void testPrepareStatementFailsForAdd() throws MailingListException { 56 mockConnection.setupThrowExceptionOnPrepareOrCreate(new SQLException ("MockConnection")); 57 58 mockConnection.addExpectedPreparedStatementString(MailingList.INSERT_SQL); 59 mockPreparedStatement.setExpectedExecuteCalls(0); 60 mockPreparedStatement.setExpectedCloseCalls(0); 61 62 try { 63 list.addMember(mockConnection, EMAIL, NAME); 64 fail("Should have thrown exception"); 65 } catch (SQLException expected) { 66 } 67 verifyJDBC(); 68 } 69 70 public void testRemoveMember() throws MailingListException, SQLException { 71 mockPreparedStatement.setupUpdateCount(1); 72 73 setExpectationsForRemoveMember(); 74 75 list.removeMember(mockConnection, EMAIL); 76 77 verifyJDBC(); 78 } 79 80 public void testRemoveMissingMember() throws SQLException { 81 mockPreparedStatement.setupUpdateCount(0); 82 83 setExpectationsForRemoveMember(); 84 85 try { 86 list.removeMember(mockConnection, EMAIL); 87 fail("Should have thrown exception"); 88 } catch (MailingListException expected) { 89 } 90 91 verifyJDBC(); 92 } 93 94 public void testListNoMembers() throws MailingListException, SQLException { 95 MockMultiRowResultSet mockResultSet = makeMultiRowResultSet(); 96 97 mockResultSet.setExpectedNextCalls(1); 98 mockListAction.setExpectNoMembers(); 99 setExpectationsForListMembers(); 100 101 list.applyToAllMembers(mockConnection, mockListAction); 102 103 verifyJDBC(); 104 mockResultSet.verify(); 105 mockListAction.verify(); 106 } 107 108 public void testListOneMember() throws MailingListException, SQLException { 109 MockSingleRowResultSet mockResultSet = new MockSingleRowResultSet(); 110 mockStatement.setupResultSet(mockResultSet); 111 112 mockResultSet.addExpectedNamedValues(COLUMN_NAMES, ONE_ROW); 113 mockResultSet.setExpectedNextCalls(2); 114 mockListAction.addExpectedMember(EMAIL, NAME); 115 setExpectationsForListMembers(); 116 117 list.applyToAllMembers(mockConnection, mockListAction); 118 119 verifyJDBC(); 120 mockResultSet.verify(); 121 mockListAction.verify(); 122 } 123 124 public void testListTwoMembers() throws MailingListException, SQLException { 125 MockMultiRowResultSet mockResultSet = makeMultiRowResultSet(); 126 mockResultSet.setupRows(TWO_ROWS); 127 128 mockResultSet.setExpectedNextCalls(3); 129 mockListAction.setExpectedMemberCount(2); 130 131 setExpectationsForListMembers(); 132 133 list.applyToAllMembers(mockConnection, mockListAction); 134 135 verifyJDBC(); 136 mockResultSet.verify(); 137 mockListAction.verify(); 138 } 139 140 public void testListmembersFailure() throws SQLException { 141 MockMultiRowResultSet mockResultSet = makeMultiRowResultSet(); 142 mockResultSet.setupRows(TWO_ROWS); 143 mockListAction.setupThrowExceptionOnMember(new MailingListException()); 144 145 mockResultSet.setExpectedNextCalls(1); 146 setExpectationsForListMembers(); 147 148 try { 149 list.applyToAllMembers(mockConnection, mockListAction); 150 fail("Should have thrown exception"); 151 } catch (MailingListException expected) { 152 } 153 mockResultSet.verify(); 154 mockListAction.verify(); 155 } 156 157 public void testListResultSetFailure() throws MailingListException { 158 MockMultiRowResultSet mockResultSet = makeMultiRowResultSet(); 159 mockResultSet.setupRows(TWO_ROWS); 160 mockResultSet.setupThrowExceptionOnGet(new SQLException ("Mock Exception")); 161 162 mockResultSet.setExpectedNextCalls(1); 163 mockListAction.setExpectNoMembers(); 164 setExpectationsForListMembers(); 165 166 try { 167 list.applyToAllMembers(mockConnection, mockListAction); 168 fail("Should have thrown exception"); 169 } catch (SQLException expected) { 170 } 171 mockResultSet.verify(); 172 mockListAction.verify(); 173 } 174 175 public void testListStatementFailure() throws MailingListException { 176 mockStatement.setupThrowExceptionOnExecute(new SQLException ("Mock exception")); 177 mockListAction.setExpectNoMembers(); 178 setExpectationsForListMembers(); 179 180 try { 181 list.applyToAllMembers(mockConnection, mockListAction); 182 fail("Should have thrown exception"); 183 } catch (SQLException expected) { 184 } 185 mockListAction.verify(); 186 } 187 188 public void testListConnectionFailure() throws MailingListException { 189 mockConnection.setupThrowExceptionOnPrepareOrCreate(new SQLException ("Mock Exception")); 190 191 mockConnection.setExpectedCreateStatementCalls(1); 192 mockStatement.setExpectedCloseCalls(0); 193 mockStatement.setExpectedExecuteCalls(0); 194 195 mockListAction.setExpectNoMembers(); 196 197 try { 198 list.applyToAllMembers(mockConnection, mockListAction); 199 fail("Should have thrown exception"); 200 } catch (SQLException expected) { 201 } 202 mockListAction.verify(); 203 } 204 205 private MockMultiRowResultSet makeMultiRowResultSet() { 206 MockMultiRowResultSet mockResultSet = new MockMultiRowResultSet(); 207 mockStatement.setupResultSet(mockResultSet); 208 mockResultSet.setupColumnNames(COLUMN_NAMES); 209 return mockResultSet; 210 } 211 212 private void setExpectationsForListMembers() { 213 mockConnection.setExpectedCreateStatementCalls(1); 214 mockStatement.setExpectedQueryString(MailingList.LIST_SQL); 215 mockStatement.setExpectedCloseCalls(1); 216 mockStatement.setExpectedExecuteCalls(1); 217 } 218 219 private void setExpectationsForAddMember() { 220 setExpectationsForPreparedStatement(MailingList.INSERT_SQL); 221 mockPreparedStatement.addExpectedSetParameters(ONE_ROW); 222 } 223 224 private void setExpectationsForRemoveMember() { 225 setExpectationsForPreparedStatement(MailingList.DELETE_SQL); 226 mockPreparedStatement.addExpectedSetParameters(new Object [] {EMAIL}); 227 } 228 229 private void setExpectationsForPreparedStatement(String sqlStatement) { 230 mockConnection.addExpectedPreparedStatementString(sqlStatement); 231 mockPreparedStatement.setExpectedExecuteCalls(1); 232 mockPreparedStatement.setExpectedCloseCalls(1); 233 } 234 235 private void verifyJDBC() { 236 mockConnection.verify(); 237 mockStatement.verify(); 238 mockPreparedStatement.verify(); 239 } 240 } 241 | Popular Tags |