001: /*
002: *
003: * Derby - Class UpdateXXXTest
004: *
005: * Licensed to the Apache Software Foundation (ASF) under one or more
006: * contributor license agreements. See the NOTICE file distributed with
007: * this work for additional information regarding copyright ownership.
008: * The ASF licenses this file to You under the Apache License, Version 2.0
009: * (the "License"); you may not use this file except in compliance with
010: * the License. You may obtain a copy of the License at
011: *
012: * http://www.apache.org/licenses/LICENSE-2.0
013: *
014: * Unless required by applicable law or agreed to in writing,
015: * software distributed under the License is distributed on an
016: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
017: * either express or implied. See the License for the specific
018: * language governing permissions and limitations under the License.
019: */
020: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
021:
022: import org.apache.derbyTesting.junit.BaseJDBCTestCase;
023: import org.apache.derbyTesting.junit.JDBC;
024:
025: import java.sql.ResultSet;
026: import java.sql.SQLException;
027: import java.sql.PreparedStatement;
028: import java.sql.Statement;
029: import java.sql.Connection;
030:
031: import java.math.BigDecimal;
032:
033: import junit.framework.Test;
034: import junit.framework.TestSuite;
035:
036: /**
037: * Tests updateXXX() methods on updatable resultsets.
038: * This is done by creating a table which has n columns with
039: * different SQL types. Then there is one testcase for each
040: * updateXXX method, which calls updateXXX on all columns.
041: */
042: final public class UpdateXXXTest extends BaseJDBCTestCase {
043: /**
044: * Constructor
045: * @param name name of testcase. Should be the name of test method.
046: */
047: public UpdateXXXTest(final String name) {
048: super (name);
049: }
050:
051: public static Test suite() {
052: TestSuite suite = new TestSuite();
053:
054: // DB2 client doesn't support this functionality
055: if (usingDerbyNet())
056: return suite;
057:
058: suite.addTestSuite(UpdateXXXTest.class);
059:
060: // requires java.math.BigDecimal
061: if (JDBC.vmSupportsJDBC2())
062: suite
063: .addTest(new UpdateXXXTest(
064: "jdbc2testUpdateBigDecimal"));
065:
066: return suite;
067: }
068:
069: /**
070: * The setup creates a Connection to the database, and also
071: * creates a table with one row. Then it creates an updatable
072: * ResultSet which is positioned on the row.
073: * @exception Exception any exception will cause test to fail with error.
074: */
075: public void setUp() throws Exception {
076: Connection con = getConnection();
077: try {
078:
079: con.setAutoCommit(false);
080:
081: Statement stmt = con.createStatement();
082: String createTableString = "CREATE TABLE " + TABLE_NAME
083: + " (" + "F01 SMALLINT," + "F02 INTEGER,"
084: + "F03 BIGINT," + "F04 REAL," + "F05 FLOAT,"
085: + "F06 DOUBLE," + "F07 DECIMAL," + "F08 NUMERIC,"
086: + "F09 CHAR(100)," + "F10 VARCHAR(256) )";
087: println(createTableString);
088: stmt.executeUpdate(createTableString);
089: PreparedStatement ps = con.prepareStatement("insert into "
090: + TABLE_NAME + " values(?,?,?,?,?,?,?,?,?,?)");
091:
092: ps.setShort(1, (short) 1);
093: ps.setInt(2, 1);
094: ps.setLong(3, 1L);
095: ps.setFloat(4, 1.0f);
096: ps.setDouble(5, 1.0);
097: ps.setDouble(6, 1.0);
098:
099: // Use setString instead of setBigDecimal to
100: // allow most of the test cases to run under J2ME
101: ps.setString(7, "1");
102: ps.setString(8, "1");
103:
104: ps.setString(9, "1");
105: ps.setString(10, "1");
106: ps.executeUpdate();
107:
108: rs = con.createStatement(ResultSet.TYPE_FORWARD_ONLY,
109: ResultSet.CONCUR_UPDATABLE).executeQuery(
110: SELECT_STMT);
111: rs.next();
112: } catch (SQLException e) {
113: con.rollback();
114: throw e;
115: }
116: }
117:
118: /**
119: * Tests calling updateString on all columns of the row.
120: * @exception SQLException database access error. Causes test to
121: * fail with an error.
122: */
123: public void testUpdateString() throws SQLException {
124: for (int i = 1; i <= COLUMNS; i++) {
125: rs.updateString(i, "2");
126: assertEquals("Expected rs.getDouble(" + i
127: + ") to match updated value", 2, (int) rs
128: .getDouble(i));
129: }
130: rs.updateRow();
131: checkColumnsAreUpdated();
132:
133: }
134:
135: /**
136: * Tests calling updateInt on all columns of the row.
137: * @exception SQLException database access error. Causes test to
138: * fail with an error.
139: */
140: public void testUpdateInt() throws SQLException {
141: for (int i = 1; i <= COLUMNS; i++) {
142: rs.updateInt(i, 2);
143: assertEquals("Expected rs.getInt(" + i
144: + ") to match updated value", 2, rs.getInt(i));
145: }
146: rs.updateRow();
147: checkColumnsAreUpdated();
148: }
149:
150: /**
151: * Tests calling updateLong on all columns of the row.
152: * @exception SQLException database access error. Causes test to
153: * fail with an error.
154: */
155: public void testUpdateLong() throws SQLException {
156: for (int i = 1; i <= COLUMNS; i++) {
157: rs.updateLong(i, 2L);
158: assertEquals("Expected rs.getLong(" + i
159: + ") to match updated value", 2L, rs.getLong(i));
160: }
161: rs.updateRow();
162: checkColumnsAreUpdated();
163: }
164:
165: /**
166: * Tests calling updateShort on all columns of the row.
167: * @exception SQLException database access error. Causes test to
168: * fail with an error.
169: */
170: public void testUpdateShort() throws SQLException {
171: for (int i = 1; i <= COLUMNS; i++) {
172: rs.updateShort(i, (short) 2);
173: assertEquals("Expected rs.getShort(" + i
174: + ") to match updated value", 2, (int) rs
175: .getShort(i));
176: }
177: rs.updateRow();
178: checkColumnsAreUpdated();
179: }
180:
181: /**
182: * Tests calling updateFloat on all columns of the row.
183: * @exception SQLException database access error. Causes test to
184: * fail with an error.
185: */
186: public void testUpdateFloat() throws SQLException {
187: for (int i = 1; i <= COLUMNS; i++) {
188: rs.updateFloat(i, 2.0f);
189: assertEquals("Expected rs.getFloat(" + i
190: + ") to match updated value", 2, (int) rs
191: .getFloat(i));
192: }
193: rs.updateRow();
194: checkColumnsAreUpdated();
195: }
196:
197: /**
198: * Tests calling updateDouble on all columns of the row.
199: * @exception SQLException database access error. Causes test to
200: * fail with an error.
201: */
202: public void testUpdateDouble() throws SQLException {
203: for (int i = 1; i <= COLUMNS; i++) {
204: rs.updateDouble(i, 2.0);
205: assertEquals("Expected rs.getDouble(" + i
206: + ") to match updated value", 2, (int) rs
207: .getDouble(i));
208: }
209: rs.updateRow();
210: checkColumnsAreUpdated();
211: }
212:
213: /**
214: * Tests calling update on all columns of the row.
215: * @exception SQLException database access error. Causes test to
216: * fail with an error.
217: */
218: public void jdbc2testUpdateBigDecimal() throws SQLException {
219: for (int i = 1; i <= COLUMNS; i++) {
220: rs.updateBigDecimal(i, BigDecimal.valueOf(2L));
221: assertEquals("Expected rs.getBigDecimal(" + i
222: + ") to match updated value", 2, rs
223: .getBigDecimal(i).intValue());
224: }
225: rs.updateRow();
226: checkColumnsAreUpdated();
227: }
228:
229: /**
230: * Tests calling updateObject with a null value on all columns.
231: * @exception SQLException database access error. Causes test to
232: * fail with an error.
233: */
234: public void testUpdateObjectWithNull() throws SQLException {
235: Object value = null;
236:
237: for (int i = 1; i <= COLUMNS; i++) {
238: rs.updateObject(i, value);
239: assertNull("Expected rs.getObject(" + i + ") to be null",
240: rs.getObject(i));
241: assertTrue("Expected rs.wasNull() to return true", rs
242: .wasNull());
243: }
244: rs.updateRow();
245: checkColumnsAreNull();
246: }
247:
248: /**
249: * Tests calling setNull on all columns
250: * @exception SQLException database access error. Causes test to
251: * fail with an error.
252: */
253: public void testUpdateNull() throws SQLException {
254: for (int i = 1; i <= COLUMNS; i++) {
255: rs.updateNull(i);
256: assertNull("Expected rs.getObject(" + i + ") to be null",
257: rs.getObject(i));
258: assertTrue("Expected rs.wasNull() to return true", rs
259: .wasNull());
260: }
261: rs.updateRow();
262: checkColumnsAreNull();
263: }
264:
265: /**
266: * Checks that the columns in the row are all SQL null.
267: * @exception SQLException database access error. Causes test to
268: * fail with an error.
269: */
270: private void checkColumnsAreNull() throws SQLException {
271: rs.close();
272:
273: rs = createStatement(ResultSet.TYPE_FORWARD_ONLY,
274: ResultSet.CONCUR_READ_ONLY).executeQuery(SELECT_STMT);
275:
276: rs.next();
277:
278: for (int i = 1; i <= COLUMNS; i++) {
279: assertNull("Expected column " + i + " to be null", rs
280: .getObject(i));
281: assertTrue("Expected wasNull() after reading column " + i
282: + " to be true when data is SQL Null on column", rs
283: .wasNull());
284: }
285: }
286:
287: /**
288: * Checks that the columns in the row are updated in the database.
289: * Using a new ResultSet to do this check.
290: * @exception SQLException database access error. Causes test to
291: * fail with an error.
292: */
293: private void checkColumnsAreUpdated() throws SQLException {
294: rs.close();
295:
296: rs = createStatement(ResultSet.TYPE_FORWARD_ONLY,
297: ResultSet.CONCUR_READ_ONLY).executeQuery(SELECT_STMT);
298:
299: rs.next();
300: for (int i = 1; i <= COLUMNS; i++) {
301: int expectedVal = 2;
302:
303: // Since rs.getInt(i) on CHAR/VARCHAR columns with value 2.0 gives:
304: // "ERROR 22018: Invalid character string format for type int"
305: // we use getDouble(i). We cast it to int, because there is not
306: // assertEquals(..) methods which takes double.
307: int actualVal = (int) rs.getDouble(i);
308: assertEquals("Unexpected value from rs.getDouble( + " + i
309: + ")", expectedVal, actualVal);
310: }
311: }
312:
313: /* Updatable ResultSet */
314: private ResultSet rs = null;
315:
316: /* Table name */
317: private static final String TABLE_NAME = "MultiTypeTable";
318:
319: /* SQL String for the SELECT statement */
320: private static final String SELECT_STMT = "SELECT * FROM "
321: + TABLE_NAME;
322:
323: /* Number of columns in table */
324: private static final int COLUMNS = 10;
325: }
|