001: package com.mockrunner.test.jdbc;
002:
003: import java.sql.BatchUpdateException;
004: import java.sql.SQLException;
005:
006: import com.mockrunner.base.BaseTestCase;
007: import com.mockrunner.jdbc.StatementResultSetHandler;
008: import com.mockrunner.mock.jdbc.MockConnection;
009: import com.mockrunner.mock.jdbc.MockResultSet;
010:
011: public class AbstractResultSetHandlerTest extends BaseTestCase {
012: private MockConnection connection;
013: private StatementResultSetHandler statementHandler;
014:
015: protected void setUp() throws Exception {
016: super .setUp();
017: connection = getJDBCMockObjectFactory().getMockConnection();
018: statementHandler = connection.getStatementResultSetHandler();
019: }
020:
021: public void testGetResultSet() {
022: MockResultSet result = new MockResultSet("id");
023: statementHandler.prepareResultSet("select column from table",
024: result);
025: statementHandler.prepareResultSet("select .*", result);
026: assertSame(result, statementHandler
027: .getResultSet("select column from table"));
028: assertNull(statementHandler
029: .getResultSet("select xyz from table"));
030: statementHandler.setUseRegularExpressions(true);
031: assertSame(result, statementHandler
032: .getResultSet("select column from table"));
033: assertSame(result, statementHandler
034: .getResultSet("SELECT xyz from table"));
035: statementHandler.setCaseSensitive(true);
036: assertNull(statementHandler
037: .getResultSet("SELECT xyz from table"));
038: MockResultSet result0 = new MockResultSet("id0");
039: MockResultSet result1 = new MockResultSet("id1");
040: MockResultSet[] results = new MockResultSet[] { result0,
041: result1 };
042: statementHandler.clearResultSets();
043: statementHandler.prepareResultSets("select abc", results);
044: assertSame(result0, statementHandler.getResultSet("select abc"));
045: }
046:
047: public void testGetResultSets() {
048: MockResultSet result0 = new MockResultSet("id0");
049: MockResultSet result1 = new MockResultSet("id1");
050: MockResultSet[] results = new MockResultSet[] { result0,
051: result1 };
052: statementHandler.prepareResultSets("select column from table",
053: results);
054: statementHandler.prepareResultSet("select .*", result0);
055: MockResultSet[] returnedResults = statementHandler
056: .getResultSets("select column from table");
057: assertNotSame(returnedResults, results);
058: assertEquals(2, returnedResults.length);
059: assertSame(result0, returnedResults[0]);
060: assertSame(result1, returnedResults[1]);
061: assertNull(statementHandler
062: .getResultSet("select xyz from table"));
063: returnedResults = statementHandler.getResultSets("select .*");
064: assertEquals(1, returnedResults.length);
065: assertSame(result0, returnedResults[0]);
066: }
067:
068: public void testHasMultipleResultSets() {
069: MockResultSet result0 = new MockResultSet("id0");
070: MockResultSet result1 = new MockResultSet("id1");
071: statementHandler.prepareResultSets("select column from table",
072: new MockResultSet[] { result0, result1 });
073: statementHandler.prepareResultSets("select column from abc",
074: new MockResultSet[] { result1 });
075: statementHandler.prepareResultSet("select .*", result0);
076: assertTrue(statementHandler
077: .hasMultipleResultSets("select column from table"));
078: assertTrue(statementHandler
079: .hasMultipleResultSets("select column from abc"));
080: assertFalse(statementHandler.hasMultipleResultSets("select .*"));
081: assertFalse(statementHandler
082: .hasMultipleResultSets("do nothing"));
083: }
084:
085: public void testGetGlobalResultSet() {
086: MockResultSet result0 = new MockResultSet("id0");
087: MockResultSet result1 = new MockResultSet("id1");
088: MockResultSet[] results = new MockResultSet[] { result0,
089: result1 };
090: statementHandler.prepareGlobalResultSet(result0);
091: assertSame(result0, statementHandler.getGlobalResultSet());
092: MockResultSet[] returnedResults = statementHandler
093: .getGlobalResultSets();
094: assertEquals(1, returnedResults.length);
095: assertSame(result0, returnedResults[0]);
096: statementHandler.prepareGlobalResultSets(results);
097: assertSame(result0, statementHandler.getGlobalResultSet());
098: returnedResults = statementHandler.getGlobalResultSets();
099: assertNotSame(results, returnedResults);
100: assertEquals(2, returnedResults.length);
101: assertSame(result0, returnedResults[0]);
102: assertSame(result1, returnedResults[1]);
103: }
104:
105: public void testHasMutipleGlobalResultSets() {
106: MockResultSet result0 = new MockResultSet("id0");
107: MockResultSet result1 = new MockResultSet("id1");
108: statementHandler.prepareGlobalResultSet(result0);
109: assertFalse(statementHandler.hasMultipleGlobalResultSets());
110: statementHandler
111: .prepareGlobalResultSets(new MockResultSet[] { result1 });
112: assertTrue(statementHandler.hasMultipleGlobalResultSets());
113: statementHandler.prepareGlobalResultSets(new MockResultSet[] {
114: result0, result1 });
115: assertTrue(statementHandler.hasMultipleGlobalResultSets());
116: }
117:
118: public void testGetGeneratedKeys() {
119: MockResultSet result = new MockResultSet("id");
120: statementHandler.prepareGeneratedKeys("insert into table",
121: result);
122: statementHandler.prepareGeneratedKeys("insert .*", result);
123: assertSame(result, statementHandler
124: .getGeneratedKeys("insert into table abc"));
125: assertSame(result, statementHandler
126: .getGeneratedKeys("insert .*"));
127: assertNull(statementHandler
128: .getGeneratedKeys("insert into othertable"));
129: statementHandler.setUseRegularExpressions(true);
130: assertSame(result, statementHandler
131: .getGeneratedKeys("insert into table"));
132: assertSame(result, statementHandler
133: .getGeneratedKeys("INSERt regular"));
134: statementHandler.setCaseSensitive(true);
135: assertNull(statementHandler.getGeneratedKeys("INSERt regular"));
136: }
137:
138: public void testGetUpdateCount() {
139: statementHandler.prepareUpdateCount(".*", 3);
140: statementHandler.prepareUpdateCount("insert xyz", 4);
141: assertEquals(new Integer(4), statementHandler
142: .getUpdateCount("insert xyz"));
143: assertEquals(new Integer(3), statementHandler
144: .getUpdateCount(".*"));
145: statementHandler.setUseRegularExpressions(true);
146: assertEquals(new Integer(3), statementHandler
147: .getUpdateCount("insert xyz"));
148: assertEquals(new Integer(3), statementHandler
149: .getUpdateCount("insert"));
150: statementHandler.setExactMatch(true);
151: assertEquals(new Integer(4), statementHandler
152: .getUpdateCount("insert Xyz"));
153: assertNull(statementHandler.getUpdateCount("insert"));
154: statementHandler.prepareUpdateCounts("update",
155: new int[] { 1, 2 });
156: assertEquals(new Integer(1), statementHandler
157: .getUpdateCount("update"));
158: }
159:
160: public void testGetUpdateCounts() {
161: int[] updateCounts = new int[] { 1, 2 };
162: statementHandler.prepareUpdateCounts(".*", updateCounts);
163: statementHandler.prepareUpdateCount("insert xyz", 4);
164: Integer[] returnedUpdateCounts = statementHandler
165: .getUpdateCounts(".*");
166: assertEquals(2, returnedUpdateCounts.length);
167: assertEquals(new Integer(1), returnedUpdateCounts[0]);
168: assertEquals(new Integer(2), returnedUpdateCounts[1]);
169: returnedUpdateCounts = statementHandler
170: .getUpdateCounts("insert xyz");
171: assertEquals(1, returnedUpdateCounts.length);
172: assertEquals(new Integer(4), returnedUpdateCounts[0]);
173: assertNull(statementHandler.getUpdateCounts("do nothing"));
174: }
175:
176: public void testHasMultipleUpdateCounts() {
177: statementHandler.prepareUpdateCount("update", 3);
178: statementHandler.prepareUpdateCounts("insert into xyz",
179: new int[] { 4 });
180: statementHandler.prepareUpdateCounts("insert into abc",
181: new int[] { 1, 2, 4 });
182: assertFalse(statementHandler.hasMultipleUpdateCounts("update"));
183: assertFalse(statementHandler
184: .hasMultipleUpdateCounts("do nothing"));
185: assertTrue(statementHandler
186: .hasMultipleUpdateCounts("insert into xyz"));
187: assertTrue(statementHandler
188: .hasMultipleUpdateCounts("insert into abc"));
189: }
190:
191: public void testGetGlobalUpdateCount() {
192: int[] updateCounts = new int[] { 1, 2 };
193: statementHandler.prepareGlobalUpdateCount(4);
194: assertEquals(4, statementHandler.getGlobalUpdateCount());
195: int[] returnedUpdateCounts = statementHandler
196: .getGlobalUpdateCounts();
197: assertEquals(1, returnedUpdateCounts.length);
198: assertEquals(4, returnedUpdateCounts[0]);
199: statementHandler.prepareGlobalUpdateCounts(updateCounts);
200: assertEquals(1, statementHandler.getGlobalUpdateCount());
201: returnedUpdateCounts = statementHandler.getGlobalUpdateCounts();
202: assertNotSame(updateCounts, returnedUpdateCounts);
203: assertEquals(2, returnedUpdateCounts.length);
204: assertEquals(1, returnedUpdateCounts[0]);
205: assertEquals(2, returnedUpdateCounts[1]);
206: }
207:
208: public void testHasMutipleGlobalUpdateCounts() {
209: statementHandler.prepareGlobalUpdateCount(4);
210: assertFalse(statementHandler.hasMultipleGlobalUpdateCounts());
211: statementHandler.prepareGlobalUpdateCounts(new int[] { 1 });
212: assertTrue(statementHandler.hasMultipleGlobalUpdateCounts());
213: statementHandler.prepareGlobalUpdateCounts(new int[] { 7, 8 });
214: assertTrue(statementHandler.hasMultipleGlobalUpdateCounts());
215: }
216:
217: public void testGetReturnsResultSet() {
218: statementHandler.prepareReturnsResultSet("select.*", false);
219: assertNull(statementHandler.getReturnsResultSet("select abc"));
220: statementHandler.setUseRegularExpressions(true);
221: assertEquals(Boolean.FALSE, statementHandler
222: .getReturnsResultSet("select abc"));
223: }
224:
225: public void testGetThrowsSQLException() {
226: SQLException exc = new BatchUpdateException();
227: statementHandler.prepareThrowsSQLException("[abc] statement",
228: exc);
229: statementHandler
230: .prepareThrowsSQLException("[abc] statementxyz");
231: assertFalse(statementHandler
232: .getThrowsSQLException("a stAtement"));
233: assertNull(statementHandler.getSQLException("a stAtement"));
234: statementHandler.setUseRegularExpressions(true);
235: assertTrue(statementHandler
236: .getThrowsSQLException("a stAtement"));
237: assertSame(exc, statementHandler.getSQLException("a stAtement"));
238: statementHandler.setCaseSensitive(true);
239: assertFalse(statementHandler
240: .getThrowsSQLException("a stAtement"));
241: assertNull(statementHandler.getSQLException("a stAtement"));
242: assertTrue(statementHandler
243: .getThrowsSQLException("b statement"));
244: assertSame(exc, statementHandler.getSQLException("b statement"));
245: assertTrue(statementHandler
246: .getThrowsSQLException("b statementxyz"));
247: assertNotSame(exc, statementHandler
248: .getSQLException("b statementxyz"));
249: String message = statementHandler.getSQLException(
250: "b statementxyz").getMessage();
251: assertTrue(message.indexOf("[abc] statementxyz") != -1);
252: }
253:
254: public void testClearMethods() {
255: statementHandler.prepareResultSet("select", new MockResultSet(
256: "id"));
257: statementHandler.prepareUpdateCount("select", 3);
258: statementHandler.prepareThrowsSQLException("select");
259: statementHandler.prepareReturnsResultSet("select", true);
260: statementHandler.prepareGeneratedKeys("select",
261: new MockResultSet("id"));
262: statementHandler.clearResultSets();
263: statementHandler.clearUpdateCounts();
264: statementHandler.clearThrowsSQLException();
265: statementHandler.clearReturnsResultSet();
266: statementHandler.clearGeneratedKeys();
267: assertNull(statementHandler.getResultSet("select"));
268: assertNull(statementHandler.getUpdateCount("select"));
269: assertFalse(statementHandler.getThrowsSQLException("select"));
270: assertNull(statementHandler.getReturnsResultSet("select"));
271: assertNull(statementHandler.getGeneratedKeys("select"));
272: }
273:
274: public void testPreparedSQLOrdered() {
275: MockResultSet result1 = new MockResultSet("id1");
276: MockResultSet result2 = new MockResultSet("id2");
277: statementHandler.prepareResultSet("select", result1);
278: statementHandler.prepareResultSet("SelecT", result2);
279: statementHandler.prepareUpdateCount("SelecT", 3);
280: statementHandler.prepareUpdateCount("select2", 2);
281: statementHandler.prepareReturnsResultSet("select", false);
282: statementHandler.prepareReturnsResultSet("selecT", true);
283: statementHandler.prepareGeneratedKeys("seLECT", result1);
284: statementHandler.prepareGeneratedKeys("select", result2);
285: assertSame(result2, statementHandler.getResultSet("select"));
286: assertSame(result2, statementHandler.getResultSets("select")[0]);
287: assertEquals(new Integer(3), statementHandler
288: .getUpdateCount("SELECT"));
289: assertEquals(new Integer(3), statementHandler
290: .getUpdateCounts("selecT")[0]);
291: assertTrue(statementHandler.getReturnsResultSet("select")
292: .booleanValue());
293: assertSame(result1, statementHandler.getGeneratedKeys("select"));
294: }
295: }
|