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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356
|
/*
Derby - Class org.apache.derbyTesting.functionTests.tests.jdbcapi.NullSQLTextTest
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.jdbcapi;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
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.CleanDatabaseTestSetup;
import org.apache.derbyTesting.junit.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* This test converts the old jdbcapi/nullSQLText.java
* test to JUnit.
*/
public class NullSQLTextTest extends BaseJDBCTestCase {
/**
* Create a test with the given name.
*
* @param name name of the test.
*/
public NullSQLTextTest(String name) {
super(name);
}
/**
* Create suite containing client and embedded tests and to run
* all tests in this class
*/
public static Test suite() {
BaseTestSuite suite = new BaseTestSuite("NullSQLTextTest");
suite.addTest(baseSuite("NullSQLTextTest:embedded"));
suite.addTest(
TestConfiguration.clientServerDecorator(
baseSuite("NullSQLTextTest:client")));
return suite;
}
private static Test baseSuite(String name) {
BaseTestSuite suite = new BaseTestSuite(name);
suite.addTestSuite(NullSQLTextTest.class);
return new CleanDatabaseTestSetup(suite) {
/**
* Creates the tables and the stored procedures used in the test
* cases.
*
* @exception SQLException if a database error occurs
*/
protected void decorateSQL(Statement stmt) throws SQLException {
Connection conn = getConnection();
/**
* Creates the table used in the test cases.
*
*/
stmt.execute("create table t1 (i int)");
stmt.execute("insert into t1 values 1, 2, 3, 4, 5, 6, 7");
stmt.execute("create procedure za() language java external name " +
"'org.apache.derbyTesting.functionTests.tests.jdbcapi.NullSQLTextTest.zeroArg'" +
" parameter style java");
}
};
}
/**
* Testing null string in prepared statement.
*
* @exception SQLException if database access errors or other errors occur
*/
public void testNullStringPreparedStatement() throws SQLException {
String nullString = null;
try {
// test null String in prepared statement
PreparedStatement ps = prepareStatement(nullString);
fail("preparedStatement(nullString) should have failed.");
} catch (SQLException e) {
assertSQLState("XJ067", e);
}
}
/**
* Testing null string in execute statement.
*
* @exception SQLException if database access errors or other errors occur
*/
public void testNullStringExecuteStatement() throws SQLException {
String nullString = null;
try {
// test null String in execute statement
Statement stmt = createStatement();
stmt.execute(nullString);
fail("execute(nullString) should have failed.");
} catch (SQLException e) {
assertSQLState("XJ067", e);
}
}
/**
* Testing null string in executeQuery statement.
*
* @exception SQLException if database access errors or other errors occur
*/
public void testNullStringExecuteQueryStatement() throws SQLException {
String nullString = null;
try {
// test null String in execute query statement
Statement stmt = createStatement();
stmt.executeQuery(nullString);
fail("executeQuery(nullString) should have failed.");
} catch (SQLException e) {
assertSQLState("XJ067", e);
}
}
/**
* Testing null string in executeUpdate statement.
*
* @exception SQLException if database access errors or other errors occur
*/
public void testNullStringExecuteUpdateStatement() throws SQLException {
String nullString = null;
try {
// test null String in execute update statement
Statement stmt = createStatement();
stmt.executeUpdate(nullString);
fail("executeUpdate(nullString) should have failed.");
} catch (SQLException e) {
assertSQLState("XJ067", e);
}
}
/**
* Testing embedded comments in execute statement.
*
* @exception SQLException if database access errors or other errors occur
*/
public void testDerby522() throws SQLException {
Statement stmt = createStatement();
// These we expect to fail with syntax errors, as in embedded mode.
testCommentStmt(stmt, " --", true);
testCommentStmt(stmt, " -- ", true);
testCommentStmt(stmt, " -- This is a comment \n --", true);
testCommentStmt(stmt,
" -- This is a comment\n --And another\n -- Andonemore", true);
/*
/* These we expect to return valid results for embedded and
/* Derby Client (as of DERBY-522 fix)
*/
testCommentStmt(stmt, " --\nvalues 2, 4, 8", false);
ResultSet rs = stmt.getResultSet();
String[][] expectedRows = new String[][] { { "2" },
{ "4" },
{ "8" } };
JDBC.assertFullResultSet(rs, expectedRows);
testCommentStmt(
stmt,
" -- This is \n -- \n --3 comments\nvalues 8",
false);
rs = stmt.getResultSet();
expectedRows = new String[][] { { "8" } };
JDBC.assertFullResultSet(rs, expectedRows);
testCommentStmt(stmt,
" -- This is a comment\n --And another\n -- Andonemore\nvalues (2,3)",
false);
rs = stmt.getResultSet();
ResultSetMetaData rsmd = rs.getMetaData();
expectedRows = new String[][] { { "2", "3" } };
JDBC.assertFullResultSet(rs, expectedRows);
testCommentStmt(stmt,
" -- This is a comment\n select i from t1",
false);
rs = stmt.getResultSet();
expectedRows = new String[][] { { "1" },
{ "2" },
{ "3" },
{ "4" },
{ "5" },
{ "6" },
{ "7" } };
JDBC.assertFullResultSet(rs, expectedRows);
testCommentStmt(stmt,
" --singleword\n insert into t1 values (8)",
false);
rs = stmt.getResultSet();
assertNull("Unexpected Not Null ResultSet", rs);
testCommentStmt(stmt,
" --singleword\ncall za()",
false);
assertNull("Unexpected Not Null ResultSet", rs);
testCommentStmt(stmt,
" -- leading comment\n(\nvalues 4, 8)",
false);
rs = stmt.getResultSet();
expectedRows = new String[][] { { "4" },
{ "8" } };
JDBC.assertFullResultSet(rs, expectedRows);
testCommentStmt(stmt,
" -- leading comment\n\n(\n\n\rvalues 4, 8)",
false);
rs = stmt.getResultSet();
expectedRows = new String[][] { { "4" },
{ "8" } };
JDBC.assertFullResultSet(rs, expectedRows);
/*
/* While we're at it, test comments in the middle and end of the
/* statement. Prior to the patch for DERBY-522, statements
/* ending with a comment threw syntax errors; that problem
/* was fixed with DERBY-522, as well, so all of these should now
/* succeed in all modes (embedded and Derby Client).
*/
testCommentStmt(stmt, "select i from t1 -- This is a comment", false);
rs = stmt.getResultSet();
expectedRows = new String[][] { { "1" },
{ "2" },
{ "3" },
{ "4" },
{ "5" },
{ "6" },
{ "7" },
{ "8" } };
JDBC.assertFullResultSet(rs, expectedRows);
testCommentStmt(stmt, "select i from t1\n -- This is a comment", false);
rs = stmt.getResultSet();
expectedRows = new String[][] { { "1" },
{ "2" },
{ "3" },
{ "4" },
{ "5" },
{ "6" },
{ "7" },
{ "8" } };
JDBC.assertFullResultSet(rs, expectedRows);
testCommentStmt(stmt, "values 8, 4, 2\n --", false);
rs = stmt.getResultSet();
expectedRows = new String[][] { { "8" },
{ "4" },
{ "2" } };
JDBC.assertFullResultSet(rs, expectedRows);
testCommentStmt(stmt, "values 8, 4,\n -- middle comment\n2\n -- end", false);
rs = stmt.getResultSet();
expectedRows = new String[][] { { "8" },
{ "4" },
{ "2" } };
JDBC.assertFullResultSet(rs, expectedRows);
testCommentStmt(stmt, "values 8, 4,\n -- middle comment\n2\n -- end\n", false);
rs = stmt.getResultSet();
expectedRows = new String[][] { { "8" },
{ "4" },
{ "2" } };
JDBC.assertFullResultSet(rs, expectedRows);
}
/**
* Helper method for testDerby522().
* executes strings containing embedded comments.
*
* @param sql sql statement
* @exception SQLException if database access errors or other errors occur
*/
private static void testCommentStmt(Statement st, String sql,
boolean expectFailure) throws SQLException {
try {
st.execute(sql);
if (expectFailure)
fail("Unexpected Failure -- execute() should have failed.");
} catch (SQLException se) {
assertSQLState("42X01", se);
}
}
/**
* Java method for procedure za()
*
*/
public static void zeroArg () {
}
private static String SQLSTATE_SYNTAX_ERROR = "42X01";
public void testExecuteEmptyString() throws SQLException {
try {
createStatement().execute("");
fail("Statement expected to fail");
} catch (SQLException sqle) {
assertSQLState(SQLSTATE_SYNTAX_ERROR, sqle);
}
}
public void testExecuteUpdateEmptyString() {
try {
createStatement().executeUpdate("");
fail("Statement expected to fail");
} catch (SQLException sqle) {
assertSQLState(SQLSTATE_SYNTAX_ERROR, sqle);
}
}
public void testExecuteQueryEmptyString() {
try {
createStatement().executeQuery("");
fail("Statement expected to fail");
} catch (SQLException sqle) {
assertSQLState(SQLSTATE_SYNTAX_ERROR, sqle);
}
}
public void testPrepareEmptyString() {
try {
prepareStatement("");
fail("Statement expected to fail");
} catch (SQLException sqle) {
assertSQLState(SQLSTATE_SYNTAX_ERROR, sqle);
}
}
}
|