1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.lang.PrepareExecuteDDL
Licensed to the Apache Software Foundation (ASF) under one or more
contributor license agreements. See the NOTICE file distributed with
this work for additional information regarding copyright ownership.
The ASF licenses this file to You under the Apache License, Version 2.0
(the "License"); you may not use this file except in compliance with
the License. You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package org.apache.derbyTesting.functionTests.tests.lang;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import junit.framework.Test;
import org.apache.derbyTesting.junit.BaseJDBCTestCase;
import org.apache.derbyTesting.junit.BaseTestSuite;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Test the dependency system for active statements when
* a DDL is executed in a separate connection after the
* prepare but before the execute.
*
*/
public class PrepareExecuteDDL extends BaseJDBCTestCase {
/**
* Connection to execute the DDL on. Needs
* to be different to the single connection
* provided by the super-class. This connection
* is used to execute DDL while the other connection
* has open objcts dependent on the objct changed by the DDL.
*/
private Connection connDDL;
/**
* List of statements that are prepared and then executed.
* The testPrepareExecute method prepares each statement
* in this list, executes one DDL, executes each prepared
* statement and then checks the result.
* <BR>
* The result checking is driven off the initial text
* of the statement.
*/
private static final String[] STMTS =
{
"SELECT * FROM PED001",
"SELECT A, B FROM PED001",
"GRANT SELECT ON PED001 TO U_PED_001",
"GRANT SELECT(A,B) ON PED001 TO U_PED_001",
"REVOKE SELECT(A,B) ON PED001 FROM U_PED_001",
"REVOKE SELECT ON PED001 FROM U_PED_001",
};
/**
* All the DDL commands that will be executed, one per
* fixture, as the mutation between the prepare and execute.
*/
private static final String[] DDL =
{
"ALTER TABLE PED001 ADD COLUMN D BIGINT",
"ALTER TABLE PED001 ADD CONSTRAINT PED001_PK PRIMARY KEY (A)",
"ALTER TABLE PED001 LOCKSIZE ROW",
"ALTER TABLE PED001 LOCKSIZE TABLE",
"DROP TABLE PED001",
};
/**
* Create a suite of tests, one per statement in DDL.
* This test is for testing the embedded dependency system
* though possibly it could be used for testing in client
* as well.
*/
public static Test suite() {
BaseTestSuite suite = new BaseTestSuite("PrepareExecuteDDL");
for (int i = 0; i < DDL.length; i++)
suite.addTest(new PrepareExecuteDDL("testPrepareExcute", DDL[i]));
return TestConfiguration.sqlAuthorizationDecorator(suite);
}
private final String ddl;
private PrepareExecuteDDL(String name, String ddl)
{
super(name);
this.ddl = ddl;
}
private boolean tableDropped()
{
return ddl.startsWith("DROP TABLE ");
}
public void testPrepareExcute() throws SQLException
{
Connection conn = getConnection();
PreparedStatement[] psa= new PreparedStatement[STMTS.length];
for (int i = 0; i < STMTS.length; i++)
{
String sql = STMTS[i];
psa[i] = conn.prepareStatement(sql);
}
connDDL.createStatement().execute(ddl);
for (int i = 0; i < STMTS.length; i++)
{
String sql = STMTS[i];
if (sql.startsWith("SELECT "))
checkSelect(psa[i], sql);
else if (sql.startsWith("GRANT ")
|| sql.startsWith("REVOKE "))
checkGrantRevoke(psa[i], sql);
else
fail("unknown SQL" + sql);
psa[i].close();
}
}
private void checkSelect(PreparedStatement ps, String sql)
throws SQLException
{
assertEquals(true, sql.startsWith("SELECT "));
boolean result;
try {
result = ps.execute();
} catch (SQLException e) {
//TODO: Use DMD to see if table exists or not.
assertSQLState("42X05", e);
assertTrue(tableDropped());
return;
}
assertTrue(result);
ResultSet rs = ps.getResultSet();
DatabaseMetaData dmd = connDDL.getMetaData();
JDBC.assertMetaDataMatch(dmd, rs.getMetaData());
boolean isSelectStar = sql.startsWith("SELECT * ");
if (isSelectStar)
;
JDBC.assertDrainResults(rs);
}
private void checkGrantRevoke(PreparedStatement ps, String sql)
throws SQLException
{
assertEquals(true, sql.startsWith("GRANT ")
|| sql.startsWith("REVOKE "));
try {
assertFalse(ps.execute());
} catch (SQLException e) {
assertSQLState("42X05", e);
assertTrue(tableDropped());
return;
}
}
/**
* Set the fixture up with a clean, standard table PED001.
*/
protected void setUp() throws SQLException
{
connDDL = openDefaultConnection();
Statement s = connDDL.createStatement();
s.execute(
"CREATE TABLE PED001 (A INT NOT NULL, B DECIMAL(6,4), C VARCHAR(20))");
s.close();
}
/**
* Tear-down the fixture by removing the table (if it still
* exists).
*/
protected void tearDown() throws Exception
{
Statement s = connDDL.createStatement();
try {
s.execute("DROP TABLE PED001");
} catch (SQLException e) {
assertSQLState("42Y55", e);
}
s.close();
JDBC.cleanup(connDDL);
connDDL = null;
super.tearDown();
}
}
|