KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > tests > lang > PrepareExecuteDDL


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.lang.PrepareExecuteDDL
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, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.tests.lang;
23
24 import org.apache.derbyTesting.junit.BaseJDBCTestCase;
25 import org.apache.derbyTesting.junit.JDBC;
26
27 import java.sql.*;
28
29 import junit.framework.Test;
30 import junit.framework.TestSuite;
31
32 /**
33  * Test the dependency system for active statements when
34  * a DDL is executed in a separate connection after the
35  * prepare but before the execute.
36  *
37  */

38 public class PrepareExecuteDDL extends BaseJDBCTestCase {
39     
40     /**
41      * Connection to execute the DDL on. Needs
42      * to be different to the single connection
43      * provided by the super-class. This connection
44      * is used to execute DDL while the other connection
45      * has open objcts dependent on the objct changed by the DDL.
46      */

47     private Connection connDDL;
48     
49     /**
50      * List of statements that are prepared and then executed.
51      * The testPrepareExecute method prepares each statement
52      * in this list, executes one DDL, executes each prepared
53      * statement and then checks the result.
54      * <BR>
55      * The result checking is driven off the initial text
56      * of the statement.
57      */

58     private static final String JavaDoc[] STMTS =
59     {
60         "SELECT * FROM PED001",
61         "SELECT A, B FROM PED001",
62         "GRANT SELECT ON PED001 TO U_PED_001",
63         "GRANT SELECT(A,B) ON PED001 TO U_PED_001",
64         "REVOKE SELECT(A,B) ON PED001 FROM U_PED_001",
65         "REVOKE SELECT ON PED001 FROM U_PED_001",
66     };
67     
68     /**
69      * All the DDL commands that will be executed, one per
70      * fixture, as the mutation between the prepare and execute.
71      */

72     private static final String JavaDoc[] DDL =
73     {
74         "ALTER TABLE PED001 ADD COLUMN D BIGINT",
75         "ALTER TABLE PED001 ADD CONSTRAINT PED001_PK PRIMARY KEY (A)",
76         "ALTER TABLE PED001 LOCKSIZE ROW",
77         "ALTER TABLE PED001 LOCKSIZE TABLE",
78         "DROP TABLE PED001",
79     };
80     
81     /**
82      * Create a suite of tests, one per statement in DDL.
83      */

84     public static Test suite() {
85         TestSuite suite = new TestSuite();
86         for (int i = 0; i < DDL.length; i++)
87             suite.addTest(new PrepareExecuteDDL("testPrepareExcute", DDL[i]));
88         return suite;
89     }
90     private final String JavaDoc ddl;
91     
92     private PrepareExecuteDDL(String JavaDoc name, String JavaDoc ddl)
93     {
94         super(name);
95         this.ddl = ddl;
96     }
97     
98     private boolean tableDropped()
99     {
100         return ddl.startsWith("DROP TABLE ");
101     }
102     
103     public void testPrepareExcute() throws SQLException
104     {
105         Connection conn = getConnection();
106         
107         PreparedStatement[] psa= new PreparedStatement[STMTS.length];
108         for (int i = 0; i < STMTS.length; i++)
109         {
110             String JavaDoc sql = STMTS[i];
111             psa[i] = conn.prepareStatement(sql);
112         }
113         
114         connDDL.createStatement().execute(ddl);
115         
116         for (int i = 0; i < STMTS.length; i++)
117         {
118             String JavaDoc sql = STMTS[i];
119             if (sql.startsWith("SELECT "))
120                 checkSelect(psa[i], sql);
121             else if (sql.startsWith("GRANT ")
122                     || sql.startsWith("REVOKE "))
123                 checkGrantRevoke(psa[i], sql);
124             else
125                 fail("unknown SQL" + sql);
126             
127             psa[i].close();
128         }
129     }
130     
131     private void checkSelect(PreparedStatement ps, String JavaDoc sql)
132     throws SQLException
133     {
134         assertEquals(true, sql.startsWith("SELECT "));
135         
136         boolean result;
137         try {
138             result = ps.execute();
139         } catch (SQLException e) {
140             
141             //TODO: Use DMD to see if table exists or not.
142
assertSQLState("42X05", e);
143             assertTrue(tableDropped());
144             
145             return;
146         }
147         assertTrue(result);
148         
149         ResultSet rs = ps.getResultSet();
150         
151         DatabaseMetaData dmd = connDDL.getMetaData();
152         JDBC.assertMetaDataMatch(dmd, rs.getMetaData());
153         
154         boolean isSelectStar = sql.startsWith("SELECT * ");
155         
156         if (isSelectStar)
157             ;
158         
159         JDBC.assertDrainResults(rs);
160     }
161     
162     
163     private void checkGrantRevoke(PreparedStatement ps, String JavaDoc sql)
164     throws SQLException
165     {
166         assertEquals(true, sql.startsWith("GRANT ")
167                 || sql.startsWith("REVOKE "));
168         
169         try {
170             assertFalse(ps.execute());
171         } catch (SQLException e) {
172             
173             assertSQLState("42X05", e);
174             assertTrue(tableDropped());
175             
176             return;
177         }
178     }
179     /**
180      * Set the fixture up with a clean, standard table PED001.
181      */

182     protected void setUp() throws SQLException
183     {
184         
185         connDDL = openDefaultConnection();
186         Statement s = connDDL.createStatement();
187         
188         s.execute(
189         "CREATE TABLE PED001 (A INT NOT NULL, B DECIMAL(6,4), C VARCHAR(20))");
190         
191         s.close();
192     }
193     
194     /**
195      * Tear-down the fixture by removing the table (if it still
196      * exists).
197      */

198     protected void tearDown() throws Exception JavaDoc
199     {
200         Statement s = connDDL.createStatement();
201         try {
202             s.execute("DROP TABLE PED001");
203         } catch (SQLException e) {
204             assertSQLState("42Y55", e);
205         }
206         s.close();
207         JDBC.cleanup(connDDL);
208         super.tearDown();
209         
210     }
211 }
212
Popular Tags