KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > mockobjects > examples > mailinglist > TestMailingList


1 package com.mockobjects.examples.mailinglist;
2
3 import java.sql.SQLException JavaDoc;
4 import com.mockobjects.sql.*;
5 import com.mockobjects.util.TestCaseMo;
6
7 public class TestMailingList extends TestCaseMo {
8     public TestMailingList(String JavaDoc name) {
9         super(name);
10     }
11
12     public final static String JavaDoc[] COLUMN_NAMES = new String JavaDoc[] {"email_address", "name"};
13
14     public final static String JavaDoc EMAIL = "fred.bloggs@an.address";
15     public final static String JavaDoc NAME = "Fred Bloggs";
16     public final static String JavaDoc[] ONE_ROW = new String JavaDoc[] {EMAIL, NAME};
17     public final static String JavaDoc[][] TWO_ROWS =
18                new String JavaDoc[][] { ONE_ROW, new String JavaDoc[] {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 JavaDoc {
33         setExpectationsForAddMember();
34
35         list.addMember(mockConnection, EMAIL, NAME);
36
37         verifyJDBC();
38     }
39
40     public void testAddExistingMember() throws SQLException JavaDoc {
41         mockPreparedStatement.setupThrowExceptionOnExecute(
42                 new SQLException JavaDoc("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 JavaDoc("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 JavaDoc expected) {
66         }
67         verifyJDBC();
68     }
69
70     public void testRemoveMember() throws MailingListException, SQLException JavaDoc {
71         mockPreparedStatement.setupUpdateCount(1);
72
73         setExpectationsForRemoveMember();
74
75         list.removeMember(mockConnection, EMAIL);
76
77         verifyJDBC();
78     }
79
80     public void testRemoveMissingMember() throws SQLException JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc {
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 JavaDoc("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 JavaDoc expected) {
170         }
171         mockResultSet.verify();
172         mockListAction.verify();
173     }
174
175     public void testListStatementFailure() throws MailingListException {
176         mockStatement.setupThrowExceptionOnExecute(new SQLException JavaDoc("Mock exception"));
177         mockListAction.setExpectNoMembers();
178         setExpectationsForListMembers();
179
180         try {
181             list.applyToAllMembers(mockConnection, mockListAction);
182             fail("Should have thrown exception");
183         } catch (SQLException JavaDoc expected) {
184         }
185         mockListAction.verify();
186     }
187
188     public void testListConnectionFailure() throws MailingListException {
189         mockConnection.setupThrowExceptionOnPrepareOrCreate(new SQLException JavaDoc("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 JavaDoc 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 JavaDoc[] {EMAIL});
227     }
228
229     private void setExpectationsForPreparedStatement(String JavaDoc 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