001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017:
018: package org.apache.harmony.sql.tests.internal.rowset;
019:
020: import java.math.BigDecimal;
021: import java.sql.Connection;
022: import java.sql.Date;
023: import java.sql.DriverManager;
024: import java.sql.PreparedStatement;
025: import java.sql.ResultSet;
026: import java.sql.ResultSetMetaData;
027: import java.sql.SQLException;
028: import java.sql.Statement;
029: import java.sql.Time;
030: import java.sql.Timestamp;
031:
032: import javax.sql.rowset.CachedRowSet;
033:
034: import junit.framework.TestCase;
035:
036: public class CachedRowSetTestCase extends TestCase {
037: public static final String DERBY_URL_Create = "jdbc:derby:src/test/resources/TESTDB;create=true";
038:
039: public static final String DERBY_URL = "jdbc:derby:src/test/resources/TESTDB";
040:
041: protected Connection conn = null;
042:
043: protected Statement st;
044:
045: protected ResultSet rs;
046:
047: protected CachedRowSet crset;
048:
049: protected CachedRowSet noInitialCrset;
050:
051: public final static int DEFAULT_COLUMN_COUNT = 12;
052:
053: public final static int DEFAULT_ROW_COUNT = 4;
054:
055: public void setUp() throws Exception {
056: Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
057:
058: try {
059: conn = DriverManager.getConnection(DERBY_URL);
060: } catch (SQLException e) {
061: try {
062: conn = DriverManager.getConnection(DERBY_URL_Create);
063: } catch (SQLException ee) {
064: throw new SQLException("Create DB Failure!");
065: }
066: }
067:
068: st = conn.createStatement();
069: rs = conn.getMetaData().getTables(null, "APP", "USER_INFO",
070: null);
071: String createTableSQL = "create table USER_INFO (ID INTEGER NOT NULL,NAME VARCHAR(10) NOT NULL, BIGINT_T BIGINT, "
072: + "NUMERIC_T NUMERIC, DECIMAL_T DECIMAL, SMALLINT_T SMALLINT, FLOAT_T FLOAT, REAL_T REAL, DOUBLE_T DOUBLE,"
073: + "DATE_T DATE, TIME_T TIME, TIMESTAMP_T TIMESTAMP)";
074: String alterTableSQL = "ALTER TABLE USER_INFO ADD CONSTRAINT USER_INFO_PK Primary Key (ID)";
075:
076: if (!rs.next()) {
077: st.execute(createTableSQL);
078: st.execute(alterTableSQL);
079: }
080:
081: insertData();
082: rs = st.executeQuery("select * from USER_INFO");
083: try {
084: crset = (CachedRowSet) Class.forName(
085: "com.sun.rowset.CachedRowSetImpl").newInstance();
086: noInitialCrset = (CachedRowSet) Class.forName(
087: "com.sun.rowset.CachedRowSetImpl").newInstance();
088: } catch (ClassNotFoundException e) {
089:
090: crset = (CachedRowSet) Class
091: .forName(
092: "org.apache.harmony.sql.internal.rowset.CachedRowSetImpl")
093: .newInstance();
094: noInitialCrset = (CachedRowSet) Class
095: .forName(
096: "org.apache.harmony.sql.internal.rowset.CachedRowSetImpl")
097: .newInstance();
098:
099: System.setProperty("Testing Harmony", "true");
100: }
101: crset.populate(rs);
102: rs = st.executeQuery("select * from USER_INFO");
103: crset.setUrl(DERBY_URL);
104: }
105:
106: public void tearDown() throws Exception {
107: if (rs != null) {
108: rs.close();
109: }
110: if (crset != null) {
111: crset.close();
112: }
113: if (st != null) {
114: st.close();
115: }
116: if (conn != null) {
117: /*
118: * if doesn't call rollback, ri will throw exception then block
119: * java.sql.SQLException: Invalid transaction state.
120: */
121: conn.rollback();
122: conn.close();
123: }
124: }
125:
126: protected void insertData() throws Exception {
127:
128: st.executeUpdate("delete from USER_INFO");
129:
130: // first row
131: st
132: .executeUpdate("insert into USER_INFO(ID,NAME) values (1,'hermit')");
133: // second row
134: st
135: .executeUpdate("insert into USER_INFO(ID,NAME) values (2,'test')");
136:
137: String insertSQL = "INSERT INTO USER_INFO(ID, NAME, BIGINT_T, NUMERIC_T, DECIMAL_T, SMALLINT_T, "
138: + "FLOAT_T, REAL_T, DOUBLE_T, DATE_T, TIME_T, TIMESTAMP_T) VALUES(?, ?, ?, ?, ?, ?,"
139: + "?, ?, ?, ?, ?, ? )";
140: PreparedStatement preStmt = conn.prepareStatement(insertSQL);
141: // third row
142: preStmt.setInt(1, 3);
143: preStmt.setString(2, "test3");
144: preStmt.setLong(3, 3333L);
145: preStmt.setBigDecimal(4, new BigDecimal(123));
146: preStmt.setBigDecimal(5, new BigDecimal(23));
147: preStmt.setInt(6, 13);
148: preStmt.setFloat(7, 3.7F);
149: preStmt.setFloat(8, 3.888F);
150: preStmt.setDouble(9, 3.9999);
151: preStmt.setDate(10, new Date(523654123));
152: preStmt.setTime(11, new Time(966554221));
153: preStmt.setTimestamp(12, new Timestamp(521342100));
154: preStmt.executeUpdate();
155: // fourth row
156: preStmt.setInt(1, 4);
157: preStmt.setString(2, "test4");
158: preStmt.setLong(3, 444423L);
159: preStmt.setBigDecimal(4, new BigDecimal(12));
160: preStmt.setBigDecimal(5, new BigDecimal(23));
161: preStmt.setInt(6, 41);
162: preStmt.setFloat(7, 4.8F);
163: preStmt.setFloat(8, 4.888F);
164: preStmt.setDouble(9, 4.9999);
165: preStmt.setDate(10, new Date(965324512));
166: preStmt.setTime(11, new Time(452368512));
167: preStmt.setTimestamp(12, new Timestamp(874532105));
168: preStmt.executeUpdate();
169:
170: if (preStmt != null) {
171: preStmt.close();
172: }
173: }
174:
175: protected void insertMoreData(int rows) throws Exception {
176: String insertSQL = "INSERT INTO USER_INFO(ID, NAME, BIGINT_T, NUMERIC_T, DECIMAL_T, SMALLINT_T, "
177: + "FLOAT_T, REAL_T, DOUBLE_T, DATE_T, TIME_T, TIMESTAMP_T) VALUES(?, ?, ?, ?, ?, ?,"
178: + "?, ?, ?, ?, ?, ? )";
179: PreparedStatement preStmt = conn.prepareStatement(insertSQL);
180:
181: // insert 15 rows
182: for (int i = DEFAULT_ROW_COUNT + 1; i <= DEFAULT_ROW_COUNT
183: + rows + 1; i++) {
184: preStmt.setInt(1, i);
185: preStmt.setString(2, "test" + i);
186: preStmt.setLong(3, 444423L);
187: preStmt.setBigDecimal(4, new BigDecimal(12));
188: preStmt.setBigDecimal(5, new BigDecimal(23));
189: preStmt.setInt(6, 41);
190: preStmt.setFloat(7, 4.8F);
191: preStmt.setFloat(8, 4.888F);
192: preStmt.setDouble(9, 4.9999);
193: preStmt.setDate(10, new Date(965324512));
194: preStmt.setTime(11, new Time(452368512));
195: preStmt.setTimestamp(12, new Timestamp(874532105));
196: preStmt.executeUpdate();
197: }
198:
199: if (preStmt != null) {
200: preStmt.close();
201: }
202: }
203:
204: protected void isMetaDataEquals(ResultSetMetaData expected,
205: ResultSetMetaData actual) throws SQLException {
206: assertEquals(expected.getColumnCount(), actual.getColumnCount());
207:
208: int columnCount = expected.getColumnCount();
209:
210: for (int column = 1; column <= columnCount; column++) {
211: assertEquals(expected.isAutoIncrement(column), actual
212: .isAutoIncrement(column));
213: assertEquals(expected.isCaseSensitive(column), actual
214: .isCaseSensitive(column));
215: assertEquals(expected.isCurrency(column), actual
216: .isCurrency(column));
217: assertEquals(expected.isDefinitelyWritable(column), actual
218: .isDefinitelyWritable(column));
219: assertEquals(expected.isReadOnly(column), actual
220: .isReadOnly(column));
221: assertEquals(expected.isSearchable(column), actual
222: .isSearchable(column));
223: assertEquals(expected.isSigned(column), actual
224: .isSigned(column));
225: assertEquals(expected.isWritable(column), actual
226: .isWritable(column));
227: assertEquals(expected.isNullable(column), actual
228: .isNullable(column));
229: assertEquals(expected.getCatalogName(column), actual
230: .getCatalogName(column));
231: assertEquals(expected.getColumnClassName(column), actual
232: .getColumnClassName(column));
233: assertEquals(expected.getColumnDisplaySize(column), actual
234: .getColumnDisplaySize(column));
235: assertEquals(expected.getColumnLabel(column), actual
236: .getColumnLabel(column));
237: assertEquals(expected.getColumnName(column), actual
238: .getColumnName(column));
239: assertEquals(expected.getColumnType(column), actual
240: .getColumnType(column));
241: assertEquals(expected.getColumnTypeName(column), actual
242: .getColumnTypeName(column));
243: assertEquals(expected.getPrecision(column), actual
244: .getPrecision(column));
245: assertEquals(expected.getScale(column), actual
246: .getScale(column));
247: assertEquals(expected.getSchemaName(column), actual
248: .getSchemaName(column));
249: assertEquals(expected.getTableName(column), actual
250: .getTableName(column));
251: }
252: }
253:
254: protected CachedRowSet newNoInitialInstance() throws Exception {
255: if ("true".equals(System.getProperty("Testing Harmony"))) {
256: return (CachedRowSet) Class
257: .forName(
258: "org.apache.harmony.sql.internal.rowset.CachedRowSetImpl")
259: .newInstance();
260: }
261: return (CachedRowSet) Class.forName(
262: "com.sun.rowset.CachedRowSetImpl").newInstance();
263: }
264:
265: protected void reloadCachedRowSet() throws SQLException {
266: rs = st.executeQuery("select * from USER_INFO");
267: crset.populate(rs);
268: rs = st.executeQuery("select * from USER_INFO");
269: crset.setUrl(DERBY_URL);
270: }
271:
272: }
|