KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > jdbcapi > UpdateXXXTest


1 /*
2  *
3  * Derby - Class UpdateXXXTest
4  *
5  * Licensed to the Apache Software Foundation (ASF) under one or more
6  * contributor license agreements. See the NOTICE file distributed with
7  * this work for additional information regarding copyright ownership.
8  * The ASF licenses this file to You under the Apache License, Version 2.0
9  * (the "License"); you may not use this file except in compliance with
10  * the License. You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing,
15  * software distributed under the License is distributed on an
16  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
17  * either express or implied. See the License for the specific
18  * language governing permissions and limitations under the License.
19  */

20 package org.apache.derbyTesting.functionTests.tests.jdbcapi;
21
22 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
23 import org.apache.derbyTesting.junit.JDBC;
24
25 import java.sql.ResultSet JavaDoc;
26 import java.sql.SQLException JavaDoc;
27 import java.sql.PreparedStatement JavaDoc;
28 import java.sql.Statement JavaDoc;
29 import java.sql.Connection JavaDoc;
30
31 import java.math.BigDecimal JavaDoc;
32
33 import junit.framework.Test;
34 import junit.framework.TestSuite;
35
36
37 /**
38  * Tests updateXXX() methods on updatable resultsets.
39  * This is done by creating a table which has n columns with
40  * different SQL types. Then there is one testcase for each
41  * updateXXX method, which calls updateXXX on all columns.
42  */

43 final public class UpdateXXXTest extends BaseJDBCTestCase
44 {
45     /**
46      * Constructor
47      * @param name name of testcase. Should be the name of test method.
48      */

49     public UpdateXXXTest(final String JavaDoc name) {
50         super(name);
51     }
52     
53     public static Test suite() {
54         TestSuite suite = new TestSuite();
55         
56         // DB2 client doesn't support this functionality
57
if (usingDerbyNet())
58             return suite;
59         
60         suite.addTestSuite(UpdateXXXTest.class);
61         
62         // requires java.math.BigDecimal
63
if (JDBC.vmSupportsJDBC2())
64             suite.addTest(new UpdateXXXTest("jdbc2testUpdateBigDecimal"));
65                       
66         return suite;
67     }
68  
69
70     /**
71      * The setup creates a Connection to the database, and also
72      * creates a table with one row. Then it creates an updatable
73      * ResultSet which is positioned on the row.
74      * @exception Exception any exception will cause test to fail with error.
75      */

76     public void setUp()
77         throws Exception JavaDoc
78     {
79         Connection JavaDoc con = getConnection();
80         try {
81             
82             con.setAutoCommit(false);
83             
84             Statement JavaDoc stmt = con.createStatement();
85             String JavaDoc createTableString = "CREATE TABLE " + TABLE_NAME + " (" +
86                 "F01 SMALLINT," +
87                 "F02 INTEGER," +
88                 "F03 BIGINT," +
89                 "F04 REAL," +
90                 "F05 FLOAT," +
91                 "F06 DOUBLE," +
92                 "F07 DECIMAL," +
93                 "F08 NUMERIC," +
94                 "F09 CHAR(100)," +
95                 "F10 VARCHAR(256) )";
96             println(createTableString);
97             stmt.executeUpdate(createTableString);
98             PreparedStatement JavaDoc ps = con.prepareStatement
99                 ("insert into " + TABLE_NAME + " values(?,?,?,?,?,?,?,?,?,?)");
100             
101             ps.setShort(1, (short) 1);
102             ps.setInt(2, 1);
103             ps.setLong(3, 1L);
104             ps.setFloat(4, 1.0f);
105             ps.setDouble(5, 1.0);
106             ps.setDouble(6, 1.0);
107             
108             // Use setString instead of setBigDecimal to
109
// allow most of the test cases to run under J2ME
110
ps.setString(7, "1");
111             ps.setString(8, "1");
112             
113             ps.setString(9, "1");
114             ps.setString(10, "1");
115             ps.executeUpdate();
116             
117             rs = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
118                                      ResultSet.CONCUR_UPDATABLE).
119                 executeQuery(SELECT_STMT);
120             rs.next();
121         } catch (SQLException JavaDoc e) {
122             con.rollback();
123             throw e;
124         }
125     }
126         
127     /**
128      * Tests calling updateString on all columns of the row.
129      * @exception SQLException database access error. Causes test to
130      * fail with an error.
131      */

132     public void testUpdateString()
133         throws SQLException JavaDoc
134     {
135         for (int i = 1; i <= COLUMNS; i++) {
136             rs.updateString(i, "2");
137             assertEquals("Expected rs.getDouble(" + i +
138                          ") to match updated value", 2, (int) rs.getDouble(i));
139         }
140         rs.updateRow();
141         checkColumnsAreUpdated();
142         
143     }
144
145     /**
146      * Tests calling updateInt on all columns of the row.
147      * @exception SQLException database access error. Causes test to
148      * fail with an error.
149      */

150     public void testUpdateInt()
151         throws SQLException JavaDoc
152     {
153         for (int i = 1; i <= COLUMNS; i++) {
154             rs.updateInt(i, 2);
155             assertEquals("Expected rs.getInt(" + i +
156                          ") to match updated value", 2, rs.getInt(i));
157         }
158         rs.updateRow();
159         checkColumnsAreUpdated();
160     }
161
162     /**
163      * Tests calling updateLong on all columns of the row.
164      * @exception SQLException database access error. Causes test to
165      * fail with an error.
166      */

167     public void testUpdateLong()
168         throws SQLException JavaDoc
169     {
170         for (int i = 1; i <= COLUMNS; i++) {
171             rs.updateLong(i, 2L);
172             assertEquals("Expected rs.getLong(" + i +
173                          ") to match updated value", 2L, rs.getLong(i));
174         }
175         rs.updateRow();
176         checkColumnsAreUpdated();
177     }
178
179     /**
180      * Tests calling updateShort on all columns of the row.
181      * @exception SQLException database access error. Causes test to
182      * fail with an error.
183      */

184     public void testUpdateShort()
185         throws SQLException JavaDoc
186     {
187         for (int i = 1; i <= COLUMNS; i++) {
188             rs.updateShort(i, (short) 2);
189             assertEquals("Expected rs.getShort(" + i +
190                          ") to match updated value", 2, (int) rs.getShort(i));
191         }
192         rs.updateRow();
193         checkColumnsAreUpdated();
194     }
195     
196     /**
197      * Tests calling updateFloat on all columns of the row.
198      * @exception SQLException database access error. Causes test to
199      * fail with an error.
200      */

201     public void testUpdateFloat()
202         throws SQLException JavaDoc
203     {
204         for (int i = 1; i <= COLUMNS; i++) {
205             rs.updateFloat(i, 2.0f);
206             assertEquals("Expected rs.getFloat(" + i +
207                          ") to match updated value", 2, (int) rs.getFloat(i));
208         }
209         rs.updateRow();
210         checkColumnsAreUpdated();
211     }
212     
213     /**
214      * Tests calling updateDouble on all columns of the row.
215      * @exception SQLException database access error. Causes test to
216      * fail with an error.
217      */

218     public void testUpdateDouble()
219         throws SQLException JavaDoc
220     {
221         for (int i = 1; i <= COLUMNS; i++) {
222             rs.updateDouble(i, 2.0);
223             assertEquals("Expected rs.getDouble(" + i +
224                          ") to match updated value", 2, (int) rs.getDouble(i));
225         }
226         rs.updateRow();
227         checkColumnsAreUpdated();
228     }
229
230     /**
231      * Tests calling update on all columns of the row.
232      * @exception SQLException database access error. Causes test to
233      * fail with an error.
234      */

235     public void jdbc2testUpdateBigDecimal()
236         throws SQLException JavaDoc
237     {
238         for (int i = 1; i <= COLUMNS; i++) {
239             rs.updateBigDecimal(i, BigDecimal.valueOf(2L));
240             assertEquals("Expected rs.getBigDecimal(" + i +
241                          ") to match updated value", 2,
242                          rs.getBigDecimal(i).intValue());
243         }
244         rs.updateRow();
245         checkColumnsAreUpdated();
246     }
247     
248     /**
249      * Tests calling updateObject with a null value on all columns.
250      * @exception SQLException database access error. Causes test to
251      * fail with an error.
252      */

253     public void testUpdateObjectWithNull()
254         throws SQLException JavaDoc
255     {
256         Object JavaDoc value = null;
257         
258         for (int i = 1; i <= COLUMNS; i++) {
259             rs.updateObject(i, value);
260             assertNull("Expected rs.getObject(" + i + ") to be null",
261                        rs.getObject(i));
262             assertTrue("Expected rs.wasNull() to return true",
263                        rs.wasNull());
264         }
265         rs.updateRow();
266         checkColumnsAreNull();
267     }
268
269     /**
270      * Tests calling setNull on all columns
271      * @exception SQLException database access error. Causes test to
272      * fail with an error.
273      */

274     public void testUpdateNull()
275         throws SQLException JavaDoc
276     {
277         for (int i = 1; i <= COLUMNS; i++) {
278             rs.updateNull(i);
279             assertNull("Expected rs.getObject(" + i + ") to be null",
280                        rs.getObject(i));
281             assertTrue("Expected rs.wasNull() to return true",
282                        rs.wasNull());
283         }
284         rs.updateRow();
285         checkColumnsAreNull();
286     }
287
288     /**
289      * Checks that the columns in the row are all SQL null.
290      * @exception SQLException database access error. Causes test to
291      * fail with an error.
292      */

293     private void checkColumnsAreNull()
294         throws SQLException JavaDoc
295     {
296         rs.close();
297         
298         rs = createStatement(ResultSet.TYPE_FORWARD_ONLY,
299                                  ResultSet.CONCUR_READ_ONLY).
300             executeQuery(SELECT_STMT);
301         
302         rs.next();
303         
304         for (int i = 1; i <= COLUMNS; i++) {
305             assertNull("Expected column " + i + " to be null",
306                        rs.getObject(i));
307             assertTrue("Expected wasNull() after reading column " + i +
308                        " to be true when data is SQL Null on column",
309                        rs.wasNull());
310         }
311     }
312
313     /**
314      * Checks that the columns in the row are updated in the database.
315      * Using a new ResultSet to do this check.
316      * @exception SQLException database access error. Causes test to
317      * fail with an error.
318      */

319     private void checkColumnsAreUpdated()
320         throws SQLException JavaDoc
321     {
322         rs.close();
323         
324         rs = createStatement(ResultSet.TYPE_FORWARD_ONLY,
325                                  ResultSet.CONCUR_READ_ONLY).
326             executeQuery(SELECT_STMT);
327         
328         rs.next();
329         for (int i = 1; i <= COLUMNS; i++) {
330             int expectedVal = 2;
331             
332             // Since rs.getInt(i) on CHAR/VARCHAR columns with value 2.0 gives:
333
// "ERROR 22018: Invalid character string format for type int"
334
// we use getDouble(i). We cast it to int, because there is not
335
// assertEquals(..) methods which takes double.
336
int actualVal = (int) rs.getDouble(i);
337             assertEquals("Unexpected value from rs.getDouble( + " + i + ")",
338                          expectedVal, actualVal);
339         }
340     }
341     
342     /* Updatable ResultSet */
343     private ResultSet JavaDoc rs = null;
344     
345     /* Table name */
346     private static final String JavaDoc TABLE_NAME = "MultiTypeTable";
347
348     /* SQL String for the SELECT statement */
349     private static final String JavaDoc SELECT_STMT =
350         "SELECT * FROM " + TABLE_NAME;
351                              
352     /* Number of columns in table */
353     private static final int COLUMNS = 10;
354 }
355
Popular Tags