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
|
/**
* Derby - Class org.apache.derbyTesting.functionTests.tests.lang.ConstantExpressionTest
*
* 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.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.JDBC;
import org.apache.derbyTesting.junit.TestConfiguration;
/**
* Test case for constantExpression.sql, which provides tests
* for constant expression evaluation.
*/
public class ConstantExpressionTest extends BaseJDBCTestCase {
/**
* Constructor.
*/
public ConstantExpressionTest(String name) throws SQLException {
super(name);
}
public static Test suite(){
return TestConfiguration.defaultSuite(
ConstantExpressionTest.class);
}
/**
* Create the table t1 with 3 rows, and two procedures.
* @throws SQLException
* if SQLException is thrown.
* @see junit.framework.TestCase#setUp()
*/
public void setUp() throws SQLException{
String sql = "create table t1(c1 int)";
Statement st = createStatement();
st.executeUpdate(sql);
sql = "insert into t1 values 1, 2, 3";
assertEquals("Fail to insert into table", 3, st.executeUpdate(sql));
st.close();
}
/**
* Drop table t1 and close two procedures.
* @throws Exception
* if Exception is thrown.
* @see org.apache.derbyTesting.junit.BaseJDBCTestCase#tearDown()
*/
public void tearDown() throws Exception {
dropTable("t1");
super.tearDown();
}
/**
* Test false constant expressions.
* @throws SQLException
* if SQLException is thrown.
*/
public void testFalseConstantExpressions() throws SQLException{
String[] falseCases = {
"1 <> 1", "1 = 1 and 1 = 0", "1 = (2 + 3 - 2)",
"(case when 1 = 1 then 0 else 1 end) = 1",
"1 in (2, 3, 4)", "1 between 2 and 3",
};
Statement st = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs;
String sql;
for(int i = 0; i < falseCases.length; i++){
sql = "select * from t1 where " + falseCases[i];
rs = st.executeQuery(sql);
JDBC.assertEmpty(rs);
}
st.close();
sql = "select * from t1 where ? = 1";
PreparedStatement ps1 = prepareStatement(sql);
ps1.setInt(1, 0);
rs = ps1.executeQuery();
JDBC.assertEmpty(rs);
ps1.close();
sql = "select * from t1 where cast(? as int) = 1";
PreparedStatement ps2 = prepareStatement(sql);
ps2.setInt(1, 0);
rs = ps2.executeQuery();
JDBC.assertEmpty(rs);
ps2.close();
}
/**
* Test true constant expressions.
* @throws SQLException
*/
public void testTrueConstantExpressions() throws SQLException{
String[] trueCases = {
"1 = 1", "1 = 0 or 1 = 1", "1 + 2 = (2 + 3 - 2)",
"(case when 1 = 1 then 1 else 0 end) = 1",
"1 in (2, 3, 4, 4, 3, 2, 1)", "1 + 1 between 0 and 3",
};
String[][] content = {
{ "1", }, { "2", }, { "3", },
};
Statement st = createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
ResultSet rs;
String sql;
for(int i = 0; i < trueCases.length; i++){
sql = "select * from t1 where " + trueCases[i];
rs = st.executeQuery(sql);
JDBC.assertFullResultSet(rs, content);
}
st.close();
sql = "select * from t1 where ? = 1";
PreparedStatement ps1 = prepareStatement(sql);
ps1.setInt(1, 1);
rs = ps1.executeQuery();
JDBC.assertFullResultSet(rs, content);
ps1.close();
sql = "select * from t1 where cast(? as int) = 1";
PreparedStatement ps2 = prepareStatement(sql);
ps2.setInt(1, 1);
rs = ps2.executeQuery();
JDBC.assertFullResultSet(rs, content);
ps2.close();
}
}
|