KickJava   Java API By Example, From Geeks To Geeks.

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


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.nullSQLText
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.jdbcapi;
23
24 import java.sql.Connection JavaDoc;
25 import java.sql.DriverManager JavaDoc;
26 import java.sql.DatabaseMetaData JavaDoc;
27 import java.sql.ResultSetMetaData JavaDoc;
28 import java.sql.PreparedStatement JavaDoc;
29 import java.sql.Statement JavaDoc;
30 import java.sql.ResultSet JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.sql.Types JavaDoc;
33
34 import org.apache.derby.tools.ij;
35 import org.apache.derbyTesting.functionTests.util.TestUtil;
36 import org.apache.derbyTesting.functionTests.util.JDBCTestDisplayUtil;
37
38 /**
39  * Test of null strings in prepareStatement and execute
40  * result set. Also test comments in SQL text that is
41  * passed to an "execute" call.
42  *
43  * @author peachey
44  */

45
46 public class nullSQLText {
47     public static void main(String JavaDoc[] args) {
48         Connection JavaDoc con;
49         PreparedStatement JavaDoc ps;
50         Statement JavaDoc s;
51         String JavaDoc nullString = null;
52     
53         System.out.println("Test nullSQLText starting");
54     
55         try
56         {
57             // use the ij utility to read the property file and
58
// make the initial connection.
59
ij.getPropertyArg(args);
60             con = ij.startJBMS();
61             con.setAutoCommit(true); // make sure it is true
62
s = con.createStatement();
63
64             // Clean-up in case anything was left from previous tests.
65
try {
66                 s.execute("drop table t1");
67             } catch (SQLException JavaDoc se) {}
68             try {
69                 s.execute("drop procedure za");
70             } catch (SQLException JavaDoc se) {}
71
72             try
73             {
74                 // test null String in prepared statement
75
System.out.println("Test prepareStatement with null argument");
76                 ps = con.prepareStatement(nullString);
77             }
78             catch (SQLException JavaDoc e) {
79                 System.out.println("FAIL -- expected exception");
80                 dumpSQLExceptions(e);
81             }
82             try
83             {
84                 // test null String in execute statement
85
System.out.println("Test execute with null argument");
86                 s.execute(nullString);
87             }
88             catch (SQLException JavaDoc e) {
89                 System.out.println("FAIL -- expected exception");
90                 dumpSQLExceptions(e);
91             }
92             try
93             {
94                 // test null String in execute query statement
95
System.out.println("Test executeQuery with null argument");
96                 s.executeQuery(nullString);
97             }
98             catch (SQLException JavaDoc e) {
99                 System.out.println("FAIL -- expected exception");
100                 dumpSQLExceptions(e);
101             }
102             try
103             {
104                 // test null String in execute update statement
105
System.out.println("Test executeUpdate with null argument");
106                 s.executeUpdate(nullString);
107             }
108             catch (SQLException JavaDoc e) {
109                 System.out.println("FAIL -- expected exception");
110                 dumpSQLExceptions(e);
111             }
112
113             // Test comments in statements.
114
derby522(s);
115
116             con.close();
117         }
118         catch (SQLException JavaDoc e) {
119             dumpSQLExceptions(e);
120             e.printStackTrace(System.out);
121         }
122         catch (Throwable JavaDoc e) {
123             System.out.println("FAIL -- unexpected exception:");
124             e.printStackTrace(System.out);
125         }
126         
127         System.out.println("Test nullSQLText finished");
128     }
129     static private void dumpSQLExceptions (SQLException JavaDoc se) {
130         while (se != null) {
131             JDBCTestDisplayUtil.ShowCommonSQLException(System.out, se);
132              se = se.getNextException();
133         }
134     }
135
136
137     /* ****
138      * Derby-522: When a statement with comments at the front
139      * is passed through to an "execute" call, the client throws
140      * error X0Y79 ("executeUpdate cannot be called with a statement
141      * that returns a result set"). The same thing works fine
142      * against Derby embedded. This method executes several
143      * statements that have comments preceding them; with the
144      * fix for DERBY-522, these should all either pass or
145      * throw the correct syntax errors (i.e. the client should
146      * behave the same way as embedded).
147      */

148     private static void derby522(Statement JavaDoc st) throws Exception JavaDoc
149     {
150         System.out.println("Starting test for DERBY-522.");
151
152         st.execute("create table t1 (i int)");
153         st.execute("insert into t1 values 1, 2, 3, 4, 5, 6, 7");
154         st.execute("create procedure za() language java external name " +
155             "'org.apache.derbyTesting.functionTests.util.ProcedureTest.zeroArg'" +
156             " parameter style java");
157         
158         // These we expect to fail with syntax errors, as in embedded mode.
159
testCommentStmt(st, " --", true);
160         testCommentStmt(st, " -- ", true);
161         testCommentStmt(st, " -- This is a comment \n --", true);
162         testCommentStmt(
163             st,
164             " -- This is a comment\n --And another\n -- Andonemore",
165             true);
166
167         // These we expect to return valid results for embedded and
168
// Derby Client (as of DERBY-522 fix); for JCC, these will
169
// fail.
170
testCommentStmt(st, " --\nvalues 2, 4, 8", TestUtil.isJCCFramework());
171         testCommentStmt(
172             st,
173             " -- This is \n -- \n --3 comments\nvalues 8",
174             TestUtil.isJCCFramework());
175         testCommentStmt(
176             st,
177             " -- This is a comment\n --And another\n -- Andonemore\nvalues (2,3)",
178             TestUtil.isJCCFramework());
179         testCommentStmt(st,
180             " -- This is a comment\n select i from t1",
181             TestUtil.isJCCFramework());
182         testCommentStmt(st,
183             " --singleword\n insert into t1 values (8)",
184             TestUtil.isJCCFramework());
185         testCommentStmt(st,
186             " --singleword\ncall za()",
187             TestUtil.isJCCFramework());
188         testCommentStmt(st,
189             " -- leading comment\n(\nvalues 4, 8)",
190             TestUtil.isJCCFramework());
191         testCommentStmt(st,
192             " -- leading comment\n\n(\n\n\rvalues 4, 8)",
193             TestUtil.isJCCFramework());
194
195         // While we're at it, test comments in the middle and end of the
196
// statement. Prior to the patch for DERBY-522, statements
197
// ending with a comment threw syntax errors; that problem
198
// was fixed with DERBY-522, as well, so all of these should now
199
// succeed in all modes (embedded, Derby Client, and JCC).
200
testCommentStmt(st, "select i from t1 -- This is a comment", false);
201         testCommentStmt(st, "select i from t1\n -- This is a comment", false);
202         testCommentStmt(st, "values 8, 4, 2\n --", false);
203         testCommentStmt(st, "values 8, 4,\n -- middle comment\n2\n -- end", false);
204         testCommentStmt(st, "values 8, 4,\n -- middle comment\n2\n -- end\n", false);
205
206         // Clean-up.
207
try {
208             st.execute("drop table t1");
209         } catch (SQLException JavaDoc se) {}
210         try {
211             st.execute("drop procedure za");
212         } catch (SQLException JavaDoc se) {}
213
214         st.close();
215         System.out.println("DERBY-522 test completed.");
216     }
217
218     /* ****
219      * Helper method for derby522.
220      */

221     private static void testCommentStmt(Statement JavaDoc st, String JavaDoc sql,
222         boolean expectError) throws SQLException JavaDoc
223     {
224
225         try {
226
227             System.out.println("[ Test Statement ]:\n" + sql);
228             st.execute(sql);
229             System.out.print("[ Results ]: ");
230             ResultSet JavaDoc rs = st.getResultSet();
231             if (rs != null) {
232                 while (rs.next())
233                     System.out.print(" " + rs.getInt(1));
234                 System.out.println();
235             }
236             else
237                 System.out.println("(NO RESULT SET)");
238
239         } catch (SQLException JavaDoc se) {
240
241             if (expectError)
242                 System.out.print("[ EXPECTED ERROR ]: ");
243             else
244                 System.out.print("[ FAILED ]: ");
245             dumpSQLExceptions(se);
246
247         }
248
249     }
250 }
251
Popular Tags