KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > jdbc > object > BatchSqlUpdateTests


1 /*
2  * Copyright 2002-2005 the original author or authors.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */

16
17 package org.springframework.jdbc.object;
18
19 import java.sql.DatabaseMetaData JavaDoc;
20 import java.sql.PreparedStatement JavaDoc;
21 import java.sql.Types JavaDoc;
22
23 import org.easymock.MockControl;
24
25 import org.springframework.jdbc.AbstractJdbcTests;
26 import org.springframework.jdbc.core.SqlParameter;
27
28 /**
29  * @author Juergen Hoeller
30  * @since 22.02.2005
31  */

32 public class BatchSqlUpdateTests extends AbstractJdbcTests {
33
34     public void testBatchUpdateWithExplicitFlush() throws Exception JavaDoc {
35         doTestBatchUpdate(false);
36     }
37
38     public void testBatchUpdateWithFlushThroughBatchSize() throws Exception JavaDoc {
39         doTestBatchUpdate(true);
40     }
41
42     private void doTestBatchUpdate(boolean flushThroughBatchSize) throws Exception JavaDoc {
43         final String JavaDoc sql = "UPDATE NOSUCHTABLE SET DATE_DISPATCHED = SYSDATE WHERE ID = ?";
44         final int[] ids = new int[] { 100, 200 };
45         final int[] rowsAffected = new int[] { 1, 2 };
46
47         MockControl ctrlPreparedStatement = MockControl.createControl(PreparedStatement JavaDoc.class);
48         PreparedStatement JavaDoc mockPreparedStatement = (PreparedStatement JavaDoc) ctrlPreparedStatement.getMock();
49         mockPreparedStatement.getConnection();
50         ctrlPreparedStatement.setReturnValue(mockConnection);
51         mockPreparedStatement.setObject(1, new Integer JavaDoc(ids[0]), Types.INTEGER);
52         ctrlPreparedStatement.setVoidCallable();
53         mockPreparedStatement.addBatch();
54         ctrlPreparedStatement.setVoidCallable();
55         mockPreparedStatement.setObject(1, new Integer JavaDoc(ids[1]), Types.INTEGER);
56         ctrlPreparedStatement.setVoidCallable();
57         mockPreparedStatement.addBatch();
58         ctrlPreparedStatement.setVoidCallable();
59         mockPreparedStatement.executeBatch();
60         ctrlPreparedStatement.setReturnValue(rowsAffected);
61         mockPreparedStatement.getWarnings();
62         ctrlPreparedStatement.setReturnValue(null);
63         mockPreparedStatement.close();
64         ctrlPreparedStatement.setVoidCallable();
65
66         MockControl ctrlDatabaseMetaData = MockControl.createControl(DatabaseMetaData JavaDoc.class);
67         DatabaseMetaData JavaDoc mockDatabaseMetaData = (DatabaseMetaData JavaDoc) ctrlDatabaseMetaData.getMock();
68         mockDatabaseMetaData.supportsBatchUpdates();
69         ctrlDatabaseMetaData.setReturnValue(true);
70
71         mockConnection.prepareStatement(sql);
72         ctrlConnection.setReturnValue(mockPreparedStatement);
73         mockConnection.getMetaData();
74         ctrlConnection.setReturnValue(mockDatabaseMetaData, 1);
75
76         ctrlPreparedStatement.replay();
77         ctrlDatabaseMetaData.replay();
78         replay();
79
80         BatchSqlUpdate update = new BatchSqlUpdate(mockDataSource, sql);
81         update.declareParameter(new SqlParameter(Types.INTEGER));
82         if (flushThroughBatchSize) {
83             update.setBatchSize(2);
84         }
85
86         update.update(ids[0]);
87         update.update(ids[1]);
88
89         if (flushThroughBatchSize) {
90             assertEquals(0, update.getQueueCount());
91             assertEquals(2, update.getRowsAffected().length);
92         }
93         else {
94             assertEquals(2, update.getQueueCount());
95             assertEquals(0, update.getRowsAffected().length);
96         }
97
98         int[] actualRowsAffected = update.flush();
99         assertEquals(0, update.getQueueCount());
100
101         if (flushThroughBatchSize) {
102             assertTrue("flush did not execute updates", actualRowsAffected.length == 0);
103         }
104         else {
105             assertTrue("executed 2 updates", actualRowsAffected.length == 2);
106             assertEquals(rowsAffected[0], actualRowsAffected[0]);
107             assertEquals(rowsAffected[1], actualRowsAffected[1]);
108         }
109
110         actualRowsAffected = update.getRowsAffected();
111         assertTrue("executed 2 updates", actualRowsAffected.length == 2);
112         assertEquals(rowsAffected[0], actualRowsAffected[0]);
113         assertEquals(rowsAffected[1], actualRowsAffected[1]);
114
115         update.reset();
116         assertEquals(0, update.getRowsAffected().length);
117
118         ctrlPreparedStatement.verify();
119         ctrlDatabaseMetaData.verify();
120     }
121
122 }
123
Popular Tags