KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2  *
3  * Derby - Class URCoveringIndexTest
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 import org.apache.derbyTesting.functionTests.util.TestUtil;
22 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
23 import org.apache.derbyTesting.junit.JDBC;
24
25 import junit.framework.*;
26 import java.sql.*;
27
28 /**
29  * Tests updatable result sets when there is a index that includes all data for
30  * the query (covering index).
31  *
32  * DERBY-1087
33  *
34  * @author Fernanda Pizzorno
35  */

36 public class URCoveringIndexTest extends BaseJDBCTestCase {
37     
38     /** Creates a new instance of SURBaseTest */
39     public URCoveringIndexTest(String JavaDoc name) {
40         super(name);
41     }
42
43     /**
44      * Set up the connection to the database.
45      */

46     public void setUp() throws Exception JavaDoc {
47         Connection con = getConnection();
48         con.setAutoCommit(false);
49
50         String JavaDoc createTableWithPK = "CREATE TABLE tableWithPK (" +
51                 "c1 int primary key," +
52                 "c2 int)";
53         String JavaDoc insertData = "INSERT INTO tableWithPK values (1, 1)";
54         Statement stmt = con.createStatement();
55         stmt.execute(createTableWithPK);
56         
57         stmt.execute(insertData);
58         
59         stmt.close();
60     }
61     
62     private void testUpdateUpdatedTupleWithCoveringIndex(
63             boolean scroll,
64             boolean usePositionedUpdate) throws SQLException{
65         
66         SQLWarning w = null;
67         int resultsetType = scroll ? ResultSet.TYPE_SCROLL_INSENSITIVE :
68                 ResultSet.TYPE_FORWARD_ONLY;
69         
70         Connection con = getConnection();
71         
72         if (!(con.getMetaData().supportsResultSetConcurrency(resultsetType,
73                 ResultSet.CONCUR_UPDATABLE))) {
74             return;
75         }
76
77             
78         Statement updStmt = con.createStatement(resultsetType,
79                 ResultSet.CONCUR_UPDATABLE);
80         Statement roStmt = con.createStatement();
81         
82         ResultSet rs = updStmt.executeQuery("SELECT c1 FROM tableWithPK");
83         rs.next();
84         int orig_c1 = rs.getInt(1);
85         roStmt.executeUpdate("UPDATE tableWithPK SET c1 = " +
86                 (orig_c1 + 10) + "WHERE c1 = " + rs.getInt(1));
87         rs.clearWarnings();
88         if (usePositionedUpdate) {
89             roStmt.executeUpdate("UPDATE tableWithPK set c1 = " +
90                     (orig_c1 + 20) + "WHERE CURRENT OF " +
91                     rs.getCursorName());
92             w = roStmt.getWarnings();
93         } else {
94             rs.updateInt(1, (orig_c1 + 20));
95             rs.updateRow();
96             w = rs.getWarnings();
97         }
98         assertTrue("Update should not produce any warnings ", w == null);
99         rs.close();
100         
101         rs = roStmt.executeQuery("SELECT c1 FROM tableWithPK");
102         rs.next();
103         assertEquals("Expecting c1 to be " + orig_c1 + " + 20",
104                 rs.getInt(1), (orig_c1 + 20));
105         rs.close();
106         roStmt.close();
107         updStmt.close();
108
109     }
110
111     /**
112      * Updates a previously updated row with a covering index using positioned
113      * updates and scrollable result sets.
114      */

115     public void testUpdateUpdatedTupleScrollPostitioned() throws SQLException{
116         testUpdateUpdatedTupleWithCoveringIndex(true, true);
117     }
118
119     /**
120      * Updates a previously updated row with a covering index using updateRow
121      * and scrollable result sets.
122      */

123     public void testUpdateUpdatedTupleScrollUpdateRow() throws SQLException{
124         testUpdateUpdatedTupleWithCoveringIndex(true, false);
125     }
126
127     /**
128      * Updates a previously updated row with a covering index using positioned
129      * updates and forward only result sets.
130      */

131     public void testUpdateUpdatedTupleFOPositioned() throws SQLException{
132         testUpdateUpdatedTupleWithCoveringIndex(false, true);
133     }
134
135     /**
136      * Updates a previously updated row with a covering index using updateRow
137      * and forward only result sets.
138      */

139     public void testUpdateUpdatedTupleFOUpdateRow() throws SQLException{
140         testUpdateUpdatedTupleWithCoveringIndex(false, false);
141     }
142 }
143
Popular Tags