001: /*
002:
003: Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.nullSQLText
004:
005: Licensed to the Apache Software Foundation (ASF) under one or more
006: contributor license agreements. See the NOTICE file distributed with
007: this work for additional information regarding copyright ownership.
008: The ASF licenses this file to You under the Apache License, Version 2.0
009: (the "License"); you may not use this file except in compliance with
010: the License. You may obtain a copy of the License at
011:
012: http://www.apache.org/licenses/LICENSE-2.0
013:
014: Unless required by applicable law or agreed to in writing, software
015: distributed under the License is distributed on an "AS IS" BASIS,
016: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: See the License for the specific language governing permissions and
018: limitations under the License.
019:
020: */
021:
022: package org.apache.derbyTesting.functionTests.tests.jdbcapi;
023:
024: import java.sql.Connection;
025: import java.sql.DriverManager;
026: import java.sql.DatabaseMetaData;
027: import java.sql.ResultSetMetaData;
028: import java.sql.PreparedStatement;
029: import java.sql.Statement;
030: import java.sql.ResultSet;
031: import java.sql.SQLException;
032: import java.sql.Types;
033:
034: import org.apache.derby.tools.ij;
035: import org.apache.derbyTesting.functionTests.util.TestUtil;
036: import org.apache.derbyTesting.functionTests.util.JDBCTestDisplayUtil;
037:
038: /**
039: * Test of null strings in prepareStatement and execute
040: * result set. Also test comments in SQL text that is
041: * passed to an "execute" call.
042: *
043: * @author peachey
044: */
045:
046: public class nullSQLText {
047: public static void main(String[] args) {
048: Connection con;
049: PreparedStatement ps;
050: Statement s;
051: String nullString = null;
052:
053: System.out.println("Test nullSQLText starting");
054:
055: try {
056: // use the ij utility to read the property file and
057: // make the initial connection.
058: ij.getPropertyArg(args);
059: con = ij.startJBMS();
060: con.setAutoCommit(true); // make sure it is true
061: s = con.createStatement();
062:
063: // Clean-up in case anything was left from previous tests.
064: try {
065: s.execute("drop table t1");
066: } catch (SQLException se) {
067: }
068: try {
069: s.execute("drop procedure za");
070: } catch (SQLException se) {
071: }
072:
073: try {
074: // test null String in prepared statement
075: System.out
076: .println("Test prepareStatement with null argument");
077: ps = con.prepareStatement(nullString);
078: } catch (SQLException e) {
079: System.out.println("FAIL -- expected exception");
080: dumpSQLExceptions(e);
081: }
082: try {
083: // test null String in execute statement
084: System.out.println("Test execute with null argument");
085: s.execute(nullString);
086: } catch (SQLException e) {
087: System.out.println("FAIL -- expected exception");
088: dumpSQLExceptions(e);
089: }
090: try {
091: // test null String in execute query statement
092: System.out
093: .println("Test executeQuery with null argument");
094: s.executeQuery(nullString);
095: } catch (SQLException e) {
096: System.out.println("FAIL -- expected exception");
097: dumpSQLExceptions(e);
098: }
099: try {
100: // test null String in execute update statement
101: System.out
102: .println("Test executeUpdate with null argument");
103: s.executeUpdate(nullString);
104: } catch (SQLException e) {
105: System.out.println("FAIL -- expected exception");
106: dumpSQLExceptions(e);
107: }
108:
109: // Test comments in statements.
110: derby522(s);
111:
112: con.close();
113: } catch (SQLException e) {
114: dumpSQLExceptions(e);
115: e.printStackTrace(System.out);
116: } catch (Throwable e) {
117: System.out.println("FAIL -- unexpected exception:");
118: e.printStackTrace(System.out);
119: }
120:
121: System.out.println("Test nullSQLText finished");
122: }
123:
124: static private void dumpSQLExceptions(SQLException se) {
125: while (se != null) {
126: JDBCTestDisplayUtil.ShowCommonSQLException(System.out, se);
127: se = se.getNextException();
128: }
129: }
130:
131: /* ****
132: * Derby-522: When a statement with comments at the front
133: * is passed through to an "execute" call, the client throws
134: * error X0Y79 ("executeUpdate cannot be called with a statement
135: * that returns a result set"). The same thing works fine
136: * against Derby embedded. This method executes several
137: * statements that have comments preceding them; with the
138: * fix for DERBY-522, these should all either pass or
139: * throw the correct syntax errors (i.e. the client should
140: * behave the same way as embedded).
141: */
142: private static void derby522(Statement st) throws Exception {
143: System.out.println("Starting test for DERBY-522.");
144:
145: st.execute("create table t1 (i int)");
146: st.execute("insert into t1 values 1, 2, 3, 4, 5, 6, 7");
147: st
148: .execute("create procedure za() language java external name "
149: + "'org.apache.derbyTesting.functionTests.util.ProcedureTest.zeroArg'"
150: + " parameter style java");
151:
152: // These we expect to fail with syntax errors, as in embedded mode.
153: testCommentStmt(st, " --", true);
154: testCommentStmt(st, " -- ", true);
155: testCommentStmt(st, " -- This is a comment \n --", true);
156: testCommentStmt(
157: st,
158: " -- This is a comment\n --And another\n -- Andonemore",
159: true);
160:
161: // These we expect to return valid results for embedded and
162: // Derby Client (as of DERBY-522 fix); for JCC, these will
163: // fail.
164: testCommentStmt(st, " --\nvalues 2, 4, 8", TestUtil
165: .isJCCFramework());
166: testCommentStmt(st,
167: " -- This is \n -- \n --3 comments\nvalues 8", TestUtil
168: .isJCCFramework());
169: testCommentStmt(
170: st,
171: " -- This is a comment\n --And another\n -- Andonemore\nvalues (2,3)",
172: TestUtil.isJCCFramework());
173: testCommentStmt(st, " -- This is a comment\n select i from t1",
174: TestUtil.isJCCFramework());
175: testCommentStmt(st,
176: " --singleword\n insert into t1 values (8)", TestUtil
177: .isJCCFramework());
178: testCommentStmt(st, " --singleword\ncall za()", TestUtil
179: .isJCCFramework());
180: testCommentStmt(st, " -- leading comment\n(\nvalues 4, 8)",
181: TestUtil.isJCCFramework());
182: testCommentStmt(st,
183: " -- leading comment\n\n(\n\n\rvalues 4, 8)", TestUtil
184: .isJCCFramework());
185:
186: // While we're at it, test comments in the middle and end of the
187: // statement. Prior to the patch for DERBY-522, statements
188: // ending with a comment threw syntax errors; that problem
189: // was fixed with DERBY-522, as well, so all of these should now
190: // succeed in all modes (embedded, Derby Client, and JCC).
191: testCommentStmt(st, "select i from t1 -- This is a comment",
192: false);
193: testCommentStmt(st, "select i from t1\n -- This is a comment",
194: false);
195: testCommentStmt(st, "values 8, 4, 2\n --", false);
196: testCommentStmt(st,
197: "values 8, 4,\n -- middle comment\n2\n -- end", false);
198: testCommentStmt(st,
199: "values 8, 4,\n -- middle comment\n2\n -- end\n", false);
200:
201: // Clean-up.
202: try {
203: st.execute("drop table t1");
204: } catch (SQLException se) {
205: }
206: try {
207: st.execute("drop procedure za");
208: } catch (SQLException se) {
209: }
210:
211: st.close();
212: System.out.println("DERBY-522 test completed.");
213: }
214:
215: /* ****
216: * Helper method for derby522.
217: */
218: private static void testCommentStmt(Statement st, String sql,
219: boolean expectError) throws SQLException {
220:
221: try {
222:
223: System.out.println("[ Test Statement ]:\n" + sql);
224: st.execute(sql);
225: System.out.print("[ Results ]: ");
226: ResultSet rs = st.getResultSet();
227: if (rs != null) {
228: while (rs.next())
229: System.out.print(" " + rs.getInt(1));
230: System.out.println();
231: } else
232: System.out.println("(NO RESULT SET)");
233:
234: } catch (SQLException se) {
235:
236: if (expectError)
237: System.out.print("[ EXPECTED ERROR ]: ");
238: else
239: System.out.print("[ FAILED ]: ");
240: dumpSQLExceptions(se);
241:
242: }
243:
244: }
245: }
|